Module: IsoDoc::WordFunction::Postprocess

Included in:
IsoDoc::WordConvert
Defined in:
lib/isodoc/word_function/postprocess.rb

Constant Summary collapse

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
EMPTY_PARA =
"<p style='margin-top:0cm;margin-right:0cm;"\
"margin-bottom:0cm;margin-left:0.0pt;margin-bottom:.0001pt;"\
"line-height:1.0pt;mso-line-height-rule:exactly'>"\
"<span lang=EN-GB style='display:none;mso-hide:all'>&nbsp;</span></p>"
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>&nbsp;</o:p></span></p>
TOC

Instance Method Summary collapse

Instance Method Details

#generate_header(filename, _dir) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/isodoc/word_function/postprocess.rb', line 124

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

#insert_toc(intro, docxml, level) ⇒ Object



112
113
114
# File 'lib/isodoc/word_function/postprocess.rb', line 112

def insert_toc(intro, docxml, level)
  intro.sub(/WORDTOC/, make_WordToC(docxml, level))
end

#list_add(xpath, level) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/isodoc/word_function/postprocess.rb', line 71

def list_add(xpath, level)
  xpath.each do |list|
    (list.xpath(".//li") - list.xpath(".//ol//li | .//ul//li")).each do |li|
      li.xpath("./p | ./div/p").each_with_index do |p, i|
        next if p == 0
        p["class"] = "ListContLevel#{level}"
      end
      list_add(li.xpath(".//ul") - li.xpath(".//ul//ul | .//ol//ul"), level + 1)
      list_add(li.xpath(".//ol") - li.xpath(".//ul//ol | .//ol//ol"), level + 1)
    end
  end
end

#make_WordToC(docxml, level) ⇒ Object



175
176
177
178
179
180
181
182
183
184
# File 'lib/isodoc/word_function/postprocess.rb', line 175

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(/(<p class="MsoToc1">)/,
          %{\\1#{word_toc_preface(level)}}) +  WORD_TOC_SUFFIX1
end

#postprocess(result, filename, dir) ⇒ Object



34
35
36
37
38
39
# File 'lib/isodoc/word_function/postprocess.rb', line 34

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| FileUtils.rm_f f }
end

#table_note_cleanup(docxml) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/isodoc/word_function/postprocess.rb', line 25

def table_note_cleanup(docxml)
  super
  # preempt html2doc putting MsoNormal there
  docxml.xpath("//p[not(self::*[@class])]"\
               "[ancestor::*[@class = 'Note']]").each do |p|
    p["class"] = "Note"
  end
end

#to_word_xhtml_fragment(xml) ⇒ Object



18
19
20
21
22
23
# File 'lib/isodoc/word_function/postprocess.rb', line 18

def to_word_xhtml_fragment(xml)
  doc = ::Nokogiri::XML.parse(WORD_NOKOHEAD)
  #fragment = doc.fragment(xml)
  fragment = ::Nokogiri::XML::DocumentFragment.new(doc, xml, doc.root)
  fragment
end

#toWord(result, filename, dir, header) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/isodoc/word_function/postprocess.rb', line 41

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_admonition_images(docxml) ⇒ Object



50
51
52
53
54
55
# File 'lib/isodoc/word_function/postprocess.rb', line 50

def word_admonition_images(docxml)
  docxml.xpath("//div[@class = 'Admonition']//img").each do |i|
    i["width"], i["height"] =
      Html2Doc.image_resize(i, File.join(@localdir, i["src"]), @maxheight, 300)
  end
end

#word_annex_cleanup(docxml) ⇒ Object



96
97
# File 'lib/isodoc/word_function/postprocess.rb', line 96

def word_annex_cleanup(docxml)
end

#word_cleanup(docxml) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/isodoc/word_function/postprocess.rb', line 57

def word_cleanup(docxml)
  word_annex_cleanup(docxml)
  word_preface(docxml)
  word_table_separator(docxml)
  word_admonition_images(docxml)
  word_list_continuations(docxml)
  docxml
end

#word_cover(docxml) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/isodoc/word_function/postprocess.rb', line 104

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

#word_intro(docxml, level) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/isodoc/word_function/postprocess.rb', line 116

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

#word_list_continuations(docxml) ⇒ Object



66
67
68
69
# File 'lib/isodoc/word_function/postprocess.rb', line 66

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_preface(docxml) ⇒ Object



99
100
101
102
# File 'lib/isodoc/word_function/postprocess.rb', line 99

def word_preface(docxml)
  word_cover(docxml) if @wordcoverpage
  word_intro(docxml, @wordToClevels) if @wordintropage
end

#word_table_separator(docxml) ⇒ Object



89
90
91
92
93
94
# File 'lib/isodoc/word_function/postprocess.rb', line 89

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

#word_toc_entry(toclevel, heading) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/isodoc/word_function/postprocess.rb', line 136

def word_toc_entry(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

#word_toc_preface(level) ⇒ Object



159
160
161
162
163
164
165
166
167
# File 'lib/isodoc/word_function/postprocess.rb', line 159

def word_toc_preface(level) 
  <<~TOC.freeze
  <span lang="EN-GB"><span
    style='mso-element:field-begin'></span><span
    style='mso-spacerun:yes'>&#xA0;</span>TOC
    \\o &quot;1-#{level}&quot; \\h \\z \\u <span
    style='mso-element:field-separator'></span></span>
  TOC
end