Class: IsoDoc::Ieee::WordConvert

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

Direct Known Subclasses

WordWPConvert

Constant Summary collapse

MAIN_ELEMENTS =
"//sections/*[@displayorder][not(@class = 'zzSTDTitle1')] | " \
"//annex[@displayorder] | " \
"//bibliography/*[@displayorder] | //colophon/*[@displayorder] | " \
"//indexsect[@displayorder]".freeze
SECTIONBREAK =
"<span lang=\"EN-US\" style='font-size:9.0pt;mso-bidi-font-size:10.0pt;font-family:\n\"Times New Roman\",serif;mso-fareast-font-family:\"Times New Roman\";mso-ansi-language:\nEN-US;mso-fareast-language:JA;mso-bidi-language:AR-SA'><br clear=\"all\"\nstyle='page-break-before:auto;mso-break-type:section-break'/></span>\n".freeze

Instance Method Summary collapse

Methods included from Init

#bibrenderer, #fileloc, #i18n_init, #metadata_init, #std_docid_sdo, #std_docid_semantic, #std_docid_semantic1, #std_docid_semantic_full, #xref_init

Methods included from BaseConvert

#clause_attrs, #example_label, #middle_clause, #para_attrs, #scope, #top_element_render

Constructor Details

#initialize(options) ⇒ WordConvert

Returns a new instance of WordConvert.



12
13
14
15
16
# File 'lib/isodoc/ieee/word_convert.rb', line 12

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

Instance Method Details

#abstract(clause, out) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/isodoc/ieee/word_convert.rb', line 60

def abstract(clause, out)
  page_break(out)
  out.div **attr_code(id: clause["id"], class: "abstract") do |s|
    clause_name(clause, clause.at(ns("./fmt-title")), s,
                { class: stylesmap[:AbstractTitle] })
    clause.elements.each { |e| parse(e, s) unless e.name == "fmt-title" }
  end
end

#abstract_cleanup(docxml) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/isodoc/ieee/word_cleanup.rb', line 151

def abstract_cleanup(docxml)
  dest = docxml.at("div[@id = 'abstract-destination']") or return
  if f = docxml.at("//div[@class = 'abstract']")
    f.previous_element.remove
    abstract_cleanup1(f.remove, dest)
    abstract_header(dest)
  elsif f = docxml.at("//div[@type = 'scope']")
    abstract_cleanup1(f, dest)
    abstract_header(dest)
  end
end

#abstract_cleanup1(source, dest) ⇒ Object



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

def abstract_cleanup1(source, dest)
  source.elements.reject { |e| %w(h1 h2).include?(e.name) }.each do |e|
    e1 = e.dup
    e1.xpath("self::p | .//p").each do |p|
      p["class"] = stylesmap[:abstract]
      p["style"] ||= ""
      p["style"] = "font-family: 'Arial', sans-serif;#{p['style']}"
    end
    dest and dest << e1
  end
end

#abstract_header(dest) ⇒ Object



175
176
177
178
179
# File 'lib/isodoc/ieee/word_cleanup.rb', line 175

def abstract_header(dest)
  dest.elements.first.add_first_child "    <span class='IEEEStdsAbstractHeader'><span lang='EN-US'>Abstract:</span></span>\n  XML\nend\n"

#admonition_class(node) ⇒ Object



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

def admonition_class(node)
  if node["type"] == "editorial" then "zzHelp"
  elsif node.ancestors("introduction").empty?
    stylesmap[:admonition]
  else stylesmap[:intro]
  end
end

#admonition_cleanup(docxml) ⇒ Object

STYLE



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/isodoc/ieee/word_cleanup_blocks.rb', line 5

def admonition_cleanup(docxml)
  super
  docxml.xpath("//div[@class = 'zzHelp']").each do |d|
    d.xpath(".//p").each do |p|
      [stylesmap[:admonition], stylesmap[:MsoNormal]]
        .include?(p["class"]) || !p["class"] or next
      p["class"] = "zzHelp"
    end
  end
  docxml
end

#admonition_name_parse(_node, div, name) ⇒ Object



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

def admonition_name_parse(_node, div, name)
  div.p class: stylesmap[:admonition], style: "text-align:center;" do |p|
    p.b do |b|
      name.children.each { |n| parse(n, b) }
    end
  end
end

#annex_attrs(node) ⇒ Object



148
149
150
# File 'lib/isodoc/ieee/word_convert.rb', line 148

def annex_attrs(node)
  { id: node["id"], class: "Annex" }
end

#annex_caption?(div) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
134
135
# File 'lib/isodoc/ieee/word_cleanup.rb', line 131

