Class: IsoDoc::Iec::WordConvert

Inherits:
IsoDoc::Iso::WordConvert
  • Object
show all
Includes:
BaseConvert, Init
Defined in:
lib/isodoc/iec/word_convert.rb

Constant Summary collapse

BOILERPLATE_PARAS =

Incredibly, the numbered boilerplate list in IEC is NOT A LIST, and it violates numbering conventions for ordered lists (arabic not alpha)

"//div[@class = 'boilerplate_legal']//li/p".freeze

Instance Method Summary collapse

Methods included from Init

#bibrenderer, #convert1, #i18n_init, #metadata_init, #std_docid_semantic, #xref_init

Methods included from BaseConvert

#biblio_list, #bibliography, #boilerplate, #clause_attrs, #foreword, #para_class, #set_termdomain, #terms_parse

Constructor Details

#initialize(options) ⇒ WordConvert

Returns a new instance of WordConvert.



9
10
11
12
# File 'lib/isodoc/iec/word_convert.rb', line 9

def initialize(options)
  super
  @libdir = File.dirname(__FILE__)
end

Instance Method Details

#annex_name(_annex, name, div) ⇒ Object



205
206
207
208
209
210
211
212
# File 'lib/isodoc/iec/word_convert.rb', line 205

def annex_name(_annex, name, div)
  return if name.nil?

  div.h1 class: "Annex" do |t|
    name.children.each { |c2| parse(c2, t) }
    clause_parse_subtitle(name, t)
  end
end

#authority_cleanup(docxml) ⇒ Object



176
177
178
179
180
181
# File 'lib/isodoc/iec/word_convert.rb', line 176

def authority_cleanup(docxml)
  auth = docxml.at("//div[@id = 'boilerplate-feedback' or " \
                   "@class = 'boilerplate-feedback']")
  auth&.remove
  super
end

#bibliography_attrsObject



189
190
191
# File 'lib/isodoc/iec/word_convert.rb', line 189

def bibliography_attrs
  { class: "Section3" }
end

#convert(input_filename, file = nil, debug = false, output_filename = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/isodoc/iec/word_convert.rb', line 14

def convert(input_filename, file = nil, debug = false,
          output_filename = nil)
  file = File.read(input_filename, encoding: "utf-8") if file.nil?
  @openmathdelim, @closemathdelim = extract_delims(file)
  docxml, filename, dir = convert_init(file, input_filename, debug)
  result = convert1(docxml, filename, dir)
  return result if debug

  output_filename ||= "#{filename}.#{@suffix}"
  postprocess(result, output_filename, dir)
  FileUtils.rm_rf dir
end

#default_file_locations(_options) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/isodoc/iec/word_convert.rb', line 45

def default_file_locations(_options)
  @libdir = File.dirname(__FILE__)
  {
    wordstylesheet: html_doc_path("wordstyle.scss"),
    standardstylesheet: html_doc_path("isodoc.scss"),
    header: html_doc_path("header.html"),
    wordcoverpage: html_doc_path("word_iec_titlepage.html"),
    wordintropage: html_doc_path("word_iec_intro.html"),
    ulstyle: "l22",
    olstyle: "l2",
  }
end

#default_fonts(options) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/isodoc/iec/word_convert.rb', line 33

def default_fonts(options)
  {
    bodyfont: font_choice(options),
    headerfont: font_choice(options),
    monospacefont: '"Courier New",monospace',
    normalfontsize: "10.0pt",
    footnotefontsize: "8.0pt",
    smallerfontsize: "8.0pt",
    monospacefontsize: "9.0pt",
  }
end

#figure_name_attrs(_node) ⇒ Object



197
198
199
# File 'lib/isodoc/iec/word_convert.rb', line 197

def figure_name_attrs(_node)
  { class: "FigureTitle", style: "text-align:center;" }
end

#font_choice(options) ⇒ Object



27
28
29
30
31
# File 'lib/isodoc/iec/word_convert.rb', line 27

def font_choice(options)
  if options[:script] == "Hans" then '"Source Han Sans",serif'
  else '"Arial",sans-serif'
  end
end

#formula_parse1(node, out) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/isodoc/iec/word_convert.rb', line 214

def formula_parse1(node, out)
  out.div **attr_code(class: "formula") do |div|
    div.p **attr_code(class: "formula") do |_p|
      insert_tab(div, 1)
      parse(node.at(ns("./stem")), div)
      if lbl = node&.at(ns("./name"))&.text
        insert_tab(div, 1)
        div << "(#{lbl})"
      end
    end
  end
end

#header_strip(hdr) ⇒ Object



88
89
90
91
# File 'lib/isodoc/iec/word_convert.rb', line 88

def header_strip(hdr)
  hdr = hdr.to_s.gsub(/<\/?p[^>]*>/, "")
  super
end

#make_body1(body, _docxml) ⇒ Object



183
# File 'lib/isodoc/iec/word_convert.rb', line 183

def make_body1(body, _docxml); end

#make_figure_word_toc(docxml) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/isodoc/iec/word_convert.rb', line 68

