From b52098cafd409ca274893649c072be0cea0c1a01 Mon Sep 17 00:00:00 2001 From: Nick Nicholas Date: Tue, 17 Dec 2024 23:01:12 +0200 Subject: [PATCH 1/2] move index code to isodoc; fix bookmark/@to: https://github.com/metanorma/bipm-si-brochure/issues/242 --- Gemfile.devel | 1 + lib/isodoc/bipm/index.rb | 163 -------------------- lib/isodoc/bipm/presentation_xml_convert.rb | 42 +++-- spec/isodoc/html_convert_spec.rb | 2 +- 4 files changed, 20 insertions(+), 188 deletions(-) create mode 100644 Gemfile.devel delete mode 100644 lib/isodoc/bipm/index.rb diff --git a/Gemfile.devel b/Gemfile.devel new file mode 100644 index 0000000..0fde8df --- /dev/null +++ b/Gemfile.devel @@ -0,0 +1 @@ +gem "isodoc", git: "https://github.com/metanorma/isodoc", branch: "features/indexsect" diff --git a/lib/isodoc/bipm/index.rb b/lib/isodoc/bipm/index.rb deleted file mode 100644 index dbfc051..0000000 --- a/lib/isodoc/bipm/index.rb +++ /dev/null @@ -1,163 +0,0 @@ -require "twitter_cldr" -require "sterile" - -module IsoDoc - module Bipm - class PresentationXMLConvert < IsoDoc::Generic::PresentationXMLConvert - def add_id - %(id="_#{UUIDTools::UUID.random_create}") - end - - 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("#{@i18n.index}" \ - "").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 - - def index1(docxml, i, index) - index.keys.sort.each do |k| - c = i.add_child "#{k}" - words = index[k].keys.each_with_object({}) do |w, v| - v[sortable(w).downcase] = w - end - 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 - - def sortable(str) - HTMLEntities.new.decode(Nokogiri::XML.fragment(str).text) - end - - def index_entries_opt - { xref_lbl: ", ", see_lbl: ", #{see_lbl}", also_lbl: ", #{also_lbl}" } - end - - 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 += "" - end - "#{ret}" - end - - 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(&:nil?) - &.each_with_object({}) { |w, v| v[w.downcase] = w } - unless words3.empty? - ret += "" - end - "#{ret}" - end - - def index_entries_head(head, entries, opt) - ret = "
  • #{head}" - xref = entries&.dig(:xref)&.join(", ") - see_sort = entries&.dig(:see)&.each_with_object({}) do |w, v| - v[sortable(w).downcase] = w - end - see = see_sort&.keys&.localize(@lang.to_sym)&.sort&.to_a&.map do |k| - see_sort[k] - end&.join(", ") - also_sort = entries&.dig(:also)&.each_with_object({}) do |w, v| - v[sortable(w).downcase] = w - end - also = also_sort&.keys&.localize(@lang.to_sym)&.sort&.to_a&.map do |k| - also_sort[k] - end&.join(", ") - ret += "#{opt[:xref_lbl]} #{xref}" if xref - ret += "#{opt[:see_lbl]} #{see}" if see - ret += "#{opt[:also_lbl]} #{also}" if also - ret - end - - def see_lbl - @lang == "en" ? @i18n.see : "#{@i18n.see}" - end - - def also_lbl - @lang == "en" ? @i18n.see_also : "#{@i18n.see_also}" - end - - 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 - - def extract_indexsee(idx, terms, label) - terms.each_with_object(idx) do |t, v| - term = to_xml(t.at(ns("./primary"))&.children) - term2 = to_xml(t.at(ns("./secondary"))&.children) - term3 = to_xml(t.at(ns("./tertiary"))&.children) - v[term] ||= {} - v[term][term2] ||= {} - v[term][term2][term3] ||= {} - v[term][term2][term3][label] ||= [] - v[term][term2][term3][label] << to_xml(t.at(ns("./target"))&.children) - t.remove - end - end - - def xml_encode_attr(str) - HTMLEntities.new.encode(str, :basic, :hexadecimal) - .gsub(/&#x([^;]+);/) { |_x| "&#x#{$1.upcase};" } - end - - # attributes are decoded into UTF-8, elements in extract_indexsee are still in entities - def extract_indexterms(terms) - terms.each_with_object({}) do |t, v| - term = to_xml(t.at(ns("./primary"))&.children) - term2 = to_xml(t.at(ns("./secondary"))&.children) - term3 = to_xml(t.at(ns("./tertiary"))&.children) - 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] << "" - end - end - - def index2bookmark(node) - node.name = "bookmark" - node.children.each(&:remove) - node["id"] = "_#{UUIDTools::UUID.random_create}" - end - end - end -end diff --git a/lib/isodoc/bipm/presentation_xml_convert.rb b/lib/isodoc/bipm/presentation_xml_convert.rb index eeb36c8..63d2d2f 100644 --- a/lib/isodoc/bipm/presentation_xml_convert.rb +++ b/lib/isodoc/bipm/presentation_xml_convert.rb @@ -2,7 +2,6 @@ require "metanorma-generic" require "metanorma-iso" require_relative "init" -require_relative "index" require_relative "doccontrol" require_relative "../../relaton/render/general" require_relative "presentation_blocks" @@ -33,16 +32,6 @@ def eref_localities1(opt) super end - # KILL - def annex1x(elem) - @jcgm and return super - elem["unnumbered"] == "true" and return - lbl = @xrefs.anchor(elem["id"], :label) - t = elem.at(ns("./title")) and - t.children = "#{to_xml(t.children)}" - prefix_name(elem, ".", lbl, "title") - end - def annex_delim(elem) @jcgm and return super "." @@ -63,19 +52,6 @@ def clause1(elem) super end - # KILL - def prefix_namex(node, delims, number, elem) - number.nil? || number.empty? and return - unless name = node.at(ns("./#{elem}[not(@type = 'quoted')]")) - node.at(ns("./#{elem}[@type = 'quoted']")) and return - node.add_first_child "<#{elem}>" - name = node.children.first - end - if name.children.empty? then name.add_child(cleanup_entities(number)) - else (name.children.first.previous = "#{number}#{delim}") - end - end - def prefix_name(node, delims, number, elem) if n = node.at(ns("./#{elem}[@type = 'quoted']")) n1 = n.dup @@ -204,6 +180,24 @@ def termsource_modification(elem) termsource_add_modification_text(elem.at(ns("./modification"))) end + def enable_indexsect + true + end + + def index1(docxml, indexsect, index) + index.keys.sort.each do |k| + c = indexsect.add_child "#{k}
      " + words = index[k].keys.each_with_object({}) do |w, v| + v[sortable(w).downcase] = w + end + 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 + include Init end end diff --git a/spec/isodoc/html_convert_spec.rb b/spec/isodoc/html_convert_spec.rb index cc40a40..603b66b 100644 --- a/spec/isodoc/html_convert_spec.rb +++ b/spec/isodoc/html_convert_spec.rb @@ -1814,7 +1814,7 @@ Index - + From a2ed8bb607ccd14ed8b91058d58cb2cb332fdfb9 Mon Sep 17 00:00:00 2001 From: Nick Nicholas Date: Wed, 18 Dec 2024 15:29:17 +0200 Subject: [PATCH 2/2] relaton 1.20 --- Gemfile.devel | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Gemfile.devel b/Gemfile.devel index 0fde8df..d24bd8a 100644 --- a/Gemfile.devel +++ b/Gemfile.devel @@ -1 +1,3 @@ -gem "isodoc", git: "https://github.com/metanorma/isodoc", branch: "features/indexsect" +gem "relaton-render", git: "https://github.com/relaton/relaton-render", branch: "main" +gem "isodoc", git: "https://github.com/metanorma/isodoc", branch: "fix/relaton-render-0.9" +gem "metanorma-standoc", git: "https://github.com/metanorma/metanorma-standoc", branch: "fix/fn-index-dup"