def annex_caption?(div)
  annex = div["annex"]
  div.delete("annex")
  annex
end

#annex_name(_annex, name, div) ⇒ Object



152
153
154
155
156
157
158
159
160
# File 'lib/isodoc/ieee/word_convert.rb', line 152

def annex_name(_annex, name, div)
  name.nil? and return
  name&.at(ns(".//strong"))&.remove # supplied by CSS list numbering
  div.h1 class: "Annex" do |t|
    # annex_name1(name, t)
    children_parse(name, t)
    clause_parse_subtitle(name, t)
  end
end

#annex_toc(annexid) ⇒ Object



24
25
26
27
28
# File 'lib/isodoc/ieee/word_toc.rb', line 24

def annex_toc(annexid)
  annexid += 1
  x = "#{@i18n.annex} #{('@'.ord + annexid).chr} "
  [x, annexid]
end

#authority_cleanup(docxml) ⇒ Object



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

def authority_cleanup(docxml)
  feedback_footnote(docxml)
  %w(copyright license disclaimers participants).each do |t|
    authority_cleanup1(docxml, t)
  end
  coverpage_note_cleanup(docxml)
  authority_style(docxml)
end

#authority_cleanup1(docxml, klass) ⇒ Object



189
190
191
192
193
194
195
196
197
198
# File 'lib/isodoc/ieee/word_authority.rb', line 189

def authority_cleanup1(docxml, klass)
  dest = docxml.at("//div[@id = 'boilerplate-#{klass}-destination']")
  auth = docxml.at("//div[@id = 'boilerplate-#{klass}' " \
                   "or @class = 'boilerplate-#{klass}']") or return
  auth.xpath(".//h1[not(text())] | .//h2[not(text())]")&.each(&:remove)
  authority_cleanup_hdr(auth.remove)
  if dest && auth
    dest.replace(auth)
  end
end

#authority_cleanup_hdr(auth) ⇒ Object



200
201
202
203
204
205
206
207
# File 'lib/isodoc/ieee/word_authority.rb', line 200

def authority_cleanup_hdr(auth)
  (1..2).each do |i|
    auth&.xpath(".//h#{i}")&.each do |h|
      h.name = "p"
      h["class"] = "level#{i}frontmatter"
    end
  end
end

#authority_style(docxml) ⇒ Object



22
23
24
25
26
# File 'lib/isodoc/ieee/word_authority.rb', line 22

def authority_style(docxml)
  copyright_style(docxml)
  license_style(docxml)
  officer_style(docxml)
end

#biblio_cleanup(docxml) ⇒ Object



35
36
37
38
39
# File 'lib/isodoc/ieee/word_cleanup.rb', line 35

def biblio_cleanup(docxml)
  docxml.xpath("//p[@class = 'Biblio']").each do |p|
    headings_strip(p)
  end
end

#caption_cleanup(docxml) ⇒ Object



80
81
82
# File 'lib/isodoc/ieee/word_cleanup_blocks.rb', line 80

def caption_cleanup(docxml)
  example_caption(docxml)
end

#caption_style_cleanup(docxml) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/isodoc/ieee/word_cleanup.rb', line 120

def caption_style_cleanup(docxml)
  docxml.xpath("//*[@class = 'TableTitle']").each do |p|
    p["class"] =
      annex_caption?(p.parent) ? "TableCaption" : stylesmap[:TableTitle]
  end
  docxml.xpath("//*[@class = 'FigureTitle']").each do |p|
    p["class"] =
      annex_caption?(p.parent) ? "FigureCaption" : stylesmap[:FigureTitle]
  end
end

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



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

def convert(input_filename, file = nil, debug = false,
    output_filename = nil)
  file ||= File.read(input_filename, encoding: "utf-8")
  docxml = Nokogiri::XML(file, &:huge)
  doctype = docxml&.at(ns("//bibdata/ext/doctype"))&.text
  if @wp && doctype == "whitepaper"
    @wp.convert(input_filename, file, debug, output_filename)
  else
    super
  end
end

#convert1(docxml, filename, dir) ⇒ Object



22
23
24
25
26
27
# File 'lib/isodoc/ieee/word_convert.rb', line 22

def convert1(docxml, filename, dir)
  if %w(amendment corrigendum).include?(@doctype)
    @header = html_doc_path("header_amd.html")
  end
  super
end


28
29
30
31
32
33
34
# File 'lib/isodoc/ieee/word_authority.rb', line 28

def copyright_style(docxml)
  docxml.at("//div[@class = 'boilerplate-copyright']")&.xpath(".//p")
    &.reverse&.each_with_index do |p, i|
    p["class"] =
      i.zero? ? "IEEEStdsTitleDraftCRBody" : "IEEEStdsTitleDraftCRaddr"
  end