def make_figure_word_toc(docxml)
  docxml.at(figure_toc_xpath) or return ""
  toc = ""
  docxml.xpath(figure_toc_xpath).each do |h|
    toc += word_toc_entry(1, header_strip(h))
  end
  toc.sub(/(<p class="MsoToc1">)/,
          %{\\1#{word_toc_figure_preface1}}) + WORD_TOC_SUFFIX1
end

#make_table_word_toc(docxml) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/isodoc/iec/word_convert.rb', line 58

def make_table_word_toc(docxml)
  docxml.at(table_toc_xpath) or return ""
  toc = ""
  docxml.xpath(table_toc_xpath).each do |h|
    toc += word_toc_entry(1, header_strip(h))
  end
  toc.sub(/(<p class="MsoToc1">)/,
          %{\\1#{word_toc_table_preface1}}) + WORD_TOC_SUFFIX1
end

#make_tr_attr(cell, row, totalrows, header, bordered) ⇒ Object



99
100
101
102
103
104
# File 'lib/isodoc/iec/word_convert.rb', line 99

def make_tr_attr(cell, row, totalrows, header, bordered)
  ret = super
  css_class =
    cell.name == "th" || header ? "TABLE-col-heading" : "TABLE-cell"
  ret.merge(class: css_class)
end

#non_annex_h1(docxml) ⇒ Object



148
149
150
151
152
153
154
155
# File 'lib/isodoc/iec/word_convert.rb', line 148

def non_annex_h1(docxml)
  docxml.xpath("//h1[not(@class)]").each do |h1|
    h1["class"] = "main"
  end
  docxml.xpath("//h1[@class = 'Section3']").each do |h1|
    h1["class"] = "main"
  end
end

#style_cleanup(docxml) ⇒ Object



187
# File 'lib/isodoc/iec/word_convert.rb', line 187

def style_cleanup(docxml); end

#table_title_attrs(_node) ⇒ Object



201
202
203
# File 'lib/isodoc/iec/word_convert.rb', line 201

def table_title_attrs(_node)
  { class: "TableTitle", style: "text-align:center;" }
end

#termref_attrsObject



193
194
195
# File 'lib/isodoc/iec/word_convert.rb', line 193

def termref_attrs
  {}
end

#tr_parse(node, out, ord, totalrows, header) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/isodoc/iec/word_convert.rb', line 106

def tr_parse(node, out, ord, totalrows, header)
  c = node.parent.parent["class"]
  bordered = %w(modspec).include?(c) || !c
  out.tr do |r|
    node.elements.each do |td|
      attrs = make_tr_attr(td, ord, totalrows - 1, header, bordered)
      attrs[:class] = "TABLE-col-heading" if header
      r.send td.name, **attr_code(attrs) do |entry|
        td.children.each { |n| parse(n, entry) }
      end
    end
  end
end

#word_annex_cleanup(docxml) ⇒ Object



143
144
145
146
# File 'lib/isodoc/iec/word_convert.rb', line 143

def word_annex_cleanup(docxml)
  super
  non_annex_h1(docxml)
end

#word_cleanup(docxml) ⇒ Object



93
94
95
96
97
# File 'lib/isodoc/iec/word_convert.rb', line 93

def word_cleanup(docxml)
  word_foreword_cleanup(docxml)
  word_table_cleanup(docxml)
  super
end

#word_cover(docxml) ⇒ Object



185
# File 'lib/isodoc/iec/word_convert.rb', line 185

def word_cover(docxml); end

#word_foreword_cleanup(docxml) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/isodoc/iec/word_convert.rb', line 162

def word_foreword_cleanup(docxml)
  docxml.xpath(BOILERPLATE_PARAS).each_with_index do |l, i|
    l["class"] = "FOREWORD"
    l.children.first.add_previous_sibling(
      %{#{i + 1})<span style="mso-tab-count:1">&#xA0; </span>},
    )
  end
  docxml.xpath("//div[@class = 'boilerplate_legal']//li").each do |l|
    l.replace(l.children)
  end
  b = docxml.at("//div[@class = 'boilerplate_legal']")
  b and b.replace(b.children)
end

#word_table_cleanup(docxml) ⇒ Object



120
121
122
123
124
125
126
127
# File 'lib/isodoc/iec/word_convert.rb', line 120

def word_table_cleanup(docxml)
  docxml.xpath("//table//*[@class = 'Sourcecode']").each do |p|
    p["class"] = "CODE-TableCell"
  end
  %w(TABLE-col-heading TABLE-cell).each do |style|
    word_table_cleanup1(docxml, style)
  end
end

#word_table_cleanup1(docxml, style) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/isodoc/iec/word_convert.rb', line 129

def word_table_cleanup1(docxml, style)
  %w(td th).each do |tdh|
    docxml.xpath("//#{tdh}[@class = '#{style}'][not(descendant::p)]")
      .each do |td|
      p = Nokogiri::XML::Element.new("p", docxml)
      td.children.each { |c| c.parent = p }
      p.parent = td
    end
    docxml.xpath("//#{tdh}[@class = '#{style}']//p").each do |p|
      p["class"] ||= style
    end
  end
end

#word_toc_preface(level) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/isodoc/iec/word_convert.rb', line 78

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 "1-#{level}" \\h \\z \\u <span
      style='mso-element:field-separator'></span></span>
  TOC
end