class IsoDoc::BIPM::PresentationXMLConvert

Constants

CHAIR
COCHAIR

Public Instance Methods

add_id() click to toggle source
# File lib/isodoc/bipm/index.rb, line 7
def add_id
  %(id="_#{UUIDTools::UUID.random_create}")
end
also_lbl() click to toggle source
# File lib/isodoc/bipm/index.rb, line 96
def also_lbl
  @lang == "en" ? @i18n.see_also : "<em>#{@i18n.see_also}</em>"
end
annex1(elem) click to toggle source
Calls superclass method
# File lib/isodoc/bipm/presentation_xml_convert.rb, line 45
def annex1(elem)
  return super if @jcgm
  return if elem["unnumbered"] == "true"

  lbl = @xrefs.anchor(elem["id"], :label)
  if t = elem.at(ns("./title"))
    t.children = "<strong>#{t.children.to_xml}</strong>"
  end
  prefix_name(elem, ".<tab/>", lbl, "title")
end
bibdata_dates(bibdata) click to toggle source
# File lib/isodoc/bipm/presentation_xml_convert.rb, line 185
def bibdata_dates(bibdata)
  pubdate = bibdata.at(ns("./date[not(@format)][@type = 'published']"))
  return unless pubdate

  meta = metadata_init(@lang, @script, @i18n)
  pubdate.next = pubdate.dup
  pubdate.next["format"] = "ddMMMyyyy"
  pubdate.next.children = meta.monthyr(pubdate.text)
end
bibdata_i18n(bibdata) click to toggle source
Calls superclass method
# File lib/isodoc/bipm/presentation_xml_convert.rb, line 180
def bibdata_i18n(bibdata)
  super
  bibdata_dates(bibdata)
end
clause(docxml) click to toggle source
Calls superclass method
# File lib/isodoc/bipm/presentation_xml_convert.rb, line 56
def clause(docxml)
  super
  if @jcgm
    docxml.xpath(ns("//preface/introduction[clause]")).each do |f|
      clause1(f)
    end
  end
end
clause1(elem) click to toggle source
Calls superclass method
# File lib/isodoc/bipm/presentation_xml_convert.rb, line 65
def clause1(elem)
  return if elem["unnumbered"] == "true"
  return if elem.at(("./ancestor::*[@unnumbered = 'true']"))

  super
end
conversions(docxml) click to toggle source
Calls superclass method
# File lib/isodoc/bipm/presentation_xml_convert.rb, line 72
def conversions(docxml)
  super
  doccontrol docxml
end
convert1(docxml, filename, dir) click to toggle source
Calls superclass method
# File lib/isodoc/bipm/presentation_xml_convert.rb, line 10
def convert1(docxml, filename, dir)
  @jcgm = docxml&.at(ns("//bibdata/ext/editorialgroup/committee/"\
                        "@acronym"))&.value == "JCGM"
  @iso = IsoDoc::Iso::PresentationXMLConvert
    .new({ language: @lang, script: @script })
  i18n = @iso.i18n_init(@lang, @script, nil)
  @iso.metadata_init(@lang, @script, i18n)
  super
end
doccontrol(doc) click to toggle source
# File lib/isodoc/bipm/presentation_xml_convert.rb, line 77
      def doccontrol(doc)
        return unless doc.at(ns("//bibdata/relation[@type = 'supersedes']"))

        clause = <<~DOCCONTROL
          <doccontrol>
          <title>Document Control</title>
          <table unnumbered="true"><tbody>
          <tr><th>Authors:</th><td/><td>#{list_authors(doc)}</td></tr>
          #{doccontrol_row1(doc)} #{doccontrol_row2(doc)} #{list_drafts(doc)}
          </tbody></table></doccontrol>
        DOCCONTROL
        doc.root << clause
      end
doccontrol_row1(doc) click to toggle source
# File lib/isodoc/bipm/presentation_xml_convert.rb, line 91
      def doccontrol_row1(doc)
        return "" if list_draft(doc, 1) == ["", ""] && list_cochairs(doc).empty?

        <<~ROW
          <tr>#{list_draft(doc, 1)&.map { |x| "<td>#{x}</td>" }&.join}
          <td>#{list_cochairs(doc)}</td></tr>
        ROW
      end
doccontrol_row2(docxml) click to toggle source
# File lib/isodoc/bipm/presentation_xml_convert.rb, line 100
      def doccontrol_row2(docxml)
        list_draft(docxml, 2) == ["", ""] && list_chairs(docxml).empty? and
          return ""

        <<~ROW
          <tr>#{list_draft(docxml, 2)&.map { |x| "<td>#{x}</td>" }&.join}
          <td>#{list_chairs(docxml)}</td></tr>
        ROW
      end
