Class: Qti::Models::Base
- Inherits:
-
Object
show all
- Defined in:
- lib/qti/models/base.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(path:, package_root: nil, html: false, resource: nil) ⇒ Base
Returns a new instance of Base.
20
21
22
23
24
25
26
27
|
# File 'lib/qti/models/base.rb', line 20
def initialize(path:, package_root: nil, html: false, resource: nil)
@path = path
@resource = resource
self.package_root = package_root || File.dirname(path)
@doc = html ? parse_html(File.read(path)) : parse_xml(File.read(path))
raise ArgumentError unless @doc
preprocess_xml_doc(@doc) unless html
end
|
Instance Attribute Details
#doc ⇒ Object
Returns the value of attribute doc.
8
9
10
|
# File 'lib/qti/models/base.rb', line 8
def doc
@doc
end
|
#manifest ⇒ Object
Returns the value of attribute manifest.
9
10
11
|
# File 'lib/qti/models/base.rb', line 9
def manifest
@manifest
end
|
#package_root ⇒ Object
Returns the value of attribute package_root.
8
9
10
|
# File 'lib/qti/models/base.rb', line 8
def package_root
@package_root
end
|
#path ⇒ Object
Returns the value of attribute path.
8
9
10
|
# File 'lib/qti/models/base.rb', line 8
def path
@path
end
|
#resource ⇒ Object
Returns the value of attribute resource.
8
9
10
|
# File 'lib/qti/models/base.rb', line 8
def resource
@resource
end
|
Class Method Details
.from_path!(path, package_root = nil, resource = nil) ⇒ Object
16
17
18
|
# File 'lib/qti/models/base.rb', line 16
def self.from_path!(path, package_root = nil, resource = nil)
new(path: path, package_root: package_root, resource: resource)
end
|
Instance Method Details
#css_with_single_check(css) ⇒ Object
49
50
51
52
53
|
# File 'lib/qti/models/base.rb', line 49
def css_with_single_check(css)
node_list = @doc.css(css)
raise Qti::ParseError, 'Too many matches' if node_list.count > 1
node_list.first
end
|
#parse_html(html_string) ⇒ Object
59
60
61
|
# File 'lib/qti/models/base.rb', line 59
def parse_html(html_string)
Nokogiri.HTML(html_string, @path.to_s, &:noblanks)
end
|
#parse_xml(xml_string) ⇒ Object
55
56
57
|
# File 'lib/qti/models/base.rb', line 55
def parse_xml(xml_string)
Nokogiri.XML(xml_string, @path.to_s, &:noblanks)
end
|
#preprocess_xml_doc(xml_doc) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/qti/models/base.rb', line 29
def preprocess_xml_doc(xml_doc)
converter = Mathml2latex::Converter.new
converter.replace_with_latex(xml_doc)
nodes = xml_doc.xpath('//mm:latex', 'mm' => Mathml2latex::INSTUCTURE_LATEX_NS)
nodes.each do |node|
text = node.text.tr("\u00a0", ' ')
latex_string = " \\(#{text}\\) "
node.replace(latex_string)
end
end
|
#raise_unsupported(message = 'Unsupported QTI version') ⇒ Object
76
77
78
|
# File 'lib/qti/models/base.rb', line 76
def raise_unsupported(message = 'Unsupported QTI version')
raise Qti::UnsupportedSchema, message
end
|
#remap_href_path(href) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/qti/models/base.rb', line 63
def remap_href_path(href)
return nil unless href
path = File.join(File.dirname(@path), href)
if @package_root.nil?
raise Qti::ParseError, "Potentially unsafe href '#{href}'" if href.split('/').include?('..')
else
unless Pathname.new(path).cleanpath.to_s.start_with?(@package_root)
raise Qti::ParseError, "Unsafe href '#{href}'"
end
end
path
end
|
#sanitize_content!(html) ⇒ Object
12
13
14
|
# File 'lib/qti/models/base.rb', line 12
def sanitize_content!(html)
sanitizer.clean(html)
end
|
#xpath_with_single_check(xpath) ⇒ Object
43
44
45
46
47
|
# File 'lib/qti/models/base.rb', line 43
def xpath_with_single_check(xpath)
node_list = @doc.xpath(xpath)
raise Qti::ParseError, 'Too many matches' if node_list.count > 1
node_list.first
end
|