Module: IsoDoc::WordFunction::Postprocess

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

Constant Summary collapse

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_PREFACE1 =
<<~TOC.freeze
  <span lang="EN-GB"><span
    style='mso-element:field-begin'></span><span
    style='mso-spacerun:yes'>&#xA0;</span>TOC
    \\o &quot;1-2&quot; \\h \\z \\u <span
    style='mso-element:field-separator'></span></span>
TOC
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



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/isodoc/word_function/postprocess.rb', line 87

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

#make_WordToC(docxml) ⇒ Object



136
137
138
139
140
141
142
143
144
# File 'lib/isodoc/word_function/postprocess.rb', line 136

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



14
15
16
17
18
19
# File 'lib/isodoc/word_function/postprocess.rb', line 14

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



5
6
7
8
9
10
11
12
# File 'lib/isodoc/word_function/postprocess.rb', line 5

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

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



21
22
23
24
25
26
27
28
# File 'lib/isodoc/word_function/postprocess.rb', line 21

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



30
31
32
33
34
35
# File 'lib/isodoc/word_function/postprocess.rb', line 30

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

force Annex h2 to be p.h2Annex, so it is not picked up by ToC



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

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



37
38
39
40
41
42
43
# File 'lib/isodoc/word_function/postprocess.rb', line 37

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

#word_cover(docxml) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/isodoc/word_function/postprocess.rb', line 70

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



78
79
80
81
82
83
84
85
# File 'lib/isodoc/word_function/postprocess.rb', line 78

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



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

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

#word_table_separator(docxml) ⇒ Object



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

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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/isodoc/word_function/postprocess.rb', line 99

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