asciidoctor-gb 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,12 @@
1
1
  module IsoDoc
2
2
  module Gb
3
- class Convert < IsoDoc::Convert
3
+ class Cleanup
4
+ def initialize(script, deprecated_lbl)
5
+ @script = script
6
+ @deprecated_lbl = deprecated_lbl
7
+ end
8
+
4
9
  def cleanup(docxml)
5
- super
6
10
  terms_cleanup(docxml)
7
11
  formula_cleanup(docxml)
8
12
  title_cleanup(docxml)
@@ -17,7 +21,6 @@ module IsoDoc
17
21
  end
18
22
 
19
23
  def example_cleanup(docxml)
20
- super
21
24
  docxml.xpath("//table[@class = 'Note']//p[not(@class)]").each do |p|
22
25
  p["class"] = "Note"
23
26
  end
@@ -1,110 +1,20 @@
1
1
  require "isodoc"
2
- require_relative "./i18n.rb"
3
2
  require_relative "./gbcleanup.rb"
4
- require_relative "./xref_gen.rb"
5
3
  require_relative "./metadata.rb"
6
- require_relative "./agencies.rb"
7
- require_relative "./section_output.rb"
8
- require_relative "./block_output.rb"
9
4
 
10
5
  module IsoDoc
11
6
  module Gb
12
7
  # A {Converter} implementation that generates GB output, and a document
13
8
  # schema encapsulation of the document for validation
14
- class Convert < IsoDoc::Convert
15
- def default_fonts(options)
16
- script = options[:script] || "Hans"
17
- b = options[:bodyfont] ||
18
- (script == "Hans" ? '"SimSun",serif' :
19
- script == "Latn" ? '"Cambria",serif' : '"SimSun",serif' )
20
- h = options[:headerfont] ||
21
- (script == "Hans" ? '"SimHei",sans-serif' :
22
- script == "Latn" ? '"Calibri",sans-serif' : '"SimHei",sans-serif' )
23
- m = options[:monospacefont] || '"Courier New",monospace'
24
- scope = options[:scope] || "national"
25
- t = options[:titlefont] ||
26
- (scope == "national" ? (script != "Hans" ? '"Cambria",serif' : '"SimSun",serif' ) :
27
- (script == "Hans" ? '"SimHei",sans-serif' : '"Calibri",sans-serif' ))
28
- "$bodyfont: #{b};\n$headerfont: #{h};\n$monospacefont: #{m};\n$titlefont: #{t};\n"
29
- end
30
-
9
+ class Common < IsoDoc::Common
31
10
  def initialize(options)
32
- super
33
- @htmlstylesheet = generate_css(html_doc_path("htmlstyle.scss"), true, default_fonts(options))
34
- @standardstylesheet = generate_css(html_doc_path("gb.scss"), true, default_fonts(options))
35
- @htmlcoverpage = html_doc_path("html_gb_titlepage.html")
36
- @htmlintropage = html_doc_path("html_gb_intro.html")
37
- @scripts = html_doc_path("scripts.html")
38
- end
39
-
40
- def html_doc_path(file)
41
- File.join(File.dirname(__FILE__), File.join("html", file))
42
- end
43
-
44
- def middle(isoxml, out)
45
- super
46
- end_line(isoxml, out)
47
- end
48
-
49
- def end_line(_isoxml, out)
50
- out.hr **{ width: "25%" }
51
- end
52
-
53
- def error_parse(node, out)
54
- # catch elements not defined in ISO
55
- case node.name
56
- when "string" then string_parse(node, out)
57
- else
58
- super
59
- end
60
- end
61
-
62
- def string_parse(node, out)
63
- if node["script"] == "Hant"
64
- out.span **{ class: "Hant" } do |s|
65
- node.children.each { |c| parse(c, s) }
66
- end
67
- else
68
- node.children.each { |c| parse(c, out) }
69
- end
11
+ @meta = options[:meta]
70
12
  end
