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



36
37
38
39
40
# File 'lib/asciidoctor/standoc/validate.rb', line 36

def content_validate(doc)
  section_validate(doc)
  repeat_id_validate(doc.root)
  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.



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/asciidoctor/standoc/validate.rb', line 89

def formattedstr_strip(doc)
  doc.xpath("//*[@format] | //stem | //bibdata//description | "\
            "//formattedref | //bibdata//note | //bibdata/abstract | "\
            "//bibitem/abstract | //bibitem/note").each do |n|
    n.elements.each do |e|
      e.traverse do |e1|
        e1.element? and e1.each { |k, _v| e1.delete(k) }
      end
    end
  end
  doc
end

#hanging_para_style(root) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/asciidoctor/standoc/validate_section.rb', line 41

def hanging_para_style(root)
  root.xpath("//clause | //annex | //foreword | //introduction | "\
             "//acknowledgements").each do |c|
    next unless c.at("./clause")
    next if c.elements.select { |n| n.name != "clause" }.empty?
    style_warning(c, "Hanging paragraph in clause")
  end
end

#iev_validate(xmldoc) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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) &&
      loc = t.xpath(SOURCELOCALITY)&.text or next
    @iev = init_iev or return
    iev = @iev.fetch(loc, xmldoc&.at("//language")&.text || "en") or next
    pref = t.xpath("./preferred").inject([]) do |m, x|
      m << x&.text&.downcase
    end
    pref.include?(iev.downcase) or
      @log.add("Bibliography", t, %(Term "#{pref[0]}" does not match ) +
               %(IEV #{loc} "#{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

#repeat_id_validate(doc) ⇒ Object



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

def repeat_id_validate(doc)
  ids = {}
  begin
    doc.xpath("//*[@id]").each do |x|
      ids = repeat_id_validate1(ids, x)
    end
  rescue StandardError => e
    clean_exit
    abort(e.message)
  end
end

#repeat_id_validate1(ids, x) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/asciidoctor/standoc/validate.rb', line 42

def repeat_id_validate1(ids, x)
  if ids[x["id"]]
    @log.add("Anchors", x, "Anchor #{x['id']} has already been used "\
             "at line #{ids[x['id']]}")
    raise StandardError.new "Error: multiple instances of same ID"
  else
    ids[x["id"]] = x.line
  end
  ids
end

#schema_validate(doc, schema) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/asciidoctor/standoc/validate.rb', line 65

def schema_validate(doc, schema)
  Tempfile.open(["tmp", ".xml"], :encoding => 'UTF-8') do |f|
    begin
      f.write(doc.to_xml) 
      f.close
      errors = Jing.new(schema).validate(f.path)
      warn "Syntax Valid!" if errors.none?
      errors.each do |e|
        @log.add("Metanorma XML Syntax",
                 "XML Line #{"%06d" % e[:line]}:#{e[:column]}",
                 e[:message])
      end
    rescue Jing::Error => e
      clean_exit
      abort "Jing failed with error: #{e}"
    ensure
      f.close!
    end
  end
end

#section_validate(doc) ⇒ Object



6
7
8
9
10
# File 'lib/asciidoctor/standoc/validate_section.rb', line 6

def section_validate(doc)
  sourcecode_style(doc.root)
  hanging_para_style(doc.root)
  asset_style(doc.root)
end

#sourcecode_style(root) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/asciidoctor/standoc/validate_section.rb', line 12

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
  @log.add("AsciiDoc Input", x, "mismatch of callouts and annotations")
    end
  end
end

#style_warning(node, msg, text = nil) ⇒ Object



22
23
24
25
26
# File 'lib/asciidoctor/standoc/validate_section.rb', line 22

def style_warning(node, msg, text = nil)
  w = msg
  w += ": #{text}" if text
  @log.add("Metanorma XML Style Warning", node, w)
end

#validate(doc) ⇒ Object



102
103
104
105
106
# File 'lib/asciidoctor/standoc/validate.rb', line 102

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