Class: IsoDoc::ITU::WordConvert

Inherits:
WordConvert
  • Object
show all
Includes:
BaseConvert
Defined in:
lib/isodoc/itu/word_convert.rb

Overview

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

Constant Summary

Constants included from BaseConvert

BaseConvert::FRONT_CLAUSE, BaseConvert::IGNORE_IDS

Instance Method Summary collapse

Methods included from BaseConvert

#add_parse, #annex, #annex_name, #annex_obligation_subtitle, #biblio_list, #bracket_if_num, #bracket_opt, #clausedelim, #cleanup, #del_parse, #doctype_title, #eref_parse, #error_parse, #fileloc, #get_eref_linkend, #i18n_init, #info, #load_yaml, #metadata_init, #middle_title, #multi_bibitem_ref_code, #nonstd_bibitem, #note_label, #note_p_parse, #note_parse1, #ol_depth, #pref_ref_code, #preface, #prefix_container, #reference_format, #reference_format_start, #reference_format_title, #refs_cleanup, #render_multi_identifiers, #std_bibitem_entry, #table_footnote_reference_format, #term_cleanup, #term_def_title, #termdef_parse, #termdef_parse1, #termnote_parse, #terms_defs, #terms_parse, #title_cleanup, #titlecase, #xref_init

Constructor Details

#initialize(options) ⇒ WordConvert



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

def initialize(options)
  @libdir = File.dirname(__FILE__)
  @hierarchical_assets = options[:hierarchical_assets]
  super
end

Instance Method Details

#abstract(isoxml, out) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/isodoc/itu/word_convert.rb', line 38

def abstract(isoxml, out)
  f = isoxml.at(ns("//preface/abstract")) || return
  out.div **attr_code(id: f["id"]) do |s|
    clause_name(nil, "Summary", s, class: "AbstractTitle")
    f.elements.each { |e| parse(e, s) unless e.name == "title" }
  end
end

#authority_cleanup(docxml) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/isodoc/itu/word_convert.rb', line 205

def authority_cleanup(docxml)
  dest = docxml.at("//div[@class = 'draft-warning']")
  auth = docxml.at("//div[@id = 'draft-warning']")
  dest and auth and dest.replace(auth.remove)
  %w(copyright license legal).each do |t|
    dest = docxml.at("//div[@id = 'boilerplate-#{t}-destination']")
    auth = docxml.at("//div[@class = 'boilerplate-#{t}']")
    next unless auth && dest
    t == "copyright" and p = auth&.at(".//p") and
      p["class"] = "boilerplateHdr"
    auth&.xpath(".//p[not(@class)]")&.each_with_index do |p, i|
      p["class"] = "boilerplate"
      i == 0 && t == "copyright" and p["style"] = "text-align:center;"
    end
    auth << "<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>" unless t == "copyright"
    dest.replace(auth.remove)
  end
end

#authority_hdr_cleanup(docxml) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/isodoc/itu/word_convert.rb', line 186

def authority_hdr_cleanup(docxml)
  docxml&.xpath("//div[@id = 'draft-warning']").each do |d|
    d.xpath(".//h1 | .//h2").each do |p|
      p.name = "p"
      p["class"] = "draftwarningHdr"
    end
  end
  %w(copyright license legal).each do |t|
    docxml&.xpath("//div[@class = 'boilerplate-#{t}']").each do |d|
      p = d&.at("./descendant::h1[2]") and
        p.previous = "<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>"
      d.xpath(".//h1 | .//h2").each do |p|
        p.name = "p"
        p["class"] = "boilerplateHdr"
      end
    end
  end
end

#default_file_locations(options) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/isodoc/itu/word_convert.rb', line 142

def default_file_locations(options)
  { wordstylesheet: html_doc_path("wordstyle.scss"),
    standardstylesheet: html_doc_path("itu.scss"),
    header: html_doc_path("header.html"),
    wordcoverpage: html_doc_path("word_itu_titlepage.html"),
    wordintropage: html_doc_path("word_itu_intro.html"),
    ulstyle: "l3",
    olstyle: "l2", }
end

#default_fonts(options) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/isodoc/itu/word_convert.rb', line 134

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

#formula_parse1(node, out) ⇒ Object



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

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)
      lbl = @xrefs.anchor(node['id'], :label, false)
      unless lbl.nil?
        insert_tab(div, 1)
        div << "(#{lbl})"
      end
    end
  end
end

#keywords(_docxml, out) ⇒ Object



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

def keywords(_docxml, out)
  kw = @meta.get[:keywords]
  kw.nil? || kw.empty? and return
  out.div do |div|
    clause_name(nil, "Keywords", div,  class: "IntroTitle")
    div.p kw.join(", ") + "."
  end
end


176
177
178
179
180
181
182
183
184
# File 'lib/isodoc/itu/word_convert.rb', line 176