71
13
 
72
14
  def fileloc(loc)
73
15
  File.join(File.dirname(__FILE__), loc)
74
16
  end
75
17
 
76
- def deprecated_term_parse(node, out)
77
- out.p **{ class: "DeprecatedTerms" } do |p|
78
- p << l10n("#{@deprecated_lbl}: ")
79
- node.children.each { |c| parse(c, p) }
80
- end
81
- end
82
-
83
- def local_logo_suffix(scope)
84
- return "" if scope != "local"
85
- local = get_metadata[:docidentifier][2,2]
86
- "<span style='font-weight:bold'>#{local}</span>"
87
- end
88
-
89
- def format_logo(prefix, scope, _format)
90
- return "" if %w(enterprise social-group).include? scope
91
- logo = standard_logo(prefix)
92
- if logo.nil?
93
- "<span style='font-size:36pt;font-weight:bold'>#{prefix}</span>"
94
- else
95
- format_logo1(logo, prefix, scope)
96
- end
97
- end
98
-
99
- def format_logo1(logo, prefix, scope)
100
- logo += ".gif"
101
- system "cp #{fileloc(File.join('html/gb-logos', logo))} #{logo}"
102
- local = local_logo_suffix(scope)
103
- @files_to_delete << logo
104
- "<img width='113' height='56' src='#{logo}' alt='#{prefix}'></img>"\
105
- "#{local}"
106
- end
107
-
108
18
  def format_agency(agency, format)
109
19
  return agency unless agency.is_a?(Array)
110
20
  if agency == ["中华人民共和国国家质量监督检验检疫总局", "中国国家标准化管理委员会"]
@@ -123,30 +33,30 @@ module IsoDoc
123
33
  ret
124
34
  end
125
35
 
126
- def termref_render(x)
127
- parts = x.split(%r{(\s*\[MODIFICATION\]|,)}m)
128
- parts[1] = l10n(", #{@source_lbl}") if parts.size > 1 && parts[1] == ","
129
- parts.map do |p|
130
- /\s*\[MODIFICATION\]/.match?(p) ? l10n(", #{@modified_lbl} &mdash; ") : p
131
- end.join.sub(/\A\s*/m, l10n("[")).sub(/\s*\z/m, l10n("]"))
36
+ def format_logo(prefix, scope, _format)
37
+ return "" if %w(enterprise social-group).include? scope
38
+ logo = @meta.standard_logo(prefix)
39
+ if logo.nil?
40
+ "<span style='font-size:36pt;font-weight:bold'>#{prefix}</span>"
41
+ else
42
+ format_logo1(logo, prefix, scope)
43
+ end
132
44
  end
133
45
 
134
- def termref_resolve(docxml)
135
- docxml.split(%r{(\[TERMREF\]|\[/TERMREF\])}).each_slice(4).
136
- map do |a|
137
- a.size < 3 ? a[0] : a[0] + termref_render(a[2])
138
- end.join
46
+ def local_logo_suffix(scope)
47
+ return "" if scope != "local"
48
+ local = @meta.get[:docidentifier][2,2]
49
+ "<span style='font-weight:bold'>#{local}</span>"
139
50
  end
140
51
 
141
- def populate_template(docxml, format)
142
- meta = get_metadata.merge(@labels)
143
- logo = format_logo(meta[:gbprefix], meta[:gbscope], format)
144
- docxml = termref_resolve(docxml)
145
- meta[:standard_agency_formatted] =
146
- format_agency(meta[:standard_agency], format)
147
- meta[:standard_logo] = logo
148
- template = Liquid::Template.parse(docxml)
149
- template.render(meta.map { |k, v| [k.to_s, v] }.to_h)
52
+
53
+ def format_logo1(logo, prefix, scope)
54
+ logo += ".gif"
55
+ system "cp #{fileloc(File.join('html/gb-logos', logo))} #{logo}"
56
+ local = local_logo_suffix(scope)
57
+ #@files_to_delete << logo
58
+ "<img width='113' height='56' src='#{logo}' alt='#{prefix}'></img>"\
59
+ "#{local}"
150
60
  end
