Class: IsoDoc::Iso::WordDISConvert

Inherits:
WordConvert
  • Object
show all
Defined in:
lib/isodoc/iso/word_dis_styles.rb,
lib/isodoc/iso/word_dis_cleanup.rb,
lib/isodoc/iso/word_dis_convert.rb

Constant Summary collapse

STYLESMAP =
{
  AltTerms: "AdmittedTerm",
  TableFootnote: "Tablefootnote",
  formula: "Formula",
  note: "Note",
  example: "Example",
  admonition: "Admonition",
  admonitiontitle: "AdmonitionTitle",
  sourcetitle: "SourceTitle",
  TableTitle: "Tabletitle",
  titlepagesbhead: "TablePageSubhead",
  NormRef: "RefNorm",
  Biblio: "BiblioEntry",
  MsoNormal: "MsoBodyText",
  FigureTitle: "Figuretitle",
  zzwarning: "zzWarning",
  zzwarninghdr: "zzWarningHdr",
  quoteattribution: "QuoteAttribution",
  Sourcecode: "Code",
  zzSTDTitle1: "zzSTDTitle",
  zzSTDTitle2: "zzSTDTitle",
  zzCopyright1: "zzCopyright",
}.freeze
FIGURE_NESTED_STYLES =
{ Note: "Figurenote", example: "Figureexample" }.freeze
SMALL_FONT_CLASSES =

TODO read $smallerfonsize from CSS definitions

%w(pseudocode Note tablefootnote figdl MsoISOTable MsoTableGrid
TableISO Example Notecontinued Noteindent Noteindentcontinued
ListNumber5- ListContinue5- BodyTextIndent22 BodyTextIndent32
Exampleindent2 Exampleindent2continued Noteindent2continued
Noteindent2 example_label note_label Tablebody MsoNormalTable).freeze
INLINE_CODE_CLASSES =
%w(ISOCodebold ISOCodeitalic ISOCode).freeze

Constants inherited from WordConvert

IsoDoc::Iso::WordConvert::MAIN_ELEMENTS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from WordConvert

#annex_name, #annex_name1, #authority_hdr_cleanup, #bibliography, #bibliography_attrs, #bibliography_parse, #br, #colophon_section, #convert, #default_fonts, #definition_parse, #figure_cleanup, #figure_name_parse, #font_choice, #footnote_reference_format, #index_cleanup, #indexsect, #indexsect_section, #indexsect_title, #make_WordToC, #make_body, #para_class, #quote_style, #style_cleanup, #table_attrs, #table_parse, #table_parse_tail, #table_title_parse, #termref_attrs, #termref_parse, #word_annex_cleanup, #word_annex_cleanup_h1

Methods included from Init

#amd?, #clausedelim, #i18n_conditional_set, #i18n_init, #metadata_init, #requirements_processor, #std_docid_semantic, #std_docid_semantic1, #std_docid_semantic_full, #update_i18n, #xref_init

Methods included from BaseConvert

#admonition_class, #admonition_name_parse, #admonition_p_parse, #admonition_parse, #admonition_parse1, #annex, #clause_etc1, #cleanup, #convert_i18n_init, #error_parse, #example_p_parse, #example_parse, #example_parse1, #example_span_label, #figure_name_parse, #figure_parse1, #foreword, #implicit_reference, #measurement_units, #node_begins_with_para, #ol_attrs, #render_identifier, #table_parse, #table_parse_tail, #table_th_center, #top_element_render

Constructor Details

#initialize(options) ⇒ WordDISConvert

Returns a new instance of WordDISConvert.



18
19
20
21
22
# File 'lib/isodoc/iso/word_dis_convert.rb', line 18

def initialize(options)
  @libdir ||= File.dirname(__FILE__) # rubocop:disable Lint/DisjunctiveAssignmentInConstructor
  options.merge!(default_file_locations(options))
  super
end

Instance Attribute Details

#bgstripcolorObject

Returns the value of attribute bgstripcolor.



6
7
8
# File 'lib/isodoc/iso/word_dis_convert.rb', line 6

def bgstripcolor
  @bgstripcolor
end

Instance Method Details

#amd_style(docxml) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/isodoc/iso/word_dis_styles.rb', line 73

def amd_style(docxml)
  @meta.get[:doctype] == "Amendment" or return
  docxml.xpath("//div[@class = 'WordSection3']//h1").each do |h|
    h.name = "p"
    h["style"] = "font-style:italic;page-break-after:avoid;"
  end
