module Metanorma::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) | hidden(code) | (repo|path):(key,code) | [? number ]? | ident

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

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

  ret.merge(id: m[:id], hidden: true)
end
analyse_ref_nofetch(ret) click to toggle source
# File lib/metanorma/standoc/ref_utility.rb, line 64
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/metanorma/standoc/ref_utility.rb, line 84
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/metanorma/standoc/ref_utility.rb, line 76
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/metanorma/standoc/ref_utility.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/metanorma/standoc/ref_utility.rb, line 40
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/metanorma/standoc/ref_utility.rb, line 52
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/metanorma/standoc/ref_utility.rb, line 16
def id_and_year(id, year)
  year ? "#{id}:#{year}" : id
end
iso_publisher(bib, code) click to toggle source
# File lib/metanorma/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
isorefmatches2_1(xml, match) click to toggle source
# File lib/metanorma/standoc/ref.rb, line 57
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
isorefmatches2code(match) click to toggle source
# File lib/metanorma/standoc/ref.rb, line 44
def isorefmatches2code(match)
  { code: match[:code], no_year: true,
    note: match[:fn], year: nil, match: match,
    title: match[:text], usrlbl: match[:usrlbl],
    lang: (@lang || :all) }
end
isorefmatches2out(item, xml) click to toggle source
# File lib/metanorma/standoc/ref.rb, line 51
def isorefmatches2out(item, xml)
  if item[:doc] then use_retrieved_relaton(item, xml)
  else isorefmatches2_1(xml, item[:ref][:match])
  end
end
isorefmatches3_1(xml, match, yr, _hasyr, _ref) click to toggle source
# File lib/metanorma/standoc/ref.rb, line 90
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
isorefmatches3code(match) click to toggle source
# File lib/metanorma/standoc/ref.rb, line 72
def isorefmatches3code(match)
  yr = norm_year(match[:year])
  hasyr = !yr.nil? && yr != "--"
  { code: match[:code], match: match, yr: yr, hasyr: hasyr,
    year: hasyr ? yr : nil,
    all_parts: true, no_year: yr == "--",
    text: match[:text], usrlbl: match[:usrlbl],
    lang: (@lang || :all) }
end
isorefmatches3out(item, xml) click to toggle source
# File lib/metanorma/standoc/ref.rb, line 82
def isorefmatches3out(item, xml)
  if item[:doc] then use_retrieved_relaton(item, xml)
  else
    isorefmatches3_1(xml, item[:ref][:match], item[:ref][:yr],
                     item[:ref][:hasyr], item[:doc])
  end
end
isorefmatchescode(match) click to toggle source
# File lib/metanorma/standoc/ref.rb, line 24
def isorefmatchescode(match)
  yr = norm_year(match[:year])
  { code: match[:code], year: yr, match: match,
    title: match[:text], usrlbl: match[:usrlbl],
    lang: (@lang || :all) }
end
isorefmatchesout(item, xml) click to toggle source
# File lib/metanorma/standoc/ref.rb, line 31
def isorefmatchesout(item, xml)
  if item[:doc] then use_retrieved_relaton(item, xml)
  else
    xml.bibitem **attr_code(ref_attributes(item[:ref][:match])) do |t|
      isorefrender1(t, item[:ref][:match], item[:ref][:year])
      item[:ref][:year] and t.date **{ type: "published" } do |d|
        set_date_range(d, item[:ref][:year])
      end
      iso_publisher(t, item[:ref][:match][:code])
    end
  end
end
isorefrender1(bib, match, year, allp = "") click to toggle source
# File lib/metanorma/standoc/ref.rb, line 17
def isorefrender1(bib, match, year, allp = "")
  bib.title(**plaintxt) { |i| i << ref_normalise(match[:text]) }
  docid(bib, match[:usrlbl]) if match[:usrlbl]
  docid(bib, id_and_year(match[:code], year) + allp)
  docnumber(bib, match[:code])
end
mn_code(code) click to toggle source
# File lib/metanorma/standoc/ref_utility.rb, line 58
def mn_code(code)
  code.sub(/^\(/, "[").sub(/\).*$/, "]")
    .sub(/^hidden\((.+)\)$/, "\\1")
    .sub(/^nofetch\((.+)\)$/, "\\1")
end
no_year_generic_ref(code) click to toggle source

if no year is supplied, interpret as no_year reference

# File lib/metanorma/standoc/ref_utility.rb, line 103
def no_year_generic_ref(code)
  /^(BSI|BS)\b/.match?(code)
end
norm_year(year) click to toggle source
# File lib/metanorma/standoc/ref_utility.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/metanorma/standoc/ref_utility.rb, line 107
def plaintxt
  { format: "text/plain" }
end
ref_attributes(match) click to toggle source
# File lib/metanorma/standoc/ref_utility.rb, line 111
def ref_attributes(match)
  { id: match[:anchor], type: "standard" }
end
ref_normalise(ref) click to toggle source
# File lib/metanorma/standoc/ref_utility.rb, line 120
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/metanorma/standoc/ref_utility.rb, line 124
def ref_normalise_no_format(ref)
  ref.gsub(/&amp;amp;/, "&amp;")