end

#default_file_locations(_options) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/isodoc/ieee/word_convert.rb', line 51

def default_file_locations(_options)
  { wordstylesheet: html_doc_path("wordstyle.scss"),
    standardstylesheet: html_doc_path("ieee.scss"),
    header: html_doc_path("header.html"),
    wordcoverpage: html_doc_path("word_ieee_titlepage.html"),
    wordintropage: html_doc_path("word_ieee_intro.html"),
    ulstyle: "l11", olstyle: "l16" }
end

#default_fonts(options) ⇒ Object



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

def default_fonts(options)
  { bodyfont: '"Times New Roman",serif',
    headerfont: '"Arial",sans-serif',
    monospacefont: '"Courier New",monospace',
    normalfontsize: "12.0pt",
    footnotefontsize: "11.0pt",
    smallerfontsize: "10.0pt",
    monospacefontsize: "10.0pt" }
end

#div_cleanup(docxml) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/isodoc/ieee/word_cleanup.rb', line 72

def div_cleanup(docxml)
  d = docxml.at("//div[@class = 'WordSection2']" \
                "[div[@class = 'WordSection2']]") and
    d.replace(d.children)
  i = 0
  docxml.xpath("//div[@class]").each do |div|
    next unless /^WordSection\d*$/.match?(div["class"])

    i += 1
    div["class"] = "WordSection#{i}"
  end
end

#dt_dd?(node) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/isodoc/ieee/word_convert.rb', line 123

def dt_dd?(node)
  %w{dt dd}.include? node.name
end

#example_caption(docxml) ⇒ Object



84
85
86
87
88
# File 'lib/isodoc/ieee/word_cleanup_blocks.rb', line 84

def example_caption(docxml)
  docxml.xpath("//p[@class = 'example-title']").each do |s|
    s["class"] = stylesmap[:MsoNormal]
  end
end

#feedback_footnote(docxml) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/isodoc/ieee/word_authority.rb', line 13

def feedback_footnote(docxml)
  feedback_style(docxml)
  feedback_table(docxml)
  f = docxml.at("//div[@class = 'boilerplate-feedback']") or return
  docxml.at("//aside").previous = "    <aside id=\"ftn0\">\#{to_xml(f.remove)}</aside>\n  FN\nend\n"

#feedback_style(docxml) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/isodoc/ieee/word_authority.rb', line 167

def feedback_style(docxml)
  f = docxml.at("//div[@class = 'boilerplate-feedback']") or return
  f.xpath("./div").each_with_index do |div, i|
    i.zero? or div.elements.first.previous = "<p>&#xa0;</p>"
    i == 4 and
      div.xpath(".//p[br]").each do |p|
        p.replace(to_xml(p).gsub(%r{<br/>}, "</p><p>"))
      end
    feedback_style1(div, i)
  end
end

#feedback_style1(div, idx) ⇒ Object

STYLE



180
181
182
183
184
185
186
187
# File 'lib/isodoc/ieee/word_authority.rb', line 180

def feedback_style1(div, idx)
  div.xpath(".//p").each_with_index do |p, j|
    p["class"] = idx == 4 ? "IEEEStdsCRTextItal" : "IEEEStdsCRTextReg"
    j.zero? && idx.zero? and p.add_first_child "      <a style=\"mso-footnote-id:ftn0\" href=\"#_ftnref0\" name=\"_ftn0\" title=\"\"/>\n    XML\n  end\nend\n"

#feedback_table(docxml) ⇒ Object



147
148
149
150
151
152
153
154
155
# File 'lib/isodoc/ieee/word_authority.rb', line 147

def feedback_table(docxml)
  docxml.at("//div[@class = 'boilerplate-feedback']")&.xpath(".//table")
    &.each do |t|
    t.xpath(".//tr").each do |tr|
      feedback_table1(tr)
    end
    t.replace(t.at(".//tbody").elements)
  end
end

#feedback_table1(trow) ⇒ Object

STYLE



158
159
160
161
162
163
164
165
# File 'lib/isodoc/ieee/word_authority.rb', line 158

def feedback_table1(trow)
  trow.name = "p"
  trow["class"] = "IEEEStdsCRTextReg"
  trow.xpath("./td").each do |td|
    td.next_element and td << "<span style='mso-tab-count:1'> </span>"
    td.replace(td.children)
  end
end

#figure_name_parse(node, div, name) ⇒ Object

Do not strip the caption in Annexes



216
217
218
219
220
221
222
223
224
# File 'lib/isodoc/ieee/word_convert.rb', line 216