def link_parse(node, out)
  out.a **attr_code(href: node["target"], title: node["alt"], class: "url") do |l|
    if node.text.empty?
      l << node["target"].sub(/^mailto:/, "")
    else
      node.children.each { |n| parse(n, l) }
    end
  end
end

#make_body(xml, docxml) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/isodoc/itu/word_convert.rb', line 17

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



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

def make_body2(body, docxml)
  body.div **{ class: "WordSection2" } do |div2|
    info docxml, div2 
    boilerplate docxml, div2
    abstract docxml, div2
    keywords docxml, div2
    preface docxml, div2
    div2.p { |p| p << "&nbsp;" } # placeholder
  end
  section_break(body)
end

#make_tr_attr(td, row, totalrows, header) ⇒ Object



152
153
154
# File 'lib/isodoc/itu/word_convert.rb', line 152

def make_tr_attr(td, row, totalrows, header)
  super.merge(valign: "top")
end

#ol_attrs(node) ⇒ Object



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

def ol_attrs(node)
  { class: node["class"], id: node["id"], style: keep_style(node) }
end

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



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

def toWord(result, filename, dir, header)
  result = populate_template(result, :word)
  result = from_xhtml(word_cleanup(to_xhtml(result)))
  unless @landscapestyle.nil? || @landscapestyle.empty?
    @wordstylesheet&.open
    @wordstylesheet&.write(@landscapestyle)
    @wordstylesheet&.close
  end
  Html2Doc.process(result, filename: filename, stylesheet: @wordstylesheet&.path,
                   header_file: header&.path, dir: dir,
                   asciimathdelims: [@openmathdelim, @closemathdelim],
                   liststyles: { ul: @ulstyle, ol: @olstyle, steps: "l4" })
  header&.unlink
  @wordstylesheet&.unlink
end

#word_cleanup(docxml) ⇒ Object



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

def word_cleanup(docxml)
  word_footnote_cleanup(docxml)
  word_title_cleanup(docxml)
  word_preface_cleanup(docxml)
  word_term_cleanup(docxml)
  word_history_cleanup(docxml)
  authority_hdr_cleanup(docxml)
  super
  docxml
end

#word_footnote_cleanup(docxml) ⇒ Object



79
80
81
82
83
84
# File 'lib/isodoc/itu/word_convert.rb', line 79

def word_footnote_cleanup(docxml)
  docxml.xpath("//aside").each do |a|
    a.first_element_child.children.first.previous =
      '<span style="mso-tab-count:1"/>'
  end
end

#word_history_cleanup(docxml) ⇒ Object



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

def word_history_cleanup(docxml)
  docxml.xpath("//div[@id='_history']//table").each do |t|
    t["class"] = "MsoNormalTable"
    t.xpath(".//td").each { |td| td["style"] = nil }
  end
end

#word_preface(docxml) ⇒ Object



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

def word_preface(docxml)
  super
  abstractbox = docxml.at("//div[@id='abstractbox']")
  historybox = docxml.at("//div[@id='historybox']")
  sourcebox = docxml.at("//div[@id='sourcebox']")
  keywordsbox = docxml.at("//div[@id='keywordsbox']")
  abstract = docxml.at("//p[@class = 'h1Preface' and text() = 'Summary']/..")
  history = docxml.at("//p[@class = 'h1Preface' and text() = 'History']/..")
  source = docxml.at("//p[@class = 'h1Preface' and text() = 'Source']/..")
  keywords = docxml.at("//p[@class = 'h1Preface' and text() = 'Keywords']/..")
  abstract.parent = abstractbox if abstract && abstractbox
  history.parent = historybox if history && historybox
  source.parent = sourcebox if source && sourcebox
  keywords.parent = keywordsbox if keywords && keywordsbox
end

#word_preface_cleanup(docxml) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/isodoc/itu/word_convert.rb', line 55

def word_preface_cleanup(docxml)
  docxml.xpath("//h1[@class = 'AbstractTitle'] | "\
               "//h1[@class = 'IntroTitle']").each do |h2|
    h2.name = "p"
    h2["class"] = "h1Preface"
  end
end

#word_term_cleanup(docxml) ⇒ Object



63
64
65
66
# File 'lib/isodoc/itu/word_convert.rb', line 63

def word_term_cleanup(docxml)
  docxml.xpath("//p[@class = 'TermNum']").each do |t|
  end
end

#word_title_cleanup(docxml) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/isodoc/itu/word_convert.rb', line 86

def word_title_cleanup(docxml)
  docxml.xpath("//p[@class = 'annex_obligation']").each do |h|
    h&.next_element&.name == "p" or next
    h.next_element["class"] ||= "Normalaftertitle"
  end
  docxml.xpath("//p[@class = 'FigureTitle']").each do |h|
    h&.parent&.next_element&.name == "p" or next
    h.parent.next_element["class"] ||= "Normalaftertitle"
  end
end