end
reference(node) click to toggle source
# File lib/metanorma/standoc/ref.rb, line 222
def reference(node)
  refs, results = reference_preproc(node)
  ret = reference_queue(refs, results)
  noko do |xml|
    ret.each { |b| reference1out(b, xml) }
  end.join
end
reference1_matches(item) click to toggle source
# File lib/metanorma/standoc/ref.rb, line 186
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
reference1code(item, node) click to toggle source
# File lib/metanorma/standoc/ref.rb, line 193
def reference1code(item, node)
  matched, matched2, matched3 = reference1_matches(item)
  if matched3.nil? && matched2.nil? && matched.nil?
    refitemcode(item, node).merge(process: 0)
  elsif !matched.nil? then isorefmatchescode(matched).merge(process: 1)
  elsif !matched2.nil? then isorefmatches2code(matched2).merge(process: 2)
  elsif !matched3.nil? then isorefmatches3code(matched3).merge(process: 3)
  end
end
reference1out(item, xml) click to toggle source
# File lib/metanorma/standoc/ref.rb, line 203
def reference1out(item, xml)
  case item[:ref][:process]
  when 0 then refitemout(item, xml)
  when 1 then isorefmatchesout(item, xml)
  when 2 then isorefmatches2out(item, xml)
  when 3 then isorefmatches3out(item, xml)
  end
end
reference_preproc(node) click to toggle source
# File lib/metanorma/standoc/ref.rb, line 212
def reference_preproc(node)
  refs = node.items.each_with_object([]) do |b, m|
    m << reference1code(b.text, node)
  end
  results = refs.each_with_index.with_object(Queue.new) do |(ref, i), res|
    fetch_ref_async(ref.merge(ord: i), i, res)
  end
  [refs, results]
end
reference_queue(refs, results) click to toggle source
# File lib/metanorma/standoc/ref.rb, line 230
def reference_queue(refs, results)
  refs.each.with_object([]) do |_, m|
    ref, i, doc = results.pop
    m[i.to_i] = { ref: ref }
    if doc.is_a?(RelatonBib::RequestError)
      @log.add("Bibliography", nil, "Could not retrieve #{ref[:code]}: "\
                                    "no access to online site")
    else m[i.to_i][:doc] = doc
    end
  end
end
refitem1code(_item, match) click to toggle source
# File lib/metanorma/standoc/ref.rb, line 138
def refitem1code(_item, match)
  code = analyse_ref_code(match[:code])
  ((code[:id] && code[:numeric]) || code[:nofetch]) and
    return { code: nil, match: match, analyse_code: code,
             hidden: code[:hidden] }
  year = refitem1yr(code[:id])
  { code: code[:id], analyse_code: code,
    year: year,
    title: match[:text], match: match, hidden: code[:hidden],
    usrlbl: match[:usrlbl], lang: (@lang || :all) }
end
refitem1yr(code) click to toggle source
# File lib/metanorma/standoc/ref.rb, line 150
def refitem1yr(code)
  yr_match = /[:-](?<year>(19|20)[0-9][0-9])\b/.match(code)
  yr_match ? yr_match[:year] : nil
end
refitem_render(xml, match, code) click to toggle source
# File lib/metanorma/standoc/ref.rb, line 118
def refitem_render(xml, match, code)
  xml.bibitem **attr_code(id: match[:anchor],
                          hidden: code[:hidden]) do |t|
    t.formattedref **{ format: "application/x-isodoc+xml" } do |i|
      i << ref_normalise_no_format(match[:text])
    end
    yr_match = /[:-](?<year>(19|20)[0-9][0-9])\b/.match(code[:id])
    refitem_render1(match, code, t)
    docnumber(t, code[:id]) unless /^\d+$|^\(.+\)$/.match?(code[:id])
    conditional_date(t, yr_match || match, false)
  end
end
refitem_render1(match, code, bib) click to toggle source
# File lib/metanorma/standoc/ref.rb, line 106
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
  code[:id].sub!(/[:-](19|20)[0-9][0-9]$/, "")
  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
refitemcode(item, node) click to toggle source

TODO: alternative where only title is available

# File lib/metanorma/standoc/ref.rb, line 132
def refitemcode(item, node)
  m = NON_ISO_REF.match(item) and return refitem1code(item, m).compact
  @log.add("AsciiDoc Input", node, "#{MALFORMED_REF}: #{item}")
  {}
end
refitemout(item, xml) click to toggle source
# File lib/metanorma/standoc/ref.rb, line 155
def refitemout(item, xml)
  return nil if item[:ref][:match].nil?

  item[:doc] or return refitem_render(xml, item[:ref][:match],
                                      item[:ref][:analyse_code])
  use_retrieved_relaton(item, xml)
end
set_date_range(date, text) click to toggle source
# File lib/metanorma/standoc/ref_utility.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, hidden) click to toggle source
# File lib/metanorma/standoc/ref_utility.rb, line 34
def use_my_anchor(ref, id, hidden)
  ref.parent.elements.last["id"] = id
  hidden and ref.parent.elements.last["hidden"] = hidden
  ref
end