end

#authority_cleanup(docxml) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/isodoc/iso/word_dis_convert.rb', line 123

def authority_cleanup(docxml)
  super
  if @meta.get[:stage_int].to_s[0] == "9" ||
      @meta.get[:stage_int].to_s[0] == "6"
    copyright_prf(docxml)
  else
    copyright_dis(docxml)
  end
end

#code_style(doc) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/isodoc/iso/word_dis_styles.rb', line 154

def code_style(doc)
  span_style((doc.xpath("//tt//b") - doc.xpath("//tt//i//b")),
             "ISOCodebold")
  span_style((doc.xpath("//tt//i") - doc.xpath("//tt//b//i")),
             "ISOCodeitalic")
  span_style((doc.xpath("//b//tt") - doc.xpath("//b//i//tt")),
             "ISOCodebold")
  span_style((doc.xpath("//i//tt") - doc.xpath("//i//b//tt")),
             "ISOCodeitalic")
  span_style(doc.xpath("//tt"), "ISOCode")
end

#convert1(docxml, filename, dir) ⇒ Object



26
27
28
29
# File 'lib/isodoc/iso/word_dis_convert.rb', line 26

def convert1(docxml, filename, dir)
  update_coverpage(docxml)
  super
end


143
144
145
146
147
148
149
# File 'lib/isodoc/iso/word_dis_convert.rb', line 143

def copyright_dis(docxml)
  docxml.xpath("//p[@id = 'boilerplate-address']")&.each do |p|
    p["class"] = "zzCopyright"
    p.replace(to_xml(p).gsub(%r{<br/>}, "</p>\n<p class='zzCopyright'>"))
  end
  docxml.xpath("//p[@class = 'zzCopyrightHdr']")&.each(&:remove)
end


133
134
135
136
137
138
139
140
141
# File 'lib/isodoc/iso/word_dis_convert.rb', line 133

def copyright_prf(docxml)
  docxml.xpath("//p[@id = 'boilerplate-address']")&.each do |p|
    p["class"] = "zzCopyright"
    p["style"] = "text-indent:20.15pt;"
    p.replace(to_xml(p).gsub(%r{<br/>}, "</p>\n<p class='zzCopyright' " \
                                        "style='text-indent:20.15pt;'>"))
  end
  docxml.xpath("//p[@class = 'zzCopyrightHdr']")&.each(&:remove)
end

#default_file_locations(_options) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/isodoc/iso/word_dis_convert.rb', line 8

def default_file_locations(_options)
  { wordstylesheet: html_doc_path("wordstyle-dis.scss"),
    standardstylesheet: html_doc_path("isodoc-dis.scss"),
    header: html_doc_path("header-dis.html"),
    wordcoverpage: html_doc_path("word_iso_titlepage-dis.html"),
    wordintropage: html_doc_path("word_iso_intro-dis.html"),
    ulstyle: "l3",
    olstyle: "l2" }
end

#dis_style_interactions(docxml) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/isodoc/iso/word_dis_styles.rb', line 64

def dis_style_interactions(docxml)
  docxml.xpath("//p[@class = 'Code' or @class = 'Code-' or " \
               "@class = 'Code--']" \
               "[following::p[@class = 'Examplecontinued']]").each do |p|
    p["style"] ||= ""
    p["style"] = "margin-bottom:12pt;#{p['style']}"
  end
end

#dis_styles1(docxml) ⇒ Object



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

def dis_styles1(docxml)
  amd_style(docxml)
  middle_title_style(docxml)
  code_style(docxml)
  figure_style(docxml)
  formula_style(docxml)
  note_style(docxml)
  example_style(docxml)
  dis_style_interactions(docxml)
  quote_style(docxml)
  smaller_code_style(docxml)
end

#example_continued_style(docxml) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/isodoc/iso/word_dis_styles.rb', line 114

def example_continued_style(docxml)
  docxml.xpath("//div[@class = 'Example']").each do |d|
    d.xpath("./p").each_with_index do |p, i|
      p["class"] && p["class"] != "Example" and next
      p["class"] = (i.zero? ? "Example" : "Examplecontinued")
    end
  end
end

#example_style(docxml) ⇒ Object



110
111
112
# File 'lib/isodoc/iso/word_dis_styles.rb', line 110

def example_style(docxml)
  example_continued_style(docxml)