def figure_name_parse(node, div, name)
  name.nil? and return
  if name.ancestors.map(&:name).include?("annex")
    div.parent["annex"] = true
  else
    strip_caption_semx(name)
  end
  super
end

#figure_toc_classObject



34
35
36
# File 'lib/isodoc/ieee/word_toc.rb', line 34

def figure_toc_class
  ["IEEEStds Regular Figure Caption", "FigureTitle", "figuretitle", "FigureCaption"]
end

#formula_parse(node, out) ⇒ Object



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

def formula_parse(node, out)
  out.div **formula_attrs(node) do |div|
    formula_parse1(node, div)
    node.children.each do |n|
      %w(fmt-stem fmt-name).include? n.name and next
      if n.name == "dl" then formula_where(n, div)
      else parse(n, div)
      end
    end
  end
end

#formula_where(dlist, out) ⇒ Object



127
128
129
130
131
132
# File 'lib/isodoc/ieee/word_convert.rb', line 127

def formula_where(dlist, out)
  dlist or return
  dlist.elements.select { |n| dt_dd? n }.each_slice(2) do |dt, dd|
    formula_where1(out, dt, dd)
  end
end

#formula_where1(out, dterm, ddefn) ⇒ Object

STYLE



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

def formula_where1(out, dterm, ddefn)
  out.p class: "IEEEStdsEquationVariableList" do |p|
    dterm.children.each { |n| parse(n, p) }
    insert_tab(p, 1)
    if ddefn.at(ns("./p"))
      ddefn.elements.each do |e|
        e.children.each { |n| parse(n, p) }
      end
    else ddefn.children.each { |n| parse(n, p) }
    end
  end
end

#headings_cleanup(docxml) ⇒ Object



41
42
43
44
# File 'lib/isodoc/ieee/word_cleanup.rb', line 41

def headings_cleanup(docxml)
  (1..9).each { |i| headings_cleanup1(docxml, i) }
  docxml.xpath("//div[@class = 'Annex']").each { |a| a.delete("class") }
end

#headings_cleanup1(docxml, idx) ⇒ Object



46
47
48
49
50
51
# File 'lib/isodoc/ieee/word_cleanup.rb', line 46

def headings_cleanup1(docxml, idx)
  docxml.xpath("//h#{idx}").each do |h|
    headings_strip(h)
    headings_style(h, idx)
  end
end

#headings_strip(hdr) ⇒ Object



65
66
67
68
69
70
# File 'lib/isodoc/ieee/word_cleanup.rb', line 65

def headings_strip(hdr)
  if hdr.children.size > 1 && hdr.children[1].name == "span" &&
      hdr.children[1]["style"] == "mso-tab-count:1"
    2.times { hdr.children.first.remove }
  end
end

#headings_style(hdr, idx) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/isodoc/ieee/word_cleanup.rb', line 53

def headings_style(hdr, idx)
  if hdr.at("./ancestor::div[@class = 'Annex']")
    hdr.delete("class")
    hdr["style"] = "mso-list:l13 level#{idx} lfo33;"
  else
    hdr.name = "p"
    front = hdr.at("./ancestor::div[@class = 'Section3' or @class = 'WordSectionContents']")
    type = front ? "frontmatter" : "header"
    hdr["class"] = stylesmap["level#{idx}#{type}".to_sym]
  end
end

#init_wp(options) ⇒ Object



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

def init_wp(options)
  @wp = ::IsoDoc::Ieee::WordWPConvert.new(options)
end

#insert_toc(intro, docxml, level) ⇒ Object



38
39
40
41
42
43
# File 'lib/isodoc/ieee/word_toc.rb', line 38

def insert_toc(intro, docxml, level)
  toc = assemble_toc(docxml, level)
  source = docxml.at("//div[@class = 'WordSectionContents']") and
    source << toc
  intro
end

#introduction_cleanup(docxml) ⇒ Object



181
182
183
184
185
186
187
188
# File 'lib/isodoc/ieee/word_cleanup.rb', line 181

def introduction_cleanup(docxml)
  dest = docxml.at("div[@id = 'introduction-destination']") or return
  unless i = docxml.at("//h1[@class = 'IntroTitle']")&.parent
    dest.parent.remove
    return
  end
  introduction_cleanup1(i, dest)
end

#introduction_cleanup1(intro, dest) ⇒ Object



190
191
192
193
# File 'lib/isodoc/ieee/word_cleanup.rb', line 190

def introduction_cleanup1(intro, dest)
  introduction_to_frontispiece(intro, dest)
  introduction_para_style(intro.document)
end

#introduction_para_style(docxml) ⇒ Object



206
207
208
209
210
211
212
213
# File 'lib/isodoc/ieee/word_cleanup.rb', line 206