eref(docxml) click to toggle source
Calls superclass method
# File lib/isodoc/bipm/presentation_xml_convert.rb, line 195
def eref(docxml)
  super
  jcgm_eref(docxml, "//eref")
end
eref_localities1(target, type, from, to, delim, n, lang = "en") click to toggle source
Calls superclass method
# File lib/isodoc/bipm/presentation_xml_convert.rb, line 20
def eref_localities1(target, type, from, to, delim, n, lang = "en")
  if @jcgm
    @iso.eref_localities1(target, type, from, to, delim, n, lang)
  else
    super
  end
end
extract_brackets(node) click to toggle source
# File lib/isodoc/bipm/presentation_xml_convert.rb, line 226
def extract_brackets(node)
  start = node.at("./text()[1]")
  finish = node.at("./text()[last()]")
  if /^\[/.match?(start.text) && /\]$/.match?(finish.text)
    start.replace(start.text[1..-1])
    node.previous = "["
    finish = node.at("./text()[last()]")
    finish.replace(finish.text[0..-2])
    node.next = "]"
  end
end
extract_indexsee(v, terms, label) click to toggle source
# File lib/isodoc/bipm/index.rb, line 110
def extract_indexsee(v, terms, label)
  terms.each_with_object(v) do |t, v|
    term = t&.at(ns("./primary"))&.children&.to_xml
    term2 = t&.at(ns("./secondary"))&.children&.to_xml
    term3 = t&.at(ns("./tertiary"))&.children&.to_xml
    v[term] ||= {}
    v[term][term2] ||= {}
    v[term][term2][term3] ||= {}
    v[term][term2][term3][label] ||= []
    v[term][term2][term3][label] << t&.at(ns("./target"))&.children&.to_xml
    t.remove
  end
end
extract_indexterms(terms) click to toggle source

attributes are decoded into UTF-8, elements in extract_indexsee are still in entities

# File lib/isodoc/bipm/index.rb, line 130
def extract_indexterms(terms)
  terms.each_with_object({}) do |t, v|
    term = t&.at(ns("./primary"))&.children&.to_xml
    term2 = t&.at(ns("./secondary"))&.children&.to_xml
    term3 = t&.at(ns("./tertiary"))&.children&.to_xml
    index2bookmark(t)
    v[term] ||= {}
    v[term][term2] ||= {}
    v[term][term2][term3] ||= {}
    v[term][term2][term3][:xref] ||= []
    to = t["to"] ? "to='#{t['to']}' " : ""
    v[term][term2][term3][:xref] << "<xref target='#{t['id']}' #{to}pagenumber='true'/>"
  end
end
figure1(elem) click to toggle source
Calls superclass method
# File lib/isodoc/bipm/presentation_xml_convert.rb, line 37
def figure1(elem)
  if @jcgm
    @iso.xrefs = @xrefs
    @iso.figure1(elem)
  else super
  end
end
index(docxml) click to toggle source
# File lib/isodoc/bipm/index.rb, line 11
def index(docxml)
  unless docxml.at(ns("//index"))
    docxml.xpath(ns("//indexsect")).each(&:remove)
    return
  end
  i = docxml.at(ns("//indexsect")) ||
    docxml.root.add_child("<indexsect #{add_id}><title>#{@i18n.index}"\
                          "</title></indexsect>").first
  index = sort_indexterms(
    docxml.xpath(ns("//index")),
    docxml.xpath(ns("//index-xref[@also = 'false']")),
    docxml.xpath(ns("//index-xref[@also = 'true']"))
  )
  index1(docxml, i, index)
end
index1(docxml, i, index) click to toggle source
# File lib/isodoc/bipm/index.rb, line 27
def index1(docxml, i, index)
  index.keys.sort.each do |k|
    c = i.add_child "<clause #{add_id}><title>#{k}</title><ul></ul></clause>"
    words = index[k].keys.each_with_object({}) { |w, v| v[sortable(w).downcase] = w }
    words.keys.localize(@lang.to_sym).sort.to_a.each do |w|
      c.first.at(ns("./ul")).add_child index_entries(words, index[k], w)
    end
  end
  docxml.xpath(ns("//indexsect//xref")).each { |x| x.children.remove }
  @xrefs.bookmark_anchor_names(docxml)
