Class: IsoDoc::JIS::WordConvert

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

Constant Summary collapse

STYLESMAP =
{}.freeze

Instance Method Summary collapse

Methods included from Init

#i18n_init, #metadata_init, #omit_docid_prefix, #std_docid_semantic, #xref_init

Methods included from BaseConvert

#admitted_term_parse, #admonition_name_parse, #make_tr_attr, #middle_subtitle_main, #middle_title, #middle_title_main, #para_class, #termnote_parse

Constructor Details

#initialize(options) ⇒ WordConvert

Returns a new instance of WordConvert.



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

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

Instance Method Details

#annex_name(_annex, name, div) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/isodoc/jis/word_convert.rb', line 90

def annex_name(_annex, name, div)
  preceding_floating_titles(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

#biblio_paras(docxml) ⇒ Object



113
114
115
116
117
118
# File 'lib/isodoc/jis/word_cleanup.rb', line 113

def biblio_paras(docxml)
  docxml.xpath("//div[@class = 'normref_div']//" \
               "p[not(@class) or @class = 'MsoNormal']").each do |p|
    p["class"] = "NormRefText"
  end
end

#bibliography(isoxml, out) ⇒ Object



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

def bibliography(isoxml, out)
  (f = isoxml.at(ns(bibliography_xpath)) and f["hidden"] != "true") or
    return
  page_break(out)
  out.div class: "bibliography" do |div|
    div.h1 class: "Section3" do |h1|
      f.at(ns("./title"))&.children&.each { |c2| parse(c2, h1) }
    end
    biblio_list(f, div, true)
  end
end

#boldface(docxml) ⇒ Object



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

def boldface(docxml)
  docxml.xpath("//b").each do |b|
    b.name = "span"
    b["class"] = "Strong"
  end
end

#clause_attrs(node) ⇒ Object



17
18
19
20
# File 'lib/isodoc/jis/word_convert.rb', line 17

def clause_attrs(node)
  # capture the type of clause
  { id: node["id"], type: node["type"] }
end

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



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/isodoc/jis/word_convert.rb', line 22

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

#convert1(docxml, filename, dir) ⇒ Object



35
36
37
38
# File 'lib/isodoc/jis/word_convert.rb', line 35

def convert1(docxml, filename, dir)
  @options.merge!(default_fonts({})) # updated @script
  super
end

#cover_split(xml) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/isodoc/jis/word_cleanup.rb', line 83

def cover_split(xml)
  xml.at("//body").elements.each do |e|
    e.name == "div" && e["class"] == "WordSection1" and next
    e.remove
  end
  xml
end

#default_file_locations(_options) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/isodoc/jis/word_convert.rb', line 50

def default_file_locations(_options)
  @libdir = File.dirname(__FILE__)
  { htmlstylesheet: html_doc_path("htmlstyle.scss"),
    htmlcoverpage: html_doc_path("html_jis_titlepage.html"),
    htmlintropage: html_doc_path("html_jis_intro.html"),
    wordstylesheet: html_doc_path("wordstyle.scss"),
    standardstylesheet: html_doc_path("isodoc.scss"),
    header: html_doc_path("header.html"),
    wordcoverpage: html_doc_path("word_jis_titlepage.html"),
    wordintropage: html_doc_path("word_jis_intro.html"),
    ulstyle: "l9",
    olstyle: "l8" }
end

#default_fonts(_options) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/isodoc/jis/word_convert.rb', line 40

def default_fonts(_options)
  { bodyfont: (@script == "Jpan" ? '"MS Mincho",serif' : '"Times New Roman",serif'),
    headerfont: (@script == "Jpan" ? '"MS Gothic",sans-serif' : '"Arial",sans-serif'),
    monospacefont: '"Courier New",monospace',
    normalfontsize: "10.0pt",
    monospacefontsize: "9.0pt",
    smallerfontsize: "10.0pt",
    footnotefontsize: "10.0pt" }
end

#fig_para(klass, row, nodes) ⇒ Object



219
220
221
222
223
224
225
# File 'lib/isodoc/jis/word_convert.rb', line 219

def fig_para(klass, row, nodes)
  row.td valign: "top", style: "padding:0cm 5.4pt 0cm 5.4pt" do |d|
    d.p class: klass do |p|
      nodes.each { |n| parse(n, p) }
    end
  end
end

#figure_attrs(node) ⇒ Object



150
151
152
153
154
155
156
157
# File 'lib/isodoc/jis/word_convert.rb', line 150

def figure_attrs(node)
  attr_code(id: node["id"], class: "MsoTableGrid",
            style: "border-collapse:collapse;" \
                   "border:none;mso-padding-alt: " \
                   "0cm 5.4pt 0cm 5.4pt;mso-border-insideh:none;" \
                   "mso-border-insidev:none;#{keep_style(node)}",
            border: 0, cellspacing: 0, cellpadding: 0)
end

#figure_components(node) ⇒ Object



159
160
161
162
163
164
165
166
167
168
# File 'lib/isodoc/jis/word_convert.rb', line 159

def figure_components(node)
  { units: node.at(ns("./note[@type = 'units']/p")),
    notes_etc: figure_notes_examples_paras(node
    .xpath(ns("./note[not(@type = 'units')] | ./example | ./p"))),
    name: node.at(ns("./name")),
    key: node.at(ns("./dl")),
    img: node.at(ns("./image")),
    aside: node.at(ns("./aside")),
    subfigs: node.xpath(ns("./figure")).map { |n| figure_components(n) } }
end

#figure_name_parse(_node, div, name) ⇒ Object



201
202
203
204
205
206
# File 'lib/isodoc/jis/word_convert.rb', line 201

def figure_name_parse(_node, div, name)
  name.nil? and return
  div.p class: "Tabletitle", style: "text-align:center;" do |p|
    name.children.each { |n| parse(n, p) }
  end
end

#figure_notes_examples_paras(xpath) ⇒ Object



170
171
172
173
174
175
176
177
178
# File 'lib/isodoc/jis/word_convert.rb', line 170

def figure_notes_examples_paras(xpath)
  xpath.empty? and return nil
  curr = ""
  xpath.each_with_object([]) do |e, m|
    e.name == curr or m << []
    curr = e.name
    m[-1] << e
  end
end

#figure_parse1(node, out) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/isodoc/jis/word_convert.rb', line 180

def figure_parse1(node, out)
  c = figure_components(node)
  out.table **figure_attrs(node) do |div|
    %i(units img subfigs key notes_etc aside name).each do |key|
      case key
      when :subfigs
        c[key].each do |n|
          n[:subname] = n[:name]
          figure_row(node, div, n, :img)
          figure_row(node, div, n, :subname)
        end
      when :notes_etc
        c[key].each do |n|
          figure_row(node, div, n, :notes_etc)
        end
      else figure_row(node, div, c, key)
      end
    end
  end
end

#figure_row(node, table, hash, key) ⇒ Object



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

def figure_row(node, table, hash, key)
  key != :notes_etc && (
  hash[key].nil? || (hash[key].is_a?(Array) && hash[key].empty?)) and
    return
  table.tr do |r|
    r.td valign: "top", style: "padding:0cm 5.4pt 0cm 5.4pt" do |d|
      figure_row1(node, d, hash, key)
    end
  end
end

#figure_row1(node, cell, hash, key) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/isodoc/jis/word_convert.rb', line 227

def figure_row1(node, cell, hash, key)
  case key
  when :units
    cell.p class: "UnitStatement" do |p|
      hash[key].children.each { |n| parse(n, p) }
    end
  when :key
    figure_key(cell)
    parse(hash[key], cell)
  when :notes_etc, :aside
    hash.each { |n| parse(n, cell) }
  when :name then figure_name_parse(node, cell, hash[key])
  when :img
    cell.p class: "Figure" do |p|
      parse(hash[key], p)
    end
  when :subname
    cell.p class: "SubfigureCaption" do |p|
      hash[key].children.each { |n| parse(n, p) }
    end
  end
end

#footnote_parse(node, out) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/isodoc/jis/word_convert.rb', line 250

def footnote_parse(node, out)
  return table_footnote_parse(node, out) if @in_table || @in_figure # &&

  # !node.ancestors.map(&:name).include?("name")

  fn = node["reference"] || UUIDTools::UUID.random_create.to_s
  return seen_footnote_parse(node, out, fn) if @seen_footnote.include?(fn)

  @fn_bookmarks[fn] = bookmarkid
  out.span style: "mso-bookmark:_Ref#{@fn_bookmarks[fn]}" do |s|
    s.a class: "FootnoteRef", "epub:type": "footnote",
        href: "#ftn#{fn}" do |a|
      a.sup { |sup| sup << fn }
    end
  end
  @in_footnote = true
  @footnotes << make_generic_footnote_text(node, fn)
  @in_footnote = false
  @seen_footnote << fn
end

#heading_to_para(docxml) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/isodoc/jis/word_cleanup.rb', line 120

def heading_to_para(docxml)
  docxml.xpath("//h1[@class = 'ForewordTitle']").each do |p|
    p.name = "p"
    p.xpath("../div/p[not(@class) or @class = 'MsoNormal']").each do |n|
      n["class"] = "ForewordText"
    end
  end
  docxml.xpath("//h1[@class = 'IntroTitle'] | //h1[@class = 'Annex'] | " \
               "//h2[@class = 'Terms'] | " \
               "//h3[@class = 'Terms'] | //h4[@class = 'Terms'] | " \
               "//h5[@class = 'Terms'] | //h6[@class = 'Terms']").each do |p|
    p.name = "p"
  end
end

#init_dis(opt) ⇒ Object



15
# File 'lib/isodoc/jis/word_convert.rb', line 15

def init_dis(opt); end

#introduction(isoxml, out) ⇒ Object



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

def introduction(isoxml, out)
  f = isoxml.at(ns("//introduction")) || return
  out.div class: "Section3", id: f["id"] do |div|
    clause_name(f, f.at(ns("./title")), div, { class: "IntroTitle" })
    f.elements.each do |e|
      parse(e, div) unless e.name == "title"
    end
  end
end

#main_split(xml) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/isodoc/jis/word_cleanup.rb', line 91

def main_split(xml)
  c = xml.at("//div[@class = 'WordSection1']")
  c.next_element&.remove
  c.remove
  c = xml.at("//div[@class = 'WordSection2']")
  c.elements.first.at("./br") and c.elements.first.remove
  xml
end

#make_body2(body, docxml) ⇒ Object



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

def make_body2(body, docxml)
  body.div class: "WordSection2" do |div2|
    boilerplate docxml, div2
    preface_block docxml, div2
    abstract docxml, div2
    foreword docxml, div2
    preface docxml, div2
    acknowledgements docxml, div2
    div2.p { |p| p << "&#xa0;" } # placeholder
  end
  section_break(body)
end

#make_table_footnote_target(out, fnid, fnref) ⇒ Object



271
272
273
274
275
276
277
278
279
280
# File 'lib/isodoc/jis/word_convert.rb', line 271

def make_table_footnote_target(out, fnid, fnref)
  attrs = { id: fnid, class: "TableFootnoteRef" }
  out.span do |s|
    s << @i18n.table_footnote
    out.span **attrs do |a|
      a << "#{fnref})"
    end
    insert_tab(s, 1)
  end
end

#make_tr_attr_style(cell, row, rowmax, totalrows, opt) ⇒ Object



296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/isodoc/jis/word_convert.rb', line 296

def make_tr_attr_style(cell, row, rowmax, totalrows, opt)
  top = row.zero? ? "#{SW1} 1.5pt;" : "none;"
  bottom = "#{SW1} #{rowmax >= totalrows ? '1.5' : '1.0'}pt;"
  ret = <<~STYLE.gsub(/\n/, "")
    border-top:#{top}mso-border-top-alt:#{top}
    border-left:#{bottom}mso-border-top-alt:#{bottom}
    border-right:#{bottom}mso-border-top-alt:#{bottom}
    border-bottom:#{bottom}mso-border-bottom-alt:#{bottom}
  STYLE
  opt[:bordered] or ret = ""
  pb = keep_rows_together(cell, rowmax, totalrows, opt) ? "avoid" : "auto"
  "#{ret}page-break-after:#{pb};"
end

#middle(isoxml, out) ⇒ Object



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

def middle(isoxml, out)
  middle_title(isoxml, out)
  middle_admonitions(isoxml, out)
  introduction isoxml, out
  scope isoxml, out, 0
  norm_ref isoxml, out, 0
  terms_defs isoxml, out, 0
  symbols_abbrevs isoxml, out, 0
  clause isoxml, out
  annex isoxml, out
  bibliography isoxml, out
  # colophon isoxml, out
end

#move_to_inner_cover(docxml) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/isodoc/jis/word_cleanup.rb', line 21

def move_to_inner_cover(docxml)
  source = docxml.at("//div[@type = 'inner-cover-note']")
  dest = docxml.at("//div[@id = 'boilerplate-inner-cover-note']")
  source && dest and dest.replace(source)
  source = docxml.at("//div[@type = 'contributors']")
  dest = docxml.at("//div[@id = 'boilerplate-contributors']")
  source && dest and dest.replace(source)
  docxml
end

#new_fullcolspan_row(table, tfoot) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/isodoc/jis/word_convert.rb', line 310

def new_fullcolspan_row(table, tfoot)
  # how many columns in the table?
  cols = 0
  table.at(".//tr").xpath("./td | ./th").each do |td|
    cols += (td["colspan"] ? td["colspan"].to_i : 1)
  end
  style = "border-top:0pt;mso-border-top-alt:0pt;" \
          "border-bottom:#{SW1} 1.5pt;mso-border-bottom-alt:#{SW1} 1.5pt;" \
          "border-left:#{SW1} 1.5pt;mso-border-left-alt:#{SW1} 1.5pt;" \
          "border-right:#{SW1} 1.5pt;mso-border-right-alt:#{SW1} 1.5pt;"
  tfoot.add_child("<tr><td colspan='#{cols}' style='#{style}'/></tr>")
  tfoot.xpath(".//td").last
end

#new_styles(docxml) ⇒ Object



107
108
109
110
111
# File 'lib/isodoc/jis/word_cleanup.rb', line 107

def new_styles(docxml)
  super
  biblio_paras(docxml)
  heading_to_para(docxml)
end

#norm_ref(isoxml, out, num) ⇒ Object



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

def norm_ref(isoxml, out, num)
  (f = isoxml.at(ns(norm_ref_xpath)) and f["hidden"] != "true") or
    return num
  out.div class: "normref_div" do |div|
    num += 1
    clause_name(f, f.at(ns("./title")), div, nil)
    if f.name == "clause"
      f.elements.each { |e| parse(e, div) unless e.name == "title" }
    else biblio_list(f, div, false)
    end
  end
  num
end

#postprocess(result, filename, dir) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/isodoc/jis/word_cleanup.rb', line 6

def postprocess(result, filename, dir)
  filename = filename.sub(/\.doc$/, "")
  header = generate_header(filename, dir)
  result = from_xhtml(cleanup(to_xhtml(textcleanup(result))))
  toWord(result, filename, dir, header)
  @files_to_delete.each { |f| FileUtils.rm_f f }
end

#preface(isoxml, out) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/isodoc/jis/word_convert.rb', line 100

def preface(isoxml, out)
  isoxml.xpath(ns("//preface/clause | //preface/references | " \
                  "//preface/definitions | //preface/terms")).each do |f|
    out.div **attr_code(class: "Section3", id: f["id"],
                        type: f["type"]) do |div|
      clause_name(f, f&.at(ns("./title")), div, { class: "IntroTitle" })
      f.elements.each do |e|
        parse(e, div) unless e.name == "title"
      end
    end
  end
end

#style_cleanup(docxml) ⇒ Object



102
103
104
105
# File 'lib/isodoc/jis/word_cleanup.rb', line 102

def style_cleanup(docxml)
  new_styles(docxml)
  index_cleanup(docxml)
end

#table_attrs(node) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
# File 'lib/isodoc/jis/word_convert.rb', line 284

def table_attrs(node)
  { id: node["id"], title: node["alt"],
    summary: node["summary"], width: node["width"],
    class: (node.text.length > 4000 ? "MsoTableGridBig" : "MsoTableGrid"),
    style: "border-collapse:collapse;" \
           "mso-table-anchor-horizontal:column;mso-table-overlap:never;" \
           "border:none;mso-padding-alt: " \
           "0cm 5.4pt 0cm 5.4pt;mso-border-insideh:none;" \
           "mso-border-insidev:none;#{keep_style(node)}",
    border: 0, cellspacing: 0, cellpadding: 0 }
end

#table_title_parse(node, out) ⇒ Object



282
# File 'lib/isodoc/jis/word_convert.rb', line 282

def table_title_parse(node, out); end

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



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

def to_word1(result, filename, dir, header)
  result or return
  result = from_xhtml(result).gsub(/-DOUBLE_HYPHEN_ESCAPE-/, "--")
  ::Html2Doc::JIS.new(
    filename: filename, imagedir: @localdir,
    stylesheet: @wordstylesheet&.path,
    header_file: header&.path, dir: dir,
    asciimathdelims: [@openmathdelim, @closemathdelim],
    liststyles: { ul: @ulstyle, ol: @olstyle }
  ).process(result)
end

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



56
57
58
59
60
61
62
63
64
# File 'lib/isodoc/jis/word_cleanup.rb', line 56

def toWord(result, filename, dir, header)
  result = word_split(word_cleanup(to_xhtml(result)))
  @wordstylesheet = wordstylesheet_update
  result.each do |k, v|
    to_word1(v, "#{filename}#{k}", dir, header)
  end
  header&.unlink
  @wordstylesheet.unlink if @wordstylesheet.is_a?(Tempfile)
end

#word_annex_cleanup1(docxml, lvl) ⇒ Object



135
136
137
138
139
140
# File 'lib/isodoc/jis/word_cleanup.rb', line 135

def word_annex_cleanup1(docxml, lvl)
  docxml.xpath("//h#{lvl}[ancestor::*[@class = 'Section3']]").each do |h2|
    h2.name = "p"
    h2["class"] = ".h#{lvl}Annex"
  end
end

#word_cleanup(docxml) ⇒ Object



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

def word_cleanup(docxml)
  word_note_cleanup(docxml)
  boldface(docxml)
  super
  move_to_inner_cover(docxml)
end

#word_intro(docxml, level) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/isodoc/jis/word_cleanup.rb', line 31

def word_intro(docxml, level)
  intro = insert_toc(File.read(@wordintropage, encoding: "UTF-8"),
                     docxml, level)
  intro = populate_template(intro, :word)
  introxml = to_word_xhtml_fragment(intro)
  docxml.at('//div[@class="WordSection2"]') << introxml
    .to_xml(encoding: "US-ASCII")
end

#word_note_cleanup(docxml) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/isodoc/jis/word_cleanup.rb', line 40

def word_note_cleanup(docxml)
  docxml.xpath("//p[@class = 'Note']").each do |p|
    p.xpath("//following-sibling::p").each do |p2|
      p2["class"] == "Note" and
        p2["class"] = "NoteCont"
    end
  end
end

#word_split(xml) ⇒ Object



78
79
80
81
# File 'lib/isodoc/jis/word_cleanup.rb', line 78

def word_split(xml)
  b = xml.dup
  { _cover: cover_split(xml), "": main_split(b) }
end