module Asciidoctor::Standoc::Refs

Constants

ISO_REF
ISO_REF_ALL_PARTS
ISO_REF_NO_YEAR
MALFORMED_REF
NON_ISO_REF

Public Instance Methods

analyse_ref_code(code) click to toggle source

ref id = (usrlbl)codeyear code = nofetch(code) | (repo|path):(key,code) | [? number ]? | ident

# File lib/asciidoctor/standoc/ref.rb, line 148
def analyse_ref_code(code)
  ret = { id: code }
  return ret if code.blank?

  analyse_ref_nofetch(analyse_ref_repo_path(analyse_ref_numeric(ret)))
end
analyse_ref_nofetch(ret) click to toggle source
# File lib/asciidoctor/standoc/ref.rb, line 126
def analyse_ref_nofetch(ret)
  return ret unless m = /^nofetch\((?<id>.+)\)$/.match(ret[:id])

  ret.merge(id: m[:id], nofetch: true)
end
analyse_ref_numeric(ret) click to toggle source
# File lib/asciidoctor/standoc/ref.rb, line 140
def analyse_ref_numeric(ret)
  return ret unless /^\d+$/.match?(ret[:id])

  ret.merge(numeric: true)
end
analyse_ref_repo_path(ret) click to toggle source
# File lib/asciidoctor/standoc/ref.rb, line 132
def analyse_ref_repo_path(ret)
  return ret unless m =
                      /^(?<type>repo|path):\((?<key>[^,]+),?(?<id>.*)\)$/.match(ret[:id])

  id = m[:id].empty? ? m[:key].sub(%r{^[^/]+/}, "") : m[:id]
  ret.merge(id: id, type: m[:type], key: m[:key], nofetch: true)
end
conditional_date(bib, match, noyr) click to toggle source
# File lib/asciidoctor/standoc/ref_date_id.rb, line 26
def conditional_date(bib, match, noyr)
  if match.names.include?("year") && !match[:year].nil?
    bib.date(**{ type: "published" }) do |d|
      noyr and d.on "--" or set_date_range(d, norm_year(match[:year]))
    end
  end
end
docid(bib, code) click to toggle source
# File lib/asciidoctor/standoc/ref_date_id.rb, line 39
def docid(bib, code)
  type, code1 = if /^\[\d+\]$|^\([^)]+\).*$/.match?(code)
                  ["metanorma", mn_code(code)]
                else
                  @bibdb&.docid_type(code) || [nil, code]
                end
  code1.sub!(/^nofetch\((.+)\)$/, "\\1")
  bib.docidentifier **attr_code(type: type) do |d|
    d << code1
  end
end
docnumber(bib, code) click to toggle source
# File lib/asciidoctor/standoc/ref_date_id.rb, line 51
def docnumber(bib, code)
  bib.docnumber do |d|
    d << HTMLEntities.new.decode(code).sub(/^[^\d]*/, "")
  end
end
id_and_year(id, year) click to toggle source
# File lib/asciidoctor/standoc/ref_date_id.rb, line 16
def id_and_year(id, year)
  year ? "#{id}:#{year}" : id
end
iso_publisher(bib, code) click to toggle source
# File lib/asciidoctor/standoc/ref.rb, line 6
def iso_publisher(bib, code)
  code.sub(/ .*$/, "").split("/").each do |abbrev|
    bib.contributor do |c|
      c.role **{ type: "publisher" }
      c.organization do |org|
        organization(org, abbrev, true)
      end
    end
  end
end
isorefmatches(xml, match) click to toggle source
# File lib/asciidoctor/standoc/ref.rb, line 32
def isorefmatches(xml, match)
  yr = norm_year(match[:year])
  ref = fetch_ref xml, match[:code], yr,
                  title: match[:text], usrlbl: match[:usrlbl],
                  lang: (@lang || :all)
  return use_my_anchor(ref, match[:anchor]) if ref

  xml.bibitem **attr_code(ref_attributes(match)) do |t|
    isorefrender1(t, match, yr)
    yr and t.date **{ type: "published" } do |d|
      set_date_range(d, yr)
    end
    iso_publisher(t, match[:code])
  end
