module Asciidoctor::Standoc::Validate

Constants

SOURCELOCALITY

Public Instance Methods

asset_style(root) click to toggle source
# File lib/asciidoctor/standoc/validate_section.rb, line 37
def asset_style(root)
  asset_title_style(root)
end
asset_title_style(root) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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.

# 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) click to toggle source
# 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) click to toggle source
# 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_iev() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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