Class: IsoDoc::Gb::WordConvert

Inherits:
WordConvert
  • Object
show all
Defined in:
lib/isodoc/gb/gbwordrender.rb,
lib/isodoc/gb/gbwordconvert.rb

Overview

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

Constant Summary collapse

EXAMPLE_TBL_ATTR =
{ valign: "top", class: "example_label",
style: "padding:2pt 2pt 2pt 2pt" }.freeze
ENDLINE =
<<~END.freeze
<v:line id="_x0000_s1026"
 alt="" style='position:absolute;left:0;text-align:left;z-index:251662848;
 mso-wrap-edited:f;mso-width-percent:0;mso-height-percent:0;
 mso-width-percent:0;mso-height-percent:0'
 from="6.375cm,20.95pt" to="10.625cm,20.95pt"
 strokeweight="1.5pt"/>
END

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ WordConvert

Returns a new instance of WordConvert.



14
15
16
17
18
19
20
21
# File 'lib/isodoc/gb/gbwordconvert.rb', line 14

def initialize(options)
  @common = IsoDoc::Gb::Common.new(options)
  @standardclassimg = options[:standardclassimg]
  @libdir = File.dirname(__FILE__)
  super
  @lang = "zh"
  @script = "Hans"
end

Instance Method Details

#annex_name(annex, name, div) ⇒ Object



183
184
185
186
187
188
# File 'lib/isodoc/gb/gbwordrender.rb', line 183

def annex_name(annex, name, div)
  div.h1 **{ class: "Annex" } do |t|
    t << "#{get_anchors[annex['id']][:label]}<br/><br/>"
    name&.children&.each { |c2| parse(c2, t) }
  end
end

#clause_name(num, title, div, header_class) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/isodoc/gb/gbwordrender.rb', line 160

def clause_name(num, title, div, header_class)
  header_class = {} if header_class.nil?
  div.h1 **attr_code(header_class) do |h1|
    if num && !@suppressheadingnumbers
      h1 << "#{num}."
      h1 << "&#x3000;"
    end
    h1 << title
  end
  div.parent.at(".//h1")
end

#clause_parse_title(node, div, c1, out) ⇒ Object



172
173
174
175
176
177
178
179
180
181
# File 'lib/isodoc/gb/gbwordrender.rb', line 172

def clause_parse_title(node, div, c1, out)
  if node["inline-header"] == "true"
    inline_header_title(out, node, c1)
  else
    div.send "h#{get_anchors[node['id']][:level]}" do |h|
      h << "#{get_anchors[node['id']][:label]}.&#x3000;" if !@suppressheadingnumbers
      c1 && c1.children.each { |c2| parse(c2, h) }
    end
  end
end

#cleanup(docxml) ⇒ Object



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

def cleanup(docxml)
  @cleanup = Cleanup.new(@script, @deprecated_lbl)
  super
  @cleanup.cleanup(docxml)
  docxml
end

#default_file_locations(options) ⇒ Object



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

def default_file_locations(options)
  {   
    wordstylesheet: html_doc_path("wordstyle.scss"),
    standardstylesheet: html_doc_path("gb.scss"),
    header: html_doc_path("header.html"),
    wordcoverpage: html_doc_path("word_gb_titlepage.html"),
    wordintropage: html_doc_path("word_gb_intro.html"),
    ulstyle: "l7",
    olstyle: "l10",
  }
end

#default_fonts(options) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/isodoc/gb/gbwordconvert.rb', line 23

def default_fonts(options)
  script = options[:script] || "Hans"
  scope = options[:scope] || "national"
  {
    bodyfont: (script == "Hans" ? '"SimSun",serif' : '"Cambria",serif'),
    headerfont: (script == "Hans" ? '"SimHei",sans-serif' : '"Calibri",sans-serif'),
    monospacefont: '"Courier New",monospace',
    titlefont: (scope == "national" ? (script != "Hans" ? '"Cambria",serif' : '"SimSun",serif' ) :
                (script == "Hans" ? '"SimHei",sans-serif' : '"Calibri",sans-serif' ))
  }     
end

#deprecated_term_parse(node, out) ⇒ Object



113
114
115
116
117
118
# File 'lib/isodoc/gb/gbwordrender.rb', line 113