end

#figure_name_attrs(_node) ⇒ Object



41
42
43
# File 'lib/isodoc/iso/word_dis_convert.rb', line 41

def figure_name_attrs(_node)
  { class: "FigureTitle", style: "text-align:center;" }
end

#figure_style(docxml) ⇒ Object



135
136
137
138
139
140
141
142
143
144
# File 'lib/isodoc/iso/word_dis_styles.rb', line 135

def figure_style(docxml)
  docxml.xpath("//div[@class = 'figure']").each do |f|
    FIGURE_NESTED_STYLES.each do |k, v|
      f.xpath(".//*[@class = '#{k}']").each { |n| n["class"] = v }
    end
    f.xpath("./img").each do |i|
      i.replace("<p class='FigureGraphic'>#{i.to_xml}</p>")
    end
  end
end

#figure_toc_classObject



228
229
230
# File 'lib/isodoc/iso/word_dis_styles.rb', line 228

def figure_toc_class
  ["Figure Title", "Annex Figure Title", "AnnexFigureTitle"] + super
end

#formula_style(docxml) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/isodoc/iso/word_dis_styles.rb', line 146

def formula_style(docxml)
  docxml.xpath("//div[@class = 'Formula']").each do |f|
    f.xpath(".//p[not(@class)]").each do |p|
      p["class"] = "Formula"
    end
  end
end

#init_dis(opt) ⇒ Object



24
# File 'lib/isodoc/iso/word_dis_convert.rb', line 24

def init_dis(opt); end

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



59
60
61
# File 'lib/isodoc/iso/word_dis_convert.rb', line 59

def make_tr_attr(cell, row, totalrows, header, bordered)
  super.merge(header: header)
end

#middle_title(_isoxml, out) ⇒ Object



79
80
81
# File 'lib/isodoc/iso/word_dis_convert.rb', line 79

def middle_title(_isoxml, out)
  middle_title_dis(out)
end

#middle_title_amd(node, out) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/isodoc/iso/word_dis_convert.rb', line 89

def middle_title_amd(node, out)
  out.p(class: "zzSTDTitle2") do |p|
    p.span(style: "font-weight:normal") do |s|
      node.children.each { |n| parse(n, s) }
    end
  end
end

#middle_title_dis(node, out) ⇒ Object



83
84
85
86
87
# File 'lib/isodoc/iso/word_dis_convert.rb', line 83

def middle_title_dis(node, out)
  out.p(class: "zzSTDTitle") do |p|
    node.children.each { |n| parse(n, p) }
  end
end

#middle_title_style(docxml) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/isodoc/iso/word_dis_styles.rb', line 56

def middle_title_style(docxml)
  docxml.xpath("//p[@class = 'zzSTDTitle2']").each do |p|
    p1 = p.previous_element && p1.name == p &&
      p1["class"] = "zzSTDTitle2" or next
    p1 << " #{p.remove.children.to_xml}"
  end
end

#new_styles(docxml) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/isodoc/iso/word_dis_styles.rb', line 28

def new_styles(docxml)
  STYLESMAP.each do |k, v|
    docxml.xpath("//*[@class = '#{k}']").each { |s| s["class"] = v }
  end
  docxml.xpath("//h1[@class = 'ForewordTitle' or @class = 'IntroTitle']")
    .each { |h| h.name = "p" }
  dis_styles1(docxml)
  docxml.xpath("//p[not(@class)]").each { |p| p["class"] = "MsoBodyText" }
  stripbgcolor(docxml)
end

#note_continued_style(docxml) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/isodoc/iso/word_dis_styles.rb', line 123

def note_continued_style(docxml)
  docxml.xpath("//div[@class = 'Note']").each do |d|
    d.xpath("./p").each_with_index do |p, i|
      p["class"] && p["class"] != "Note" and next
      p["class"] = (i.zero? ? "Note" : "Notecontinued")
    end
  end
end

#note_style(docxml) ⇒ Object



105
106
107
108
# File 'lib/isodoc/iso/word_dis_styles.rb', line 105

def note_style(docxml)
  remove_note_label(docxml)
  note_continued_style(docxml)
end

#para_parse(node, out) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/isodoc/iso/word_dis_convert.rb', line 97

def para_parse(node, out)
  case node["class"]
  when "zzSTDTitle1" then middle_title_dis(node, out)
  when "zzSTDTitle2" then middle_title_amd(node, out)
  else super
  end
