module IsoDoc::WordFunction module Postprocess def word_preface(docxml) word_cover(docxml) if @wordcoverpage word_intro(docxml, @wordToClevels) if @wordintropage end def word_cover(docxml) cover = File.read(@wordcoverpage, encoding: "UTF-8") cover = populate_template(cover, :word) coverxml = to_word_xhtml_fragment(cover) docxml.at('//div[@class="WordSection1"]').children.first.previous = coverxml.to_xml(encoding: "US-ASCII") end def word_intro(docxml, level) intro = insert_toc(File.read(@wordintropage, encoding: "UTF-8"), docxml, level) intro = populate_template(intro, :word) introxml = to_word_xhtml_fragment(intro) docxml.at('//div[@class="WordSection2"]').children.first.previous = introxml.to_xml(encoding: "US-ASCII") end def insert_toc(intro, docxml, level) intro.sub(/WORDTOC/, make_WordToC(docxml, level)) end def word_toc_entry(toclevel, heading) bookmark = bookmarkid # Random.rand(1000000000) <<~TOC

#{heading} . PAGEREF _Toc#{bookmark} \\h 1

TOC end def word_toc_preface(level) <<~TOC.freeze  TOC \\o "1-#{level}" \\h \\z \\u TOC end WORD_TOC_SUFFIX1 = <<~TOC.freeze

 

TOC def make_WordToC(docxml, level) toc = "" #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(/(

)/, %{\\1#{word_toc_preface(level)}}) + WORD_TOC_SUFFIX1 end def authority_cleanup1(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 { |h| h.remove } auth&.xpath(".//h1 | .//h2")&.each do |h| h.name = "p" h["class"] = "TitlePageSubhead" end dest and auth and dest.replace(auth.remove) end def authority_cleanup(docxml) %w(copyright license legal feedback).each do |t| authority_cleanup1(docxml, t) end end def generate_header(filename, _dir) return nil unless @header template = IsoDoc::Common.liquid(File.read(@header, encoding: "UTF-8")) meta = @meta.get.merge(@labels ? { labels: @labels } : {}).merge(@meta.labels ? { labels: @meta.labels } : {}) meta[:filename] = filename params = meta.map { |k, v| [k.to_s, v] }.to_h Tempfile.open(%w(header html), :encoding => "utf-8") do |f| f.write(template.render(params)) f end end 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 { |br| br.delete("orientation") } end 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 def split_at_section_break(docxml, sect, br, i) move = br.parent.xpath("following::node()") & br.document.xpath("//div[@class = '#{sect}']//*") ins = docxml.at("//div[@class = '#{sect}']").after("

").next_element move.each do |m| next if m.at("./ancestor::div[@class = '#{sect}_#{i}']") ins << m.remove end end end end