def deprecated_term_parse(node, out) 
  out.p **{ class: "DeprecatedTerms" } do |p|
    p << l10n("#{@deprecated_lbl}: ")
    node.children.each { |c| parse(c, p) }
  end
end

#end_line(_isoxml, out) ⇒ Object



90
91
92
# File 'lib/isodoc/gb/gbwordrender.rb', line 90

def end_line(_isoxml, out)
  out.hr **{ width: "25%" }
end

#error_parse(node, out) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/isodoc/gb/gbwordrender.rb', line 94

def error_parse(node, out)
  # catch elements not defined in ISO
  case node.name
  when "string" then string_parse(node, out)
  else
    super
  end
end

#example_cleanup(docxml) ⇒ Object



72
73
74
75
# File 'lib/isodoc/gb/gbwordconvert.rb', line 72

def example_cleanup(docxml)
  super
  @cleanup.example_cleanup(docxml)
end

#example_parse(node, out) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/isodoc/gb/gbwordrender.rb', line 42

def example_parse(node, out)
  out.table **attr_code(id: node["id"], class: "example") do |t|
    t.tr do |tr|
      tr.td **EXAMPLE_TBL_ATTR do |td|
        td << l10n(example_label(node) + ":")
      end
      tr.td **{ valign: "top", class: "example" } do |td|
        node.children.each { |n| parse(n, td) }
      end
    end
  end
end

#extract_fonts(options) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/isodoc/gb/gbwordconvert.rb', line 47

def extract_fonts(options)
  b = options[:bodyfont] || "Arial"
  h = options[:headerfont] || "Arial"
  m = options[:monospacefont] || "Courier"
  t = options[:titlefont] || "Arial"
  "$bodyfont: #{b};\n$headerfont: #{h};\n$monospacefont: #{m};\n$titlefont: #{t};\n"
end

#foreword(isoxml, out) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/isodoc/gb/gbwordrender.rb', line 148

def foreword(isoxml, out)
  f = isoxml.at(ns("//foreword")) || return
  page_break(out)
  out.div do |s|
    s.h1 **{ class: "ForewordTitle" } do |h1|
      h1 << "#{@foreword_lbl}&nbsp;"
      # insert_tab(h1, 1)
    end
    f.elements.each { |e| parse(e, s) unless e.name == "title" }
  end
end

#formula_dl_parse(node, out) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/isodoc/gb/gbwordrender.rb', line 22

def formula_dl_parse(node, out)
  out.table **{ class: "dl" } do |v|
    node.elements.each_slice(2) do |dt, dd|
      v.tr do |tr|
        tr.td **{ valign: "top", align: "left" } do |term|
          dt_parse(dt, term)
        end
        tr.td(**{ valign: "top" }) { |td| td << "&mdash;" }
        tr.td **{ valign: "top" } do |listitem|
          dd.children.each { |n| parse(n, listitem) }
        end
      end
    end
  end
end

#formula_parse(node, out) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/isodoc/gb/gbwordrender.rb', line 6

def formula_parse(node, out)
  out.div **attr_code(id: node["id"], class: "formula") do |div|
    insert_tab(div, 1)
    parse(node.at(ns("./stem")), out)
    insert_tab(div, 1)
    div << "(#{get_anchors[node['id']][:label]})"
  end
  formula_where(node.at(ns("./dl")), out)
end

#formula_where(dl, out) ⇒ Object



16
17
18
19
20
# File 'lib/isodoc/gb/gbwordrender.rb', line 16

def formula_where(dl, out)
  return unless dl
  out.p { |p| p << @where_lbl }
  formula_dl_parse(dl, out)
end

#i18n_init(lang, script) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/isodoc/gb/gbwordconvert.rb', line 77

def i18n_init(lang, script)
  super
  y = if lang == "en"
        YAML.load_file(File.join(File.dirname(__FILE__), "i18n-en.yaml"))
      elsif lang == "zh" && script == "Hans"
        YAML.load_file(File.join(File.dirname(__FILE__),
                                 "i18n-zh-Hans.yaml"))
      else
        YAML.load_file(File.join(File.dirname(__FILE__), "i18n-zh-Hans.yaml"))
      end
  @labels = @labels.merge(y)
end

#metadata_init(lang, script, labels) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/isodoc/gb/gbwordconvert.rb', line 55

