Class: IsoDoc::Iso::PresentationXMLConvert
- Inherits:
-
PresentationXMLConvert
- Object
- PresentationXMLConvert
- IsoDoc::Iso::PresentationXMLConvert
- Includes:
- Init
- Defined in:
- lib/isodoc/iso/index.rb,
lib/isodoc/iso/presentation_xml_convert.rb
Instance Method Summary collapse
- #add_id ⇒ Object
- #also_lbl ⇒ Object
- #annex(isoxml) ⇒ Object
- #clause(docxml) ⇒ Object
- #clause1(node) ⇒ Object
- #concept(docxml) ⇒ Object
- #concept1(node) ⇒ Object
- #concept1_ref_content(ref) ⇒ Object
- #concept_term(docxml) ⇒ Object
- #concept_term1(node, seen) ⇒ Object
- #convert1(docxml, filename, dir) ⇒ Object
-
#display_order(docxml) ⇒ Object
we’re assuming terms and clauses in the right place for display, to cope with multiple terms sections.
- #eref_delim(delim, type) ⇒ Object
- #eref_localities1(target, type, from, upto, delim, node, lang = "en") ⇒ Object
- #eref_localities1_zh(target, type, from, upto, node, delim) ⇒ Object
- #example1(node) ⇒ Object
- #example_span_label(_node, div, name) ⇒ Object
- #extract_indexsee(val, terms, label) ⇒ Object
-
#extract_indexterms(terms) ⇒ Object
attributes are decoded into UTF-8, elements in extract_indexsee are still in entities.
- #extract_indexterms_init(term) ⇒ Object
- #figure1(node) ⇒ Object
- #index(xml) ⇒ Object
- #index1(docxml, indexsect, index) ⇒ Object
- #index1_cleanup(docxml) ⇒ Object
- #index2bookmark(node) ⇒ Object
- #index_entries(words, index, primary) ⇒ Object
- #index_entries2(words, index, secondary) ⇒ Object
- #index_entries_head(head, entries, opt) ⇒ Object
- #index_entries_opt ⇒ Object
- #index_entries_see(entries, label) ⇒ Object
- #insertall_after_here(node, insert, name) ⇒ Object
- #prefix_container(container, linkend, target) ⇒ Object
- #prefix_domain_to_definition(elem) ⇒ Object
- #see_lbl ⇒ Object
- #sort_indexterms(terms, see, also) ⇒ Object
- #sortable(str) ⇒ Object
- #term_hash_init(hash, term, term2, term3, label) ⇒ Object
- #termdefinition1(elem) ⇒ Object
- #termexamples_before_termnotes(node) ⇒ Object
- #terms(docxml) ⇒ Object
- #xml_encode_attr(str) ⇒ Object
- #xref_init(lang, script, klass, labels, options) ⇒ Object
Methods included from Init
#amd, #clausedelim, #i18n_init, #metadata_init
Instance Method Details
#add_id ⇒ Object
4 5 6 |
# File 'lib/isodoc/iso/index.rb', line 4 def add_id %(id="_#{UUIDTools::UUID.random_create}") end |
#also_lbl ⇒ Object
109 110 111 |
# File 'lib/isodoc/iso/index.rb', line 109 def also_lbl @lang == "en" ? @i18n.see_also : "<em>#{@i18n.see_also}</em>" end |
#annex(isoxml) ⇒ Object
16 17 18 19 20 21 22 23 |
# 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) ⇒ Object
106 107 108 109 110 111 112 113 |
# File 'lib/isodoc/iso/presentation_xml_convert.rb', line 106 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) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/isodoc/iso/presentation_xml_convert.rb', line 98 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) ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/isodoc/iso/presentation_xml_convert.rb', line 115 def concept(docxml) concept_term(docxml) docxml.xpath(ns("//concept")).each do |node| concept_render(node, ital: "false", ref: "false", linkref: "true", linkmention: "false") end end |
#concept1(node) ⇒ Object
153 154 155 156 157 |
# 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) ⇒ Object
144 145 146 147 148 149 150 151 |
# 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(docxml) ⇒ Object
123 124 125 126 127 128 129 130 |
# File 'lib/isodoc/iso/presentation_xml_convert.rb', line 123 def concept_term(docxml) docxml.xpath(ns("//term")).each do |f| m = {} f.xpath(ns(".//concept")).each do |c| concept_term1(c, m) end end end |
#concept_term1(node, seen) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/isodoc/iso/presentation_xml_convert.rb', line 132 def concept_term1(node, seen) term = node&.at(ns("./refterm"))&.to_xml if term && seen[term] concept_render(node, ital: "false", ref: "false", linkref: "true", linkmention: "false") else concept_render(node, ital: "true", ref: "true", linkref: "true", linkmention: "false") end seen[term] = true if term seen end |
#convert1(docxml, filename, dir) ⇒ Object
8 9 10 11 12 13 14 |
# 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) ⇒ Object
we’re assuming terms and clauses in the right place for display, to cope with multiple terms sections
162 163 164 165 166 167 168 169 170 171 172 173 |
# 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_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_delim(delim, type) ⇒ Object
48 49 50 51 52 |
# File 'lib/isodoc/iso/presentation_xml_convert.rb', line 48 def eref_delim(delim, type) if delim == ";" then ";" else type == "list" ? "" : delim end end |
#eref_localities1(target, type, from, upto, delim, node, lang = "en") ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/isodoc/iso/presentation_xml_convert.rb', line 67 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 = eref_delim(delim, type) 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) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/isodoc/iso/presentation_xml_convert.rb', line 54 def eref_localities1_zh(target, type, from, upto, node, delim) subsection = from&.text&.match(/\./) ret = eref_delim(delim, type) 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) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# 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) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/isodoc/iso/presentation_xml_convert.rb', line 90 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) ⇒ Object
123 124 125 126 127 128 129 130 |
# File 'lib/isodoc/iso/index.rb', line 123 def extract_indexsee(val, terms, label) terms.each_with_object(val) do |t, v| term, term2, term3 = extract_indexterms_init(t) term_hash_init(v, term, term2, term3, label) v[term][term2][term3][label] << t&.at(ns("./target"))&.children&.to_xml t.remove end end |
#extract_indexterms(terms) ⇒ Object
attributes are decoded into UTF-8, elements in extract_indexsee are still in entities
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/isodoc/iso/index.rb', line 141 def extract_indexterms(terms) terms.each_with_object({}) do |t, v| term, term2, term3 = extract_indexterms_init(t) index2bookmark(t) term_hash_init(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 |
#extract_indexterms_init(term) ⇒ Object
152 153 154 155 156 |
# File 'lib/isodoc/iso/index.rb', line 152 def extract_indexterms_init(term) %w(primary secondary tertiary).each_with_object([]) do |x, m| m << term&.at(ns("./#{x}"))&.children&.to_xml end end |
#figure1(node) ⇒ Object
29 30 31 32 33 34 35 36 |
# 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(xml) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/isodoc/iso/index.rb', line 8 def index(xml) if xml.at(ns("//index")) i = xml.at(ns("//indexsect")) || xml.root.add_child("<indexsect #{add_id}><title>#{@i18n.index}"\ "</title></indexsect>").first index = sort_indexterms(xml.xpath(ns("//index")), xml.xpath(ns("//index-xref[@also = 'false']")), xml.xpath(ns("//index-xref[@also = 'true']"))) index1(xml, i, index) else xml.xpath(ns("//indexsect")).each(&:remove) end end |
#index1(docxml, indexsect, index) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/isodoc/iso/index.rb', line 21 def index1(docxml, indexsect, index) c = indexsect.add_child("<ul></ul>").first index.keys.sort.each do |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.add_child index_entries(words, index[k], w) end end index1_cleanup(docxml) end |
#index1_cleanup(docxml) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/isodoc/iso/index.rb', line 34 def index1_cleanup(docxml) docxml.xpath(ns("//indexsect//xref")).each do |x| x.children.remove end @xrefs.bookmark_anchor_names(docxml) end |
#index2bookmark(node) ⇒ Object
165 166 167 168 169 |
# File 'lib/isodoc/iso/index.rb', line 165 def index2bookmark(node) node.name = "bookmark" node.children.each(&:remove) node["id"] = "_#{UUIDTools::UUID.random_create}" end |
#index_entries(words, index, primary) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/isodoc/iso/index.rb', line 49 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) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/isodoc/iso/index.rb', line 66 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) ⇒ Object
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/isodoc/iso/index.rb', line 85 def index_entries_head(head, entries, opt) ret = "<li>#{head}" xref = entries&.dig(:xref)&.join(", ") see = index_entries_see(entries, :see) also = index_entries_see(entries, :also) 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 ⇒ Object
45 46 47 |
# File 'lib/isodoc/iso/index.rb', line 45 def index_entries_opt { xref_lbl: ", ", see_lbl: ", #{see_lbl}", also_lbl: ", #{also_lbl}" } end |
#index_entries_see(entries, label) ⇒ Object
96 97 98 99 100 101 102 103 |
# File 'lib/isodoc/iso/index.rb', line 96 def index_entries_see(entries, label) see_sort = entries&.dig(label)&.each_with_object({}) do |w, v| v[sortable(w).downcase] = w end see_sort&.keys&.localize(@lang.to_sym)&.sort&.to_a&.map do |k| see_sort[k] end&.join(", ") end |
#insertall_after_here(node, insert, name) ⇒ Object
188 189 190 191 192 193 194 195 196 |
# File 'lib/isodoc/iso/presentation_xml_convert.rb', line 188 def insertall_after_here(node, insert, name) node.children.each do |n| next unless n.name == name insert.next = n.remove insert = n end insert end |
#prefix_container(container, linkend, target) ⇒ Object
85 86 87 88 |
# File 'lib/isodoc/iso/presentation_xml_convert.rb', line 85 def prefix_container(container, linkend, target) delim = @xrefs.anchor(target, :type) == "listitem" ? " " : ", " l10n(@xrefs.anchor(container, :xref) + delim + linkend) end |
#prefix_domain_to_definition(elem) ⇒ Object
180 181 182 183 184 185 186 |
# File 'lib/isodoc/iso/presentation_xml_convert.rb', line 180 def prefix_domain_to_definition(elem) ((d = elem.at(ns("./domain"))) && (v = elem.at(ns("./definition/verbal-definition"))) && v.elements.first.name == "p") or return v.elements.first.children.first.previous = "<#{d.remove.children.to_xml}> " end |
#see_lbl ⇒ Object
105 106 107 |
# File 'lib/isodoc/iso/index.rb', line 105 def see_lbl @lang == "en" ? @i18n.see : "<em>#{@i18n.see}</em>" end |
#sort_indexterms(terms, see, also) ⇒ Object
113 114 115 116 117 118 119 120 121 |
# File 'lib/isodoc/iso/index.rb', line 113 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) ⇒ Object
41 42 43 |
# File 'lib/isodoc/iso/index.rb', line 41 def sortable(str) HTMLEntities.new.decode(Nokogiri::XML.fragment(str).text) end |
#term_hash_init(hash, term, term2, term3, label) ⇒ Object
158 159 160 161 162 163 |
# File 'lib/isodoc/iso/index.rb', line 158 def term_hash_init(hash, term, term2, term3, label) hash[term] ||= {} hash[term][term2] ||= {} hash[term][term2][term3] ||= {} hash[term][term2][term3][label] ||= [] end |
#termdefinition1(elem) ⇒ Object
175 176 177 178 |
# File 'lib/isodoc/iso/presentation_xml_convert.rb', line 175 def termdefinition1(elem) prefix_domain_to_definition(elem) super end |
#termexamples_before_termnotes(node) ⇒ Object
198 199 200 201 202 203 |
# File 'lib/isodoc/iso/presentation_xml_convert.rb', line 198 def termexamples_before_termnotes(node) return unless insert = node.at(ns("./definition")) insert = insertall_after_here(node, insert, "termexample") insertall_after_here(node, insert, "termnote") end |
#terms(docxml) ⇒ Object
205 206 207 208 209 210 |
# File 'lib/isodoc/iso/presentation_xml_convert.rb', line 205 def terms(docxml) docxml.xpath(ns("//term[termnote][termexample]")).each do |node| termexamples_before_termnotes(node) end super end |
#xml_encode_attr(str) ⇒ Object
132 133 134 135 136 137 |
# File 'lib/isodoc/iso/index.rb', line 132 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) ⇒ Object
25 26 27 |
# File 'lib/isodoc/iso/presentation_xml_convert.rb', line 25 def xref_init(lang, script, klass, labels, ) @xrefs = Xref.new(lang, script, klass, labels, ) end |