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

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(name)]").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



33
34
35
36
# File 'lib/asciidoctor/standoc/validate.rb', line 33

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.



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/asciidoctor/standoc/validate.rb', line 54

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



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/asciidoctor/standoc/validate.rb', line 21

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 = init_iev or return
    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

#init_ievObject



14
15
16
17
18
19
# File 'lib/asciidoctor/standoc/validate.rb', line 14

def init_iev
  return nil if @no_isobib
  return @iev if @iev
  @iev = Iev::Db.new(@iev_globalname, @iev_localname) unless @no_isobib
  @iev
end

#schema_validate(doc, filename) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/asciidoctor/standoc/validate.rb', line 38

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



66
67
68
69
70
# File 'lib/asciidoctor/standoc/validate.rb', line 66

def validate(doc)
  content_validate(doc)
  schema_validate(formattedstr_strip(doc.dup),
                  File.join(File.dirname(__FILE__), "isodoc.rng"))
end