Class: IsoDoc::JIS::WordConvert

Inherits:
Iso::WordConvert
  • Object
show all
Includes:
BaseConvert, Init
Defined in:
lib/isodoc/jis/table.rb,
lib/isodoc/jis/figure.rb,
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

#admonition_name_parse, #commentary_title, #commentary_title_hdr, #make_tr_attr, #middle_subtitle_main, #middle_title, #middle_title_hdr, #middle_title_main, #para_class, #termnote_parse

Constructor Details

#initialize(options) ⇒ WordConvert

Returns a new instance of WordConvert.



11
12
13
14
15
# File 'lib/isodoc/jis/word_convert.rb', line 11

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

Instance Method Details

#annex(isoxml, out) ⇒ Object



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

def annex(isoxml, out)
  amd(isoxml) and @suppressheadingnumbers = @oldsuppressheadingnumbers
  isoxml.xpath(ns("//annex[not(@commentary = 'true')]")).each do |c|
    page_break(out)
    render_annex(out, c)
  end
  amd(isoxml) and @suppressheadingnumbers = true
end

#annex_name(_annex, name, div) ⇒ Object



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

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



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

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



19
20
21
22
# File 'lib/isodoc/jis/word_convert.rb', line 19

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

#commentary(isoxml, out) ⇒ Object



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

def commentary(isoxml, out)
  isoxml.xpath(ns("//annex[@commentary = 'true']")).each do |c|
    section_break(out)
    out.div class: "WordSectionCommentary" do |div|
      commentary_title(isoxml, div)
      render_annex(div, c)
    end
  end
end

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



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

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



37
38
39
40
# File 'lib/isodoc/jis/word_convert.rb', line 37

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



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

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



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

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

#figure_attrs(node) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/isodoc/jis/figure.rb', line 4

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



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/isodoc/jis/figure.rb', line 13

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

#figure_name_parse(_node, div, name) ⇒ Object



56
57
58
59
60
61
# File 'lib/isodoc/jis/figure.rb', line 56

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



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

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



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/isodoc/jis/figure.rb', line 35

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

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



63
64
65
66
67
68
69
70
71
72
# File 'lib/isodoc/jis/figure.rb', line 63

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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/isodoc/jis/figure.rb', line 74

def figure_row1(node, cell, hash, key)
  case key
  when :units then units_render(hash[key], cell)
  when :notes_etc, :aside, :key then hash.each { |n| parse(n, cell) }
  when :source then parse(hash[key], 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

#figure_subfigs(node, div, elem) ⇒ Object



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

def figure_subfigs(node, div, elem)
  elem.each do |n|
    n[:subname] = n[:name]
    figure_row(node, div, n, :img)
    figure_row(node, div, n, :subname)
  end
end

#footnote_parse(node, out) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/isodoc/jis/word_convert.rb', line 143

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.parent.xpath("./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 |n|
    n.name = "p"
  end
end

#init_dis(opt) ⇒ Object



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

def init_dis(opt); end

#introduction(clause, out) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/isodoc/jis/word_convert.rb', line 107

def introduction(clause, out)
  out.div class: "Section3", id: clause["id"] do |div|
    clause_name(clause, clause.at(ns("./title")), div,
                { class: "IntroTitle" })
    clause.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



117
118
119
120
121
122
123
124
# File 'lib/isodoc/jis/word_convert.rb', line 117

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

#make_body3(body, docxml) ⇒ Object



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

def make_body3(body, docxml)
  super
  commentary docxml, body
end

#make_table_footnote_target(out, fnid, fnref) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/isodoc/jis/table.rb', line 4

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



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/isodoc/jis/table.rb', line 29

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



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

def middle(isoxml, out)
  middle_title(isoxml, out)
  middle_admonitions(isoxml, out)
  i = isoxml.at(ns("//sections/introduction")) and
    introduction i, out
  scope isoxml, out, 0
  norm_ref isoxml, out, 0
  clause_etc isoxml, out, 0
  annex isoxml, out
  bibliography 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.remove)
  source = docxml.at("//div[@type = 'contributors']")
  dest = docxml.at("//div[@id = 'boilerplate-contributors']")
  source && dest and dest.replace(source.remove)
  docxml
end

#new_fullcolspan_row(table, tfoot) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/isodoc/jis/table.rb', line 43

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



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

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_attrs(node) ⇒ Object



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

def preface_attrs(node)
  { id: node["id"], type: node["type"],
    class: node["type"] == "toc" ? "TOC" : "Section3" }
end

#render_annex(out, clause) ⇒ Object



183
184
185
186
187
188
189
190
191
# File 'lib/isodoc/jis/word_convert.rb', line 183

def render_annex(out, clause)
  out.div **attr_code(annex_attrs(clause)) do |s|
    clause.elements.each do |c1|
      if c1.name == "title" then annex_name(clause, c1, s)
      else parse(c1, s)
      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



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/isodoc/jis/table.rb', line 17

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



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

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

#units_render(note, cell) ⇒ Object



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

def units_render(note, cell)
  para = note.at(ns("./p")) and note = para
  cell.p class: "UnitStatement" do |p|
    note.children.each { |n| parse(n, p) }
  end
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