end
index2bookmark(node) click to toggle source
# File lib/isodoc/bipm/index.rb, line 145
def index2bookmark(node)
  node.name = "bookmark"
  node.children.each(&:remove)
  node["id"] = "_#{UUIDTools::UUID.random_create}"
end
index_entries(words, index, primary) click to toggle source
# File lib/isodoc/bipm/index.rb, line 47
def index_entries(words, index, primary)
  ret = index_entries_head(words[primary],
                           index.dig(words[primary], nil, nil),
                           index_entries_opt)
  words2 = index[words[primary]]&.keys&.compact
    &.each_with_object({}) { |w, v| v[w.downcase] = w }
  unless words2.empty?
    ret += "<ul>"
    words2.keys.localize(@lang.to_sym).sort.to_a.each do |w|
      ret += index_entries2(words2, index[words[primary]], w)
    end
    ret += "</ul>"
  end
  "#{ret}</li>"
end
index_entries2(words, index, secondary) click to toggle source
# File lib/isodoc/bipm/index.rb, line 63
def index_entries2(words, index, secondary)
  ret = index_entries_head(words[secondary],
                           index.dig(words[secondary], nil),
                           index_entries_opt)
  words3 = index[words[secondary]]&.keys&.reject { |k| k.nil? }
    &.each_with_object({}) { |w, v| v[w.downcase] = w }
  unless words3.empty?
    ret += "<ul>"
    words3.keys.localize(@lang.to_sym).sort.to_a.each do |w|
      ret += (index_entries_head(words3[w], index[words[secondary]][words3[w]], index_entries_opt) + "</li>")
    end
    ret += "</ul>"
  end
  "#{ret}</li>"
end
index_entries_head(head, entries, opt) click to toggle source
# File lib/isodoc/bipm/index.rb, line 79
def index_entries_head(head, entries, opt)
  ret = "<li>#{head}"
  xref = entries&.dig(:xref)&.join(", ")
  see_sort = entries&.dig(:see)&.each_with_object({}) { |w, v| v[sortable(w).downcase] = w }
  see = see_sort&.keys&.localize(@lang.to_sym)&.sort&.to_a&.map { |k| see_sort[k] }&.join(", ")
  also_sort = entries&.dig(:also)&.each_with_object({}) { |w, v| v[sortable(w).downcase] = w }
  also = also_sort&.keys&.localize(@lang.to_sym)&.sort&.to_a&.map { |k| also_sort[k] }&.join(", ")
  ret += "#{opt[:xref_lbl]} #{xref}" if xref
  ret += "#{opt[:see_lbl]} #{see}" if see
  ret += "#{opt[:also_lbl]} #{also}" if also
  ret
end
index_entries_opt() click to toggle source
# File lib/isodoc/bipm/index.rb, line 43
def index_entries_opt
  { xref_lbl: ", ", see_lbl: ", #{see_lbl}", also_lbl: ", #{also_lbl}" }
