Class: IsoDoc::Gb::HtmlConvert
- Inherits:
-
HtmlConvert
- Object
- HtmlConvert
- IsoDoc::Gb::HtmlConvert
- Defined in:
- lib/isodoc/gb/gbhtmlrender.rb,
lib/isodoc/gb/gbhtmlconvert.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
Instance Method Summary collapse
- #annex_name(annex, name, div) ⇒ Object
- #clause_name(num, title, div, header_class) ⇒ Object
- #clause_parse_title(node, div, c1, out) ⇒ Object
- #cleanup(docxml) ⇒ Object
- #default_file_locations(options) ⇒ Object
- #default_fonts(options) ⇒ Object
- #deprecated_term_parse(node, out) ⇒ Object
- #end_line(_isoxml, out) ⇒ Object
- #error_parse(node, out) ⇒ Object
- #example_cleanup(docxml) ⇒ Object
- #example_parse(node, out) ⇒ Object
- #extract_fonts(options) ⇒ Object
- #foreword(isoxml, out) ⇒ Object
- #formula_dl_parse(node, out) ⇒ Object
- #formula_parse(node, out) ⇒ Object
- #formula_where(dl, out) ⇒ Object
- #i18n_init(lang, script) ⇒ Object
-
#initialize(options) ⇒ HtmlConvert
constructor
A new instance of HtmlConvert.
- #metadata_init(lang, script, labels) ⇒ Object
- #middle(isoxml, out) ⇒ Object
- #note_parse(node, out) ⇒ Object
- #populate_template(docxml, format) ⇒ Object
- #reference_names(ref) ⇒ Object
- #string_parse(node, out) ⇒ Object
- #term_defs_boilerplate(div, source, term, preface) ⇒ Object
- #termnote_parse(node, out) ⇒ Object
- #termref_render(x) ⇒ Object
- #termref_resolve(docxml) ⇒ Object
Constructor Details
#initialize(options) ⇒ HtmlConvert
Returns a new instance of HtmlConvert.
12 13 14 15 16 17 18 19 |
# File 'lib/isodoc/gb/gbhtmlconvert.rb', line 12 def initialize() @common = IsoDoc::Gb::Common.new() @standardclassimg = [: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/gbhtmlrender.rb', line 183 def annex_name(annex, name, div) div.h1 **{ class: "Annex" } do |t| t << "#{get_anchors[annex['id']][:label]}<br/><br/>" t << name.text 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/gbhtmlrender.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 h1 << "#{num}." h1 << " " 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/gbhtmlrender.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]}. " c1.children.each { |c2| parse(c2, h) } end end end |
#cleanup(docxml) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/isodoc/gb/gbhtmlconvert.rb', line 60 def cleanup(docxml) @cleanup = Cleanup.new(@script, @deprecated_lbl) super @cleanup.cleanup(docxml) docxml end |
#default_file_locations(options) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/isodoc/gb/gbhtmlconvert.rb', line 33 def default_file_locations() { htmlstylesheet: [:compliant] ? html_doc_path("htmlcompliantstyle.scss") : html_doc_path("htmlstyle.scss"), htmlcoverpage: html_doc_path("html_compliant_gb_titlepage.html"), htmlintropage: html_doc_path("html_gb_intro.html"), scripts: html_doc_path("scripts.html"), } end |
#default_fonts(options) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/isodoc/gb/gbhtmlconvert.rb', line 21 def default_fonts() script = [:script] || "Hans" scope = [: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/gbhtmlrender.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/gbhtmlrender.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/gbhtmlrender.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
67 68 69 70 |
# File 'lib/isodoc/gb/gbhtmlconvert.rb', line 67 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/gbhtmlrender.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
42 43 44 45 46 47 48 |
# File 'lib/isodoc/gb/gbhtmlconvert.rb', line 42 def extract_fonts() b = [:bodyfont] || "Arial" h = [:headerfont] || "Arial" m = [:monospacefont] || "Courier" t = [: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/gbhtmlrender.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} " # 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/gbhtmlrender.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 << "—" } 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/gbhtmlrender.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/gbhtmlrender.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
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/isodoc/gb/gbhtmlconvert.rb', line 72 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
50 51 52 53 54 55 56 57 58 |
# File 'lib/isodoc/gb/gbhtmlconvert.rb', line 50 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 end |
#middle(isoxml, out) ⇒ Object
85 86 87 88 |
# File 'lib/isodoc/gb/gbhtmlrender.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/gbhtmlrender.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 |
#populate_template(docxml, format) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/isodoc/gb/gbhtmlrender.rb', line 135 def populate_template(docxml, format) = @meta.get.merge(@labels) logo = @common.format_logo([:gbprefix], [:gbscope], format) logofile = @meta.standard_logo([:gbprefix]) @files_to_delete << logofile + ".gif" unless logofile.nil? docxml = termref_resolve(docxml) [:standard_agency_formatted] = @common.format_agency([:standard_agency], format) [:standard_logo] = logo template = Liquid::Template.parse(docxml) template.render(.map { |k, v| [k.to_s, v] }.to_h) end |
#reference_names(ref) ⇒ Object
198 199 200 201 202 203 204 205 |
# File 'lib/isodoc/gb/gbhtmlrender.rb', line 198 def reference_names(ref) isopub = ref.at(ns(ISO_PUBLISHER_XPATH)) docid = ref.at(ns("./docidentifier")) date = ref.at(ns("./date[@type = 'published']")) allparts = ref.at(ns("./allparts")) reference = format_ref(docid.text, isopub, date, allparts) @anchors[ref["id"]] = { xref: reference } end |
#string_parse(node, out) ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/isodoc/gb/gbhtmlrender.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 195 196 |
# File 'lib/isodoc/gb/gbhtmlrender.rb', line 190 def term_defs_boilerplate(div, source, term, preface) unless preface (source.empty? && term.nil?) and div << @no_terms_boilerplate or div << term_defs_boilerplate_cont(source, term) end #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/gbhtmlrender.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/gbhtmlrender.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} — ") : 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/gbhtmlrender.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 |