Class: IsoDoc::Mpfd::WordConvert

Inherits:
WordConvert
  • Object
show all
Defined in:
lib/isodoc/mpfd/word_convert.rb

Overview

A Converter implementation that generates Word output, and a document schema encapsulation of the document for validation

Constant Summary collapse

TERM_CLAUSE =
"//preface/terms | "\
"//preface/clause[descendant::terms]".freeze
SECTIONS_XPATH =
"//foreword | //introduction | //preface/terms | //preface/clause | //annex | "\
"//sections/clause | //bibliography/references | "\
"//bibliography/clause".freeze
FRONT_CLAUSE =
"//*[parent::preface]".freeze

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ WordConvert

Returns a new instance of WordConvert.



10
11
12
13
14
15
16
17
# File 'lib/isodoc/mpfd/word_convert.rb', line 10

def initialize(options)
  @libdir = File.dirname(__FILE__)
  super
  FileUtils.cp html_doc_path("logo.jpg"), "logo.jpg"
  FileUtils.cp html_doc_path('[email protected]'), "[email protected]"
  @files_to_delete << "logo.jpg"
  @files_to_delete << "[email protected]"
end

Instance Method Details

#annex_name(annex, name, div) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/isodoc/mpfd/word_convert.rb', line 56

def annex_name(annex, name, div)
  div.h1 **{ class: "Annex" } do |t|
    t << "#{get_anchors[annex['id']][:label]} "
    t.b do |b|
      name&.children&.each { |c2| parse(c2, b) }
    end
  end
end

#annex_name_lbl(clause, num) ⇒ Object



159
160
161
# File 'lib/isodoc/mpfd/word_convert.rb', line 159

def annex_name_lbl(clause, num)
  l10n("<b>#{@annex_lbl} #{num}</b>")
end

#annex_names(clause, num) ⇒ Object



218
219
220
221
222
223
224
225
226
227
# File 'lib/isodoc/mpfd/word_convert.rb', line 218

def annex_names(clause, num)
  @anchors[clause["id"]] = { label: annex_name_lbl(clause, num),
                             xref: "#{@annex_lbl} #{num}", level: 1 }
  i = 0
  clause.xpath(ns("./clause")).each do |c|
    container_names(c, 0)
    i = annex_naming(c, num, 1, i)
  end
  hierarchical_asset_names(clause, num)
end

#annex_names1(clause, num, level) ⇒ Object



229
230
231
232
233
234
235
236
# File 'lib/isodoc/mpfd/word_convert.rb', line 229

def annex_names1(clause, num, level)
  clause["container"] or @anchors[clause["id"]] = 
    { label: num, xref: "#{@annex_lbl} #{num}", level: level }
  i = 0
  clause.xpath(ns("./clause")).each do |c|
    i = annex_naming(c, num, level, i)
  end
end

#annex_naming(c, num, lvl, i) ⇒ Object



209
210
211
212
213
214
215
216
# File 'lib/isodoc/mpfd/word_convert.rb', line 209

def annex_naming(c, num, lvl, i)
  if c["guidance"] then annex_names1(c, "#{num}E", lvl + 1)
  else
    i+= 1 unless c["container"]
    annex_names1(c, "#{num}.#{i}", lvl + 1)
  end
  i
end

#clause(isoxml, out) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
# File 'lib/isodoc/mpfd/word_convert.rb', line 238

def clause(isoxml, out)
  isoxml.xpath(ns(MIDDLE_CLAUSE)).each do |c|
    out.div **attr_code(id: c["id"]) do |s|
      clause_name(get_anchors[c['id']][:label],
                  c&.at(ns("./title"))&.content, s, class: c["container"] ? "containerhdr" : nil )
      c.elements.reject { |c1| c1.name == "title" }.each do |c1|
        parse(c1, s)
      end
    end
  end
end

#clause_names(docxml, sect_num) ⇒ Object



