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



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

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.



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/asciidoctor/standoc/validate.rb', line 77

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



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

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
    @log.add("Bibliography", t, %(Term "#{pref[0]}" does not match IEV #{locality} "#{iev}"))
  end
end

#init_ievObject



13
14
15
16
17
18
# 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

#repeat_id_validate(doc) ⇒ Object



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

def repeat_id_validate(doc)
  ids = {}
  crash = false
  doc.xpath("//*[@id]").each do |x|
    if ids[x["id"]]
      @log.add("Anchors", x, "Anchor #{x['id']} has already been used at line #{ids[x['id']]}")
      crash = true
    else
      ids[x["id"]] = x.line
    end
  end
  if crash
    clean_exit
    abort("Cannot deal with multiple instances of same ID")
  end
end

#schema_validate(doc, schema) ⇒ Object



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

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 |error|
        @log.add("Syntax", "XML Line #{"%06d" % error[:line]}:#{error[:column]}", error[: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("Style Warning", node, w)
end

#validate(doc) ⇒ Object



90
91
92
93
94
# File 'lib/asciidoctor/standoc/validate.rb', line 90

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