end
isorefmatches2(xml, match) click to toggle source
# File lib/asciidoctor/standoc/ref.rb, line 48
def isorefmatches2(xml, match)
  ref = fetch_ref xml, match[:code], nil,
                  no_year: true, note: match[:fn],
                  title: match[:text], usrlbl: match[:usrlbl],
                  lang: (@lang || :all)
  return use_my_anchor(ref, match[:anchor]) if ref

  isorefmatches2_1(xml, match)
end
isorefmatches2_1(xml, match) click to toggle source
# File lib/asciidoctor/standoc/ref.rb, line 58
def isorefmatches2_1(xml, match)
  xml.bibitem **attr_code(ref_attributes(match)) do |t|
    isorefrender1(t, match, "--")
    t.date **{ type: "published" } do |d|
      d.on "--"
    end
    iso_publisher(t, match[:code])
    unless match[:fn].nil?
      t.note(**plaintxt.merge(type: "Unpublished-Status")) do |p|
        p << (match[:fn]).to_s
      end
    end
  end
end
isorefmatches3(xml, match) click to toggle source
# File lib/asciidoctor/standoc/ref.rb, line 73
def isorefmatches3(xml, match)
  yr = norm_year(match[:year])
  hasyr = !yr.nil? && yr != "--"
  ref = fetch_ref(xml, match[:code], hasyr ? yr : nil,
                  all_parts: true, no_year: yr == "--",
                  text: match[:text], usrlbl: match[:usrlbl],
                  lang: (@lang || :all))
  return use_my_anchor(ref, match[:anchor]) if ref

  isorefmatches3_1(xml, match, yr, hasyr, ref)
end
isorefmatches3_1(xml, match, yr, _hasyr, _ref) click to toggle source
# File lib/asciidoctor/standoc/ref.rb, line 85
def isorefmatches3_1(xml, match, yr, _hasyr, _ref)
  xml.bibitem(**attr_code(ref_attributes(match))) do |t|
    isorefrender1(t, match, yr, " (all parts)")
    conditional_date(t, match, yr == "--")
    iso_publisher(t, match[:code])
    if match.names.include?("fn") && match[:fn]
      t.note(**plaintxt.merge(type: "Unpublished-Status")) do |p|
        p << (match[:fn]).to_s
      end
    end
    t.extent **{ type: "part" } do |e|
      e.referenceFrom "all"
    end
  end
end
isorefrender1(bib, match, yr, allp = "") click to toggle source
# File lib/asciidoctor/standoc/ref.rb, line 25
def isorefrender1(bib, match, yr, allp = "")
  bib.title(**plaintxt) { |i| i << ref_normalise(match[:text]) }
  docid(bib, match[:usrlbl]) if match[:usrlbl]
  docid(bib, id_and_year(match[:code], yr) + allp)
  docnumber(bib, match[:code])
end
mn_code(code) click to toggle source
# File lib/asciidoctor/standoc/ref_date_id.rb, line 57
def mn_code(code)
  code.sub(/^\(/, "[").sub(/\).*$/, "]").sub(/^nofetch\((.+)\)$/, "\\1")
end
norm_year(year) click to toggle source
# File lib/asciidoctor/standoc/ref_date_id.rb, line 20
def norm_year(year)
  /^&\#821[12];$/.match(year) and return "--"
  /^\d\d\d\d-\d\d\d\d$/.match(year) and return year
  year&.sub(/(?<=[0-9])-.*$/, "")
end
plaintxt() click to toggle source
# File lib/asciidoctor/standoc/ref.rb, line 17
def plaintxt
  { format: "text/plain" }
end
ref_attributes(match) click to toggle source
# File lib/asciidoctor/standoc/ref.rb, line 21
def ref_attributes(match)
  { id: match[:anchor], type: "standard" }