163
164
165
166
167
168
169
170
171
# File 'lib/isodoc/mpfd/word_convert.rb', line 163

def clause_names(docxml, sect_num)
  q = "//clause[parent::sections]"
  @topnum = nil
  lvl = 0
  docxml.xpath(ns(q)).each do |c|
    container_names(c, 0)
    sect_num, lvl = sect_names(c, nil, sect_num, 0, lvl)
  end
end

#clause_parse_title(node, div, c1, out) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/isodoc/mpfd/word_convert.rb', line 250

def clause_parse_title(node, div, c1, out)
  if node["inline-header"] == "true"
    inline_header_title(out, node, c1)
  else
    attrs = { class: node["container"] ? "containerhdr" : nil }
    div.send "h#{get_anchors[node['id']][:level]}", **attr_code(attrs) do |h|
      lbl = get_anchors[node['id']][:label]
      h << "#{lbl}. " if lbl && !@suppressheadingnumbers
      c1&.children&.each { |c2| parse(c2, h) }
    end
  end
end

#container_names(clause, lvl) ⇒ Object



173
174
175
176
177
178
179
180
181
182
# File 'lib/isodoc/mpfd/word_convert.rb', line 173

def container_names(clause, lvl)
  if clause["container"]
    @anchors[clause["id"]] =
      { label: nil, xref: clause.at(ns("./title"))&.text, level: lvl+1 }
  end
  clause.xpath(ns("./clause | ./term  | ./terms | "\
                  "./definitions")).each do |c|
    container_names(c, clause["container"] ? lvl+1 : lvl)
  end
end

#default_file_locations(options) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/isodoc/mpfd/word_convert.rb', line 27

def default_file_locations(options)
  {
    htmlstylesheet: html_doc_path("htmlstyle.scss"),
    htmlcoverpage: html_doc_path("html_rsd_titlepage.html"),
    htmlintropage: html_doc_path("html_rsd_intro.html"),
    scripts: html_doc_path("scripts.html"),
    wordstylesheet: html_doc_path("wordstyle.scss"),
    standardstylesheet: html_doc_path("rsd.scss"),
    header: html_doc_path("header.html"),
    wordcoverpage: html_doc_path("word_rsd_titlepage.html"),
    wordintropage: html_doc_path("word_rsd_intro.html"),
    ulstyle: "l3",
    olstyle: "l2",
  }
end

#default_fonts(options) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/isodoc/mpfd/word_convert.rb', line 19

def default_fonts(options)
  {
    bodyfont: (options[:script] == "Hans" ? '"SimSun",serif' : '"Arial",sans-serif'),
    headerfont: (options[:script] == "Hans" ? '"SimHei",sans-serif' : '"Arial",sans-serif'),
    monospacefont: '"Courier New",monospace'
  }
end

#error_parse(node, out) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/isodoc/mpfd/word_convert.rb', line 69

def error_parse(node, out)
  # catch elements not defined in ISO
  case node.name
  when "pre"
    pre_parse(node, out)
  when "keyword"
    out.span node.text, **{ class: "keyword" }
  else
    super
  end
end

#fileloc(loc) ⇒ Object



81
82
83
# File 'lib/isodoc/mpfd/word_convert.rb', line 81

def fileloc(loc)
  File.join(File.dirname(__FILE__), loc)
end

#i18n_init(lang, script) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/isodoc/mpfd/word_convert.rb', line 85

def i18n_init(lang, script)
  super
  y = if lang == "en"
        YAML.load_file(File.join(File.dirname(__FILE__), "i18n-en.yaml"))
      elsif lang == "zh" && script == "Hans"
        YAML.load_file(File.join(File.dirname(__FILE__),
                                 "i18n-zh-Hans.yaml"))
      else
        YAML.load_file(File.join(File.dirname(__FILE__), "i18n-en.yaml"))
      end
  @labels = @labels.merge(y)
  @annex_lbl = y["annex"]
  @clause_lbl = y["clause"]
end

