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
FRONT_CLAUSE =
"//*[parent::preface]".freeze

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ WordConvert

Returns a new instance of WordConvert.



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

def initialize(options)
  super
  @wordstylesheet = generate_css(rsd_html_path("wordstyle.scss"), false, default_fonts(options))
  @standardstylesheet = generate_css(rsd_html_path("rsd.scss"), false, default_fonts(options))
  @header = rsd_html_path("header.html")
  @wordcoverpage = rsd_html_path("word_rsd_titlepage.html")
  @wordintropage = rsd_html_path("word_rsd_intro.html")
  @ulstyle = "l3"
  @olstyle = "l2"
  system "cp #{rsd_html_path('logo.jpg')} logo.jpg"
  system "cp #{rsd_html_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



96
97
98
99
100
101
# File 'lib/isodoc/mpfd/word_convert.rb', line 96

def annex_name(annex, name, div)
  div.h1 **{ class: "Annex" } do |t|
    t << "#{get_anchors[annex['id']][:label]} "
    t << "<b>#{name.text}</b>"
  end
end

#annex_name_lbl(clause, num) ⇒ Object



103
104
105
106
107
# File 'lib/isodoc/mpfd/word_convert.rb', line 103

def annex_name_lbl(clause, num)
  obl = l10n("(#{@inform_annex_lbl})")
  obl = l10n("(#{@norm_annex_lbl})") if clause["obligation"] == "normative"
  l10n("<b>#{@annex_lbl} #{num}</b> #{obl}")
end

#annex_names(clause, num) ⇒ Object



297
298
299
300
301
302
303
304
305
# File 'lib/isodoc/mpfd/word_convert.rb', line 297

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|
    i = annex_naming(c, num, 1, i)
  end
  hierarchical_asset_names(clause, num)
end

#annex_names1(clause, num, level) ⇒ Object



307
308
309
310
311
312
313
314
# File 'lib/isodoc/mpfd/word_convert.rb', line 307

def annex_names1(clause, num, level)
  @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



288
289
290
291
292
293
294
295
# File 'lib/isodoc/mpfd/word_convert.rb', line 288

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

#clause(isoxml, out) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
# File 'lib/isodoc/mpfd/word_convert.rb', line 316

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



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

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



328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/isodoc/mpfd/word_convert.rb', line 328

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
      c1&.children&.each { |c2| parse(c2, h) }
    end
  end
end

#container_names(clause, lvl) ⇒ Object



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

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_fonts(options) ⇒ Object



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

def default_fonts(options)
  b = options[:bodyfont] ||
    (options[:script] == "Hans" ? '"SimSun",serif' :
     '"Arial",sans-serif')
  h = options[:headerfont] ||
    (options[:script] == "Hans" ? '"SimHei",sans-serif' :
     '"Arial",sans-serif')
  m = options[:monospacefont] || '"Courier New",monospace'
  "$bodyfont: #{b};\n$headerfont: #{h};\n$monospacefont: #{m};\n"
end

#error_parse(node, out) ⇒ Object



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

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



138
139
140
# File 'lib/isodoc/mpfd/word_convert.rb', line 138

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

#generate_header(filename, dir) ⇒ Object



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

def generate_header(filename, dir)
  return unless @header
  template = Liquid::Template.parse(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") { |f| f.write(template.render(params)) }
  @files_to_delete << "header.html"
  "header.html"
end

#header_strip(h) ⇒ Object



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

def header_strip(h)
  h = h.to_s.gsub(%r{<br/>}, " ").sub(/<\/?h[12][^>]*>/, "")
  h1 = to_xhtml_fragment(h.dup)
  h1.traverse do |x|
    x.replace(" ") if x.name == "span" &&
      /mso-tab-count/.match(x["style"])
    x.remove if x.name == "span" && x["class"] == "MsoCommentReference"
    x.remove if x.name == "a" && x["epub:type"] == "footnote"
    x.replace(x.children) if x.name == "a"
  end
  from_xhtml(h1)
end

#i18n_init(lang, script) ⇒ Object



121
122
123
124
# File 'lib/isodoc/mpfd/word_convert.rb', line 121

def i18n_init(lang, script)
  super
  @annex_lbl = "Appendix"
end

#info(isoxml, out) ⇒ Object



91
92
93
94
# File 'lib/isodoc/mpfd/word_convert.rb', line 91

def info(isoxml, out)
  @meta.security isoxml, out
  super
end

#initial_anchor_names(d) ⇒ Object



193
194
195
196
197
198
199
200
201
202
# File 'lib/isodoc/mpfd/word_convert.rb', line 193

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



44
45
46
47
48
49
50
51
# File 'lib/isodoc/mpfd/word_convert.rb', line 44

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



53
54
55
56
57
58
59
60
# File 'lib/isodoc/mpfd/word_convert.rb', line 53

def make_body2(body, docxml)
  body.div **{ class: "WordSection2" } do |div2|
    info docxml, div2
    div2.p { |p| p << "&nbsp;" } # placeholder
  end
  #body.br **{ clear: "all", style: "page-break-before:auto;mso-break-type:section-break;" }
  section_break(body)
end

#metadata_init(lang, script, labels) ⇒ Object



40
41
42
# File 'lib/isodoc/mpfd/word_convert.rb', line 40

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

#middle(isoxml, out) ⇒ Object



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

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

#ol_depth(node) ⇒ Object



341
342
343
# File 'lib/isodoc/mpfd/word_convert.rb', line 341

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

#pre_parse(node, out) ⇒ Object



109
110
111
# File 'lib/isodoc/mpfd/word_convert.rb', line 109

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

#preface(isoxml, out) ⇒ Object

FRONT_CLAUSE = “//clause | //terms”.freeze



178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/isodoc/mpfd/word_convert.rb', line 178

def preface(isoxml, out)
  isoxml.xpath(ns(FRONT_CLAUSE)).each do |c|
    if c.name == "terms" then  terms_defs isoxml, out, 0
    else
      out.div **attr_code(id: c["id"]) do |s|
        clause_name(get_anchors[c['id']][:label],
                    c&.at(ns("./title"))&.content, s, nil)
        c.elements.reject { |c1| c1.name == "title" }.each do |c1|
          parse(c1, s)
        end
      end
    end
  end
end

#rsd_html_path(file) ⇒ Object



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

def rsd_html_path(file)
  File.join(File.dirname(__FILE__), File.join("html", file))
end

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



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/isodoc/mpfd/word_convert.rb', line 261

def sect_names(clause, num, i, lvl, prev_lvl)
  return i if clause.nil?
  curr = i
  if clause["container"]
    retlvl = lvl+1
  else
    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

#term_defs_boilerplate(div, source, term, preface) ⇒ Object



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

def term_defs_boilerplate(div, source, term, preface)
  if source.empty? && term.nil?
    div << @no_terms_boilerplate
  else
    div << term_defs_boilerplate_cont(source, term)
  end
end

#termdef_parse(node, out) ⇒ Object



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

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

#terms_defs(isoxml, out, num) ⇒ Object



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

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



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

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

#title(isoxml, _out) ⇒ Object



62
63
64
65
# File 'lib/isodoc/mpfd/word_convert.rb', line 62

def title(isoxml, _out)
  main = isoxml&.at(ns("//title[@language='en']"))&.text
  (:doctitle, main)
end