Module: IsoDoc::WordFunction::Postprocess
- Included in:
- IsoDoc::WordConvert
- Defined in:
- lib/isodoc/word_function/postprocess.rb,
lib/isodoc/word_function/postprocess_toc.rb,
lib/isodoc/word_function/postprocess_cover.rb,
lib/isodoc/word_function/postprocess_table.rb
Constant Summary collapse
- WORD_TOC_SUFFIX1 =
<<~TOC.freeze <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
- WORD_NOKOHEAD =
add namespaces for Word fragments
<<~HERE.freeze <!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml"> <head> <title></title> <meta charset="UTF-8" /> </head> <body> </body> </html> HERE
Instance Method Summary collapse
- #assemble_toc(docxml, level) ⇒ Object
- #authority_cleanup(docxml) ⇒ Object
- #authority_cleanup1(docxml, klass) ⇒ Object
-
#colgroup_widths(table) ⇒ Object
assume percentages.
- #coverpage_note_cleanup(docxml) ⇒ Object
- #figure_toc_class ⇒ Object
- #figure_toc_xpath ⇒ Object
- #generate_header(filename, _dir) ⇒ Object
- #header_params(filename) ⇒ Object
- #insert_toc(intro, docxml, level) ⇒ Object
- #list_add(xpath, lvl) ⇒ Object
- #make_figure_word_toc(docxml) ⇒ Object
- #make_recommendation_word_toc(docxml) ⇒ Object
- #make_table_word_toc(docxml) ⇒ Object
- #make_WordToC(docxml, level) ⇒ Object
- #postprocess(result, filename, dir) ⇒ Object
- #postprocess_cleanup(result) ⇒ Object
- #recommmendation_sort_key(header) ⇒ Object
- #recommmendation_sort_key1(type) ⇒ Object
- #remove_toc_div(docxml) ⇒ Object
- #reqt_toc_class ⇒ Object
- #reqt_toc_xpath ⇒ Object
- #sourcecode_style ⇒ Object
- #split_at_section_break(docxml, sect, brk, idx) ⇒ Object
- #style_update(node, css) ⇒ Object
- #table_note_cleanup(docxml) ⇒ Object
-
#table_toc_class ⇒ Object
inheriting gems need to add native Word name of style, if different including both CSS style name and human readable style name.
- #table_toc_xpath ⇒ Object
- #to_sourcecode_para(pre) ⇒ Object
- #to_word_xhtml_fragment(xml) ⇒ Object
- #toc_word_class_list(classes) ⇒ Object
- #toWord(result, filename, dir, header) ⇒ Object
- #word_admonition_images(docxml) ⇒ Object
- #word_annex_cleanup(docxml) ⇒ Object
- #word_cleanup(docxml) ⇒ Object
- #word_colgroup(docxml) ⇒ Object
- #word_cover(docxml) ⇒ Object
- #word_example_cleanup(docxml) ⇒ Object
-
#word_floating_titles(docxml) ⇒ Object
move p.h1 (floating title) after any page, section breaks.
- #word_footnote_format(docxml) ⇒ Object
- #word_image_caption(docxml) ⇒ Object
- #word_intro(docxml, level) ⇒ Object
- #word_list_continuations(docxml) ⇒ Object
- #word_nested_tables(docxml) ⇒ Object
- #word_preface(docxml) ⇒ Object
- #word_pseudocode_cleanup(docxml) ⇒ Object
- #word_remove_empty_sections(docxml) ⇒ Object
- #word_remove_empty_toc(docxml) ⇒ Object
-
#word_remove_pb_before_annex(docxml) ⇒ Object
applies for <div class=“WordSectionN_M”><p><pagebreak/></p>…
- #word_section_breaks(docxml) ⇒ Object
- #word_section_breaks1(docxml, sect) ⇒ Object
- #word_sourcecode_annotations(html) ⇒ Object
- #word_sourcecode_table(docxml) ⇒ Object
- #word_tab_clean(docxml) ⇒ Object
- #word_table_align(docxml) ⇒ Object
- #word_table_pagebreak(docxml) ⇒ Object
- #word_table_separator(docxml) ⇒ Object
- #word_toc_entry(toclevel, heading) ⇒ Object
- #word_toc_figure_preface1 ⇒ Object
- #word_toc_preface(level) ⇒ Object
- #word_toc_reqt_preface1 ⇒ Object
- #word_toc_table_preface1 ⇒ Object
- #wordstylesheet_update ⇒ Object
Instance Method Details
#assemble_toc(docxml, level) ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/isodoc/word_function/postprocess_toc.rb', line 24 def assemble_toc(docxml, level) toc = "" toc += make_WordToC(docxml, level) toc += make_table_word_toc(docxml) toc += make_figure_word_toc(docxml) toc += make_recommendation_word_toc(docxml) toc end |
#authority_cleanup(docxml) ⇒ Object
83 84 85 86 87 88 |
# File 'lib/isodoc/word_function/postprocess_cover.rb', line 83 def (docxml) %w(copyright license legal feedback).each do |t| (docxml, t) end coverpage_note_cleanup(docxml) end |
#authority_cleanup1(docxml, klass) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/isodoc/word_function/postprocess_cover.rb', line 71 def (docxml, klass) dest = docxml.at("//div[@id = 'boilerplate-#{klass}-destination']") auth = docxml.at("//div[@id = 'boilerplate-#{klass}' " \ "or @class = 'boilerplate-#{klass}']") auth&.xpath(".//h1[not(text())] | .//h2[not(text())]")&.each(&:remove) auth&.xpath(".//h1 | .//h2")&.each do |h| h.name = "p" h["class"] = "TitlePageSubhead" end dest and auth and dest.replace(auth.remove) end |
#colgroup_widths(table) ⇒ Object
assume percentages
39 40 41 42 43 |
# File 'lib/isodoc/word_function/postprocess_table.rb', line 39 def colgroup_widths(table) table.xpath("./colgroup/col").each_with_object([]) do |c, m| m << c["width"].sub(/%$/, "").to_f end end |
#coverpage_note_cleanup(docxml) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/isodoc/word_function/postprocess_cover.rb', line 90 def coverpage_note_cleanup(docxml) if dest = docxml.at("//div[@id = 'coverpage-note-destination']") auth = docxml.xpath("//*[@coverpage]") if auth.empty? then dest.remove else auth.each do |x| dest << x.remove end end end docxml.xpath("//*[@coverpage]").each { |x| x.delete("coverpage") } end |
#figure_toc_class ⇒ Object
85 86 87 |
# File 'lib/isodoc/word_function/postprocess_toc.rb', line 85 def figure_toc_class %w(FigureTitle figuretitle) end |
#figure_toc_xpath ⇒ Object
137 138 139 140 |
# File 'lib/isodoc/word_function/postprocess_toc.rb', line 137 def figure_toc_xpath attr = figure_toc_class.map { |x| "@class = '#{x}'" } "//p[#{attr.join(' or ')}]" end |
#generate_header(filename, _dir) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/isodoc/word_function/postprocess_cover.rb', line 103 def generate_header(filename, _dir) @header or return nil template = IsoDoc::Common.liquid(File.read(@header, encoding: "UTF-8")) params = header_params(filename) Tempfile.open(%w(header html), mode: File::BINARY | File::SHARE_DELETE, encoding: "utf-8") do |f| f.write(template.render(params)) f end end |
#header_params(filename) ⇒ Object
115 116 117 118 119 120 |
# File 'lib/isodoc/word_function/postprocess_cover.rb', line 115 def header_params(filename) = @meta.get.merge(@labels ? { labels: @labels } : {}) .merge(@meta.labels ? { labels: @meta.labels } : {}) [:filename] = filename .transform_keys(&:to_s) end |
#insert_toc(intro, docxml, level) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/isodoc/word_function/postprocess_toc.rb', line 4 def insert_toc(intro, docxml, level) toc = assemble_toc(docxml, level) if intro&.include?("WORDTOC") remove_toc_div(docxml) intro.sub("WORDTOC", toc) else source = docxml.at("//div[@class = 'TOC']") and source.children = toc intro end end |
#list_add(xpath, lvl) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/isodoc/word_function/postprocess.rb', line 135 def list_add(xpath, lvl) xpath.each do |list| (list.xpath(".//li") - list.xpath(".//ol//li | .//ul//li")).each do |l| l.xpath("./p | ./div | ./table").each_with_index do |p, i| i.zero? or p.wrap(%{<div class="ListContLevel#{lvl}"/>}) end list_add(l.xpath(".//ul") - l.xpath(".//ul//ul | .//ol//ul"), lvl + 1) list_add(l.xpath(".//ol") - l.xpath(".//ul//ol | .//ol//ol"), lvl + 1) end end end |
#make_figure_word_toc(docxml) ⇒ Object
142 143 144 145 146 147 148 149 150 |
# File 'lib/isodoc/word_function/postprocess_toc.rb', line 142 def make_figure_word_toc(docxml) (docxml.at(figure_toc_xpath) && @tocfigurestitle) or return "" toc = %{<p class="TOCTitle">#{@tocfigurestitle}</p>} docxml.xpath(figure_toc_xpath).each do |h| toc += word_toc_entry(1, header_strip(h)) end toc.sub(/(<p class="MsoToc1">)/, %{\\1#{word_toc_figure_preface1}}) + WORD_TOC_SUFFIX1 end |
#make_recommendation_word_toc(docxml) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/isodoc/word_function/postprocess_toc.rb', line 157 def make_recommendation_word_toc(docxml) (docxml.at(reqt_toc_xpath) && @tocrecommendationstitle) or return "" toc = %{<p class="TOCTitle">#{@tocrecommendationstitle}</p>} docxml.xpath(reqt_toc_xpath).sort_by do |h| recommmendation_sort_key(h.text) end.each do |h| toc += word_toc_entry(1, header_strip(h)) end toc.sub(/(<p class="MsoToc1">)/, %{\\1#{word_toc_reqt_preface1}}) + WORD_TOC_SUFFIX1 end |
#make_table_word_toc(docxml) ⇒ Object
127 128 129 130 131 132 133 134 135 |
# File 'lib/isodoc/word_function/postprocess_toc.rb', line 127 def make_table_word_toc(docxml) (docxml.at(table_toc_xpath) && @toctablestitle) or return "" toc = %{<p class="TOCTitle">#{@toctablestitle}</p>} docxml.xpath(table_toc_xpath).each do |h| toc += word_toc_entry(1, header_strip(h)) end toc.sub(/(<p class="MsoToc1">)/, %{\\1#{word_toc_table_preface1}}) + WORD_TOC_SUFFIX1 end |
#make_WordToC(docxml, level) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/isodoc/word_function/postprocess_toc.rb', line 63 def make_WordToC(docxml, level) toc = "" if source = docxml.at("//div[@class = 'TOC']") toc = to_xml(source.children) end # docxml.xpath("//h1 | //h2[not(ancestor::*[@class = 'Section3'])]"). xpath = (1..level).each.map { |i| "//h#{i}" }.join (" | ") docxml.xpath(xpath).each do |h| toc += word_toc_entry(h.name[1].to_i, header_strip(h)) end toc.sub(/(<p class="MsoToc1">)/, %{\\1#{word_toc_preface(level)}}) + WORD_TOC_SUFFIX1 end |
#postprocess(result, filename, dir) ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/isodoc/word_function/postprocess.rb', line 9 def postprocess(result, filename, dir) result = postprocess_cleanup(result) filename = filename.sub(/\.doc$/, "") header = generate_header(filename, dir) @wordstylesheet = wordstylesheet_update toWord(result, filename, dir, header) @files_to_delete.each { |f| FileUtils.rm_f f } end |
#postprocess_cleanup(result) ⇒ Object
18 19 20 21 22 |
# File 'lib/isodoc/word_function/postprocess.rb', line 18 def postprocess_cleanup(result) result = cleanup(to_xhtml(textcleanup(result))) from_xhtml(word_cleanup(result)) .gsub("-DOUBLE_HYPHEN_ESCAPE-", "--") end |
#recommmendation_sort_key(header) ⇒ Object
169 170 171 172 173 174 175 |
# File 'lib/isodoc/word_function/postprocess_toc.rb', line 169 def recommmendation_sort_key(header) m = /^([^0-9]+) (\d+)/.match(header) || /^([^:]+)/.match(header) m ||= [header, nil] ret = "#{recommmendation_sort_key1(m[1])}::" m[2] and ret += ("%04d" % m[2].to_i).to_s ret end |
#recommmendation_sort_key1(type) ⇒ Object
177 178 179 180 181 182 183 184 |
# File 'lib/isodoc/word_function/postprocess_toc.rb', line 177 def recommmendation_sort_key1(type) case type&.downcase when "requirement" then "04" when "recommendation" then "05" when "permission" then "06" else type end end |
#remove_toc_div(docxml) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/isodoc/word_function/postprocess_toc.rb', line 16 def remove_toc_div(docxml) s = docxml.at("//div[@class = 'TOC']") or return prev = s.previous_element prev&.elements&.first&.name == "br" and prev&.remove # page break s.remove end |
#reqt_toc_class ⇒ Object
89 90 91 92 |
# File 'lib/isodoc/word_function/postprocess_toc.rb', line 89 def reqt_toc_class %w(RecommendationTitle RecommendationTestTitle recommendationtitle recommendationtesttitle) end |
#reqt_toc_xpath ⇒ Object
152 153 154 155 |
# File 'lib/isodoc/word_function/postprocess_toc.rb', line 152 def reqt_toc_xpath attr = reqt_toc_class.map { |x| "@class = '#{x}'" } "//p[#{attr.join(' or ')}]" end |
#sourcecode_style ⇒ Object
36 37 38 |
# File 'lib/isodoc/word_function/postprocess.rb', line 36 def sourcecode_style "Sourcecode" end |
#split_at_section_break(docxml, sect, brk, idx) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/isodoc/word_function/postprocess_cover.rb', line 142 def split_at_section_break(docxml, sect, brk, idx) move = brk.parent.xpath("following::node()") & brk.document.xpath("//div[@class = '#{sect}']//*") ins = docxml.at("//div[@class = '#{sect}']") .after("<div class='#{sect}_#{idx}'/>").next_element move.each do |m| next if m.at("./ancestor::div[@class = '#{sect}_#{idx}']") ins << m.remove end end |
#style_update(node, css) ⇒ Object
53 54 55 56 57 |
# File 'lib/isodoc/word_function/postprocess_table.rb', line 53 def style_update(node, css) node or return node["style"] = node["style"] ? node["style"].sub(/;?$/, ";#{css}") : css end |
#table_note_cleanup(docxml) ⇒ Object
4 5 6 7 8 9 |
# File 'lib/isodoc/word_function/postprocess_table.rb', line 4 def table_note_cleanup(docxml) super # preempt html2doc putting MsoNormal there docxml.xpath("//p[not(self::*[@class])][ancestor::*[@class = 'Note']]") .each { |p| p["class"] = "Note" } end |
#table_toc_class ⇒ Object
inheriting gems need to add native Word name of style, if different including both CSS style name and human readable style name. Any human readable style name needs to come first for the Word template to work in regenerating the ToC
81 82 83 |
# File 'lib/isodoc/word_function/postprocess_toc.rb', line 81 def table_toc_class %w(TableTitle tabletitle) end |
#table_toc_xpath ⇒ Object
122 123 124 125 |
# File 'lib/isodoc/word_function/postprocess_toc.rb', line 122 def table_toc_xpath attr = table_toc_class.map { |x| "@class = '#{x}'" } "//p[#{attr.join(' or ')}]" end |
#to_sourcecode_para(pre) ⇒ Object
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/isodoc/word_function/postprocess.rb', line 98 def to_sourcecode_para(pre) @sourcecode = "pre" pre.traverse do |x| x.text? or next ret = [] text_parse(x, ret) x.replace(ret.join) end @sourcecode = false end |
#to_word_xhtml_fragment(xml) ⇒ Object
50 51 52 53 |
# File 'lib/isodoc/word_function/postprocess_cover.rb', line 50 def to_word_xhtml_fragment(xml) doc = ::Nokogiri::XML.parse(WORD_NOKOHEAD) ::Nokogiri::XML::DocumentFragment.new(doc, xml, doc.root) end |
#toc_word_class_list(classes) ⇒ Object
94 95 96 97 98 |
# File 'lib/isodoc/word_function/postprocess_toc.rb', line 94 def toc_word_class_list(classes) classes.map do |x| / /.match?(x) ? %("#{x}") : x end.join(",") end |
#toWord(result, filename, dir, header) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/isodoc/word_function/postprocess.rb', line 24 def toWord(result, filename, dir, header) Html2Doc.new( filename: filename, imagedir: @localdir, stylesheet: @wordstylesheet&.path, header_file: header&.path, dir: dir, asciimathdelims: [@openmathdelim, @closemathdelim], liststyles: { ul: @ulstyle, ol: @olstyle } ).process(result) header&.unlink @wordstylesheet.unlink if @wordstylesheet.is_a?(Tempfile) end |
#word_admonition_images(docxml) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/isodoc/word_function/postprocess.rb', line 40 def word_admonition_images(docxml) docxml.xpath("//div[@class = 'Admonition']//img").each do |i| i["width"], i["height"] = Vectory.image_resize(i, image_localfile(i), @maxheight, 300) end end |
#word_annex_cleanup(docxml) ⇒ Object
149 |
# File 'lib/isodoc/word_function/postprocess.rb', line 149 def word_annex_cleanup(docxml); end |
#word_cleanup(docxml) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/isodoc/word_function/postprocess.rb', line 47 def word_cleanup(docxml) word_annex_cleanup(docxml) word_preface(docxml) word_sourcecode_annotations(docxml) word_sourcecode_table(docxml) word_nested_tables(docxml) word_colgroup(docxml) word_table_align(docxml) word_table_pagebreak(docxml) word_table_separator(docxml) word_admonition_images(docxml) word_list_continuations(docxml) word_example_cleanup(docxml) word_pseudocode_cleanup(docxml) (docxml) word_floating_titles(docxml) word_section_breaks(docxml) word_tab_clean(docxml) (docxml) word_footnote_format(docxml) word_remove_empty_toc(docxml) word_remove_empty_sections(docxml) docxml end |
#word_colgroup(docxml) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/isodoc/word_function/postprocess_table.rb', line 11 def word_colgroup(docxml) cells2d = {} docxml.xpath("//table[colgroup]").each do |t| w = colgroup_widths(t) t.xpath(".//tr").each_with_index { |_tr, r| cells2d[r] = {} } t.xpath(".//tr").each_with_index do |tr, r| tr.xpath("./td | ./th").each_with_index do |td, _i| x = 0 rs = td.attr("rowspan")&.to_i || 1 cs = td.attr("colspan")&.to_i || 1 while cells2d[r][x] x += 1 end (r..(r + rs - 1)).each do |y2| cells2d[y2].nil? and next (x..(x + cs - 1)).each { |x2| cells2d[y2][x2] = 1 } end width = (x..(x + cs - 1)).each_with_object({ width: 0 }) do |z, m| m[:width] += w[z] end td["width"] = "#{width[:width]}%" x += cs end end end end |
#word_cover(docxml) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/isodoc/word_function/postprocess_cover.rb', line 20 def word_cover(docxml) ins = docxml.at('//div[@class="WordSection1"]') or return cover = File.read(@wordcoverpage, encoding: "UTF-8") cover = populate_template(cover, :word) coverxml = to_word_xhtml_fragment(cover) #ins.children.first.previous = coverxml.to_xml(encoding: "US-ASCII") ins.add_first_child coverxml.to_xml(encoding: "US-ASCII") end |
#word_example_cleanup(docxml) ⇒ Object
151 152 153 154 155 |
# File 'lib/isodoc/word_function/postprocess.rb', line 151 def word_example_cleanup(docxml) docxml.xpath("//div[@class = 'example']//p[not(@class)]").each do |p| p["class"] = "example" end end |
#word_floating_titles(docxml) ⇒ Object
move p.h1 (floating title) after any page, section breaks
189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/isodoc/word_function/postprocess.rb', line 189 def word_floating_titles(docxml) docxml.xpath("//p[@class = 'section-break' or @class = 'page-break']") .each do |b| out = b.xpath("./preceding-sibling::*").reverse .each_with_object([]) do |p, m| (p.name == "p" && p["class"] == "h1") or break m m << p end b.delete("class") out.empty? and next out[-1].previous = b.remove end end |
#word_footnote_format(docxml) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/isodoc/word_function/postprocess.rb', line 175 def word_footnote_format(docxml) # the content is in a[@epub:type = 'footnote']//sup, but in Word, # we need to inject content around the autonumbered footnote reference docxml.xpath("//a[@epub:type = 'footnote']").each do |x| footnote_reference_format(x) end docxml.xpath("//a[@class = 'TableFootnoteRef'] | " \ "//span[@class = 'TableFootnoteRef']").each do |x| table_footnote_reference_format(x) end docxml end |
#word_image_caption(docxml) ⇒ Object
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/isodoc/word_function/postprocess.rb', line 117 def (docxml) docxml.xpath("//p[@class = 'FigureTitle' or @class = 'SourceTitle']") .each do |t| if t.previous_element&.name == "img" img = t.previous_element t.previous_element.swap("<p class='figure'>#{img.to_xml}</p>") end style_update(t.previous_element, "page-break-after:avoid;") end end |
#word_intro(docxml, level) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/isodoc/word_function/postprocess_cover.rb', line 29 def word_intro(docxml, level) ins = docxml.at('//div[@class="WordSection2"]') or return intro = insert_toc(File.read(@wordintropage, encoding: "UTF-8"), docxml, level) intro = populate_template(intro, :word) introxml = to_word_xhtml_fragment(intro) #ins.children.first.previous = introxml.to_xml(encoding: "US-ASCII") ins.add_first_child introxml.to_xml(encoding: "US-ASCII") end |
#word_list_continuations(docxml) ⇒ Object
128 129 130 131 132 133 |
# File 'lib/isodoc/word_function/postprocess.rb', line 128 def word_list_continuations(docxml) list_add(docxml.xpath("//ul[not(ancestor::ul) and not(ancestor::ol)]"), 1) list_add(docxml.xpath("//ol[not(ancestor::ul) and not(ancestor::ol)]"), 1) end |
#word_nested_tables(docxml) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/isodoc/word_function/postprocess_table.rb', line 45 def word_nested_tables(docxml) docxml.xpath("//table").each do |t| t.xpath(".//table").reverse.each do |tt| t.next = tt.remove end end end |
#word_preface(docxml) ⇒ Object
4 5 6 7 8 9 |
# File 'lib/isodoc/word_function/postprocess_cover.rb', line 4 def word_preface(docxml) @wordcoverpage && !@wordcoverpage.empty? and word_cover(docxml) @wordintropage && !@wordintropage.empty? and word_intro(docxml, @wordToClevels) end |
#word_pseudocode_cleanup(docxml) ⇒ Object
157 158 159 160 161 |
# File 'lib/isodoc/word_function/postprocess.rb', line 157 def word_pseudocode_cleanup(docxml) docxml.xpath("//div[@class = 'pseudocode']//p[not(@class)]").each do |p| p["class"] = "pseudocode" end end |
#word_remove_empty_sections(docxml) ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/isodoc/word_function/postprocess_cover.rb', line 11 def word_remove_empty_sections(docxml) %w(WordSection1 WordSection2).each do |x| ins = docxml.at("//div[@class='#{x}']") or next @c.decode(ins.text).gsub(/\p{Z}|\p{C}/, "").strip.empty? or next ins.next_element.remove ins.remove end end |
#word_remove_empty_toc(docxml) ⇒ Object
72 73 74 75 |
# File 'lib/isodoc/word_function/postprocess.rb', line 72 def word_remove_empty_toc(docxml) docxml.at("//div[@class = 'TOC']//p[@class = 'MsoToc1']") and return remove_toc_div(docxml) end |
#word_remove_pb_before_annex(docxml) ⇒ Object
applies for <div class=“WordSectionN_M”><p><pagebreak/></p>…
164 165 166 167 168 169 170 171 172 173 |
# File 'lib/isodoc/word_function/postprocess.rb', line 164 def word_remove_pb_before_annex(docxml) docxml.xpath("//div[p/br]").each do |d| /^WordSection\d+_\d+$/.match(d["class"]) or next (d.elements[0].name == "p" && !d.elements[0].elements.empty?) or next (d.elements[0].elements[0].name == "br" && d.elements[0].elements[0]["style"] == "mso-special-character:line-break;page-break-before:always") or next d.elements[0].remove end end |
#word_section_breaks(docxml) ⇒ Object
122 123 124 125 126 127 128 129 130 |
# File 'lib/isodoc/word_function/postprocess_cover.rb', line 122 def word_section_breaks(docxml) @landscapestyle = "" word_section_breaks1(docxml, "WordSection2") word_section_breaks1(docxml, "WordSection3") word_remove_pb_before_annex(docxml) docxml.xpath("//br[@orientation]").each do |br| br.delete("orientation") end end |
#word_section_breaks1(docxml, sect) ⇒ Object
132 133 134 135 136 137 138 139 140 |
# File 'lib/isodoc/word_function/postprocess_cover.rb', line 132 def word_section_breaks1(docxml, sect) docxml.xpath("//div[@class = '#{sect}']//br[@orientation]").reverse .each_with_index do |br, i| @landscapestyle += "\ndiv.#{sect}_#{i} {page:#{sect}" \ "#{br['orientation'] == 'landscape' ? 'L' : 'P'};}\n" split_at_section_break(docxml, sect, br, i) end end |
#word_sourcecode_annotations(html) ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/isodoc/word_function/postprocess.rb', line 77 def word_sourcecode_annotations(html) ann = ".//div[@class = 'annotation']" html.xpath("//p[@class = '#{sourcecode_style}'][#{ann}]") .each do |p| ins = p.after("<p class='#{sourcecode_style}'/>").next_element p.xpath(ann).each do |d| ins << d.remove.children end end end |
#word_sourcecode_table(docxml) ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/isodoc/word_function/postprocess.rb', line 88 def word_sourcecode_table(docxml) s = "p[@class='#{sourcecode_style}']" docxml.xpath("//#{s}/div[@class='table_container']").each do |d| pre = d.at(".//#{s}") to_sourcecode_para(pre) d["id"] = d.parent["id"] d.parent.replace(d) end end |
#word_tab_clean(docxml) ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/isodoc/word_function/postprocess.rb', line 109 def word_tab_clean(docxml) docxml.xpath("//p[@class='Biblio']//span[@style='mso-tab-count:1']") .each do |s| s.next&.text? or next s.next.replace(@c.encode(s.next.text.sub(/^\s+/, ""), :hexadecimal)) end end |
#word_table_align(docxml) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/isodoc/word_function/postprocess_table.rb', line 59 def word_table_align(docxml) docxml.xpath("//td[@align]/p | //th[@align]/p").each do |p| p["align"] and next style_update(p, "text-align: #{p.parent['align']}") end end |
#word_table_pagebreak(docxml) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/isodoc/word_function/postprocess_table.rb', line 74 def word_table_pagebreak(docxml) docxml.xpath("//td[@style] | //th[@style]").each do |t| s = /(page-break-after:[^;]+)/.match(t["style"]) (s && s[1]) or next t.xpath(".//div | .//p | .//pre").each do |p| style_update(p, s[1]) end end end |
#word_table_separator(docxml) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/isodoc/word_function/postprocess_table.rb', line 66 def word_table_separator(docxml) docxml.xpath("//p[@class = 'TableTitle']").each do |t| t.children.empty? or next t["style"] = t["style"].sub(/;?$/, ";font-size:0pt;") t.children = " " end end |
#word_toc_entry(toclevel, heading) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/isodoc/word_function/postprocess_toc.rb', line 33 def word_toc_entry(toclevel, heading) bookmark = bookmarkid # 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 |
#word_toc_figure_preface1 ⇒ Object
115 116 117 118 119 120 |
# File 'lib/isodoc/word_function/postprocess_toc.rb', line 115 def word_toc_figure_preface1 <<~TOC <span lang="EN-GB"><span style='mso-element:field-begin'></span><span style='mso-spacerun:yes'> </span>TOC \\h \\z \\t #{toc_word_class_list figure_toc_class} <span style='mso-element:field-separator'></span></span> TOC end |
#word_toc_preface(level) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/isodoc/word_function/postprocess_toc.rb', line 49 def word_toc_preface(level) <<~TOC <span lang="EN-GB"><span style='mso-element:field-begin'></span><span style='mso-spacerun:yes'> </span>TOC \\o "1-#{level}" \\h \\z \\u <span style='mso-element:field-separator'></span></span> TOC end |
#word_toc_reqt_preface1 ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/isodoc/word_function/postprocess_toc.rb', line 100 def word_toc_reqt_preface1 <<~TOC <span lang="EN-GB"><span style='mso-element:field-begin'></span><span style='mso-spacerun:yes'> </span>TOC \\h \\z \\t #{toc_word_class_list reqt_toc_class} <span style='mso-element:field-separator'></span></span> TOC end |
#word_toc_table_preface1 ⇒ Object
108 109 110 111 112 113 |
# File 'lib/isodoc/word_function/postprocess_toc.rb', line 108 def word_toc_table_preface1 <<~TOC <span lang="EN-GB"><span style='mso-element:field-begin'></span><span style='mso-spacerun:yes'> </span>TOC \\h \\z \\t #{toc_word_class_list table_toc_class} <span style='mso-element:field-separator'></span></span> TOC end |
#wordstylesheet_update ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/isodoc/word_function/postprocess_cover.rb', line 55 def wordstylesheet_update @wordstylesheet.nil? and return f = File.open(@wordstylesheet.path, "a") @landscapestyle.empty? or f.write(@landscapestyle) s = @meta.get[:code_css] and f.write(s.gsub(/sourcecode/, "p.#{sourcecode_style}")) if @wordstylesheet_override && @wordstylesheet f.write(@wordstylesheet_override.read) @wordstylesheet_override.close elsif @wordstylesheet_override && !@wordstylesheet @wordstylesheet = @wordstylesheet_override end f.close @wordstylesheet end |