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.
23
24
25
26
27
28
29
30
|
# File 'lib/qti/models/base.rb', line 23
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.
10
11
12
|
# File 'lib/qti/models/base.rb', line 10
def doc
@doc
end
|
#manifest ⇒ Object
Returns the value of attribute manifest.
11
12
13
|
# File 'lib/qti/models/base.rb', line 11
def manifest
@manifest
end
|
#package_root ⇒ Object
Returns the value of attribute package_root.
10
11
12
|
# File 'lib/qti/models/base.rb', line 10
def package_root
@package_root
end
|
#path ⇒ Object
Returns the value of attribute path.
10
11
12
|
# File 'lib/qti/models/base.rb', line 10
def path
@path
end
|
#resource ⇒ Object
Returns the value of attribute resource.
10
11
12
|
# File 'lib/qti/models/base.rb', line 10
def resource
@resource
end
|
Class Method Details
.from_path!(path, package_root: nil, resource: nil) ⇒ Object
19
20
21
|
# File 'lib/qti/models/base.rb', line 19
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
52
53
54
55
56
|
# File 'lib/qti/models/base.rb', line 52
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
62
63
64
|
# File 'lib/qti/models/base.rb', line 62
def parse_html(html_string)
Nokogiri.HTML(html_string, @path.to_s, &:noblanks)
end
|
#parse_xml(xml_string) ⇒ Object
58
59
60
|
# File 'lib/qti/models/base.rb', line 58
def parse_xml(xml_string)
Nokogiri.XML(xml_string, @path.to_s, &:noblanks)
end
|
#preprocess_xml_doc(xml_doc) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/qti/models/base.rb', line 32
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
79
80
81
|
# File 'lib/qti/models/base.rb', line 79
def raise_unsupported(message = 'Unsupported QTI version')
raise Qti::UnsupportedSchema, message
end
|
#remap_href_path(href) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/qti/models/base.rb', line 66
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
15
16
17
|
# File 'lib/qti/models/base.rb', line 15
def sanitize_content!(html)
sanitizer.clean(html)
end
|
#xpath_with_single_check(xpath) ⇒ Object
46
47
48
49
50
|
# File 'lib/qti/models/base.rb', line 46
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
|