151
61
  end
152
62
  end
@@ -0,0 +1,73 @@
1
+ require_relative "gbconvert"
2
+ require_relative "agencies"
3
+ require_relative "gbcleanup"
4
+ require_relative "metadata"
5
+ require_relative "gbhtmlrender"
6
+
7
+ module IsoDoc
8
+ module Gb
9
+ # A {Converter} implementation that generates GB output, and a document
10
+ # schema encapsulation of the document for validation
11
+ class Convert < IsoDoc::Convert
12
+ def default_fonts(options)
13
+ script = options[:script] || "Hans"
14
+ b = options[:bodyfont] ||
15
+ (script == "Hans" ? '"SimSun",serif' :
16
+ script == "Latn" ? '"Cambria",serif' : '"SimSun",serif' )
17
+ h = options[:headerfont] ||
18
+ (script == "Hans" ? '"SimHei",sans-serif' :
19
+ script == "Latn" ? '"Calibri",sans-serif' : '"SimHei",sans-serif' )
20
+ m = options[:monospacefont] || '"Courier New",monospace'
21
+ scope = options[:scope] || "national"
22
+ t = options[:titlefont] ||
23
+ (scope == "national" ? (script != "Hans" ? '"Cambria",serif' : '"SimSun",serif' ) :
24
+ (script == "Hans" ? '"SimHei",sans-serif' : '"Calibri",sans-serif' ))
25
+ "$bodyfont: #{b};\n$headerfont: #{h};\n$monospacefont: #{m};\n$titlefont: #{t};\n"
26
+ end
27
+
28
+ def initialize(options)
29
+ @common = IsoDoc::Gb::Common.new(options)
30
+ super
31
+ @htmlstylesheet = generate_css(html_doc_path("htmlstyle.scss"), true, default_fonts(options))
32
+ @standardstylesheet = generate_css(html_doc_path("gb.scss"), true, default_fonts(options))
33
+ @htmlcoverpage = html_doc_path("html_gb_titlepage.html")
34
+ @htmlintropage = html_doc_path("html_gb_intro.html")
35
+ @scripts = html_doc_path("scripts.html")
36
+ end
37
+
38
+ def metadata_init(lang, script, labels)
39
+ @meta = Metadata.new(lang, script, labels)
40
+ @common = IsoDoc::Gb::Common.new(meta: @meta)
41
+ end
42
+
43
+ def cleanup(docxml)
44
+ @cleanup = Cleanup.new(@script, @deprecated_lbl)
45
+ super
46
+ @cleanup.cleanup(docxml)
47
+ docxml
48
+ end
49
+
50
+ def example_cleanup(docxml)
51
+ super
52
+ @cleanup.example_cleanup(docxml)
53
+ end
54
+
55
+ def html_doc_path(file)
56
+ File.join(File.dirname(__FILE__), File.join("html", file))
57
+ end
58
+
59
+ def i18n_init(lang, script)
60
+ super
61
+ y = if lang == "en"
62
+ YAML.load_file(File.join(File.dirname(__FILE__), "i18n-en.yaml"))
63
+ elsif lang == "zh" && script == "Hans"
64
+ YAML.load_file(File.join(File.dirname(__FILE__),
65
+ "i18n-zh-Hans.yaml"))
66
+ else
67
+ YAML.load_file(File.join(File.dirname(__FILE__), "i18n-zh-Hans.yaml"))
68
+ end
69
+ @labels = @labels.merge(y)
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,206 @@
1
+ module IsoDoc
2
+ module Gb
3
+ # A {Converter} implementation that generates GB output, and a document
4
+ # schema encapsulation of the document for validation
5
+ class Convert < IsoDoc::Convert
6
+ def formula_parse(node, out)
7
+ out.div **attr_code(id: node["id"], class: "formula") do |div|
8
+ insert_tab(div, 1)
9
+ parse(node.at(ns("./stem")), out)
10
+ insert_tab(div, 1)
11
+ div << "(#{get_anchors[node['id']][:label]})"
12
+ end
13
+ formula_where(node.at(ns("./dl")), out)
14
+ end
15
+
16
+ def formula_where(dl, out)
17
+ return unless dl
18
+ out.p { |p| p << @where_lbl }
19
+ formula_dl_parse(dl, out)
20
+ end
21
+
22
+ def formula_dl_parse(node, out)
23
+ out.table **{ class: "dl" } do |v|
24
+ node.elements.each_slice(2) do |dt, dd|
25
+ v.tr do |tr|
26
+ tr.td **{ valign: "top", align: "left" } do |term|
27
+ dt_parse(dt, term)
28
+ end
29
+ tr.td(**{ valign: "top" }) { |td| td << "&mdash;" }
30
+ tr.td **{ valign: "top" } do |listitem|
31
+ dd.children.each { |n| parse(n, listitem) }
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ EXAMPLE_TBL_ATTR =
39
+ { valign: "top", class: "example_label",
40
+ style: "padding:2pt 2pt 2pt 2pt" }.freeze
41
+
42
+ def example_parse(node, out)
43
+ out.table **attr_code(id: node["id"], class: "example") do |t|
44
+ t.tr do |tr|
45
+ tr.td **EXAMPLE_TBL_ATTR do |td|
46
+ td << l10n(example_label(node) + ":")
47
+ end
48
+ tr.td **{ valign: "top", class: "example" } do |td|
49
+ node.children.each { |n| parse(n, td) }
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ def note_parse(node, out)
56
+ @note = true
57
+ out.table **attr_code(id: node["id"], class: "Note") do |t|
58
+ t.tr do |tr|
59
+ tr.td **EXAMPLE_TBL_ATTR do |td|
60
+ td << l10n(note_label(node) + ":")
61
+ end
62
+ tr.td **{ valign: "top", class: "Note" } do |td|
63
+ node.children.each { |n| parse(n, td) }
64
+ end
65
+ end
66
+ end
67
+ @note = false
68
+ end
69
+
70
+ def termnote_parse(node, out)
71
+ @note = true
72
+ out.table **attr_code(id: node["id"], class: "Note") do |t|
73
+ t.tr do |tr|
74
+ tr.td **EXAMPLE_TBL_ATTR do |td|
75
+ td << l10n("#{get_anchors[node['id']][:label]}:")
76
+ end
77
+ tr.td **{ valign: "top", class: "Note" } do |td|
78
+ node.children.each { |n| parse(n, td) }
79
+ end
80
+ end
81
+ end
82
+ @note = false
83
+ end
84
+
85
+ def middle(isoxml, out)
86
+ super
87
+ end_line(isoxml, out)
88
+ end
89
+
90
+ def end_line(_isoxml, out)
91
+ out.hr **{ width: "25%" }
92
+ end
93
+
94
+ def error_parse(node, out)
95
+ # catch elements not defined in ISO
96
+ case node.name
97
+ when "string" then string_parse(node, out)
98
+ else
99
+ super
100
+ end
101
+ end
102
+
103
+ def string_parse(node, out)
104
+ if node["script"] == "Hant"
105
+ out.span **{ class: "Hant" } do |s|
106
+ node.children.each { |c| parse(c, s) }
107
+ end
108
+ else
109
+ node.children.each { |c| parse(c, out) }
110
+ end
111
+ end
112
+
113
+ def deprecated_term_parse(node, out)
114
+ out.p **{ class: "DeprecatedTerms" } do |p|
115
+ p << l10n("#{@deprecated_lbl}: ")
116
+ node.children.each { |c| parse(c, p) }
117
+ end
118
+ end
119
+
120
+ def termref_render(x)
121
+ parts = x.split(%r{(\s*\[MODIFICATION\]|,)}m)
122
+ parts[1] = l10n(", #{@source_lbl}") if parts.size > 1 && parts[1] == ","
123
+ parts.map do |p|
124
+ /\s*\[MODIFICATION\]/.match?(p) ? l10n(", #{@modified_lbl} &mdash; ") : p
125
+ end.join.sub(/\A\s*/m, l10n("[")).sub(/\s*\z/m, l10n("]"))
126
+ end
127
+
128
+ def termref_resolve(docxml)
129
+ docxml.split(%r{(\[TERMREF\]|\[/TERMREF\])}).each_slice(4).
130
+ map do |a|
131
+ a.size < 3 ? a[0] : a[0] + termref_render(a[2])
132
+ end.join
133
+ end
134
+
135
+ def populate_template(docxml, format)
136
+ meta = @meta.get.merge(@labels)
137
+ logo = @common.format_logo(meta[:gbprefix], meta[:gbscope], format)
138
+ logofile = @meta.standard_logo(meta[:gbprefix])
139
+ @files_to_delete << logofile + ".gif" unless logofile.nil?
140
+ docxml = termref_resolve(docxml)
141
+ meta[:standard_agency_formatted] =
142
+ @common.format_agency(meta[:standard_agency], format)
143
+ meta[:standard_logo] = logo
144
+ template = Liquid::Template.parse(docxml)
145
+ template.render(meta.map { |k, v| [k.to_s, v] }.to_h)
146
+ end
147
+
148
+ def foreword(isoxml, out)
149
+ f = isoxml.at(ns("//foreword")) || return
150
+ page_break(out)
151
+ out.div do |s|
152
+ s.h1 **{ class: "ForewordTitle" } do |h1|
153
+ h1 << "#{@foreword_lbl}&nbsp;"
154
+ # insert_tab(h1, 1)
155
+ end
156
+ f.elements.each { |e| parse(e, s) unless e.name == "title" }
157
+ end
158
+ end
159
+
160
+ def clause_name(num, title, div, header_class)
161
+ header_class = {} if header_class.nil?
162
+ div.h1 **attr_code(header_class) do |h1|
163
+ if num
164
+ h1 << num
165
+ h1 << "&#x3000;"
166
+ end
167
+ h1 << title
168
+ end
169
+ div.parent.at(".//h1")
170
+ end
171
+
172
+ def clause_parse_title(node, div, c1, out)
173
+ if node["inline-header"] == "true"
174
+ inline_header_title(out, node, c1)
175
+ else
176
+ div.send "h#{get_anchors[node['id']][:level]}" do |h|
177
+ h << "#{get_anchors[node['id']][:label]}.&#x3000;"
178
+ c1.children.each { |c2| parse(c2, h) }
179
+ end
180
+ end
181
+ end
182
+
183
+ def annex_name(annex, name, div)
184
+ div.h1 **{ class: "Annex" } do |t|
185
+ t << "#{get_anchors[annex['id']][:label]}<br/><br/>"
186
+ t << name.text
187
+ end
188
+ end
189
+
190
+ def term_defs_boilerplate(div, source, term, preface)
191
+ (source.empty? && term.nil?) and div << @no_terms_boilerplate or
192
+ div << term_defs_boilerplate_cont(source, term)
193
+ div << @term_def_boilerplate unless preface
194
+ end
195
+
196
+ def reference_names(ref)
197
+ isopub = ref.at(ns(ISO_PUBLISHER_XPATH))
198
+ docid = ref.at(ns("./docidentifier"))
199
+ date = ref.at(ns("./date[@type = 'published']"))
200
+ reference = format_ref(docid.text, isopub, date)
201
+ @anchors[ref["id"]] = { xref: reference }
202
+ end
203
+ end
204
+ end
205
+ end
206
+