def introduction_para_style(docxml)
  docxml.xpath("//h1[@class = 'IntroTitle']").each do |i|
    i.next_element or next
    i.next_element.name == "div" &&
      i.next_element["class"] == stylesmap[:intro] and
      i.next_element.name = "p"
  end
end

#introduction_to_frontispiece(intro, dest) ⇒ Object



195
196
197
198
199
200
201
202
203
204
# File 'lib/isodoc/ieee/word_cleanup.rb', line 195

def introduction_to_frontispiece(intro, dest)
  docxml = intro.document
  intro.previous_element.remove
  introcontent = docxml.xpath("//h1[@class = 'IntroTitle']")
    .map(&:parent).uniq.map(&:remove)
  introcontent.each do |node|
    dest.add_previous_sibling(node)
  end
  dest.remove
end

#license_style(docxml) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/isodoc/ieee/word_authority.rb', line 36

def license_style(docxml)
  docxml.at("//div[@class = 'boilerplate-license']")&.xpath(".//p")
    &.reverse&.each_with_index do |p, i|
    p["class"] =
      i.zero? ? "IEEEStdsTitleDraftCRBody" : "IEEEStdsTitleDraftCRaddr"
  end
end

#make_body3(body, docxml) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/isodoc/ieee/word_convert.rb', line 75

def make_body3(body, docxml)
  body.div class: "WordSectionMiddleTitle" do |div3|
    middle_title_ieee(docxml, div3)
  end
  section_break(body, continuous: true)
  body.div class: "WordSectionMain" do |div3|
    content(div3, docxml, ns(self.class::MAIN_ELEMENTS))
    footnotes docxml, div3
    comments docxml, div3
  end
end

#make_WordToC(docxml, level) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/isodoc/ieee/word_toc.rb', line 4

