class IsoDoc::Iso::PresentationXMLConvert
Public Instance Methods
add_id()
click to toggle source
# File lib/isodoc/iso/index.rb, line 4 def add_id %(id="_#{UUIDTools::UUID.random_create}") end
also_lbl()
click to toggle source
# File lib/isodoc/iso/index.rb, line 106 def also_lbl @lang == "en" ? @i18n.see_also : "<em>#{@i18n.see_also}</em>" end
annex(isoxml)
click to toggle source
Calls superclass method
# File lib/isodoc/iso/presentation_xml_convert.rb, line 16 def annex(isoxml) amd(isoxml) and @suppressheadingnumbers = @oldsuppressheadingnumbers super isoxml.xpath(ns("//annex//clause | //annex//appendix")).each do |f| clause1(f) end amd(isoxml) and @suppressheadingnumbers = true end
clause(docxml)
click to toggle source
# File lib/isodoc/iso/presentation_xml_convert.rb, line 107 def clause(docxml) docxml.xpath(ns("//clause[not(ancestor::annex)] | "\ "//terms | //definitions | //references | "\ "//preface/introduction[clause]")) .each do |f| clause1(f) end end
clause1(node)
click to toggle source
Calls superclass method
# File lib/isodoc/iso/presentation_xml_convert.rb, line 99 def clause1(node) if !node.at(ns("./title")) && !%w(sections preface bibliography).include?(node.parent.name) node["inline-header"] = "true" end super end
concept(docxml)
click to toggle source
# File lib/isodoc/iso/presentation_xml_convert.rb, line 116 def concept(docxml) docxml.xpath(ns("//terms//concept")).each_with_object({}) do |f, m| concept_term(f, m) end docxml.xpath(ns("//concept")).each do |node| concept_render(node, ital: node["ital"] || "false", ref: node["ref"] || "false", linkref: node["linkref"] || "true", linkmention: node["linkmention"] || "false") end end
concept1(node)
click to toggle source
# File lib/isodoc/iso/presentation_xml_convert.rb, line 153 def concept1(node) node.replace(node&.at(ns("./renderterm"))&.children || node&.at(ns("./refterm"))&.children || node.children) end
concept1_ref_content(ref)
click to toggle source
# File lib/isodoc/iso/presentation_xml_convert.rb, line 144 def concept1_ref_content(ref) if ref.name == "termref" ref.replace(@i18n.term_defined_in.sub(/%/, ref.to_xml)) else ref.replace("(#{ref.to_xml})") end end
concept_term(node, seen)
click to toggle source
# File lib/isodoc/iso/presentation_xml_convert.rb, line 128 def concept_term(node, seen) term = node&.at(ns("./refterm"))&.to_xml if term && seen[term] concept_render(node, ital: node["ital"] || "false", ref: node["ref"] || "false", linkref: node["linkref"] || "true", linkmention: node["linkmention"] || "false") else concept_render(node, ital: node["ital"] || "true", ref: node["ref"] || "true", linkref: node["linkref"] || "true", linkmention: node["linkmention"] || "false") end seen[term] = true if term seen end
convert1(docxml, filename, dir)
click to toggle source
Calls superclass method
# File lib/isodoc/iso/presentation_xml_convert.rb, line 8 def convert1(docxml, filename, dir) if amd(docxml) @oldsuppressheadingnumbers = @suppressheadingnumbers @suppressheadingnumbers = true end super end
display_order(docxml)
click to toggle source
we're assuming terms and clauses in the right place for display, to cope with multiple terms sections
# File lib/isodoc/iso/presentation_xml_convert.rb, line 162 def display_order(docxml) i = 0 i = display_order_xpath(docxml, "//preface/*", i) i = display_order_at(docxml, "//clause[@type = 'scope']", i) i = display_order_at(docxml, @xrefs.klass.norm_ref_xpath, i) # i = display_order_at(docxml, "//sections/terms | "\ # "//sections/clause[descendant::terms]", i) # i = display_order_at(docxml, "//sections/definitions", i) # i = display_order_xpath(docxml, @xrefs.klass.middle_clause(docxml), i) i = display_order_xpath(docxml, "//sections/clause[not(@type = 'scope')] | "\ "//sections/terms | //sections/definitions", i) i = display_order_xpath(docxml, "//annex", i) i = display_order_xpath(docxml, @xrefs.klass.bibliography_xpath, i) display_order_xpath(docxml, "//indexsect", i) end
eref_localities1(target, type, from, upto, delim, node, lang = "en")
click to toggle source
# File lib/isodoc/iso/presentation_xml_convert.rb, line 65 def eref_localities1(target, type, from, upto, delim, node, lang = "en") return "" if type == "anchor" subsection = from&.text&.match(/\./) type = type.downcase lang == "zh" and return l10n(eref_localities1_zh(target, type, from, upto, node, delim)) ret = if delim == ";" then ";" else type == "list" ? "" : delim end ret += eref_locality_populate(type, node) unless subsection && type == "clause" || type == "list" || target.match(/^IEV$|^IEC 60050-/) ret += " #{from.text}" if from ret += "–#{upto.text}" if upto ret += ")" if type == "list" l10n(ret) end
eref_localities1_zh(target, type, from, upto, node, delim)
click to toggle source
# File lib/isodoc/iso/presentation_xml_convert.rb, line 48 def eref_localities1_zh(target, type, from, upto, node, delim) subsection = from&.text&.match(/\./) ret = if delim == ";" ";" else type == "list" ? "" : delim end ret += " 第#{from.text}" if from ret += "–#{upto.text}" if upto loc = (@i18n.locality[type] || type.sub(/^locality:/, "").capitalize) ret += " #{loc}" unless subsection && type == "clause" || type == "list" || target.match(/^IEV$|^IEC 60050-/) || node["droploc"] == "true" ret += ")" if type == "list" ret end
example1(node)
click to toggle source
# File lib/isodoc/iso/presentation_xml_convert.rb, line 38 def example1(node) n = @xrefs.get[node["id"]] lbl = if n.nil? || n[:label].nil? || n[:label].empty? @i18n.example else l10n("#{@i18n.example} #{n[:label]}") end prefix_name(node, " — ", lbl, "name") end
example_span_label(_node, div, name)
click to toggle source
# File lib/isodoc/iso/presentation_xml_convert.rb, line 91 def example_span_label(_node, div, name) return if name.nil? div.span **{ class: "example_label" } do |_p| name.children.each { |n| parse(n, div) } end end
extract_indexsee(val, terms, label)
click to toggle source
# File lib/isodoc/iso/index.rb, line 120 def extract_indexsee(val, terms, label) terms.each_with_object(val) 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/iso/index.rb, line 142 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(node)
click to toggle source
# File lib/isodoc/iso/presentation_xml_convert.rb, line 29 def figure1(node) return if labelled_ancestor(node) && node.ancestors("figure").empty? lbl = @xrefs.anchor(node["id"], :label, false) or return figname = node.parent.name == "figure" ? "" : "#{@i18n.figure} " connective = node.parent.name == "figure" ? " " : " — " prefix_name(node, connective, l10n("#{figname}#{lbl}"), "name") end
index(docxml)
click to toggle source
# File lib/isodoc/iso/index.rb, line 8 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/iso/index.rb, line 21 def index1(docxml, i, index) c = i.add_child("<ul></ul>").first 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({}) 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) c.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/iso/index.rb, line 157 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/iso/index.rb, line 45 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&.reject do |k| k.nil? end&.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/iso/index.rb, line 62 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 do |k| k.nil? end&.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/iso/index.rb, line 81 def index_entries_head(head, entries, opt) ret = "<li>#{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
index_entries_opt()
click to toggle source
# File lib/isodoc/iso/index.rb, line 41 def index_entries_opt { xref_lbl: ", ", see_lbl: ", #{see_lbl}", also_lbl: ", #{also_lbl}" } end
prefix_container(container, linkend, target)
click to toggle source
# File lib/isodoc/iso/presentation_xml_convert.rb, line 86 def prefix_container(container, linkend, target) delim = @xrefs.anchor(target, :type) == "listitem" ? " " : ", " l10n(@xrefs.anchor(container, :xref) + delim + linkend) end
see_lbl()
click to toggle source
# File lib/isodoc/iso/index.rb, line 102 def see_lbl @lang == "en" ? @i18n.see : "<em>#{@i18n.see}</em>" end
sort_indexterms(terms, see, also)
click to toggle source
# File lib/isodoc/iso/index.rb, line 110 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/iso/index.rb, line 37 def sortable(str) HTMLEntities.new.decode(Nokogiri::XML.fragment(str).text) end
xml_encode_attr(str)
click to toggle source
# File lib/isodoc/iso/index.rb, line 134 def xml_encode_attr(str) HTMLEntities.new.encode(str, :basic, :hexadecimal) .gsub(/&#x([^;]+);/) do |_x| "&#x#{$1.upcase};" end end
xref_init(lang, script, klass, labels, options)
click to toggle source
# File lib/isodoc/iso/presentation_xml_convert.rb, line 25 def xref_init(lang, script, klass, labels, options) @xrefs = Xref.new(lang, script, klass, labels, options) end