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

#concept_validate(doc) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/asciidoctor/standoc/validate.rb', line 58

def concept_validate(doc)
  found = false
  doc.xpath("//concept/xref").each do |x|
    next if doc.at("//term[@id = '#{x['target']}']")
    next if doc.at("//definitions//dt[@id = '#{x['target']}']")

    ref = x&.at("../refterm")&.text
    @log.add("Anchors", x, "Concept #{ref} is pointing to "\
             "#{x['target']}, which is not a term or symbol")
    found = true
  end
  found and clean_abort("Concept not cross-referencing term or symbol",
                        doc.to_xml)
end

#content_validate(doc) ⇒ Object



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

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



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/asciidoctor/standoc/validate.rb', line 119

def formattedstr_strip(doc)
  doc.xpath("//*[@format] | //stem | //bibdata//description | "\
            "//formattedref | //bibdata//note | //bibdata/abstract | "\
            "//bibitem/abstract | //bibitem/note | //misc-container")
    .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
49
# 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.reject { |n| %w(clause title).include? n.name }.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)
  @iev = init_iev or return
  xmldoc.xpath("//term").each do |t|
    /^IEC 60050-/.match(t&.at("./termsource/origin/@citeas")&.text) &&
      loc = t.xpath(SOURCELOCALITY)&.text or next
    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



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

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

#norm_ref_validate(doc) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/asciidoctor/standoc/validate.rb', line 44

def norm_ref_validate(doc)
  found = false
  doc.xpath("//references[@normative = 'true']/bibitem").each do |b|
    next unless docid = b.at("./docidentifier[@type = 'metanorma']")
    next unless /^\[\d+\]$/.match?(docid.text)

    @log.add("Bibliography", b,
             "Numeric reference in normative references")
    found = true
  end
  found and
    clean_abort("Numeric reference in normative references", doc.to_xml)
end

#repeat_id_validate(doc) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/asciidoctor/standoc/validate.rb', line 84

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

#repeat_id_validate1(ids, elem) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/asciidoctor/standoc/validate.rb', line 73

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

#schema_validate(doc, schema) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/asciidoctor/standoc/validate.rb', line 95

def schema_validate(doc, schema)
  Tempfile.open(["tmp", ".xml"], encoding: "UTF-8") do |f|
    schema_validate1(f, doc, schema)
  rescue Jing::Error => e
    clean_abort("Jing failed with error: #{e}", doc.to_xml)
  ensure
    f.close!
  end
end

#schema_validate1(file, doc, schema) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/asciidoctor/standoc/validate.rb', line 105

def schema_validate1(file, doc, schema)
  file.write(doc.to_xml)
  file.close
  errors = Jing.new(schema).validate(file.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
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



133
134
135
136
137
# File 'lib/asciidoctor/standoc/validate.rb', line 133

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