end

#para_style_change(div, class1, class2) ⇒ Object



81
82
83
84
85
86
# File 'lib/isodoc/iso/word_dis_styles.rb', line 81

def para_style_change(div, class1, class2)
  s = class1 ? "@class = '#{class1}'" : "not(@class)"
  div.xpath(".//p[#{s}]").each do |p|
    p["class"] = class2
  end
end

#quote_style1(div) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/isodoc/iso/word_dis_styles.rb', line 88

def quote_style1(div)
  para_style_change(div, nil, "BodyTextindent1")
  para_style_change(div, "Code-", "Code--")
  para_style_change(div, "Code", "Code-")
  if div["class"] != "Example"
    para_style_change(div, "Example", "Exampleindent")
    para_style_change(div, "Examplecontinued", "Exampleindentcontinued")
  end
  if div["class"] != "Note"
    para_style_change(div, "Note", "Noteindent")
    para_style_change(div, "Notecontinued", "Noteindentcontinued")
  end
  div.xpath(".//table[@class = 'dl']").each do |t|
    t["style"] = "margin-left: 1cm;"
  end
end

#remove_note_label(doc) ⇒ Object



6
7
8
9
10
11
# File 'lib/isodoc/iso/word_dis_cleanup.rb', line 6

def remove_note_label(doc)
  doc.xpath("//span[@class = 'note_label' or @class = 'example_label']")
    .each do |s|
    s.replace(s.children)
  end
end

#smaller_code_style(doc) ⇒ Object



173
174
175
176
# File 'lib/isodoc/iso/word_dis_styles.rb', line 173

def smaller_code_style(doc)
  smaller_code_style_names(doc)
  smaller_code_style_names2spans(doc)
end

#smaller_code_style_names(doc) ⇒ Object



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

def smaller_code_style_names(doc)
  klass = SMALL_FONT_CLASSES.map { |x| "@class = '#{x}'" }.join(" or ")
  doc.xpath("//*[#{klass}]") - doc.xpath("//*[#{klass}]//*[#{klass}]")
    .each do |d|
      INLINE_CODE_CLASSES.each do |n|
        d.xpath(".//span[@class = '#{n}']").each do |s|
          s["class"] += "-"
        end
      end
    end
end

#smaller_code_style_names2spans(doc) ⇒ Object



200
201
202
203
204
205
206
207
208
# File 'lib/isodoc/iso/word_dis_styles.rb', line 200

def smaller_code_style_names2spans(doc)
  INLINE_CODE_CLASSES.each do |n|
    doc.xpath("//span[@class = '#{n}-']").each do |s|
      s["class"] = n
      s.children =
        "<span style='font-size: 9pt;'>#{s.children.to_xml}</span>"
    end
  end
end

#sourcecode_styleObject



39
40
41
# File 'lib/isodoc/iso/word_dis_styles.rb', line 39

def sourcecode_style
  "Code"
end

#span_parse(node, out) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/isodoc/iso/word_dis_convert.rb', line 105

def span_parse(node, out)
  st = node["style"]
  case node["class"]
  when "nonboldtitle"
    out.span **attr_code(style: "#{st};font-weight:normal") do |s|
      node.children.each { |n| parse(n, s) }
    end
  when "boldtitle"
    out.span **attr_code(style: st) do |s|
      node.children.each { |n| parse(n, s) }
    end
  else
    out.span **attr_code(class: node["class"], style: st) do |x|
      node.children.each { |n| parse(n, x) }
    end
  end
end

#span_style(xpath, style) ⇒ Object



166
167
168
169
170
171
# File 'lib/isodoc/iso/word_dis_styles.rb', line 166

def span_style(xpath, style)
  xpath.each do |elem|
    elem.name = "span"
    elem["class"] = style
  end
end

#stripbgcolor(docxml) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/isodoc/iso/word_dis_cleanup.rb', line 36

