Class: IsoDoc::Convert
- Inherits:
-
Object
- Object
- IsoDoc::Convert
- Defined in:
- lib/isodoc.rb,
lib/isodoc/html.rb,
lib/isodoc/lists.rb,
lib/isodoc/table.rb,
lib/isodoc/utils.rb,
lib/isodoc/blocks.rb,
lib/isodoc/cleanup.rb,
lib/isodoc/metadata.rb,
lib/isodoc/xref_gen.rb,
lib/isodoc/references.rb,
lib/isodoc/postprocessing.rb
Constant Summary collapse
- OL_STYLE =
{ arabic: "1", roman: "i", alphabet: "a", roman_upper: "I", alphabet_upper: "A", }.freeze
- SW =
"solid windowtext"
- STAGE_ABBRS =
{ "00": "PWI", "10": "NWIP", "20": "WD", "30": "CD", "40": "DIS", "50": "FDIS", "60": "IS", "90": "(Review)", "95": "(Withdrawal)", }.freeze
- NOKOHEAD =
"90": "(Review)", "95": "(Withdrawal)", }.freeze def stage_abbreviation(stage) STAGE_ABBRS[stage.to_sym] || "??" end NOKOHEAD = <<~HERE <!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <meta charset="UTF-8" /> </head> <body> </body> </html> HERE # block for processing XML document fragments as XHTML, # to allow for HTMLentities def noko(&block) doc = ::Nokogiri::XML.parse(NOKOHEAD) fragment = doc.fragment("") ::Nokogiri::XML::Builder.with fragment, &block fragment.to_xml(encoding: "US-ASCII").lines.map do |l| l.gsub(/\s*\n/, "") end end def attr_code(attributes) attributes = attributes.reject { |_, val| val.nil? }.map attributes.map do |k, v| [k, (v.is_a? String) ? HTMLEntities.new.decode(v) : v] end.to_h end NOKOHEAD = <<~HERE <!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <meta charset="UTF-8" /> </head> <body> </body> </html> HERE
- FIGURE_WITH_FOOTNOTES =
"//div[@class = 'figure'][descendant::aside]"\ "[not(descendant::div[@class = 'figure'])]".freeze
- TABLE_WITH_FOOTNOTES =
"//table[descendant::aside]".freeze
- NORM_WITH_REFS_PREF =
"id": b["id"], class: "Biblio") do |r| ref_entry_code(r, ordinal, ref.text.gsub(/[\[\]]/, "")) para.children.each { |n| parse(n, r) } end end def split_bibitems(f) iso_bibitem = [] non_iso_bibitem = [] f.xpath(ns("./bibitem")).each do |x| if x.at(ns("./publisher/affiliation[name = 'ISO']")).nil? non_iso_bibitem << x else iso_bibitem << x end end { iso: iso_bibitem, noniso: non_iso_bibitem } end def biblio_list(f, div, bibliography) bibitems = split_bibitems(f) bibitems[:iso].each_with_index do |b, i| iso_bibitem_entry(div, b, (i + 1), bibliography) end bibitems[:noniso].each_with_index do |b, i| noniso_bibitem(div, b, (i + 1 + bibitems[:iso].size), bibliography) end end NORM_WITH_REFS_PREF = <<~BOILERPLATE The following documents are referred to in the text in such a way that some or all of their content constitutes requirements of this document. For dated references, only the edition cited applies. For undated references, the latest edition of the referenced document (including any amendments) applies. BOILERPLATE
- NORM_EMPTY_PREF =
"There are no normative references in this document."
- WORD_TOC_PREFACE =
<<~TOC <span lang="EN-GB"><span style='mso-element:field-begin'></span><span style='mso-spacerun:yes'> </span>TOC \\o "1-2" \\h \\z \\u <span style='mso-element:field-separator'></span></span> TOC
- WORD_TOC_SUFFIX =
<<~TOC <p class="MsoToc1"><span lang="EN-GB"><span style='mso-element:field-end'></span></span><span lang="EN-GB"><o:p> </o:p></span></p> TOC
Instance Method Summary collapse
- #admonition_parse(node, out) ⇒ Object
-
#anchor_names(docxml) ⇒ Object
extract names for all anchors, xref and label.
- #annex_names(clause, num) ⇒ Object
- #annex_names1(clause, num, level) ⇒ Object
- #annotation_parse(node, out) ⇒ Object
- #attr_code(attributes) ⇒ Object
- #author(isoxml, _out) ⇒ Object
- #back_anchor_names(docxml) ⇒ Object
- #biblio_list(f, div, bibliography) ⇒ Object
- #bibliography(isoxml, out) ⇒ Object
- #clause_names(docxml, sect_num) ⇒ Object
- #cleanup(docxml) ⇒ Object
- #comment_cleanup(docxml) ⇒ Object
- #compose_title(main, intro, part, partnumber) ⇒ Object
- #convert(filename) ⇒ Object
- #date_note_process(b, ref) ⇒ Object
-
#define_head(html, filename, dir) ⇒ Object
isodoc.css overrides any CSS injected by Html2Doc, which is inserted before this CSS.
- #dl_parse(node, out) ⇒ Object
- #draftinfo(draft, revdate) ⇒ Object
- #figure_aside_process(f, aside, key) ⇒ Object
- #figure_cleanup(docxml) ⇒ Object
- #figure_get_or_make_dl(t) ⇒ Object
- #figure_key(out) ⇒ Object
- #figure_name_parse(node, div, name) ⇒ Object
- #figure_parse(node, out) ⇒ Object
- #footnote_cleanup(docxml) ⇒ Object
- #format_ref(ref, isopub) ⇒ Object
- #formula_parse(node, out) ⇒ Object
- #from_xhtml(xml) ⇒ Object
- #generate_header(filename, dir) ⇒ Object
- #get_anchors ⇒ Object
- #get_metadata ⇒ Object
- #get_termexample ⇒ Object
- #header_strip(h) ⇒ Object
- #hierarchical_asset_names(clause, num) ⇒ Object
- #hierarchical_figure_names(clause, num) ⇒ Object
-
#html_header(html, docxml, filename, dir) ⇒ Object
these are in fact preprocess, but they are extraneous to main HTML file.
- #htmlPreface(docxml) ⇒ Object
- #htmlstyle(docxml) ⇒ Object
- #htmlstylesheet ⇒ Object
- #id(isoxml, _out) ⇒ Object
- #image_parse(url, out, caption) ⇒ Object
- #image_title_parse(out, caption) ⇒ Object
- #in_sourcecode ⇒ Object
- #initial_anchor_names(d) ⇒ Object
-
#initialize(options) ⇒ Convert
constructor
htmlstylesheet: Generic stylesheet for HTML wordstylesheet: Generic stylesheet for Word standardsheet: Stylesheet specific to Standard header: Header file for Word htmlcoverpage: Cover page for HTML wordcoverpage: Cover page for Word htmlintropage: Introductory page for HTML wordintropage: Introductory page for Word.
- #inline_header_cleanup(docxml) ⇒ Object
- #insert_tab(out, n) ⇒ Object
- #introduction_names(clause) ⇒ Object
- #is_note ⇒ Object
- #iso_bibitem_entry(list, b, ordinal, biblio) ⇒ Object
- #iso_bibitem_ref_code(b) ⇒ Object
- #li_parse(node, out) ⇒ Object
- #make_table_attr(node) ⇒ Object
-
#make_tr_attr(td, row, totalrows, col, totalcols, header) ⇒ Object
border-left:#? “#{SW 1.5pt;” : “none;”} border-right:#SW #== totalcols && !header ? “1.5” : “1.0”pt;.
- #makeWordToC(docxml) ⇒ Object
- #merge_fnref_into_fn_text(a) ⇒ Object
- #middle_anchor_names(docxml) ⇒ Object
- #middle_section_asset_names(d) ⇒ Object
- #new_fullcolspan_row(t, tfoot) ⇒ Object
-
#noko(&block) ⇒ Object
block for processing XML document fragments as XHTML, to allow for HTMLentities.
- #noniso_bibitem(list, b, ordinal, bibliography) ⇒ Object
- #norm_ref(isoxml, out) ⇒ Object
- #norm_ref_preface(f, div) ⇒ Object
- #note_label(node) ⇒ Object
- #note_p_parse(node, div) ⇒ Object
- #note_parse(node, out) ⇒ Object
- #ns(xpath) ⇒ Object
- #ol_parse(node, out) ⇒ Object
- #ol_style(type) ⇒ Object
- #para_attrs(node) ⇒ Object
- #para_parse(node, out) ⇒ Object
- #populate_template(docxml) ⇒ Object
- #postprocess(result, filename, dir) ⇒ Object
- #quote_attribution(node, out) ⇒ Object
- #quote_parse(node, out) ⇒ Object
- #ref_entry(list, b, ordinal, bibliography) ⇒ Object
- #ref_entry_code(r, ordinal, t) ⇒ Object
- #ref_names(ref) ⇒ Object
- #reference_names(ref) ⇒ Object
- #remove_bottom_border(td) ⇒ Object
- #section_names(clause, num, level) ⇒ Object
- #section_names1(clause, num, level) ⇒ Object
- #sequential_asset_names(clause) ⇒ Object
- #sequential_figure_names(clause) ⇒ Object
- #set_metadata(key, value) ⇒ Object
-
#set_termdomain(termdomain) ⇒ Object
attr_accessor :termdomain, :termexample, :sourcecode, :note.
- #set_termexample(value) ⇒ Object
- #sourcecode_name_parse(node, div, name) ⇒ Object
- #sourcecode_parse(node, out) ⇒ Object
- #split_bibitems(f) ⇒ Object
- #stage_abbreviation(stage) ⇒ Object
- #subtitle(isoxml, _out) ⇒ Object
- #table_cleanup(docxml) ⇒ Object
- #table_footnote_cleanup(docxml) ⇒ Object
- #table_get_or_make_tfoot(t) ⇒ Object
- #table_note_anchor_names(docxml) ⇒ Object
- #table_note_cleanup(docxml) ⇒ Object
- #table_parse(node, out) ⇒ Object
- #table_title_parse(node, out) ⇒ Object
- #tbody_parse(node, t) ⇒ Object
- #termnote_anchor_names(docxml) ⇒ Object
- #tfoot_parse(node, t) ⇒ Object
- #thead_parse(node, t) ⇒ Object
- #title(isoxml, _out) ⇒ Object
- #titlepage(_docxml, div) ⇒ Object
- #to_xhtml(xml) ⇒ Object
- #to_xhtml_fragment(xml) ⇒ Object
- #toHTML(result, filename) ⇒ Object
- #toWord(result, filename, dir) ⇒ Object
- #tr_parse(node, out, ord, totalrows, header) ⇒ Object
- #ul_parse(node, out) ⇒ Object
- #version(isoxml, _out) ⇒ Object
-
#wordAnnexCleanup(docxml) ⇒ Object
force Annex h2 to be p.h2Annex, so it is not picked up by ToC.
- #wordCleanup(docxml) ⇒ Object
- #wordPreface(docxml) ⇒ Object
- #wordTocEntry(toclevel, heading) ⇒ Object
Constructor Details
#initialize(options) ⇒ Convert
htmlstylesheet: Generic stylesheet for HTML wordstylesheet: Generic stylesheet for Word standardsheet: Stylesheet specific to Standard header: Header file for Word htmlcoverpage: Cover page for HTML wordcoverpage: Cover page for Word htmlintropage: Introductory page for HTML wordintropage: Introductory page for Word
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.rb', line 38 def initialize() @htmlstylesheet = [:htmlstylesheet] @wordstylesheet = [:wordstylesheet] @standardstylesheet = [:standardstylesheet] @header = [:header] @htmlcoverpage = [:htmlcoverpage] @wordcoverpage = [:wordcoverpage] @htmlintropage = [:htmlintropage] @wordintropage = [:wordintropage] @termdomain = "" @termexample = false @note = false @sourcecode = false @anchors = {} @meta = {} @footnotes = [] @comments = [] @in_footnote = false @in_table = false @in_figure = false @seen_footnote = Set.new end |
Instance Method Details
#admonition_parse(node, out) ⇒ Object
109 110 111 112 113 114 115 116 117 |
# File 'lib/isodoc/blocks.rb', line 109 def admonition_parse(node, out) name = node["type"] out.div **{ class: "Admonition" } do |t| t.p.b { |b| b << name.upcase } if name node.children.each do |n| parse(n, t) end end end |
#anchor_names(docxml) ⇒ Object
extract names for all anchors, xref and label
77 78 79 80 81 82 |
# File 'lib/isodoc/xref_gen.rb', line 77 def anchor_names(docxml) initial_anchor_names(docxml) middle_anchor_names(docxml) back_anchor_names(docxml) table_note_anchor_names(docxml) end |
#annex_names(clause, num) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/isodoc/xref_gen.rb', line 161 def annex_names(clause, num) obligation = "(Informative)" obligation = "(Normative)" if clause["subtype"] == "normative" label = "<b>Annex #{num}</b><br/>#{obligation}" @anchors[clause["id"]] = { label: label, xref: "Annex #{num}", level: 1 } clause.xpath(ns("./subsection")).each_with_index do |c, i| annex_names1(c, "#{num}.#{i + 1}", 2) end hierarchical_asset_names(clause, num) end |
#annex_names1(clause, num, level) ⇒ Object
173 174 175 176 177 178 179 180 |
# File 'lib/isodoc/xref_gen.rb', line 173 def annex_names1(clause, num, level) @anchors[clause["id"]] = { label: num, xref: num, level: level } clause.xpath(ns(".//subsection")).each_with_index do |c, i| annex_names1(c, "#{num}.#{i + 1}", level + 1) end end |
#annotation_parse(node, out) ⇒ Object
103 104 105 106 107 |
# File 'lib/isodoc/blocks.rb', line 103 def annotation_parse(node, out) out.p **{ class: "Sourcecode" } do |li| node.children.each { |n| parse(n, li) } end end |
#attr_code(attributes) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/isodoc/utils.rb', line 54 def attr_code(attributes) attributes = attributes.reject { |_, val| val.nil? }.map attributes.map do |k, v| [k, (v.is_a? String) ? HTMLEntities.new.decode(v) : v] end.to_h end |
#author(isoxml, _out) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/isodoc/metadata.rb', line 14 def (isoxml, _out) # tc = isoxml.at(ns("//technical-committee")) tc_num = isoxml.at(ns("//technical-committee/@number")) # sc = isoxml.at(ns("//subcommittee")) sc_num = isoxml.at(ns("//subcommittee/@number")) # wg = isoxml.at(ns("//workgroup")) wg_num = isoxml.at(ns("//workgroup/@number")) secretariat = isoxml.at(ns("//secretariat")) (:tc, "XXXX") (:sc, "XXXX") (:wg, "XXXX") (:secretariat, "XXXX") (:tc, tc_num.text) if tc_num (:sc, sc_num.text) if sc_num (:wg, wg_num.text) if wg_num (:secretariat, secretariat.text) if secretariat end |
#back_anchor_names(docxml) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/isodoc/xref_gen.rb', line 13 def back_anchor_names(docxml) docxml.xpath(ns("//annex")).each_with_index do |c, i| annex_names(c, (65 + i).chr.to_s) end docxml.xpath(ns("//bibitem")).each do |ref| reference_names(ref) end end |
#biblio_list(f, div, bibliography) ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/isodoc/references.rb', line 77 def biblio_list(f, div, bibliography) bibitems = split_bibitems(f) bibitems[:iso].each_with_index do |b, i| iso_bibitem_entry(div, b, (i + 1), bibliography) end bibitems[:noniso].each_with_index do |b, i| noniso_bibitem(div, b, (i + 1 + bibitems[:iso].size), bibliography) end end |
#bibliography(isoxml, out) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/isodoc/references.rb', line 116 def bibliography(isoxml, out) q = "//sections/references[title = 'Bibliography']" f = isoxml.at(ns(q)) or return page_break(out) out.div do |div| div.h1 "Bibliography", **{ class: "Section3" } f.elements.reject do |e| ["reference", "title", "bibitem"].include? e.name end.each { |e| parse(e, div) } biblio_list(f, div, true) end end |
#clause_names(docxml, sect_num) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/isodoc/xref_gen.rb', line 38 def clause_names(docxml,sect_num) q = "//clause[parent::sections][not(xmlns:title = 'Scope')]" docxml.xpath(ns(q)).each_with_index do |c, i| section_names(c, (i + sect_num).to_s, 1) end end |
#cleanup(docxml) ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/isodoc/cleanup.rb', line 8 def cleanup(docxml) comment_cleanup(docxml) footnote_cleanup(docxml) inline_header_cleanup(docxml) figure_cleanup(docxml) table_cleanup(docxml) docxml end |
#comment_cleanup(docxml) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/isodoc/cleanup.rb', line 67 def comment_cleanup(docxml) docxml.xpath('//div/span[@style="MsoCommentReference"]'). each do |x| prev = x.previous_element x.parent = prev unless prev.nil? end docxml end |
#compose_title(main, intro, part, partnumber) ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/isodoc/metadata.rb', line 70 def compose_title(main, intro, part, partnumber) c = HTMLEntities.new main = c.encode(main.text, :hexadecimal) intro && main = "#{c.encode(intro.text, :hexadecimal)} — #{main}" part && main = "#{main} — Part #{partnumber}: "\ "#{c.encode(part.text, :hexadecimal)}" main end |
#convert(filename) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/isodoc.rb', line 61 def convert(filename) docxml = Nokogiri::XML(File.read(filename)) filename, dir = init_file(filename) docxml.root.default_namespace = "" result = noko do |xml| xml.html do |html| html_header(html, docxml, filename, dir) make_body(html, docxml) end end.join("\n") postprocess(result, filename, dir) end |
#date_note_process(b, ref) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/isodoc/references.rb', line 11 def date_note_process(b, ref) date_note = b.xpath(ns("./note[text()][contains(.,'ISO DATE:')]")) unless date_note.empty? date_note.first.content = date_note.first.content.gsub(/ISO DATE: /, "") date_note.wrap("<p></p>") footnote_parse(date_note.first, ref) end end |
#define_head(html, filename, dir) ⇒ Object
isodoc.css overrides any CSS injected by Html2Doc, which is inserted before this CSS.
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/isodoc/postprocessing.rb', line 87 def define_head(html, filename, dir) html.head do |head| head.title { |t| t << filename } head.style do |style| stylesheet = File.read(@standardstylesheet). gsub("FILENAME", filename) style.comment "\n#{stylesheet}\n" end end end |
#dl_parse(node, out) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/isodoc/lists.rb', line 35 def dl_parse(node, out) out.dl do |v| node.elements.each_slice(2) do |dt, dd| v.dt do |term| if dt.elements.empty? term.p **attr_code(class: is_note ? "Note" : nil) do |p| p << dt.text end else dt.children.each { |n| parse(n, term) } end end v.dd do |listitem| dd.children.each { |n| parse(n, listitem) } end end end end |
#draftinfo(draft, revdate) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/isodoc/metadata.rb', line 48 def draftinfo(draft, revdate) draftinfo = "" if draft draftinfo = " (draft #{draft.text}" draftinfo += ", #{revdate.text}" if revdate draftinfo += ")" end draftinfo end |
#figure_aside_process(f, aside, key) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/isodoc/cleanup.rb', line 30 def figure_aside_process(f, aside, key) # get rid of footnote link, it is in diagram f.at("./a[@class='zzFootnote']").remove fnref = f.at(".//a[@class='zzFootnote']") dt = key.add_child("<dt></dt>").first dd = key.add_child("<dd></dd>").first fnref.parent = dt aside.xpath(".//p").each do |a| a.delete("class") a.parent = dd end end |
#figure_cleanup(docxml) ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/isodoc/cleanup.rb', line 43 def figure_cleanup(docxml) # move footnotes into key, and get rid of footnote reference # since it is in diagram docxml.xpath(FIGURE_WITH_FOOTNOTES).each do |f| key = figure_get_or_make_dl(f) f.xpath(".//aside").each do |aside| figure_aside_process(f, aside, key) end end end |
#figure_get_or_make_dl(t) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/isodoc/cleanup.rb', line 17 def figure_get_or_make_dl(t) dl = t.at(".//dl") if dl.nil? t.add_child("<p><b>Key</b></p><dl></dl>") dl = t.at(".//dl") end dl end |
#figure_key(out) ⇒ Object
64 65 66 67 68 |
# File 'lib/isodoc/blocks.rb', line 64 def figure_key(out) out.p do |p| p.b { |b| b << "Key" } end end |
#figure_name_parse(node, div, name) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/isodoc/blocks.rb', line 55 def figure_name_parse(node, div, name) div.p **{ class: "FigureTitle", align: "center" } do |p| p.b do |b| b << "#{get_anchors()[node['id']][:label]} — " b << name.text end end end |
#figure_parse(node, out) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/isodoc/blocks.rb', line 70 def figure_parse(node, out) @in_figure = true name = node.at(ns("./name")) out.div **attr_code(id: node["id"], class: "figure") do |div| node.children.each do |n| figure_key(out) if n.name == "dl" parse(n, div) unless n.name == "name" end figure_name_parse(node, div, name) if name end @in_figure = false end |
#footnote_cleanup(docxml) ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/isodoc/cleanup.rb', line 76 def footnote_cleanup(docxml) docxml.xpath('//div[@style="mso-element:footnote"]/a'). each do |x| n = x.next_element n&.children&.first&.add_previous_sibling(x.remove) end docxml end |
#format_ref(ref, isopub) ⇒ Object
182 183 184 185 186 |
# File 'lib/isodoc/xref_gen.rb', line 182 def format_ref(ref, isopub) return "ISO #{ref}" if isopub return "[#{ref}]" if /^\d+$/.match?(ref) && !/^\[.*\]$/.match?(ref) ref end |
#formula_parse(node, out) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/isodoc/blocks.rb', line 119 def formula_parse(node, out) dl = node.at(ns("./dl")) out.div **attr_code(id: node["id"], class: "formula") do |div| parse(node.at(ns("./stem")), out) insert_tab(div, 1) div << "(#{get_anchors()[node['id']][:label]})" end if dl out.p { |p| p << "where" } parse(dl, out) end end |
#from_xhtml(xml) ⇒ Object
84 85 86 |
# File 'lib/isodoc/utils.rb', line 84 def from_xhtml(xml) xml.to_xml.sub(%r{ xmlns="http://www.w3.org/1999/xhtml"}, "") end |
#generate_header(filename, dir) ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/isodoc/postprocessing.rb', line 67 def generate_header(filename, dir) header = File.read(@header, encoding: "UTF-8"). gsub(/FILENAME/, filename). gsub(/DOCYEAR/, ()[:docyear]). gsub(/[ ]?DRAFTINFO/, ()[:draftinfo]). gsub(/DOCNUMBER/, ()[:docnumber]) File.open("header.html", "w") do |f| f.write(header) end end |
#get_anchors ⇒ Object
9 10 11 |
# File 'lib/isodoc/xref_gen.rb', line 9 def get_anchors @anchors end |
#get_metadata ⇒ Object
6 7 8 |
# File 'lib/isodoc/metadata.rb', line 6 def @meta end |
#get_termexample ⇒ Object
8 9 10 |
# File 'lib/isodoc/blocks.rb', line 8 def get_termexample @termexample end |
#header_strip(h) ⇒ Object
140 141 142 143 |
# File 'lib/isodoc/postprocessing.rb', line 140 def header_strip(h) h.to_s.gsub(%r{<br/>}, " "). sub(/<h[12][^>]*>/, "").sub(%r{</h[12]>}, "") end |
#hierarchical_asset_names(clause, num) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/isodoc/xref_gen.rb', line 125 def hierarchical_asset_names(clause, num) clause.xpath(ns(".//table")).each_with_index do |t, i| @anchors[t["id"]] = { label: "Table #{num}.#{i + 1}", xref: "Table #{num}.#{i + 1}" } end hierarchical_figure_names(clause, num) clause.xpath(ns(".//formula")).each_with_index do |t, i| @anchors[t["id"]] = { label: "#{num}.#{i + 1}", xref: "Formula #{num}.#{i + 1}" } end end |
#hierarchical_figure_names(clause, num) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/isodoc/xref_gen.rb', line 111 def hierarchical_figure_names(clause, num) i = j = 0 clause.xpath(ns(".//figure")).each do |t| if t.parent.name == "figure" j += 1 else j = 0 i += 1 end label = "Figure #{num}.#{i}" + ( j.zero? ? "" : "-#{j}" ) @anchors[t["id"]] = { label: label, xref: label } end end |
#html_header(html, docxml, filename, dir) ⇒ Object
these are in fact preprocess, but they are extraneous to main HTML file
80 81 82 83 |
# File 'lib/isodoc/postprocessing.rb', line 80 def html_header(html, docxml, filename, dir) anchor_names docxml define_head html, filename, dir end |
#htmlPreface(docxml) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/isodoc/html.rb', line 12 def htmlPreface(docxml) cover = Nokogiri::HTML(File.read(@htmlcoverpage, encoding: "UTF-8")) d = docxml.at('//div[@class="WordSection1"]') d.children.first.add_previous_sibling cover.to_xml(encoding: 'US-ASCII') cover = Nokogiri::HTML(File.read(@htmlintropage, encoding: "UTF-8")) d = docxml.at('//div[@class="WordSection2"]') d.children.first.add_previous_sibling cover.to_xml(encoding: 'US-ASCII') body = docxml.at("//*[local-name() = 'body']") body << '<script src="https://cdn.mathjax.org/mathjax/latest/'\ 'MathJax.js?config=AM_HTMLorMML"></script>' docxml end |
#htmlstyle(docxml) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/isodoc/html.rb', line 32 def htmlstyle(docxml) title = docxml.at("//*[local-name() = 'head']/*[local-name() = 'title']") head = docxml.at("//*[local-name() = 'head']") css = htmlstylesheet if title.nil? then head.children.first.add_previous_sibling css else title.add_next_sibling css end docxml end |
#htmlstylesheet ⇒ Object
25 26 27 28 29 30 |
# File 'lib/isodoc/html.rb', line 25 def htmlstylesheet stylesheet = File.read(@htmlstylesheet, encoding: "UTF-8") xml = Nokogiri::XML("<style/>") xml.children.first << Nokogiri::XML::Comment.new(xml, "\n#{stylesheet}\n") xml.root.to_s end |
#id(isoxml, _out) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/isodoc/metadata.rb', line 32 def id(isoxml, _out) docnumber = isoxml.at(ns("//project-number")) partnumber = isoxml.at(ns("//project-number/@part")) documentstatus = isoxml.at(ns("//status/stage")) dn = docnumber.text dn += "-#{partnumber.text}" if partnumber if documentstatus (:stage, documentstatus.text) abbr = stage_abbreviation(documentstatus.text) (:stageabbr, abbr) documentstatus.text.to_i < 60 and dn = abbr + " " + dn end (:docnumber, dn) end |
#image_parse(url, out, caption) ⇒ Object
179 180 181 182 |
# File 'lib/isodoc/blocks.rb', line 179 def image_parse(url, out, ) out.img **attr_code(src: url) image_title_parse(out, ) end |
#image_title_parse(out, caption) ⇒ Object
171 172 173 174 175 176 177 |
# File 'lib/isodoc/blocks.rb', line 171 def image_title_parse(out, ) unless .nil? out.p **{ class: "FigureTitle", align: "center" } do |p| p.b { |b| b << .to_s } end end end |
#in_sourcecode ⇒ Object
16 17 18 |
# File 'lib/isodoc/blocks.rb', line 16 def in_sourcecode @sourcecode end |
#initial_anchor_names(d) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/isodoc/xref_gen.rb', line 22 def initial_anchor_names(d) introduction_names(d.at(ns("//content[title = 'Introduction']"))) section_names(d.at(ns("//clause[title = 'Scope']")), "1", 1) section_names(d.at(ns( "//references[title = 'Normative References']")), "2", 1) section_names(d.at(ns("//terms")), "3", 1) middle_section_asset_names(d) end |
#inline_header_cleanup(docxml) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/isodoc/cleanup.rb', line 54 def inline_header_cleanup(docxml) docxml.xpath('//span[@class="zzMoveToFollowing"]').each do |x| n = x.next_element if n.nil? html = Nokogiri::XML.fragment("<p></p>") html.parent = x.parent x.parent = html else n.children.first.add_previous_sibling(x.remove) end end end |
#insert_tab(out, n) ⇒ Object
12 13 14 15 16 |
# File 'lib/isodoc/utils.rb', line 12 def insert_tab(out, n) out.span **attr_code(style: "mso-tab-count:#{n}") do |span| [1..n].each { |i| span << "  " } end end |
#introduction_names(clause) ⇒ Object
137 138 139 140 141 |
# File 'lib/isodoc/xref_gen.rb', line 137 def introduction_names(clause) clause.xpath(ns("./subsection")).each_with_index do |c, i| section_names(c, "0.#{i + 1}") end end |
#is_note ⇒ Object
20 21 22 |
# File 'lib/isodoc/blocks.rb', line 20 def is_note @note end |
#iso_bibitem_entry(list, b, ordinal, biblio) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/isodoc/references.rb', line 21 def iso_bibitem_entry(list, b, ordinal, biblio) attrs = { id: b["id"], class: biblio ? "Biblio" : nil } list.p **attr_code(attrs) do |ref| if biblio ref << "[#{ordinal}]" insert_tab(ref, 1) end ref << iso_bibitem_ref_code(b) date_note_process(b, ref) ref << ", " if biblio ref.i { |i| i << " #{b.at(ns('./name')).text}" } end end |
#iso_bibitem_ref_code(b) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/isodoc/references.rb', line 3 def iso_bibitem_ref_code(b) isocode = b.at(ns("./docidentifier")) isodate = b.at(ns("./publishdate")) reference = "ISO #{isocode.text}" reference += ": #{isodate.text}" if isodate reference end |
#li_parse(node, out) ⇒ Object
29 30 31 32 33 |
# File 'lib/isodoc/lists.rb', line 29 def li_parse(node, out) out.li do |li| node.children.each { |n| parse(n, li) } end end |
#make_table_attr(node) ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/isodoc/table.rb', line 46 def make_table_attr(node) { id: node["id"], class: "MsoISOTable", border: 1, cellspacing: 0, cellpadding: 0, } end |
#make_tr_attr(td, row, totalrows, col, totalcols, header) ⇒ Object
border-left:#? “#{SW 1.5pt;” : “none;”} border-right:#SW #== totalcols && !header ? “1.5” : “1.0”pt;
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/isodoc/table.rb', line 74 def make_tr_attr(td, row, totalrows, col, totalcols, header) style = td.name == "th" ? "font-weight:bold;" : "" rowmax = td["rowspan"] ? row + td["rowspan"].to_i - 1 : row style += <<~STYLE border-top:#{row.zero? ? "#{SW} 1.5pt;" : "none;"} mso-border-top-alt:#{row.zero? ? "#{SW} 1.5pt;" : "none;"} border-bottom:#{SW} #{rowmax == totalrows ? "1.5" : "1.0"}pt; mso-border-bottom-alt:#{SW} #{rowmax == totalrows ? "1.5" : "1.0"}pt; STYLE { rowspan: td["rowspan"], colspan: td["colspan"], align: td["align"], style: style.gsub(/\n/, "") } end |
#makeWordToC(docxml) ⇒ Object
145 146 147 148 149 150 151 152 153 |
# File 'lib/isodoc/postprocessing.rb', line 145 def makeWordToC(docxml) toc = "" docxml.xpath("//h1 | //h2[not(ancestor::*[@class = 'Section3'])]"). each do |h| toc += wordTocEntry(h.name == "h1" ? 1 : 2, header_strip(h)) end toc.sub(/(<p class="MsoToc1">)/, %{\\1#{WORD_TOC_PREFACE}}) + WORD_TOC_SUFFIX end |
#merge_fnref_into_fn_text(a) ⇒ Object
85 86 87 88 89 |
# File 'lib/isodoc/cleanup.rb', line 85 def merge_fnref_into_fn_text(a) fn = a.at('.//a[@class="zzFootnote"]') n = fn.next_element n&.children&.first&.add_previous_sibling(fn.remove) end |
#middle_anchor_names(docxml) ⇒ Object
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/isodoc/xref_gen.rb', line 65 def middle_anchor_names(docxml) symbols_abbrevs = docxml.at(ns("//symbols-abbrevs")) sect_num = 4 if symbols_abbrevs section_names(symbols_abbrevs, sect_num.to_s, 1) sect_num += 1 end clause_names(docxml, sect_num) termnote_anchor_names(docxml) end |
#middle_section_asset_names(d) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/isodoc/xref_gen.rb', line 31 def middle_section_asset_names(d) middle_sections = "//clause[title = 'Scope'] | "\ "//references[title = 'Normative References'] | //terms | "\ "//symbols-abbrevs | //clause[parent::sections]" sequential_asset_names(d.xpath(ns(middle_sections))) end |
#new_fullcolspan_row(t, tfoot) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/isodoc/cleanup.rb', line 122 def new_fullcolspan_row(t, tfoot) # how many columns in the table? cols = 0 t.at(".//tr").xpath("./td | ./th").each do |td| cols += (td["colspan"] ? td["colspan"].to_i : 1) end style = %{border-top:0pt;mso-border-top-alt:0pt; border-bottom:#{SW} 1.5pt;mso-border-bottom-alt:#{SW} 1.5pt;} tfoot.add_child("<tr><td colspan='#{cols}' style='#{style}'/></tr>") tfoot.xpath(".//td").last end |
#noko(&block) ⇒ Object
block for processing XML document fragments as XHTML, to allow for HTMLentities
45 46 47 48 49 50 51 52 |
# File 'lib/isodoc/utils.rb', line 45 def noko(&block) doc = ::Nokogiri::XML.parse(NOKOHEAD) fragment = doc.fragment("") ::Nokogiri::XML::Builder.with fragment, &block fragment.to_xml(encoding: "US-ASCII").lines.map do |l| l.gsub(/\s*\n/, "") end end |
#noniso_bibitem(list, b, ordinal, bibliography) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/isodoc/references.rb', line 55 def noniso_bibitem(list, b, ordinal, bibliography) ref = b.at(ns("./docidentifier")) para = b.at(ns("./formatted")) list.p **attr_code("id": b["id"], class: "Biblio") do |r| ref_entry_code(r, ordinal, ref.text.gsub(/[\[\]]/, "")) para.children.each { |n| parse(n, r) } end end |
#norm_ref(isoxml, out) ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'lib/isodoc/references.rb', line 106 def norm_ref(isoxml, out) q = "//sections/references[title = 'Normative References']" f = isoxml.at(ns(q)) or return out.div do |div| clause_name("2.", "Normative References", div, false) norm_ref_preface(f, div) biblio_list(f, div, false) end end |
#norm_ref_preface(f, div) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/isodoc/references.rb', line 98 def norm_ref_preface(f, div) refs = f.elements.select do |e| ["reference", "bibitem"].include? e.name end pref = refs.empty? ? NORM_EMPTY_PREF : NORM_WITH_REFS_PREF div.p pref end |
#note_label(node) ⇒ Object
24 25 26 27 28 |
# File 'lib/isodoc/blocks.rb', line 24 def note_label(node) n = get_anchors()[node["id"]] return "NOTE" if n.nil? n[:label] end |
#note_p_parse(node, div) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/isodoc/blocks.rb', line 30 def note_p_parse(node, div) div.p **{ class: "Note" } do |p| p << note_label(node) insert_tab(p, 1) node.first_element_child.children.each { |n| parse(n, p) } end node.element_children[1..-1].each { |n| parse(n, div) } end |
#note_parse(node, out) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/isodoc/blocks.rb', line 39 def note_parse(node, out) @note = true out.div **{ id: node["id"], class: "Note" } do |div| if node.first_element_child.name == "p" note_p_parse(node, div) else div.p **{ class: "Note" } do |p| p << note_label(node) insert_tab(p, 1) end node.children.each { |n| parse(n, div) } end end @note = false end |
#ns(xpath) ⇒ Object
5 6 7 8 9 10 |
# File 'lib/isodoc/utils.rb', line 5 def ns(xpath) xpath.gsub(%r{/([a-zA-z])}, "/xmlns:\\1"). gsub(%r{::([a-zA-z])}, "::xmlns:\\1"). gsub(%r{\[([a-zA-z]+ ?=)}, "[xmlns:\\1"). gsub(%r{\[([a-zA-z]+\])}, "[xmlns:\\1") end |
#ol_parse(node, out) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/isodoc/lists.rb', line 21 def ol_parse(node, out) # attrs = { numeration: node["type"] } style = ol_style(node["type"]) out.ol **attr_code(type: style) do |ol| node.children.each { |n| parse(n, ol) } end end |
#ol_style(type) ⇒ Object
17 18 19 |
# File 'lib/isodoc/lists.rb', line 17 def ol_style(type) OL_STYLE[type.to_sym] end |
#para_attrs(node) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/isodoc/blocks.rb', line 132 def para_attrs(node) classtype = nil classtype = "Note" if @note classtype = "MsoFootnoteText" if in_footnote attrs = { class: classtype } unless node["align"].nil? attrs[:align] = node["align"] unless node["align"] == "justify" attrs[:style] = "text-align:#{node["align"]}" end attrs end |
#para_parse(node, out) ⇒ Object
144 145 146 147 148 149 150 151 152 |
# File 'lib/isodoc/blocks.rb', line 144 def para_parse(node, out) out.p **attr_code(para_attrs(node)) do |p| unless @termdomain.empty? p << "<#{@termdomain}> " @termdomain = "" end node.children.each { |n| parse(n, p) } end end |
#populate_template(docxml) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/isodoc/postprocessing.rb', line 48 def populate_template(docxml) = docxml. gsub(/DOCYEAR/, [:docyear]). gsub(/DOCNUMBER/, [:docnumber]). gsub(/TCNUM/, [:tc]). gsub(/SCNUM/, [:sc]). gsub(/WGNUM/, [:wg]). gsub(/DOCTITLE/, [:doctitle]). gsub(/DOCSUBTITLE/, [:docsubtitle]). gsub(/SECRETARIAT/, [:secretariat]). gsub(/[ ]?DRAFTINFO/, [:draftinfo]). gsub(/\[TERMREF\]\s*/, "[SOURCE: "). gsub(/\s*\[\/TERMREF\]\s*/, "]"). gsub(/\s*\[ISOSECTION\]/, ", "). gsub(/\s*\[MODIFICATION\]/, ", modified — "). gsub(%r{WD/CD/DIS/FDIS}, [:stageabbr]) end |
#postprocess(result, filename, dir) ⇒ Object
9 10 11 12 13 14 |
# File 'lib/isodoc/postprocessing.rb', line 9 def postprocess(result, filename, dir) generate_header(filename, dir) result = from_xhtml(cleanup(to_xhtml(result))) toWord(result, filename, dir) toHTML(result, filename) end |
#quote_attribution(node, out) ⇒ Object
154 155 156 157 158 |
# File 'lib/isodoc/blocks.rb', line 154 def quote_attribution(node, out) = node.at(ns("./author/fullname/")) source = node.at(ns("./source")) # TODO implement end |
#quote_parse(node, out) ⇒ Object
160 161 162 163 164 165 166 167 168 169 |
# File 'lib/isodoc/blocks.rb', line 160 def quote_parse(node, out) attrs = para_attrs(node) attrs[:class] = "Quote" out.p **attr_code(attrs) do |p| node.children.each do |n| parse(n, p) unless ["author", "source"].include? n.name end quote_attribution(node, out) end end |
#ref_entry(list, b, ordinal, bibliography) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/isodoc/references.rb', line 46 def ref_entry(list, b, ordinal, bibliography) ref = b.at(ns("./ref")) para = b.at(ns("./p")) list.p **attr_code("id": ref["id"], class: "Biblio") do |r| ref_entry_code(r, ordinal, ref.text.gsub(/[\[\]]/, "")) para.children.each { |n| parse(n, r) } end end |
#ref_entry_code(r, ordinal, t) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/isodoc/references.rb', line 35 def ref_entry_code(r, ordinal, t) if /^\d+$/.match?(t) r << "[#{t}]" insert_tab(r, 1) else r << "[#{ordinal}]" insert_tab(r, 1) r << "#{t}," end end |
#ref_names(ref) ⇒ Object
198 199 200 201 202 |
# File 'lib/isodoc/xref_gen.rb', line 198 def ref_names(ref) linkend = ref.text linkend.gsub!(/[\[\]]/, "") unless /^\[\d+\]$/.match? linkend @anchors[ref["id"]] = { xref: linkend } end |
#reference_names(ref) ⇒ Object
188 189 190 191 192 193 194 195 196 |
# File 'lib/isodoc/xref_gen.rb', line 188 def reference_names(ref) isopub = ref.at(ns("./publisher/affiliation[name = 'ISO']")) docid = ref.at(ns("./docidentifier")) return ref_names(ref) unless docid date = ref.at(ns("./publisherdate")) reference = format_ref(docid.text, isopub) reference += ": #{date.text}" if date && isopub @anchors[ref["id"]] = { xref: reference } end |
#remove_bottom_border(td) ⇒ Object
104 105 106 107 108 |
# File 'lib/isodoc/cleanup.rb', line 104 def remove_bottom_border(td) td["style"] = td["style"].gsub(/border-bottom:[^;]+;/, "border-bottom:0pt;"). gsub(/mso-border-bottom-alt:[^;]+;/, "mso-border-bottom-alt:0pt;") end |
#section_names(clause, num, level) ⇒ Object
143 144 145 146 147 148 149 |
# File 'lib/isodoc/xref_gen.rb', line 143 def section_names(clause, num, level) @anchors[clause["id"]] = { label: num, xref: "Clause #{num}", level: level } clause.xpath(ns("./subsection | ./term")).each_with_index do |c, i| section_names1(c, "#{num}.#{i + 1}", level + 1) end end |
#section_names1(clause, num, level) ⇒ Object
151 152 153 154 155 156 157 158 159 |
# File 'lib/isodoc/xref_gen.rb', line 151 def section_names1(clause, num, level) @anchors[clause["id"]] = { label: num, level: level, xref: clause.name == "term" ? num : "Clause #{num}" } clause.xpath(ns("./subsection ")). each_with_index do |c, i| section_names1(c, "#{num}.#{i + 1}", level + 1) end end |
#sequential_asset_names(clause) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/isodoc/xref_gen.rb', line 99 def sequential_asset_names(clause) clause.xpath(ns(".//table")).each_with_index do |t, i| @anchors[t["id"]] = { label: "Table #{i + 1}", xref: "Table #{i + 1}" } end sequential_figure_names(clause) clause.xpath(ns(".//formula")).each_with_index do |t, i| @anchors[t["id"]] = { label: (i + 1).to_s, xref: "Formula #{i + 1}" } end end |
#sequential_figure_names(clause) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/isodoc/xref_gen.rb', line 84 def sequential_figure_names(clause) i = j = 0 clause.xpath(ns(".//figure")).each do |t| label = "Figure #{i}" + ( j.zero? ? "" : "-#{j}" ) if t.parent.name == "figure" j += 1 else j = 0 i += 1 end label = "Figure #{i}" + ( j.zero? ? "" : "-#{j}" ) @anchors[t["id"]] = { label: label, xref: label } end end |
#set_metadata(key, value) ⇒ Object
10 11 12 |
# File 'lib/isodoc/metadata.rb', line 10 def (key, value) @meta[key] = value end |
#set_termdomain(termdomain) ⇒ Object
attr_accessor :termdomain, :termexample, :sourcecode, :note
4 5 6 |
# File 'lib/isodoc/blocks.rb', line 4 def set_termdomain(termdomain) @termdomain = termdomain end |
#set_termexample(value) ⇒ Object
12 13 14 |
# File 'lib/isodoc/blocks.rb', line 12 def set_termexample(value) @termexample = value end |
#sourcecode_name_parse(node, div, name) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/isodoc/blocks.rb', line 83 def sourcecode_name_parse(node, div, name) div.p **{ class: "FigureTitle", align: "center" } do |p| p.b do |b| b << name.text end end end |
#sourcecode_parse(node, out) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/isodoc/blocks.rb', line 91 def sourcecode_parse(node, out) name = node.at(ns("./name")) out.p **attr_code(id: node["id"], class: "Sourcecode") do |div| @sourcecode = true node.children.each do |n| parse(n, div) unless n.name == "name" end @sourcecode = false sourcecode_name_parse(node, div, name) if name end end |
#split_bibitems(f) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/isodoc/references.rb', line 64 def split_bibitems(f) iso_bibitem = [] non_iso_bibitem = [] f.xpath(ns("./bibitem")).each do |x| if x.at(ns("./publisher/affiliation[name = 'ISO']")).nil? non_iso_bibitem << x else iso_bibitem << x end end { iso: iso_bibitem, noniso: non_iso_bibitem } end |
#stage_abbreviation(stage) ⇒ Object
30 31 32 |
# File 'lib/isodoc/utils.rb', line 30 def stage_abbreviation(stage) STAGE_ABBRS[stage.to_sym] || "??" end |
#subtitle(isoxml, _out) ⇒ Object
90 91 92 93 94 95 96 97 |
# File 'lib/isodoc/metadata.rb', line 90 def subtitle(isoxml, _out) intro = isoxml.at(ns("//title[@language='fr']/title-intro")) main = isoxml.at(ns("//title[@language='fr']/title-main")) part = isoxml.at(ns("//title[@language='fr']/title-part")) partnumber = isoxml.at(ns("//id/project-number/@part")) main = compose_title(main, intro, part, partnumber) (:docsubtitle, main) end |
#table_cleanup(docxml) ⇒ Object
144 145 146 147 |
# File 'lib/isodoc/cleanup.rb', line 144 def table_cleanup(docxml) table_footnote_cleanup(docxml) table_note_cleanup(docxml) end |
#table_footnote_cleanup(docxml) ⇒ Object
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/isodoc/cleanup.rb', line 93 def table_footnote_cleanup(docxml) docxml.xpath(TABLE_WITH_FOOTNOTES).each do |t| t.xpath(".//aside").each do |a| merge_fnref_into_fn_text(a) a.name = "div" a["class"] = "Note" t << a.remove end end end |
#table_get_or_make_tfoot(t) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/isodoc/cleanup.rb', line 110 def table_get_or_make_tfoot(t) tfoot = t.at(".//tfoot") if tfoot.nil? t.add_child("<tfoot></tfoot>") tfoot = t.at(".//tfoot") else # nuke its bottom border tfoot.xpath(".//td | .//th").each { |td| remove_bottom_border(td) } end tfoot end |
#table_note_anchor_names(docxml) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/isodoc/xref_gen.rb', line 55 def table_note_anchor_names(docxml) docxml.xpath(ns("//table[note]")).each do |t| t.xpath(ns("./note")).each_with_index do |n, i| @anchors[n["id"]] = { label: "NOTE #{i + 1}", xref: "#{@anchors[t["id"]][:xref]},"\ "Note #{i + 1}" } end end end |
#table_note_cleanup(docxml) ⇒ Object
134 135 136 137 138 139 140 141 142 |
# File 'lib/isodoc/cleanup.rb', line 134 def table_note_cleanup(docxml) docxml.xpath("//table[div[@class = 'Note']]").each do |t| tfoot = table_get_or_make_tfoot(t) insert_here = new_fullcolspan_row(t, tfoot) t.xpath("div[@class = 'Note']").each do |d| d.parent = insert_here end end end |
#table_parse(node, out) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/isodoc/table.rb', line 56 def table_parse(node, out) @in_table = true table_title_parse(node, out) out.table **make_table_attr(node) do |t| thead_parse(node, t) tbody_parse(node, t) tfoot_parse(node, t) dl = node.at(ns("./dl")) and parse(dl, out) node.xpath(ns("./note")).each { |n| parse(n, out) } end @in_table = false # out.p { |p| p << " " } end |
#table_title_parse(node, out) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 |
# File 'lib/isodoc/table.rb', line 3 def table_title_parse(node, out) name = node.at(ns("./name")) if name out.p **{ class: "TableTitle", align: "center" } do |p| p.b do |b| b << "#{get_anchors()[node['id']][:label]} — " b << name.text end end end end |
#tbody_parse(node, t) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/isodoc/table.rb', line 26 def tbody_parse(node, t) tbody = node.at(ns("./tbody")) t.tbody do |h| tbody.element_children.each_with_index do |n, i| tr_parse(n, h, i, tbody.element_children.size, false) end end end |
#termnote_anchor_names(docxml) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/isodoc/xref_gen.rb', line 45 def termnote_anchor_names(docxml) docxml.xpath(ns("//term[termnote]")).each do |t| t.xpath(ns("./termnote")).each_with_index do |n, i| @anchors[n["id"]] = { label: "Note #{i + 1} to entry", xref: "#{@anchors[t["id"]][:xref]},"\ "Note #{i + 1}" } end end end |
#tfoot_parse(node, t) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/isodoc/table.rb', line 35 def tfoot_parse(node, t) tfoot = node.at(ns("./tfoot")) if tfoot t.tfoot do |h| tfoot.element_children.each_with_index do |n, i| tr_parse(n, h, i, tfoot.element_children.size, false) end end end end |
#thead_parse(node, t) ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/isodoc/table.rb', line 15 def thead_parse(node, t) thead = node.at(ns("./thead")) if thead t.thead do |h| thead.element_children.each_with_index do |n, i| tr_parse(n, h, i, thead.element_children.size, true) end end end end |
#title(isoxml, _out) ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/isodoc/metadata.rb', line 81 def title(isoxml, _out) intro = isoxml.at(ns("//title[@language='en']/title-intro")) main = isoxml.at(ns("//title[@language='en']/title-main")) part = isoxml.at(ns("//title[@language='en']/title-part")) partnumber = isoxml.at(ns("//id/project-number/@part")) main = compose_title(main, intro, part, partnumber) (:doctitle, main) end |
#titlepage(_docxml, div) ⇒ Object
98 99 100 101 |
# File 'lib/isodoc/postprocessing.rb', line 98 def titlepage(_docxml, div) titlepage = File.read(@wordcoverpage, encoding: "UTF-8") div.parent.add_child titlepage end |
#to_xhtml(xml) ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/isodoc/utils.rb', line 69 def to_xhtml(xml) xml.gsub!(/<\?xml[^>]*>/, "") unless /<!DOCTYPE /.match? xml xml = '<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' + xml end Nokogiri::XML.parse(xml) end |
#to_xhtml_fragment(xml) ⇒ Object
78 79 80 81 82 |
# File 'lib/isodoc/utils.rb', line 78 def to_xhtml_fragment(xml) doc = ::Nokogiri::XML.parse(NOKOHEAD) fragment = doc.fragment(xml) fragment end |
#toHTML(result, filename) ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/isodoc/html.rb', line 4 def toHTML(result, filename) result = htmlPreface(htmlstyle(Nokogiri::HTML(result))).to_xml result = populate_template(result) File.open("#{filename}.html", "w") do |f| f.write(result) end end |
#toWord(result, filename, dir) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/isodoc/postprocessing.rb', line 16 def toWord(result, filename, dir) result = from_xhtml(wordCleanup(to_xhtml(result))) result = populate_template(result) Html2Doc.process(result, filename, @wordstylesheet, "header.html", dir, ['`', '`']) end |
#tr_parse(node, out, ord, totalrows, header) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/isodoc/table.rb', line 87 def tr_parse(node, out, ord, totalrows, header) out.tr do |r| node.elements.each_with_index do |td, i| attrs = make_tr_attr(td, ord, totalrows - 1, i, node.elements.size - 1, header) r.send td.name, **attr_code(attrs) do |entry| td.children.each { |n| parse(n, entry) } end end end end |
#ul_parse(node, out) ⇒ Object
3 4 5 6 7 |
# File 'lib/isodoc/lists.rb', line 3 def ul_parse(node, out) out.ul do |ul| node.children.each { |n| parse(n, ul) } end end |
#version(isoxml, _out) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/isodoc/metadata.rb', line 58 def version(isoxml, _out) yr = isoxml.at(ns("//copyright/from")) (:docyear, yr.text) draft = isoxml.at(ns("//version/draft")) (:draft, draft.nil? ? nil : draft.text) revdate = isoxml.at(ns("//version/revision-date")) (:revdate, revdate.nil? ? nil : revdate.text) draftinfo = draftinfo(draft, revdate) (:draftinfo, draftinfo(draft, revdate)) end |
#wordAnnexCleanup(docxml) ⇒ Object
force Annex h2 to be p.h2Annex, so it is not picked up by ToC
30 31 32 33 34 35 |
# File 'lib/isodoc/postprocessing.rb', line 30 def wordAnnexCleanup(docxml) d = docxml.xpath("//h2[ancestor::*[@class = 'Section3']]").each do |h2| h2.name = "p" h2["class"] = "h2Annex" end end |
#wordCleanup(docxml) ⇒ Object
23 24 25 26 27 |
# File 'lib/isodoc/postprocessing.rb', line 23 def wordCleanup(docxml) wordPreface(docxml) wordAnnexCleanup(docxml) docxml end |
#wordPreface(docxml) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/isodoc/postprocessing.rb', line 37 def wordPreface(docxml) cover = to_xhtml_fragment(File.read(@wordcoverpage, encoding: "UTF-8")) d = docxml.at('//div[@class="WordSection1"]') d.children.first.add_previous_sibling cover.to_xml(encoding: 'US-ASCII') intro = to_xhtml_fragment( File.read(@wordintropage, encoding: "UTF-8"). sub(/WORDTOC/, makeWordToC(docxml))) d = docxml.at('//div[@class="WordSection2"]') d.children.first.add_previous_sibling intro.to_xml(encoding: 'US-ASCII') end |
#wordTocEntry(toclevel, heading) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/isodoc/postprocessing.rb', line 103 def wordTocEntry(toclevel, heading) bookmark = Random.rand(1000000000) <<~TOC <p class="MsoToc#{toclevel}"><span class="MsoHyperlink"><span lang="EN-GB" style='mso-no-proof:yes'> <a href="#_Toc#{bookmark}">#{heading}<span lang="EN-GB" class="MsoTocTextSpan"> <span style='mso-tab-count:1 dotted'>. </span> </span><span lang="EN-GB" class="MsoTocTextSpan"> <span style='mso-element:field-begin'></span></span> <span lang="EN-GB" class="MsoTocTextSpan"> PAGEREF _Toc#{bookmark} \\h </span> <span lang="EN-GB" class="MsoTocTextSpan"><span style='mso-element:field-separator'></span></span><span lang="EN-GB" class="MsoTocTextSpan">1</span> <span lang="EN-GB" class="MsoTocTextSpan"></span><span lang="EN-GB" class="MsoTocTextSpan"><span style='mso-element:field-end'></span></span></a></span></span></p> TOC end |