def (lang, script, labels)
  unless ["en", "zh"].include? lang
    lang = "zh"
    script = "Hans"
  end
  @meta = Metadata.new(lang, script, labels)
  @meta.set(:standardclassimg, @standardclassimg)
  @common.meta = @meta
end

#middle(isoxml, out) ⇒ Object



85
86
87
88
# File 'lib/isodoc/gb/gbwordrender.rb', line 85

def middle(isoxml, out)
  super
  end_line(isoxml, out)
end

#note_parse(node, out) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/isodoc/gb/gbwordrender.rb', line 55

def note_parse(node, out)
  @note = true
  out.table **attr_code(id: node["id"], class: "Note") do |t|
    t.tr do |tr|
      tr.td **EXAMPLE_TBL_ATTR do |td|
        td << l10n(note_label(node) + ":")
      end
      tr.td **{ valign: "top", class: "Note" } do |td|
        node.children.each { |n| parse(n, td) }
      end
    end
  end
  @note = false
end

#omit_docid_prefix(prefix) ⇒ Object



110
111
112
# File 'lib/isodoc/gb/gbwordconvert.rb', line 110

def omit_docid_prefix(prefix)
  super || prefix == "Chinese Standard"
end

#populate_template(docxml, format) ⇒ Object



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

def populate_template(docxml, format)
  meta = @meta.get.merge(@labels)
   = @common.(meta[:gbprefix], meta[:gbscope], format)
  logofile = @meta.(meta[:gbprefix])
  @files_to_delete << logofile + ".gif" unless logofile.nil?
  docxml = termref_resolve(docxml)
  meta[:standard_agency_formatted] =
    @common.format_agency(meta[:standard_agency], format)
  meta[:standard_logo] = 
  template = Liquid::Template.parse(docxml)
  template.render(meta.map { |k, v| [k.to_s, v] }.to_h)
end

#string_parse(node, out) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/isodoc/gb/gbwordrender.rb', line 103

def string_parse(node, out)
  if node["script"] == "Hant"
    out.span **{ class: "Hant" } do |s|
      node.children.each { |c| parse(c, s) }
    end
  else
    node.children.each { |c| parse(c, out) }
  end
end

#term_defs_boilerplate(div, source, term, preface) ⇒ Object



190
191
192
193
194
# File 'lib/isodoc/gb/gbwordrender.rb', line 190

def term_defs_boilerplate(div, source, term, preface)
  (source.empty? && term.nil?) and div << @no_terms_boilerplate or
    div << term_defs_boilerplate_cont(source, term)
  #div << @term_def_boilerplate unless preface
end

#termnote_parse(node, out) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/isodoc/gb/gbwordrender.rb', line 70

def termnote_parse(node, out)
  @note = true
  out.table **attr_code(id: node["id"], class: "Note") do |t|
    t.tr do |tr|
      tr.td **EXAMPLE_TBL_ATTR do |td|
        td << l10n("#{get_anchors[node['id']][:label]}:")
      end
      tr.td **{ valign: "top", class: "Note" } do |td|
        node.children.each { |n| parse(n, td) }
      end
    end
  end
  @note = false
end

#termref_render(x) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/isodoc/gb/gbwordrender.rb', line 120

def termref_render(x)
  parts = x.split(%r{(\s*\[MODIFICATION\]|,)}m)
  parts[1] = l10n(", #{@source_lbl}") if parts.size > 1 && parts[1] == ","
  parts.map do |p|
    /\s*\[MODIFICATION\]/.match(p) ? l10n(", #{@modified_lbl} &mdash; ") : p
  end.join.sub(/\A\s*/m, l10n("[")).sub(/\s*\z/m, l10n("]"))
end

#termref_resolve(docxml) ⇒ Object



128
129
130
131
132
133
# File 'lib/isodoc/gb/gbwordrender.rb', line 128

def termref_resolve(docxml)
  docxml.split(%r{(\[TERMREF\]|\[/TERMREF\])}).each_slice(4).
    map do |a|
    a.size < 3 ? a[0] : a[0] + termref_render(a[2])
  end.join
end

#word_cleanup(docxml) ⇒ Object



103
104
105
106
107
108
# File 'lib/isodoc/gb/gbwordconvert.rb', line 103

def word_cleanup(docxml)
  word_preface(docxml)
  word_annex_cleanup(docxml)
  @cleanup.title_cleanup(docxml.at('//div[@class="WordSection2"]'))
  docxml
end