def stripbgcolor(docxml)
  @bgstripcolor == "true" or return
  %w(aucollab audeg aufname aurole ausuffix ausurname bibarticle bibetal
     bibfname bibfpage bibissue bibjournal biblpage bibnumber
     biborganization bibsuppl bibsurname biburl bibvolume bibyear
     citebib citeen citefig citefn citetbl bibextlink citeeq citetfn
     auprefix citeapp citesec stddocNumber stddocPartNumber
     stddocTitle aumember stdfootnote stdpublisher stdsection stdyear
     stddocumentType bibalt-year bibbook bbichapterno bibchaptertitle
     bibed-etal bibed-fname bibeditionno bibed-organization bibed-suffix
     bibed-surname bibinstitution bibisbn biblocation bibpagecount
     bibpatent bibpublisher bibreportnum bibschool bibseries bibseriesno
     bibtrans stdsuppl citesection).each do |t|
    docxml.xpath("//span[@class = '#{t}']").each do |s|
      s["style"] ||= ""
      s["style"] = "mso-pattern:none;#{s['style']}"
    end
  end
end

#table_title_attrs(_node) ⇒ Object



45
46
47
# File 'lib/isodoc/iso/word_dis_convert.rb', line 45

def table_title_attrs(_node)
  { class: "TableTitle", style: "text-align:center;" }
end

#table_toc_classObject



223
224
225
226
# File 'lib/isodoc/iso/word_dis_styles.rb', line 223

def table_toc_class
  ["Table title", "Tabletitle", "Annex Table Title", "AnnexTableTitle"] +
    super
end

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/isodoc/iso/word_dis_convert.rb', line 63

def toWord(result, filename, dir, header)
  result = from_xhtml(word_cleanup(to_xhtml(result)))
    .gsub("-DOUBLE_HYPHEN_ESCAPE-", "--")
  @wordstylesheet = wordstylesheet_update
  ::Html2Doc::IsoDIS.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

#update_coverpage(docxml) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/isodoc/iso/word_dis_convert.rb', line 31

def update_coverpage(docxml)
  stage = docxml.at(ns("//bibdata/status/stage"))&.text
  substage = docxml.at(ns("//bibdata/status/substage"))&.text
  if /^9/.match?(stage) || (stage == "60" && substage == "60")
    @wordcoverpage = html_doc_path("word_iso_titlepage.html")
  elsif stage == "60" && substage == "00"
    @wordcoverpage = html_doc_path("word_iso_titlepage-prf.html")
  end
end

#word_annex_cleanup1(docxml, lvl) ⇒ Object



210
211
212
213
214
215
# File 'lib/isodoc/iso/word_dis_styles.rb', line 210

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

#word_cleanup(docxml) ⇒ Object



13
14
15
16
17
18
# File 'lib/isodoc/iso/word_dis_cleanup.rb', line 13

def word_cleanup(docxml)
  word_table_cell_para(docxml)
  super
  word_section_end_empty_para(docxml)
  docxml
end

#word_section_end_empty_para(docxml) ⇒ Object



20
21
22
# File 'lib/isodoc/iso/word_dis_cleanup.rb', line 20

def word_section_end_empty_para(docxml)
  docxml.at("//div[@class='WordSection1']/p[last()]").remove
end

#word_table_cell_para(docxml) ⇒ Object



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

def word_table_cell_para(docxml)
  docxml.xpath("//td | //th").each do |t|
    s = word_table_cell_para_style(t)
    t.delete("header")
    if t.at("./p |./div")
      t.xpath("./p | ./div").each { |p| p["class"] = s }
    else
      t.children = "<div class='#{s}'>#{to_xml(t.children)}</div>"
    end
  end
end

#word_table_cell_para_style(cell) ⇒ Object



217
218
219
220
221
# File 'lib/isodoc/iso/word_dis_styles.rb', line 217

def word_table_cell_para_style(cell)
  ret = cell["header"] == "true" ? "Tableheader" : "Tablebody"
  cell["class"] == "rouge-code" and ret = "Code"
  ret
end

#word_toc_preface(level) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/isodoc/iso/word_dis_convert.rb', line 49

def word_toc_preface(level)
  <<~TOC.freeze
    <span lang="EN-GB"><span
    style='mso-element:field-begin'></span><span
    style='mso-spacerun:yes'>&#xA0;</span>TOC \\o "2-#{level}" \\h \\z \\t
    "Heading 1,1,ANNEX,1,Biblio Title,1,Foreword Title,1,Intro Title,1,ANNEXN,1,ANNEXZ,1,na2,1,na3,1,na4,1,na5,1,na6,1,Title,1,Base_Heading,1,Box-title,1,Front Head,1,Index Head,1,AMEND Terms Heading,1,AMEND Heading 1 Unnumbered,1"
     <span style='mso-element:field-separator'></span></span>
  TOC
end