Class: IsoDoc::ITU::WordConvert

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

Constant Summary collapse

TOPLIST =
"[not(ancestor::ul) and not(ancestor::ol)]".freeze

Constants included from BaseConvert

BaseConvert::FRONT_CLAUSE

Instance Method Summary collapse

Methods included from Init

#bibrenderer, #fileloc, #i18n_init, #metadata_init, #xref_init

Methods included from BaseConvert

#acknowledgements, #annex, #annex_name, #annex_obligation_subtitle, #biblio_list, #bracket_if_num, #bracket_opt, #clause, #clausedelim, #cleanup, #dl1, #dl2tbody, #dl_parse, #foreword, #info, #introduction, #nonstd_bibitem, #note_delim, #note_p_parse, #note_parse, #note_parse1, #ol_depth, #para_class, #pref_ref_code, #preface_normal, #refs_cleanup, #std_bibitem_entry, #table_footnote_reference_format, #term_cleanup, #term_cleanup1, #term_cleanup2, #termdef_parse, #termdef_parse1, #termnote_delim, #title_cleanup, #unbracket

Constructor Details

#initialize(options) ⇒ WordConvert

Returns a new instance of WordConvert.



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

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

Instance Method Details

#abstract(clause, out) ⇒ Object



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

def abstract(clause, out)
  out.div **attr_code(id: clause["id"], class: "Abstract") do |s|
    @doctype == "contribution" or
      clause_name(clause, "Summary", s, class: "AbstractTitle")
    clause.elements.each { |e| parse(e, s) unless e.name == "title" }
  end
end

#authority_cleanup(docxml) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/isodoc/itu/word_cleanup.rb', line 126

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}']")
    auth.remove if auth && !dest
    next unless auth && dest

    t == "copyright" and para = auth&.at(".//p") and
      para["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
    t == "copyright" or
      auth << "<p>&#xa0;</p><p>&#xa0;</p><p>&#xa0;</p>"
    dest.replace(auth.remove)
  end
  coverpage_note_cleanup(docxml)
end

#authority_hdr_cleanup(docxml) ⇒ Object



99
100
101
102
# File 'lib/isodoc/itu/word_cleanup.rb', line 99

def authority_hdr_cleanup(docxml)
  authority_hdr_cleanup1(docxml)
  authority_hdr_cleanup2(docxml)
end

#authority_hdr_cleanup1(docxml) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/isodoc/itu/word_cleanup.rb', line 104

def authority_hdr_cleanup1(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
end

#authority_hdr_cleanup2(docxml) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/isodoc/itu/word_cleanup.rb', line 113

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

#clause_attrs(node) ⇒ Object



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

def clause_attrs(node)
  ret = {}
  %w(source history).include?(node["type"]) and
    ret = { class: node["type"] }
  super.merge(ret)
end

#convert1(docxml, filename, dir) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/isodoc/itu/word_convert.rb', line 59

def convert1(docxml, filename, dir)
  case @doctype
  when "service-publication"
    @wordcoverpage = html_doc_path("word_itu_titlepage_sp.html")
    options[:bodyfont] = "Arial"
    options[:headerfont] = "Arial"
  when "contribution"
    @wordcoverpage = nil
    @wordintropage = nil
  end
  super
end

#default_file_locations(_options) ⇒ Object



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

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/isodoc/itu/word_convert.rb', line 72

def default_fonts(options)
  { bodyfont: (if options[:script] == "Hans"
                 '"Source Han Sans",serif'
               else
                 '"Times New Roman",serif'
               end),
    headerfont: (if options[:script] == "Hans"
                   '"Source Han Sans",sans-serif'
                 else
                   '"Times New Roman",serif'
                 end),
    monospacefont: '"Courier New",monospace',
    normalfontsize: "12.0pt",
    footnotefontsize: "11.0pt",
    smallerfontsize: "11.0pt",
    monospacefontsize: "10.0pt" }
end

#formula_parse1(node, out) ⇒ Object



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

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)
      if lbl = node&.at(ns("./name"))&.text
        insert_tab(div, 1)
        div << "(#{lbl})"
      end
    end
  end
end

#indent_list(list, num) ⇒ Object



170
171
172
173
# File 'lib/isodoc/itu/word_cleanup.rb', line 170

def indent_list(list, num)
  list["style"] = (list["style"] ? "#{list['style']};" : "")
  list["style"] += "margin-left: #{num * 0.5}cm;text-indent: -0.5cm;"
end


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

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



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

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_body1(body, _docxml) ⇒ Object



24
25
26
27
# File 'lib/isodoc/itu/word_convert.rb', line 24

def make_body1(body, _docxml)
  @wordcoverpage or return
  super
end

#make_body2(body, docxml) ⇒ Object



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

def make_body2(body, docxml)
  body.div class: "WordSection2" do |div2|
    boilerplate docxml, div2
    content(div2, docxml, ns("//preface/*[@displayorder]"))
    div2.p { |p| p << "&#xa0;" } # placeholder
  end
  @doctype == "contribution" or section_break(body)
end

#make_tr_attr(tcell, row, totalrows, header, bordered) ⇒ Object



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

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

#ol_attrs(node) ⇒ Object



104
105
106
# File 'lib/isodoc/itu/word_convert.rb', line 104

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

#postprocess_cleanup(result) ⇒ Object



82
83
84
85
86
87
# File 'lib/isodoc/itu/word_cleanup.rb', line 82

def postprocess_cleanup(result)
  result = from_xhtml(cleanup(to_xhtml(textcleanup(result))))
  result = populate_template(result, :word)
  result = from_xhtml(word_cleanup(to_xhtml(result)))
    .gsub(/-DOUBLE_HYPHEN_ESCAPE-/, "--")
end

#table_list_style(xml) ⇒ Object



151
152
153
154
155
# File 'lib/isodoc/itu/word_cleanup.rb', line 151

def table_list_style(xml)
  xml.xpath("//table//ul#{TOPLIST} | //table//ol#{TOPLIST}").each do |t|
    table_list_style1(t, 1)
  end
end

#table_list_style1(tab, num) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/isodoc/itu/word_cleanup.rb', line 157

def table_list_style1(tab, num)
  (tab.xpath(".//li") - tab.xpath(".//ol//li | .//ul//li")).each do |t1|
    indent_list(t1, num)
    t1.xpath("./div | ./p").each { |p| indent_list(p, num) }
    (t1.xpath(".//ul") - t1.xpath(".//ul//ul | .//ol//ul")).each do |t2|
      table_list_style1(t2, num + 1)
    end
    (t1.xpath(".//ol") - t1.xpath(".//ul//ol | .//ol//ol")).each do |t2|
      table_list_style1(t2, num + 1)
    end
  end
end

#table_of_contents(clause, out) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/isodoc/itu/word_convert.rb', line 126

def table_of_contents(clause, out)
  page_break(out)
  out.div **attr_code(preface_attrs(clause)) do |div|
    div.p class: "zzContents" do |p|
      clause.at(ns("./title"))&.children&.each do |c|
        parse(c, p)
      end
    end
    div.p style: "tab-stops:right 17.0cm" do |p|
      insert_tab(p, 1)
      p << "<b>#{@i18n.page}</b>"
    end
    clause.elements.each do |e|
      parse(e, div) unless e.name == "title"
    end
  end
end

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



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/isodoc/itu/word_cleanup.rb', line 70

def toWord(result, filename, dir, header)
  Html2Doc.new(
    filename: filename, imagedir: @localdir,
    stylesheet: @wordstylesheet&.path,
    header_file: header&.path, dir: dir,
    asciimathdelims: [@openmathdelim, @closemathdelim],
    liststyles: { ul: @ulstyle, ol: @olstyle, steps: "l4" }
  ).process(result)
  header&.unlink
  @wordstylesheet&.unlink if @wordstylesheet.is_a?(Tempfile)
end

#word_cleanup(docxml) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/isodoc/itu/word_cleanup.rb', line 14

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)
  table_list_style(docxml)
  super
  docxml