def make_WordToC(docxml, level)
  toc = ""
  if source = docxml.at("//div[@class = 'TOC']")
    toc = to_xml(source.children)
  end
  xpath = (1..level).each.map do |i|
    "//h#{i}[not(ancestor::*[@class = 'WordSection2'])]"
  end.join (" | ")
  annexid = 0
  docxml.xpath(xpath).each do |h|
    x = ""
    if h.name == "h1" && h["class"] == "Annex"
      x, annexid = annex_toc(annexid)
    end
    toc += word_toc_entry(h.name[1].to_i, x + header_strip(h))
  end
  toc.sub(/(<p class="MsoToc1">)/,
          %{\\1#{word_toc_preface(level)}}) + WORD_TOC_SUFFIX1
end

#middle_title_ieee(docxml, out) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/isodoc/ieee/word_convert.rb', line 87

def middle_title_ieee(docxml, out)
  title = docxml.at(ns("//p[@class = 'zzSTDTitle1']")) or return
  out.p(class: stylesmap[:zzSTDTitle1],
        style: "margin-left:0cm;margin-top:70.0pt") do |p|
    title.children.each { |n| parse(n, p) }
  end
end

#note_style_cleanup(docxml) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/isodoc/ieee/word_cleanup_blocks.rb', line 105

def note_style_cleanup(docxml)
  docxml.xpath("//span[@class = 'note_label']").each do |s|
    multi = /^#{@i18n.note}\s+[A-Z0-9.]+/.match?(s.text)
    div = s.at("./ancestor::div[@class = 'Note']")
    if multi
      s.remove
      seq = notesequence(div)
    else seq = nil
    end
    note_style_cleanup1(multi, div, seq)
  end
end

#note_style_cleanup1(multi, div, seq) ⇒ Object

hardcoded list style for notes



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/isodoc/ieee/word_cleanup_blocks.rb', line 131

def note_style_cleanup1(multi, div, seq)
  div.xpath(".//p[@class = 'Note' or not(@class)]")
    .each_with_index do |p, i|
    p["class"] =
      i.zero? && multi ? "IEEEStdsMultipleNotes" : "IEEEStdsSingleNote"
    if multi
      p["style"] ||= ""
      p["style"] += "mso-list:l17 level1 lfo#{seq};"
    end
  end
end

#notesequence(div) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/isodoc/ieee/word_cleanup_blocks.rb', line 118

def notesequence(div)
  @notesequences ||= { max: 0, lookup: {} }
  unless id = @notesequences[:lookup][@xrefs.anchor(div["id"],
                                                    :sequence)]
    @notesequences[:max] += 1
    id = @notesequences[:max]
    @notesequences[:lookup][@xrefs.anchor(div["id"], :sequence)] =
      id
  end
  id
end

#officeholder_style(para) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/isodoc/ieee/word_authority.rb', line 54

def officeholder_style(para)
  n = para.next_element
  p = para.previous_element
  n && n.name == "p" && n["type"] != "officeholder" and
    klass = "IEEEStdsNamesCtrCxSpLast"
  p && p.name == "p" && p["type"] != "officeholder" and
    klass = "IEEEStdsNamesCtrCxSpFirst"
  para["class"] = klass || "IEEEStdsNamesCtrCxSpMiddle"
end

#officemember_style(docxml) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/isodoc/ieee/word_authority.rb', line 71

def officemember_style(docxml)
  docxml.xpath("//p[@type = 'officemember' or @type = 'officeorgmember']")
    .each do |p|
    p["class"] = stylesmap[:nameslist]
  end
  docxml.xpath("//p[@type = 'emeritus_sign']").each do |p|
    p["class"] = "IEEEStdsParaMemEmeritus"
  end
end

#officeorgrep_style(docxml) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/isodoc/ieee/word_authority.rb', line 81

def officeorgrep_style(docxml)
  docxml.xpath("//p[@type = 'officeorgrepmemberhdr']").each do |p|
    p["class"] = stylesmap[:nameslist]
    p["style"] =
      "margin-bottom:6.0pt;tab-stops:right 432.0pt;"
  end
  docxml.xpath("//p[@type = 'officeorgrepmember']").each do |p|
    p["class"] = stylesmap[:nameslist]
    p["style"] =
      "margin-top:6.0pt;tab-stops:right dotted 432.0pt;"
  end
end

#officer_style(docxml) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/isodoc/ieee/word_authority.rb', line 44

def officer_style(docxml)
  docxml.xpath("//p[@type = 'officeholder']").each do |p|
    officeholder_style(p)
  end
  officemember_style(docxml)
  officeorgrep_style(docxml)
  three_column_officemembers(docxml
    .at("//div[@id = 'boilerplate-participants']"))
end

#para_type_cleanup(html) ⇒ Object



101
102
103
# File 'lib/isodoc/ieee/word_cleanup_blocks.rb', line 101

def para_type_cleanup(html)
  html.xpath("//p[@type]").each { |p| p.delete("type") }
end

#sourcecode_cleanup(docxml) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/isodoc/ieee/word_cleanup_blocks.rb', line 90

def sourcecode_cleanup(docxml)
  docxml.xpath("//p[@class = 'Sourcecode']").each do |para|
    br_elements = para.xpath(".//br")
    br_elements.empty? and next
    split_para_at_breaks(para).reverse.each do |new_para|
      para.add_next_sibling(new_para)
    end
    para.remove
  end
end

#sourcecode_styleObject



16
17
18
# File 'lib/isodoc/ieee/word_cleanup.rb', line 16

def sourcecode_style
  stylesmap[:Sourcecode]
end

#span_parse(node, out) ⇒ Object



162
163
164
165
166
167
168
# File 'lib/isodoc/ieee/word_convert.rb', line 162

def span_parse(node, out)
  if node["class"] == "fmt-obligation"
    node.delete("class")
    node["style"] = "font-weight:normal;"
  end
  super
end

#strip_caption_semx(name) ⇒ Object

“Figure 1” remove each of these: strip elements in caption associated with autonumbering – # but not in footnotes. The autonumber captions are provided by Word styles instead. Retain the em-dash delimiting caption



203
204
205
206
207
208
209
210
211
212
213
# File 'lib/isodoc/ieee/word_convert.rb', line 203

def strip_caption_semx(name)
  name.xpath(".//xmlns:semx[@element = 'autonum']/"\
             "preceding-sibling::text()[normalize-space() = '']")
    .each do |s|
    s.ancestors("fn").empty? and s.remove
  end
  name.xpath(ns(".//span[@class = 'fmt-element-name']  | "\
                ".//semx[@element = 'autonum']")).each do |s|
                  s.ancestors("fn").empty? and s.remove
                end
end

#style_cleanup(docxml) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/isodoc/ieee/word_cleanup.rb', line 137

def style_cleanup(docxml)
  note_style_cleanup(docxml)
  caption_style_cleanup(docxml)
  docxml.xpath("//div[@class = 'formula']/p").each do |p|
    p["class"] = stylesmap[:formula]
  end
  stylesmap.each do |k, v|
    docxml.xpath("//*[@class = '#{k}']").each { |s| s["class"] = v }
  end
  docxml.xpath("//p[not(@class)]").each do |p|
    p["class"] = stylesmap[:MsoNormal]
  end
end

#stylesmapObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/isodoc/ieee/word_cleanup.rb', line 85

def stylesmap
  { example: "IEEEStdsParagraph",
    MsoNormal: "IEEEStdsParagraph",
    NormRef: "IEEEStdsParagraph",
    Biblio: "IEEEStdsBibliographicEntry",
    figure: "IEEEStdsImage",
    formula: "IEEEStdsEquation",
    Sourcecode: "IEEEStdsComputerCode",
    TableTitle: "IEEEStdsRegularTableCaption",
    FigureTitle: "IEEEStdsRegularFigureCaption",
    admonition: "IEEEStdsWarning",
    abstract: "IEEEStdsAbstractBody",
    AbstractTitle: "AbstractTitle",
    TOCTitle: "IEEEStdsLevel1frontmatter",
    level1frontmatter: "IEEEStdsLevel1frontmatter",
    level2frontmatter: "IEEEStdsLevel2frontmatter",
    level3frontmatter: "IEEEStdsLevel3frontmatter",
    level1header: "IEEEStdsLevel1Header",
    level2header: "IEEEStdsLevel2Header",
    level3header: "IEEEStdsLevel3Header",
    level4header: "IEEEStdsLevel4Header",
    level5header: "IEEEStdsLevel5Header",
    level6header: "IEEEStdsLevel6Header",
    zzSTDTitle1: "IEEEStdsTitle",
    tabledata_center: "IEEEStdsTableData-Center",
    tabledata_left: "IEEEStdsTableData-Left",
    table_head: "IEEEStdsTableLineHead",
    table_subhead: "IEEEStdsTableLineSubhead",
    table_columnhead: "IEEEStdsTableColumnHead",
    nameslist: "IEEEStdsNamesList",
    intro: "IEEEStdsIntroduction",
    surname: "au_surname",
    forename: "au_fname" }
end

#table_cleanup(docxml) ⇒ Object



17
18
19
20
# File 'lib/isodoc/ieee/word_cleanup_blocks.rb', line 17

def table_cleanup(docxml)
  thead_cleanup(docxml)
  tbody_cleanup(docxml)
end

#table_of_contents(clause, out) ⇒ Object

STYLE



189
190
191
192
193
194
195
196
197
# File 'lib/isodoc/ieee/word_convert.rb', line 189

def table_of_contents(clause, out)
  out.div class: "WordSectionContents" do |div|
    clause_name(clause, clause.at(ns("./fmt-title")), div,
                { class: "IEEEStdsLevel1frontmatter" })
    clause.elements.each do |e|
      parse(e, div) unless e.name == "fmt-title"
    end
  end
end

#table_title_parse(node, out) ⇒ Object



226
227
228
229
230
231
232
233
234
# File 'lib/isodoc/ieee/word_convert.rb', line 226

def table_title_parse(node, out)
  name = node.at(ns("./fmt-name")) or return
  if name.ancestors.map(&:name).include?("annex")
    out.parent["annex"] = true
  else
    strip_caption_semx(name)
  end
  super
end

#table_toc_classObject



30
31
32
# File 'lib/isodoc/ieee/word_toc.rb', line 30

def table_toc_class
  ["IEEEStds Regular Table Caption", "TableTitle", "tabletitle", "TableCaption"]
end

#tbody_cleanup(docxml) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/isodoc/ieee/word_cleanup_blocks.rb', line 42

def tbody_cleanup(docxml)
  docxml.xpath("//tbody | //tfoot").each do |h|
    next if h.at("./ancestor::div[@class = 'boilerplate-feedback']")

    h.xpath(".//th").each { |t| tbody_head_cleanup(t) }
    h.xpath(".//td | .//th").each { |t| tbody_cleanup1(t) }
  end
end

#tbody_cleanup1(cell) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/isodoc/ieee/word_cleanup_blocks.rb', line 59

def tbody_cleanup1(cell)
  if cell.at("./p")
    cell.xpath("./p").each_with_index do |p, i|
      p["class"] = td_style(cell, i)
    end
  else
    cell.children =
      "<p class='#{td_style(cell, 0)}'>#{to_xml(cell.children)}</p>"
  end
end

#tbody_head_cleanup(cell) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/isodoc/ieee/word_cleanup_blocks.rb', line 51

def tbody_head_cleanup(cell)
  cell.at("./p") or
    cell.children = "<p>#{to_xml(cell.children)}</p>"
  cell.xpath("./p").each do |p|
    p.replace to_xml(p).gsub(%r{<br/>}, "</p><p>")
  end
end

#td_style(cell, idx) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/isodoc/ieee/word_cleanup_blocks.rb', line 70

def td_style(cell, idx)
  if cell.name == "th" && idx.zero? then stylesmap[:table_head]
  elsif cell.name == "th" then stylesmap[:table_subhead]
  elsif cell["align"] == "center" ||
      cell["style"].include?("text-align:center")
    stylesmap[:tabledata_center]
  else stylesmap[:tabledata_left]
  end
end

#termnote_label(para, name) ⇒ Object



182
183
184
185
186
# File 'lib/isodoc/ieee/word_convert.rb', line 182

def termnote_label(para, name)
  para.span class: "note_label" do |s|
    name.children.each { |n| parse(n, s) }
  end
end

#termnote_parse(node, out) ⇒ Object



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

def termnote_parse(node, out)
  name = node.at(ns("./fmt-name"))
  para = node.at(ns("./p"))
  out.div **note_attrs(node) do |div|
    div.p do |p|
      name and termnote_label(p, name)
      children_parse(para, p)
    end
    para.xpath("./following-sibling::*").each { |n| parse(n, div) }
  end
end

#thead_cell_cleanup(cell) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/isodoc/ieee/word_cleanup_blocks.rb', line 30

def thead_cell_cleanup(cell)
  s = stylesmap[:table_columnhead]
  if cell.at("./p")
    cell.xpath("./p").each do |p|
      p["class"] = s
    end
  else
    cell.children =
      "<p class='#{s}'>#{to_xml(cell.children)}</p>"
  end
end

#thead_cleanup(docxml) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/isodoc/ieee/word_cleanup_blocks.rb', line 22

def thead_cleanup(docxml)
  docxml.xpath("//thead").each do |h|
    h.xpath(".//td | .//th").each do |t|
      thead_cell_cleanup(t)
    end
  end
end

#three_column_officemembers(div) ⇒ Object



94
95
96
97
98
# File 'lib/isodoc/ieee/word_authority.rb', line 94

def three_column_officemembers(div)
  div or return
  ret = three_column_officemembers_split(div)
  three_column_officemembers_render(div, ret)
end

#three_column_officemembers_render(div, ret) ⇒ Object



137
138
139
140
141
142
143
144
145
# File 'lib/isodoc/ieee/word_authority.rb', line 137

def three_column_officemembers_render(div, ret)
  div.children = ret[0]
  out = ret[1..-1].map.with_index do |d, i|
    para = i % 2 == 1 && i != ret.size - 2 ? "<p>&#xa0;</p>" : ""
    "<div class='WordSection'>#{para}#{d}</div>"
  end.join(SECTIONBREAK)
  div.document.at("//div[@class = 'WordSectionIntro']")
    .previous_element.previous = SECTIONBREAK + out + SECTIONBREAK
end

#three_column_officemembers_split(div) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/isodoc/ieee/word_authority.rb', line 100

def three_column_officemembers_split(div)
  div.xpath(".//div").each { |d| d.replace(d.children) }
  ret = three_column_officemembers_split_main(false, div)
  ret = three_column_officemembers_split_balance(ret)
  ret.map do |r|
    r.map { |r1| to_xml(r1) }
  end.map(&:join)
end

#three_column_officemembers_split_balance(ret) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/isodoc/ieee/word_authority.rb', line 124

def three_column_officemembers_split_balance(ret)
  blank = Nokogiri::XML("<p class='IEEEStdsNamesList'>&#xa0;</p>").root
  ret.each do |r|
    s = r.size
    r[0].name == "p" && r[0]["type"] == "officemember" && s > 3 or next
    extras = s % 3
    extras == 1 and r.insert((s / 3).floor, blank)
    extras == 2 and
      r.insert((s / 3).ceil + (s / 3).floor + 1, blank)
  end
  ret
end

#three_column_officemembers_split_main(prev, div) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/isodoc/ieee/word_authority.rb', line 109

def three_column_officemembers_split_main(prev, div)
  div.elements.each_with_object([[]]) do |e, m|
    member = e.name == "p" && e["type"] == "officemember"
    if prev == member
      !m[-1].empty? && m[-1][-1]["class"] == stylesmap[:nameslist] &&
        e["class"] != stylesmap[:nameslist] and
        m[-1] << Nokogiri::XML("<p>&#xa0;</p>").root
      m[-1] << e
    else
      m << [e]
    end
    prev = member
  end
end

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



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

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

#word_cleanup(docxml) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/isodoc/ieee/word_cleanup.rb', line 20

def word_cleanup(docxml)
  super
  abstract_cleanup(docxml)
  introduction_cleanup(docxml)
  sourcecode_cleanup(docxml)
  div_cleanup(docxml)
  biblio_cleanup(docxml)
  headings_cleanup(docxml)
  caption_cleanup(docxml)
  table_cleanup(docxml)
  style_cleanup(docxml)
  para_type_cleanup(docxml)
  docxml
end