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'>"\
"<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



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

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"))
  meta = @meta.get
  meta[:filename] = filename
  params = meta.map { |k, v| [k.to_s, v] }.to_h
  File.open("header.html", "w:UTF-8") do |f|
    f.write(template.render(params))
  end
  @files_to_delete << "header.html"
  "header.html"
end

#make_WordToC(docxml) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'lib/isodoc/word_function/postprocess.rb', line 129

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

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



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

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



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

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

#word_cover(docxml) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/isodoc/word_function/postprocess.rb', line 61

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



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

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



56
57
58
59
# File 'lib/isodoc/word_function/postprocess.rb', line 56

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

#word_table_separator(docxml) ⇒ Object



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

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/isodoc/word_function/postprocess.rb', line 92

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