end
ref_normalise(ref) click to toggle source
# File lib/asciidoctor/standoc/ref.rb, line 175
def ref_normalise(ref)
  ref.gsub(/&amp;amp;/, "&amp;").gsub(%r{^<em>(.*)</em>}, "\\1")
end
ref_normalise_no_format(ref) click to toggle source
# File lib/asciidoctor/standoc/ref.rb, line 179
def ref_normalise_no_format(ref)
  ref.gsub(/&amp;amp;/, "&amp;")
end
reference1(node, item, xml) click to toggle source
# File lib/asciidoctor/standoc/ref.rb, line 214
def reference1(node, item, xml)
  matched, matched2, matched3 = reference1_matches(item)
  if matched3.nil? && matched2.nil? && matched.nil?
    refitem(xml, item, node)
  elsif !matched.nil? then isorefmatches(xml, matched)
  elsif !matched2.nil? then isorefmatches2(xml, matched2)
  elsif !matched3.nil? then isorefmatches3(xml, matched3)
  end
end
reference1_matches(item) click to toggle source
# File lib/asciidoctor/standoc/ref.rb, line 207
def reference1_matches(item)
  matched = ISO_REF.match item
  matched2 = ISO_REF_NO_YEAR.match item
  matched3 = ISO_REF_ALL_PARTS.match item
  [matched, matched2, matched3]
end
refitem(xml, item, node) click to toggle source

TODO: alternative where only title is available

# File lib/asciidoctor/standoc/ref.rb, line 156
def refitem(xml, item, node)
  m = NON_ISO_REF.match(item) and return refitem1(xml, item, m)
  @log.add("AsciiDoc Input", node, "#{MALFORMED_REF}: #{item}")
  nil
end
refitem1(xml, _item, match) click to toggle source
# File lib/asciidoctor/standoc/ref.rb, line 162
def refitem1(xml, _item, match)
  code = analyse_ref_code(match[:code])
  unless code[:id] && code[:numeric] || code[:nofetch]
    ref = fetch_ref(xml, code[:id],
                    match.names.include?("year") ? match[:year] : nil,
                    title: match[:text],
                    usrlbl: match[:usrlbl], lang: (@lang || :all)) and
      return use_my_anchor(ref, match[:anchor])
  end

  refitem_render(xml, match, code)
end
refitem_render(xml, match, code) click to toggle source
# File lib/asciidoctor/standoc/ref.rb, line 112
def refitem_render(xml, match, code)
  xml.bibitem **attr_code(id: match[:anchor]) do |t|
    t.formattedref **{ format: "application/x-isodoc+xml" } do |i|
      i << ref_normalise_no_format(match[:text])
    end
    refitem_render1(match, code, t)
    docnumber(t, code[:id]) unless /^\d+$|^\(.+\)$/.match?(code[:id])
  end
end
refitem_render1(match, code, bib) click to toggle source
# File lib/asciidoctor/standoc/ref.rb, line 101
def refitem_render1(match, code, bib)
  if code[:type] == "path"
    bib.uri code[:key].sub(/\.[a-zA-Z0-9]+$/, ""), **{ type: "URI" }
    bib.uri code[:key].sub(/\.[a-zA-Z0-9]+$/, ""), **{ type: "citation" }
  end
  docid(bib, match[:usrlbl]) if match[:usrlbl]
  docid(bib, /^\d+$/.match?(code[:id]) ? "[#{code[:id]}]" : code[:id])
  code[:type] == "repo" and
    bib.docidentifier code[:key], **{ type: "repository" }
end
set_date_range(date, text) click to toggle source
# File lib/asciidoctor/standoc/ref_date_id.rb, line 4
def set_date_range(date, text)
  matched = /^(?<from>[0-9]+)(-+(?<to>[0-9]+))?$/.match text
  return unless matched[:from]

  if matched[:to]
    date.from matched[:from]
    date.to matched[:to]
  else
    date.on matched[:from]
  end
end
use_my_anchor(ref, id) click to toggle source
# File lib/asciidoctor/standoc/ref_date_id.rb, line 34
def use_my_anchor(ref, id)
  ref.parent.elements.last["id"] = id
  ref
end