Module: IsoDoc::WordFunction::Postprocess
- Included in:
- IsoDoc::WordConvert
- Defined in:
- lib/isodoc/word_function/postprocess.rb
Constant Summary collapse
- WORD_TOC_PREFACE1 =
"<span lang=\"EN-GB\"><span\n style='mso-element:field-begin'></span><span\n style='mso-spacerun:yes'> </span>TOC\n \\\\o "1-2" \\\\h \\\\z \\\\u <span\n style='mso-element:field-separator'></span></span>\n".freeze
- WORD_TOC_SUFFIX1 =
"<p class=\"MsoToc1\"><span lang=\"EN-GB\"><span\n style='mso-element:field-end'></span></span><span\n lang=\"EN-GB\"><o:p> </o:p></span></p>\n".freeze
Instance Method Summary collapse
- #generate_header(filename, _dir) ⇒ Object
- #make_WordToC(docxml) ⇒ Object
- #postprocess(result, filename, dir) ⇒ Object
- #toWord(result, filename, dir, header) ⇒ Object
-
#word_annex_cleanup(docxml) ⇒ Object
force Annex h2 to be p.h2Annex, so it is not picked up by ToC.
- #word_cleanup(docxml) ⇒ Object
- #word_cover(docxml) ⇒ Object
- #word_intro(docxml) ⇒ Object
- #word_preface(docxml) ⇒ Object
- #word_toc_entry(toclevel, heading) ⇒ Object
Instance Method Details
#generate_header(filename, _dir) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/isodoc/word_function/postprocess.rb', line 56 def generate_header(filename, _dir) return nil unless @header #template = Liquid::Template.parse(File.read(@header, encoding: "UTF-8")) template = IsoDoc::Common.liquid(File.read(@header, encoding: "UTF-8")) = .get [:filename] = filename params = .map { |k, v| [k.to_s, v] }.to_h File.open("header.html", "w") do |f| f.write(template.render(params)) end @files_to_delete << "header.html" "header.html" end |
#make_WordToC(docxml) ⇒ Object
107 108 109 110 111 112 113 114 115 |
# File 'lib/isodoc/word_function/postprocess.rb', line 107 def make_WordToC(docxml) toc = "" docxml.xpath("//h1 | //h2[not(ancestor::*[@class = 'Section3'])]"). each do |h| toc += word_toc_entry(h.name == "h1" ? 1 : 2, header_strip(h)) end toc.sub(/(<p class="MsoToc1">)/, %{\\1#{WORD_TOC_PREFACE1}}) + WORD_TOC_SUFFIX1 end |
#postprocess(result, filename, dir) ⇒ Object
4 5 6 7 8 9 |
# File 'lib/isodoc/word_function/postprocess.rb', line 4 def postprocess(result, filename, dir) header = generate_header(filename, dir) result = from_xhtml(cleanup(to_xhtml(result))) toWord(result, filename, dir, header) @files_to_delete.each { |f| system "rm #{f}" } end |
#toWord(result, filename, dir, header) ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/isodoc/word_function/postprocess.rb', line 11 def toWord(result, filename, dir, header) result = populate_template(result, :word) result = from_xhtml(word_cleanup(to_xhtml(result))) Html2Doc.process(result, filename: filename, stylesheet: @wordstylesheet, header_file: header, dir: dir, asciimathdelims: [@openmathdelim, @closemathdelim], liststyles: { ul: @ulstyle, ol: @olstyle }) end |
#word_annex_cleanup(docxml) ⇒ Object
force Annex h2 to be p.h2Annex, so it is not picked up by ToC
27 28 29 30 31 32 |
# File 'lib/isodoc/word_function/postprocess.rb', line 27 def word_annex_cleanup(docxml) docxml.xpath("//h2[ancestor::*[@class = 'Section3']]").each do |h2| h2.name = "p" h2["class"] = "h2Annex" end end |
#word_cleanup(docxml) ⇒ Object
20 21 22 23 24 |
# File 'lib/isodoc/word_function/postprocess.rb', line 20 def word_cleanup(docxml) word_preface(docxml) word_annex_cleanup(docxml) docxml end |
#word_cover(docxml) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/isodoc/word_function/postprocess.rb', line 39 def word_cover(docxml) cover = File.read(@wordcoverpage, encoding: "UTF-8") cover = populate_template(cover, :word) coverxml = to_xhtml_fragment(cover) docxml.at('//div[@class="WordSection1"]').children.first.previous = coverxml.to_xml(encoding: "US-ASCII") end |
#word_intro(docxml) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/isodoc/word_function/postprocess.rb', line 47 def word_intro(docxml) intro = File.read(@wordintropage, encoding: "UTF-8"). sub(/WORDTOC/, make_WordToC(docxml)) intro = populate_template(intro, :word) introxml = to_xhtml_fragment(intro) docxml.at('//div[@class="WordSection2"]').children.first.previous = introxml.to_xml(encoding: "US-ASCII") end |
#word_preface(docxml) ⇒ Object
34 35 36 37 |
# File 'lib/isodoc/word_function/postprocess.rb', line 34 def word_preface(docxml) word_cover(docxml) if @wordcoverpage word_intro(docxml) if @wordintropage end |
#word_toc_entry(toclevel, heading) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/isodoc/word_function/postprocess.rb', line 70 def word_toc_entry(toclevel, heading) bookmark = Random.rand(1000000000) " <p class=\"MsoToc\#{toclevel}\"><span class=\"MsoHyperlink\"><span\n lang=\"EN-GB\" style='mso-no-proof:yes'>\n <a href=\"#_Toc\#{bookmark}\">\#{heading}<span lang=\"EN-GB\"\n class=\"MsoTocTextSpan\">\n <span style='mso-tab-count:1 dotted'>. </span>\n </span><span lang=\"EN-GB\" class=\"MsoTocTextSpan\">\n <span style='mso-element:field-begin'></span></span>\n <span lang=\"EN-GB\"\n class=\"MsoTocTextSpan\"> PAGEREF _Toc\#{bookmark} \\\\h </span>\n <span lang=\"EN-GB\" class=\"MsoTocTextSpan\"><span\n style='mso-element:field-separator'></span></span><span\n lang=\"EN-GB\" class=\"MsoTocTextSpan\">1</span>\n <span lang=\"EN-GB\"\n class=\"MsoTocTextSpan\"></span><span\n lang=\"EN-GB\" class=\"MsoTocTextSpan\"><span\n style='mso-element:field-end'></span></span></a></span></span></p>\n\n TOC\nend\n" |