end
jcgm_eref(docxml, xpath) click to toggle source
# File lib/isodoc/bipm/presentation_xml_convert.rb, line 210
def jcgm_eref(docxml, xpath)
  return unless @jcgm

  docxml.xpath(ns(xpath)).each do |x|
    extract_brackets(x)
  end
  # merge adjacent text nodes
  docxml.root.replace(Nokogiri::XML(docxml.root.to_xml).root)
  docxml.xpath(ns(xpath)).each do |x| # rubocop: disable Style/CombinableLoops
    if x&.next&.text? && /^\],\s+\[$/.match?(x&.next&.text) &&
        %w(eref origin source).include?(x&.next&.next&.name)
      x.next.replace(", ")
    end
  end
end
list_authors(xml) click to toggle source
# File lib/isodoc/bipm/presentation_xml_convert.rb, line 130
def list_authors(xml)
  ret = list_people(
    xml, "//bibdata/contributor[xmlns:role/@type = 'author']/person"
  )
  @i18n.multiple_and(ret, @i18n.get["and"])
end
list_chairs(xml) click to toggle source
# File lib/isodoc/bipm/presentation_xml_convert.rb, line 149
def list_chairs(xml)
  ret = list_people(xml, "//bibdata/contributor#{CHAIR}/person")
  ret.empty? and return ""
  role = xml&.at(ns("//bibdata/contributor#{CHAIR}/role"))&.text
  label = ret.size > 1 && role ? "#{role}s" : role
  "#{label}: #{@i18n.multiple_and(ret, @i18n.get['and'])}"
end
list_cochairs(xml) click to toggle source
# File lib/isodoc/bipm/presentation_xml_convert.rb, line 141
def list_cochairs(xml)
  ret = list_people(xml, "//bibdata/contributor[#{COCHAIR}]/person")
  ret.empty? and return ""
  role = xml&.at(ns("//bibdata/contributor[#{COCHAIR}]/role"))&.text
  label = ret.size > 1 && role ? "#{role}s" : role
  "#{label}: #{@i18n.multiple_and(ret, @i18n.get['and'])}"
end
list_draft(xml, idx) click to toggle source
# File lib/isodoc/bipm/presentation_xml_convert.rb, line 121
def list_draft(xml, idx)
  d = xml.at(ns("//bibdata/relation[@type = 'supersedes'][#{idx}]"\
                "/bibitem")) or return ["", ""]

  draft = d&.at(ns("./version/draft"))&.text and draft = "Draft #{draft}"
  edn = d&.at(ns("./edition"))&.text and edn = "Version #{edn}"
  [[draft, edn].join(" "), d&.at(ns("./date"))&.text]
end
list_drafts(xml) click to toggle source
# File lib/isodoc/bipm/presentation_xml_convert.rb, line 110
def list_drafts(xml)
  ret = ""
  i = 3
  while list_draft(xml, i) != ["", ""]
    ret += "<tr>#{list_draft(xml, i).map { |x| "<td>#{x}</td>" }.join} "\
           "<td/></tr>"
    i += 1
  end
  ret
end
list_people(xml, xpath) click to toggle source
# File lib/isodoc/bipm/presentation_xml_convert.rb, line 157
def list_people(xml, xpath)
  ret = []
  xml.xpath(ns(xpath)).each do |p|
    name = p&.at(ns("./name/completename"))&.text
    aff = p&.at(ns("./affiliation/organization/abbreviation"))&.text ||
      p&.at(ns("./affiliation/organization/name"))&.text
    c = name || ""
    aff and c += " (#{aff})"
    ret << c
  end
  ret
end
mathml1(elem, locale) click to toggle source
# File lib/isodoc/bipm/presentation_xml_convert.rb, line 175
def mathml1(elem, locale)
  asciimath_dup(elem)
  localize_maths(elem, locale)
end
origin(docxml) click to toggle source
Calls superclass method
# File lib/isodoc/bipm/presentation_xml_convert.rb, line 200
def origin(docxml)
  super
  jcgm_eref(docxml, "//origin[not(termref)]")
end
quotesource(docxml) click to toggle source
Calls superclass method
# File lib/isodoc/bipm/presentation_xml_convert.rb, line 205
def quotesource(docxml)
  super
  jcgm_eref(docxml, "//quote/source")
end
see_lbl() click to toggle source
# File lib/isodoc/bipm/index.rb, line 92
def see_lbl
  @lang == "en" ? @i18n.see : "<em>#{@i18n.see}</em>"
end
sort_indexterms(terms, see, also) click to toggle source
# File lib/isodoc/bipm/index.rb, line 100
def sort_indexterms(terms, see, also)
  index = extract_indexterms(terms)
  index = extract_indexsee(index, see, :see)
  index = extract_indexsee(index, also, :also)
  index.keys.sort.each_with_object({}) do |k, v|
    v[sortable(k)[0].upcase.transliterate] ||= {}
    v[sortable(k)[0].upcase.transliterate][k] = index[k]
  end
end
sortable(str) click to toggle source
# File lib/isodoc/bipm/index.rb, line 39
def sortable(str)
  HTMLEntities.new.decode(Nokogiri::XML.fragment(str).text)
end
table1(elem) click to toggle source
# File lib/isodoc/bipm/presentation_xml_convert.rb, line 28
def table1(elem)
  return if labelled_ancestor(elem)
  return if elem["unnumbered"]

  n = @xrefs.anchor(elem["id"], :label, false)
  prefix_name(elem, ".<tab/>",
              l10n("#{@i18n.table.capitalize} #{n}"), "name")
end
twitter_cldr_localiser_symbols() click to toggle source
# File lib/isodoc/bipm/presentation_xml_convert.rb, line 170
def twitter_cldr_localiser_symbols
  { group: "&#xA0;", fraction_group: "&#xA0;",
    fraction_group_digits: 3 }
end
xml_encode_attr(str) click to toggle source
# File lib/isodoc/bipm/index.rb, line 124
def xml_encode_attr(str)
  HTMLEntities.new.encode(str, :basic, :hexadecimal)
    .gsub(/&#x([^;]+);/) { |_x| "&#x#{$1.upcase};" }
end