#initial_anchor_names(d) ⇒ Object



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

def initial_anchor_names(d)
  d.xpath(ns(FRONT_CLAUSE)).each do |c|
    preface_names(c)
    sequential_asset_names(c)
  end
  middle_section_asset_names(d)
  clause_names(d, 0)
  termnote_anchor_names(d)
  termexample_anchor_names(d)
end

#make_body(xml, docxml) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/isodoc/mpfd/word_convert.rb', line 47

def make_body(xml, docxml)
  body_attr = { lang: "EN-US", link: "blue", vlink: "#954F72" }
  xml.body **body_attr do |body|
    make_body1(body, docxml)
    make_body2(body, docxml)
    make_body3(body, docxml)
  end
end

#make_body2(body, docxml) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'lib/isodoc/mpfd/word_convert.rb', line 136

def make_body2(body, docxml)
  body.div **{ class: "WordSection2" } do |div2|
    info docxml, div2
    foreword docxml, div2
    introduction docxml, div2
    terms_defs docxml, div2, 0
    div2.p { |p| p << "&nbsp;" } # placeholder
  end
  section_break(body)
end

#metadata_init(lang, script, labels) ⇒ Object



43
44
45
# File 'lib/isodoc/mpfd/word_convert.rb', line 43

def (lang, script, labels)
  @meta = Metadata.new(lang, script, labels)
end

#middle(isoxml, out) ⇒ Object



147
148
149
150
151
152
# File 'lib/isodoc/mpfd/word_convert.rb', line 147

def middle(isoxml, out)
  middle_title(out)
  clause isoxml, out
  annex isoxml, out
  bibliography isoxml, out
end

#ol_depth(node) ⇒ Object



263
264
265
# File 'lib/isodoc/mpfd/word_convert.rb', line 263

def ol_depth(node)
  ol_style(node["type"])
end

#pre_parse(node, out) ⇒ Object



65
66
67
# File 'lib/isodoc/mpfd/word_convert.rb', line 65

def pre_parse(node, out)
  out.pre node.text # content.gsub(/</, "&lt;").gsub(/>/, "&gt;")
end

#sect_names(clause, num, i, lvl, prev_lvl) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/isodoc/mpfd/word_convert.rb', line 184

def sect_names(clause, num, i, lvl, prev_lvl)
  return i if clause.nil?
  curr = i
  if !clause["container"]
    retlvl = lvl
    i+=1
    curr = i
    name = num.nil? ? i.to_s : "#{num}.#{i}"
    @anchors[clause["id"]] = { label: name, xref: l10n("#{@clause_lbl} #{name}"), level: lvl+1 }
  end
  prev = lvl
  j = 0
  clause.xpath(ns("./clause | ./term  | ./terms | "\
                  "./definitions")).each do |c|
    if clause["container"]
      i, lvl = sect_names(c, num, i, lvl, lvl)
    else
      j, prev = sect_names(c, name, j, lvl+1, prev)
    end
  end
  i = j if j >0
  i = curr if lvl < prev
  [i, prev]
end

#termdef_parse(node, out) ⇒ Object



154
155
156
157
# File 'lib/isodoc/mpfd/word_convert.rb', line 154

def termdef_parse(node, out)
  set_termdomain("")
  node.children.each { |n| parse(n, out) }
end

#terms_defs(isoxml, out, num) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/isodoc/mpfd/word_convert.rb', line 112

def terms_defs(isoxml, out, num)
  f = isoxml.at(ns(TERM_CLAUSE)) or return num
  out.div **attr_code(id: f["id"]) do |div|
    clause_name(nil, terms_defs_title(f), div, nil)
    f.elements.each do |e|
      parse(e, div) unless %w{title source}.include? e.name
    end
  end
  num
end

#terms_defs_title(f) ⇒ Object



100
101
102
# File 'lib/isodoc/mpfd/word_convert.rb', line 100

def terms_defs_title(f)
  return f&.at(ns("./title"))&.content
end