end

#word_footnote_cleanup(docxml) ⇒ Object



26
27
28
29
30
31
# File 'lib/isodoc/itu/word_cleanup.rb', line 26

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



44
45
46
47
48
49
# File 'lib/isodoc/itu/word_cleanup.rb', line 44

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/isodoc/itu/word_cleanup.rb', line 51

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']")
  changelogbox = docxml.at("//div[@id='change_logbox']")
  abstract = docxml.at("//div[@class = 'Abstract']")
  history = docxml.at("//div[@class = 'history']")
  source = docxml.at("//div[@class = 'source']")
  keywords = docxml.at("//div[@class = 'Keyword']")
  changelog = docxml.at("//div[@id = 'change_log']")
  abstract.parent = abstractbox if abstract && abstractbox
  history.parent = historybox if history && historybox
  source.parent = sourcebox if source && sourcebox
  keywords.parent = keywordsbox if keywords && keywordsbox
  changelog.parent = changelogbox if changelog && changelogbox
end

#word_preface_cleanup(docxml) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/isodoc/itu/word_cleanup.rb', line 4

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



12
# File 'lib/isodoc/itu/word_cleanup.rb', line 12

def word_term_cleanup(docxml); end

#word_title_cleanup(docxml) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/isodoc/itu/word_cleanup.rb', line 33

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

#wordstylesheet_updateObject



89
90
91
92
93
94
95
96
97
# File 'lib/isodoc/itu/word_cleanup.rb', line 89

def wordstylesheet_update
  super
  unless @landscapestyle.nil? || @landscapestyle.empty?
    @wordstylesheet&.open
    @wordstylesheet&.write(@landscapestyle)
    @wordstylesheet&.close
  end
  @wordstylesheet
end