Module: IsoDoc::ITU::BaseConvert
- Included in:
- HtmlConvert, WordConvert
- Defined in:
- lib/isodoc/itu/ref.rb,
lib/isodoc/itu/terms.rb,
lib/isodoc/itu/base_convert.rb
Constant Summary collapse
- IGNORE_IDS =
"@type = 'DOI' or @type = 'ISSN' or @type = 'ISBN' or @type = 'rfc-anchor'".freeze
- FRONT_CLAUSE =
"//*[parent::preface]"\ "[not(local-name() = 'abstract')]".freeze
Instance Method Summary collapse
- #add_parse(node, out) ⇒ Object
- #annex(isoxml, out) ⇒ Object
- #annex_name(annex, name, div) ⇒ Object
- #annex_obligation_subtitle(annex, div) ⇒ Object
- #biblio_list(f, div, biblio) ⇒ Object
- #bracket_if_num(x) ⇒ Object
- #bracket_opt(b) ⇒ Object
- #clausedelim ⇒ Object
- #cleanup(docxml) ⇒ Object
- #del_parse(node, out) ⇒ Object
- #doctype_title(id) ⇒ Object
- #eref_parse(node, out) ⇒ Object
- #error_parse(node, out) ⇒ Object
- #fileloc(loc) ⇒ Object
- #get_eref_linkend(node) ⇒ Object
- #i18n_init(lang, script) ⇒ Object
- #info(isoxml, out) ⇒ Object
- #load_yaml(lang, script) ⇒ Object
- #metadata_init(lang, script, labels) ⇒ Object
- #middle_title(out) ⇒ Object
- #multi_bibitem_ref_code(b) ⇒ Object
-
#nonstd_bibitem(list, b, ordinal, biblio) ⇒ Object
def norm_ref(isoxml, out, num) q = “//bibliography/references[@normative = ‘true’]” f = isoxml.at(ns(q)) or return num out.div do |div| num = num + 1 clause_name(num, @normref_lbl, div, nil) biblio_list(f, div, false) end num end.
- #note_label(node) ⇒ Object
- #note_p_parse(node, div) ⇒ Object
- #note_parse1(node, div) ⇒ Object
- #ol_depth(node) ⇒ Object
- #pref_ref_code(b) ⇒ Object
- #preface(isoxml, out) ⇒ Object
- #prefix_container(container, linkend, _target) ⇒ Object
- #reference_format(b, r) ⇒ Object
- #reference_format_start(b, r) ⇒ Object
- #reference_format_title(b, r) ⇒ Object
- #refs_cleanup(docxml) ⇒ Object
- #render_multi_identifiers(ids) ⇒ Object
- #std_bibitem_entry(list, b, ordinal, biblio) ⇒ Object
- #table_footnote_reference_format(a) ⇒ Object
- #term_cleanup(docxml) ⇒ Object
- #term_def_title(node) ⇒ Object
- #termdef_parse(node, out) ⇒ Object
- #termdef_parse1(node, div, term, defn, source) ⇒ Object
- #termnote_parse(node, out) ⇒ Object
- #terms_defs(node, out, num) ⇒ Object
- #terms_parse(node, out) ⇒ Object
- #title_cleanup(docxml) ⇒ Object
- #titlecase(s) ⇒ Object
- #xref_init(lang, script, klass, labels, options) ⇒ Object
Instance Method Details
#add_parse(node, out) ⇒ Object
202 203 204 205 206 |
# File 'lib/isodoc/itu/base_convert.rb', line 202 def add_parse(node, out) out.span **{class: "addition"} do |e| node.children.each { |n| parse(n, e) } end end |
#annex(isoxml, out) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/isodoc/itu/base_convert.rb', line 99 def annex(isoxml, out) isoxml.xpath(ns("//annex")).each do |c| @meta.get[:doctype_original] == "recommendation-annex" or page_break(out) out.div **attr_code(id: c["id"], class: "Section3") do |s| annex_name(c, nil, s) unless c.at(ns("./title")) c.elements.each do |c1| if c1.name == "title" then annex_name(c, c1, s) else parse(c1, s) end end end end end |
#annex_name(annex, name, div) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/isodoc/itu/base_convert.rb', line 78 def annex_name(annex, name, div) r_a = @meta.get[:doctype_original] == "recommendation-annex" div.h1 **{ class: r_a ? "RecommendationAnnex" : "Annex" } do |t| t << "#{@xrefs.anchor(annex['id'], :label)} " t.br t.br t.b do |b| name&.children&.each { |c2| parse(c2, b) } end end annex_obligation_subtitle(annex, div) end |
#annex_obligation_subtitle(annex, div) ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/isodoc/itu/base_convert.rb', line 91 def annex_obligation_subtitle(annex, div) info = annex["obligation"] == "informative" div.p **{class: "annex_obligation" } do |p| p << (info ? @inform_annex_lbl : @norm_annex_lbl). sub(/%/, @meta.get[:doctype] || "") end end |
#biblio_list(f, div, biblio) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/isodoc/itu/ref.rb', line 37 def biblio_list(f, div, biblio) div.table **{ class: "biblio", border: "0" } do |t| i = 0 t.tbody do |tbody| f.elements.each do |b| if b.name == "bibitem" next if implicit_reference(b) i += 1 nonstd_bibitem(tbody, b, i, biblio) else unless %w(title clause references).include? b.name tbody.tx do |tx| parse(b, tx) end end end end end end f.xpath(ns("./clause | ./references")).each do |x| parse(x, div) end end |
#bracket_if_num(x) ⇒ Object
61 62 63 64 65 |
# File 'lib/isodoc/itu/ref.rb', line 61 def bracket_if_num(x) return nil if x.nil? x = x.text.sub(/^\[/, "").sub(/\]$/, "") "[#{x}]" end |
#bracket_opt(b) ⇒ Object
45 46 47 48 49 |
# File 'lib/isodoc/itu/base_convert.rb', line 45 def bracket_opt(b) return b if b.nil? return b if /^\[.+\]$/.match(b) "[#{b}]" end |
#clausedelim ⇒ Object
51 52 53 |
# File 'lib/isodoc/itu/base_convert.rb', line 51 def clausedelim "" end |
#cleanup(docxml) ⇒ Object
123 124 125 126 127 128 |
# File 'lib/isodoc/itu/base_convert.rb', line 123 def cleanup(docxml) super term_cleanup(docxml) refs_cleanup(docxml) title_cleanup(docxml) end |
#del_parse(node, out) ⇒ Object
208 209 210 211 212 |
# File 'lib/isodoc/itu/base_convert.rb', line 208 def del_parse(node, out) out.span **{class: "deletion"} do |e| node.children.each { |n| parse(n, e) } end end |
#doctype_title(id) ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/isodoc/itu/ref.rb', line 97 def doctype_title(id) type = id.parent&.at(ns("./ext/doctype"))&.text || "recommendation" if type == "recommendation" && /^(?<prefix>ITU-[A-Z] [A-Z])[ .-]Sup[a-z]*\.[ ]?(?<num>\d+)$/ =~ id.text "#{prefix}-series Recommendations – Supplement #{num}" else "#{titlecase(type)} #{docid_prefix(id["type"], id.text.sub(/^\[/, '').sub(/\]$/, ''))}" end end |
#eref_parse(node, out) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/isodoc/itu/base_convert.rb', line 181 def eref_parse(node, out) linkend = get_eref_linkend(node) href = eref_target(node) if node["type"] == "footnote" out.sup do |s| s.a(**{ "href": href }) { |l| l << linkend } end else out.a(**{ "href": href }) { |l| l << linkend } end end |
#error_parse(node, out) ⇒ Object
214 215 216 217 218 219 220 221 |
# File 'lib/isodoc/itu/base_convert.rb', line 214 def error_parse(node, out) case node.name when "add" then add_parse(node, out) when "del" then del_parse(node, out) else super end end |
#fileloc(loc) ⇒ Object
119 120 121 |
# File 'lib/isodoc/itu/base_convert.rb', line 119 def fileloc(loc) File.join(File.dirname(__FILE__), loc) end |
#get_eref_linkend(node) ⇒ Object
170 171 172 173 174 175 176 177 178 179 |
# File 'lib/isodoc/itu/base_convert.rb', line 170 def get_eref_linkend(node) l = anchor_linkend(node, docid_l10n(node["target"] || node["citeas"])) l && !/^\[.*\]$/.match(l) and l = "[#{l}]" l += eref_localities(node.xpath(ns("./locality | ./localityStack")), l) contents = node.children.select do |c| !%w{locality localityStack}.include? c.name end return l if contents.nil? || contents.empty? Nokogiri::XML::NodeSet.new(node.document, contents).to_xml end |
#i18n_init(lang, script) ⇒ Object
115 116 117 |
# File 'lib/isodoc/itu/base_convert.rb', line 115 def i18n_init(lang, script) super end |
#info(isoxml, out) ⇒ Object
165 166 167 168 |
# File 'lib/isodoc/itu/base_convert.rb', line 165 def info(isoxml, out) @meta.ip_notice_received isoxml, out super end |
#load_yaml(lang, script) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/isodoc/itu/base_convert.rb', line 11 def load_yaml(lang, script) y = if @i18nyaml then YAML.load_file(@i18nyaml) elsif lang == "en" YAML.load_file(File.join(File.dirname(__FILE__), "i18n-en.yaml")) else YAML.load_file(File.join(File.dirname(__FILE__), "i18n-en.yaml")) end super.merge(y) end |
#metadata_init(lang, script, labels) ⇒ Object
21 22 23 |
# File 'lib/isodoc/itu/base_convert.rb', line 21 def (lang, script, labels) @meta = Metadata.new(lang, script, labels) end |
#middle_title(out) ⇒ Object
193 194 195 196 197 198 199 200 |
# File 'lib/isodoc/itu/base_convert.rb', line 193 def middle_title(out) out.p(**{ class: "zzSTDTitle1" }) do |p| id = @meta.get[:docnumber] and p << "#{@meta.get[:doctype]} #{id}" end out.p(**{ class: "zzSTDTitle2" }) { |p| p << @meta.get[:doctitle] } s = @meta.get[:docsubtitle] and out.p(**{ class: "zzSTDTitle3" }) { |p| p << s } end |
#multi_bibitem_ref_code(b) ⇒ Object
83 84 85 86 87 88 |
# File 'lib/isodoc/itu/ref.rb', line 83 def multi_bibitem_ref_code(b) id = b.xpath(ns("./docidentifier[not(@type = 'metanorma' or #{IGNORE_IDS})]")) id.empty? and id = b.xpath(ns("./docidentifier[not(@type = 'metanorma')]")) return [] if id.empty? id.sort_by { |i| i["type"] == "ITU" ? 0 : 1 } end |
#nonstd_bibitem(list, b, ordinal, biblio) ⇒ Object
def norm_ref(isoxml, out, num)
q = "//bibliography/references[@normative = 'true']"
f = isoxml.at(ns(q)) or return num
out.div do |div|
num = num + 1
clause_name(num, @normref_lbl, div, nil)
biblio_list(f, div, false)
end
num
end
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/isodoc/itu/ref.rb', line 21 def nonstd_bibitem(list, b, ordinal, biblio) list.tr **attr_code(iso_bibitem_entry_attrs(b, biblio)) do |ref| id = render_identifier(bibitem_ref_code(b)) ref.td **{style: "vertical-align:top"} do |td| td << (id[0] || "[#{id[1]}]")&. gsub(/-/, "‑")&.gsub(/ /, " ") date_note_process(b, td) end ref.td { |td| reference_format(b, td) } end end |
#note_label(node) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/isodoc/itu/base_convert.rb', line 55 def note_label(node) n = @xrefs.get[node["id"]] (n.nil? || n[:label].nil? || n[:label].empty?) and return "#{@note_lbl} – " l10n("#{@note_lbl} #{n[:label]} – ") end |
#note_p_parse(node, div) ⇒ Object
223 224 225 226 227 228 229 230 231 |
# File 'lib/isodoc/itu/base_convert.rb', line 223 def note_p_parse(node, div) div.p do |p| p.span **{ class: "note_label" } do |s| s << note_label(node) end node.first_element_child.children.each { |n| parse(n, p) } end node.element_children[1..-1].each { |n| parse(n, div) } end |
#note_parse1(node, div) ⇒ Object
233 234 235 236 237 238 239 240 |
# File 'lib/isodoc/itu/base_convert.rb', line 233 def note_parse1(node, div) div.p do |p| p.span **{ class: "note_label" } do |s| s << note_label(node) end end node.children.each { |n| parse(n, div) } end |
#ol_depth(node) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/isodoc/itu/base_convert.rb', line 66 def ol_depth(node) return super unless node["class"] == "steps" or node.at(".//ancestor::xmlns:ol[@class = 'steps']") depth = node.ancestors("ul, ol").size + 1 type = :arabic type = :alphabet if [2, 7].include? depth type = :roman if [3, 8].include? depth type = :alphabet_upper if [4, 9].include? depth type = :roman_upper if [5, 10].include? depth ol_style(type) end |
#pref_ref_code(b) ⇒ Object
76 77 78 |
# File 'lib/isodoc/itu/ref.rb', line 76 def pref_ref_code(b) b.at(ns("./docidentifier[@type = 'ITU']")) || super end |
#preface(isoxml, out) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/isodoc/itu/base_convert.rb', line 33 def preface(isoxml, out) isoxml.xpath(ns(FRONT_CLAUSE)).each do |c| title = c&.at(ns("./title")) out.div **attr_code(id: c["id"]) do |s| clause_name(nil, title, s, class: "IntroTitle") c.elements.reject { |c1| c1.name == "title" }.each do |c1| parse(c1, s) end end end end |
#prefix_container(container, linkend, _target) ⇒ Object
62 63 64 |
# File 'lib/isodoc/itu/base_convert.rb', line 62 def prefix_container(container, linkend, _target) l10n("#{linkend} #{@labels["in"]} #{@xrefs.anchor(container, :xref)}") end |
#reference_format(b, r) ⇒ Object
67 68 69 70 |
# File 'lib/isodoc/itu/ref.rb', line 67 def reference_format(b, r) reference_format_start(b, r) reference_format_title(b, r) end |
#reference_format_start(b, r) ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/isodoc/itu/ref.rb', line 107 def reference_format_start(b, r) id = multi_bibitem_ref_code(b) id1 = render_multi_identifiers(id) r << id1 date = b.at(ns("./date[@type = 'published']")) and r << " (#{date.text.sub(/-.*$/, '')})" r << ", " if (date || !id1.empty?) end |
#reference_format_title(b, r) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/isodoc/itu/ref.rb', line 116 def reference_format_title(b, r) if ftitle = b.at(ns("./formattedref")) ftitle&.children&.each { |n| parse(n, r) } /\.$/.match(ftitle&.text) or r << "." elsif title = iso_title(b) r.i do |i| title&.children&.each { |n| parse(n, i) } end /\.$/.match(title&.text) or r << "." end end |
#refs_cleanup(docxml) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/isodoc/itu/base_convert.rb', line 153 def refs_cleanup(docxml) docxml.xpath("//tx[following-sibling::tx]").each do |tx| tx << tx.next_element.remove.children end docxml.xpath("//tx").each do |tx| tx.name = "td" tx["colspan"] = "2" tx.wrap("<tr></tr>") end docxml end |
#render_multi_identifiers(ids) ⇒ Object
90 91 92 93 94 95 |
# File 'lib/isodoc/itu/ref.rb', line 90 def render_multi_identifiers(ids) ids.map do |id| id["type"] == "ITU" ? doctype_title(id) : docid_prefix(id["type"], id.text.sub(/^\[/, "").sub(/\]$/, "")) end.join(" | ") end |
#std_bibitem_entry(list, b, ordinal, biblio) ⇒ Object
33 34 35 |
# File 'lib/isodoc/itu/ref.rb', line 33 def std_bibitem_entry(list, b, ordinal, biblio) nonstd_bibitem(list, b, ordinal, biblio) end |
#table_footnote_reference_format(a) ⇒ Object
242 243 244 |
# File 'lib/isodoc/itu/base_convert.rb', line 242 def table_footnote_reference_format(a) a.content = a.content + ")" end |
#term_cleanup(docxml) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/isodoc/itu/base_convert.rb', line 138 def term_cleanup(docxml) docxml.xpath("//p[@class = 'Terms']").each do |d| h2 = d.at("./preceding-sibling::*[@class = 'TermNum'][1]") d.children.first.previous = "<b>#{h2.children.to_xml}</b> " d["id"] = h2["id"] h2.remove end docxml.xpath("//p[@class = 'TermNum']").each do |d| d1 = d.next_element and d1.name == "p" or next d1.children.each { |e| e.parent = d } d1.remove end docxml end |
#term_def_title(node) ⇒ Object
4 5 6 |
# File 'lib/isodoc/itu/terms.rb', line 4 def term_def_title(node) node end |
#termdef_parse(node, out) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/isodoc/itu/terms.rb', line 48 def termdef_parse(node, out) term = node.at(ns("./preferred")) defn = node.at(ns("./definition")) source = node.at(ns("./termsource/origin/@citeas")) out.div **attr_code(id: node["id"]) do |div| termdef_parse1(node, div, term, defn, source) set_termdomain("") node.children.each do |n| next if %w(preferred definition termsource title).include? n.name parse(n, out) end end end |
#termdef_parse1(node, div, term, defn, source) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/isodoc/itu/terms.rb', line 35 def termdef_parse1(node, div, term, defn, source) div.p **{ class: "TermNum", id: node["id"] } do |p| p.b do |b| b << @xrefs.anchor(node["id"], :label) insert_tab(b, 1) term.children.each { |n| parse(n, b) } end source and p << " #{bracket_opt(source.value)}" p << ": " end defn and defn.children.each { |n| parse(n, div) } end |
#termnote_parse(node, out) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/isodoc/itu/terms.rb', line 62 def termnote_parse(node, out) out.div **note_attrs(node) do |div| first = node.first_element_child div.p do |p| p << note_label(node) # "#{@xrefs.anchor(node['id'], :label) || '???'}: " para_then_remainder(first, node, p, div) end end end |
#terms_defs(node, out, num) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/isodoc/itu/terms.rb', line 8 def terms_defs(node, out, num) f = node.at(ns(IsoDoc::Convert::TERM_CLAUSE)) or return num out.div **attr_code(id: f["id"]) do |div| num = num + 1 clause_name(num, term_def_title(f.at(ns("./title"))), div, nil) if f.at(ns("./clause | ./terms | ./term")).nil? then out.p "None." else f.children.reject { |c1| c1.name == "title" }.each do |c1| parse(c1, div) end end end num end |
#terms_parse(node, out) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/isodoc/itu/terms.rb', line 23 def terms_parse(node, out) out.div **attr_code(id: node["id"]) do |div| clause_parse_title(node, div, node.at(ns("./title")), out) if node.at(ns("./clause | ./term")).nil? then out.p "None." else node.children.reject { |c1| c1.name == "title" }.each do |c1| parse(c1, div) end end end end |
#title_cleanup(docxml) ⇒ Object
130 131 132 133 134 135 136 |
# File 'lib/isodoc/itu/base_convert.rb', line 130 def title_cleanup(docxml) docxml.xpath("//h1[@class = 'RecommendationAnnex']").each do |h| h.name = "p" h["class"] = "h1Annex" end docxml end |
#titlecase(s) ⇒ Object
72 73 74 |
# File 'lib/isodoc/itu/ref.rb', line 72 def titlecase(s) s.gsub(/ |\_|\-/, " ").split(/ /).map(&:capitalize).join(" ") end |