require "fileutils" module IsoDoc::WordFunction module Postprocess # add namespaces for Word fragments WORD_NOKOHEAD = <<~HERE.freeze
"\ "
" def word_table_separator(docxml) docxml.xpath("//table").each do |t| next unless t&.next_element&.name == "table" t.add_next_sibling(EMPTY_PARA) end end # force Annex h2 to be p.h2Annex, so it is not picked up by ToC def word_annex_cleanup(docxml) docxml.xpath("//h2[ancestor::*[@class = 'Section3']]").each do |h2| h2.name = "p" h2["class"] = "h2Annex" end end def word_preface(docxml) word_cover(docxml) if @wordcoverpage word_intro(docxml) 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) intro = File.read(@wordintropage, encoding: "UTF-8"). sub(/WORDTOC/, make_WordToC(docxml)) 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 generate_header(filename, _dir) return nil unless @header template = IsoDoc::Common.liquid(File.read(@header, encoding: "UTF-8")) meta = @meta.get meta[:filename] = filename params = meta.map { |k, v| [k.to_s, v] }.to_h headerfile = "header.html" File.open(headerfile, "w:UTF-8") { |f| f.write(template.render(params)) } @files_to_delete << headerfile headerfile end def word_toc_entry(toclevel, heading) bookmark = Random.rand(1000000000) <<~TOC#{heading} . PAGEREF _Toc#{bookmark} \\h 1
TOC end WORD_TOC_PREFACE1 = <<~TOC.freeze TOC \\o "1-2" \\h \\z \\u TOC WORD_TOC_SUFFIX1 = <<~TOC.freeze)/, %{\\1#{WORD_TOC_PREFACE1}}) + WORD_TOC_SUFFIX1 end end end