Module: Asciidoctor::Standoc::Validate
- Included in:
- Converter
- Defined in:
- lib/asciidoctor/standoc/validate.rb,
lib/asciidoctor/standoc/validate_section.rb
Constant Summary collapse
- SOURCELOCALITY =
"./termsource/origin/locality[@type = 'clause']/referenceFrom".freeze
Instance Method Summary collapse
- #asset_style(root) ⇒ Object
- #asset_title_style(root) ⇒ Object
- #content_validate(doc) ⇒ Object
-
#formattedstr_strip(doc) ⇒ Object
RelaxNG cannot cope well with wildcard attributes.
- #iev_validate(xmldoc) ⇒ Object
- #schema_validate(doc, filename) ⇒ Object
- #section_validate(doc) ⇒ Object
- #sourcecode_style(root) ⇒ Object
- #style_warning(node, msg, text) ⇒ Object
- #validate(doc) ⇒ Object
Instance Method Details
#asset_style(root) ⇒ Object
37 38 39 |
# File 'lib/asciidoctor/standoc/validate_section.rb', line 37 def asset_style(root) asset_title_style(root) end |
#asset_title_style(root) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/asciidoctor/standoc/validate_section.rb', line 28 def asset_title_style(root) root.xpath("//figure[image][not(title)]").each do |node| style_warning(node, "Figure should have title", nil) end root.xpath("//table[not(name)]").each do |node| style_warning(node, "Table should have title", nil) end end |
#content_validate(doc) ⇒ Object
25 26 27 28 |
# File 'lib/asciidoctor/standoc/validate.rb', line 25 def content_validate(doc) section_validate(doc) iev_validate(doc.root) end |
#formattedstr_strip(doc) ⇒ Object
RelaxNG cannot cope well with wildcard attributes. So we strip any attributes from FormattedString instances (which can contain xs:any markup, and are signalled with @format) before validation.
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/asciidoctor/standoc/validate.rb', line 46 def formattedstr_strip(doc) doc.xpath("//*[@format]").each do |n| n.elements.each do |e| e.traverse do |e1| next unless e1.element? e1.each { |k, _v| e.delete(k) } end end end doc end |
#iev_validate(xmldoc) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/asciidoctor/standoc/validate.rb', line 14 def iev_validate(xmldoc) xmldoc.xpath("//term").each do |t| /^IEC 60050-/.match(t&.at("./termsource/origin/@citeas")&.text) or next pref = t.xpath("./preferred").inject([]) { |m, x| m << x&.text&.downcase } locality = t.xpath(SOURCELOCALITY)&.text or next iev = @iev.fetch(locality, xmldoc&.at("//language")&.text || "en") or next pref.include?(iev.downcase) or warn %(Term "#{pref[0]}" does not match IEV #{locality} "#{iev}") end end |
#schema_validate(doc, filename) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/asciidoctor/standoc/validate.rb', line 30 def schema_validate(doc, filename) File.open(".tmp.xml", "w:UTF-8") { |f| f.write(doc.to_xml) } begin errors = Jing.new(filename).validate(".tmp.xml") rescue Jing::Error => e abort "what what what #{e}" end warn "Valid!" if errors.none? errors.each do |error| warn "#{error[:message]} @ #{error[:line]}:#{error[:column]}" end end |
#section_validate(doc) ⇒ Object
6 7 8 9 |
# File 'lib/asciidoctor/standoc/validate_section.rb', line 6 def section_validate(doc) sourcecode_style(doc.root) asset_style(doc.root) end |
#sourcecode_style(root) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/asciidoctor/standoc/validate_section.rb', line 11 def sourcecode_style(root) root.xpath("//sourcecode").each do |x| callouts = x.elements.select { |e| e.name == "callout" } annotations = x.elements.select { |e| e.name == "annotation" } if callouts.size != annotations.size warn "#{x['id']}: mismatch of callouts and annotations" end end end |
#style_warning(node, msg, text) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/asciidoctor/standoc/validate_section.rb', line 21 def style_warning(node, msg, text) return if @novalid w = "ISO style: WARNING (#{Utils::current_location(node)}): #{msg}" w += ": #{text}" if text warn w end |
#validate(doc) ⇒ Object
58 59 60 61 62 |
# File 'lib/asciidoctor/standoc/validate.rb', line 58 def validate(doc) content_validate(doc) schema_validate(formattedstr_strip(doc.dup), File.join(File.dirname(__FILE__), "isodoc.rng")) end |