Class: IsoDoc::Convert

Inherits:
Object
  • Object
show all
Defined in:
lib/isodoc/html.rb,
lib/isodoc/i18n.rb,
lib/isodoc/lists.rb,
lib/isodoc/table.rb,
lib/isodoc/terms.rb,
lib/isodoc/utils.rb,
lib/isodoc/blocks.rb,
lib/isodoc/cleanup.rb,
lib/isodoc/convert.rb,
lib/isodoc/comments.rb,
lib/isodoc/metadata.rb,
lib/isodoc/xref_gen.rb,
lib/isodoc/footnotes.rb,
lib/isodoc/references.rb,
lib/isodoc/xref_sect_gen.rb

Direct Known Subclasses

Iso::Convert, Iso::WordConvert, WordConvert

Constant Summary collapse

MATHJAX_ADDR =
"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js".freeze
MATHJAX =
"<script type=\"text/x-mathjax-config\">\n  MathJax.Hub.Config({\n    asciimath2jax: {\n      delimiters: [['OPEN', 'CLOSE']]\n    }\n });\n</script>\n<script src=\"\#{MATHJAX_ADDR}?config=AM_HTMLorMML\"></script>\n".freeze
OL_STYLE =
{
  arabic: "1",
  roman: "i",
  alphabet: "a",
  roman_upper: "I",
  alphabet_upper: "A",
}.freeze
SW =
"solid windowtext".freeze
STAGE_ABBRS =
{
  "00": "PWI",
  "10": "NWIP",
  "20": "WD",
  "30": "CD",
  "40": "DIS",
  "50": "FDIS",
  "60": "IS",
  "90": "(Review)",
  "95": "(Withdrawal)",
}.freeze
NOKOHEAD =
"95": "(Withdrawal)",
}.freeze

def stage_abbrev(stage, iter, draft)
  stage = STAGE_ABBRS[stage.to_sym] || "??"
  stage += iter.text if iter
  stage = "Pre" + stage if draft&.text =~ /^0\./
  stage
end

NOKOHEAD = "<!DOCTYPE html SYSTEM\n\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head> <title></title> <meta charset=\"UTF-8\" /> </head>\n<body> </body> </html>\n".freeze
CLAUSE_ANCESTOR =
".//ancestor::*[local-name() = 'annex' or "\
"local-name() = 'appendix' or local-name() = 'foreword' or "\
"local-name() = 'introduction' or local-name() = 'terms' or "\
"local-name() = 'clause' or local-name() = 'references']/@id".freeze
NOTE_CONTAINER_ANCESTOR =
".//ancestor::*[local-name() = 'annex' or "\
"local-name() = 'foreword' or local-name() = 'appendix' or "\
"local-name() = 'introduction' or local-name() = 'terms' or "\
"local-name() = 'clause' or local-name() = 'references' or "\
"local-name() = 'figure' or local-name() = 'formula' or "\
"local-name() = 'table' or local-name() = 'example']/@id".freeze
EXAMPLE_TBL_ATTR =
{ width: "110pt", valign: "top", class: "example_label",
style: "width:82.8pt;padding:.75pt .75pt .75pt .75pt" }.freeze
FIGURE_WITH_FOOTNOTES =
"//div[@class = 'figure'][descendant::aside]"\
"[not(descendant::div[@class = 'figure'])]".freeze
COMMENT_IN_COMMENT_LIST =
'//div[@style="mso-element:comment-list"]//'\
'span[@style="MsoCommentReference"]'.freeze
COMMENT_TARGET_XREFS =
"//span[@style='mso-special-character:comment']/@target".freeze
DATETYPES =
%w{published accessed created implemented obsoleted confirmed
updated issued}.freeze
SECTIONS_XPATH =
"//foreword | //introduction | //sections/terms | //annex | "\
"//sections/clause | //bibliography/references | "\
"//bibliography/clause".freeze
CHILD_NOTES_XPATH =
"./*[not(self::xmlns:clause) and "\
"not(self::xmlns:appendix)]//xmlns:note | ./xmlns:note".freeze
CHILD_EXAMPLES_XPATH =
"./*[not(self::xmlns:clause) and not(self::xmlns:appendix)]//"\
"xmlns:example | ./xmlns:example".freeze
ISO_PUBLISHER_XPATH =
"./contributor[xmlns:role/@type = 'publisher']/"\
"organization[abbreviation = 'ISO' or xmlns:abbreviation = 'IEC' or "\
"xmlns:name = 'International Organization for Standardization' or "\
"xmlns:name = 'International Electrotechnical Commission']".freeze
BIBLIOGRAPHY_XPATH =
"//bibliography/clause[title = 'Bibliography'] | "\
"//bibliography/references[title = 'Bibliography']".freeze

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Convert

htmlstylesheet: Generic stylesheet for HTML wordstylesheet: Generic stylesheet for Word standardsheet: Stylesheet specific to Standard header: Header file for Word htmlcoverpage: Cover page for HTML wordcoverpage: Cover page for Word htmlintropage: Introductory page for HTML wordintropage: Introductory page for Word i18nyaml: YAML file for internationalisation of text ulstyle: list style in Word CSS for unordered lists olstyle: list style in Word CSS for ordered lists



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/isodoc/convert.rb', line 34

def initialize(options)
  @htmlstylesheet = options[:htmlstylesheet]
  @wordstylesheet = options[:wordstylesheet]
  @standardstylesheet = options[:standardstylesheet]
  @header = options[:header]
  @htmlcoverpage = options[:htmlcoverpage]
  @wordcoverpage = options[:wordcoverpage]
  @htmlintropage = options[:htmlintropage]
  @wordintropage = options[:wordintropage]
  @scripts = options[:scripts]
  @i18nyaml = options[:i18nyaml]
  @ulstyle = options[:ulstyle]
  @olstyle = options[:olstyle]
  @termdomain = ""
  @termexample = false
  @note = false
  @sourcecode = false
  @anchors = {}
  @meta = {}
  
  @footnotes = []
  @comments = []
  @in_footnote = false
  @in_comment = false
  @in_table = false
  @in_figure = false
  @seen_footnote = Set.new
  @c = HTMLEntities.new
  @openmathdelim = "`"
  @closemathdelim = "`"
  @lang = "en"
  @script = "Latn"
  @files_to_delete = []
end

Instance Method Details

#admitted_term_parse(node, out) ⇒ Object



20
21
22
23
24
# File 'lib/isodoc/terms.rb', line 20

def admitted_term_parse(node, out)
  out.p **{ class: "AltTerms", style:"text-align:left;" } do |p|
    node.children.each { |c| parse(c, p) }
  end
end

#admonition_cleanup(docxml) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/isodoc/cleanup.rb', line 19

def admonition_cleanup(docxml)
  docxml.xpath("//div[@class = 'Admonition'][title]").each do |d|
    title = d.at("./title")
    n = title.next_element
    n&.children&.first&.add_previous_sibling(title.remove.text + "&mdash;")
  end
  docxml
end

#admonition_parse(node, out) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/isodoc/blocks.rb', line 131

def admonition_parse(node, out)
  name = node["type"]
  out.div **{ class: "Admonition" } do |t|
    t.title { |b| b << @admonition[name].upcase } if name
    node.children.each do |n|
      parse(n, t)
    end
  end
end

#agency(xml) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/isodoc/metadata.rb', line 99

def agency(xml)
  agency = ""
  xml.xpath(ns("//bibdata/contributor[xmlns:role/@type = 'publisher']/"\
               "organization")).each do |org|
    name = org&.at(ns("./name"))&.text
    abbrev = org&.at(ns("./abbreviation"))&.text
    agency1 = abbrev || name
    agency = iso?(org) ?  "ISO/#{agency}" : "#{agency}#{agency1}/"
  end
  (:agency, agency.sub(%r{/$}, ""))
end

#anchor_names(docxml) ⇒ Object

extract names for all anchors, xref and label



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

def anchor_names(docxml)
  initial_anchor_names(docxml)
  back_anchor_names(docxml)
  # preempt clause notes with all other types of note
  note_anchor_names(docxml.xpath(ns("//table | //example | //formula | "\
                                    "//figure")))
  note_anchor_names(docxml.xpath(ns(SECTIONS_XPATH)))
  example_anchor_names(docxml.xpath(ns(SECTIONS_XPATH)))
  list_anchor_names(docxml.xpath(ns(SECTIONS_XPATH)))
end

#anchor_struct(lbl, container, elem) ⇒ Object



151
152
153
154
155
156
157
158
# File 'lib/isodoc/xref_gen.rb', line 151

def anchor_struct(lbl, container, elem)
  ret = {}
  ret[:label] = anchor_struct_label(lbl, elem)
  ret[:xref] = anchor_struct_xref(lbl, elem)
  ret[:xref].gsub!(/ $/, "")
  ret[:container] = get_clause_id(container) unless container.nil?
  ret
end

#anchor_struct_label(lbl, elem) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/isodoc/xref_gen.rb', line 135

def anchor_struct_label(lbl, elem)
  case elem
  when @appendix_lbl then l10n("#{elem} #{lbl}")
  else
    lbl.to_s
  end
end

#anchor_struct_xref(lbl, elem) ⇒ Object



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

def anchor_struct_xref(lbl, elem)
  case elem
  when @formula_lbl then l10n("#{elem} (#{lbl})")
  else
    l10n("#{elem} #{lbl}")
  end
end

#annex_name_lbl(clause, num) ⇒ Object



72
73
74
75
76
# File 'lib/isodoc/xref_sect_gen.rb', line 72

def annex_name_lbl(clause, num)
  obl = l10n("(#{@inform_annex_lbl})")
  obl = l10n("(#{@norm_annex_lbl})") if clause["obligation"] == "normative"
  l10n("<b>#{@annex_lbl} #{num}</b><br/>#{obl}")
end

#annex_names(clause, num) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/isodoc/xref_sect_gen.rb', line 78

def annex_names(clause, num)
  @anchors[clause["id"]] = { label: annex_name_lbl(clause, num), 
                             xref: "#{@annex_lbl} #{num}", level: 1 }
  clause.xpath(ns("./clause")).each_with_index do |c, i|
    annex_names1(c, "#{num}.#{i + 1}", 2)
  end
  appendix_names(clause, num)
  hierarchical_asset_names(clause, num)
end

#annex_names1(clause, num, level) ⇒ Object



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

def annex_names1(clause, num, level)
  @anchors[clause["id"]] = { label: num, xref: num, level: level }
  clause.xpath(ns(".//clause")).each_with_index do |c, i|
    annex_names1(c, "#{num}.#{i + 1}", level + 1)
  end
end

#annotation_parse(node, out) ⇒ Object



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

def annotation_parse(node, out)
  @sourcecode = false
  @annotation = true
  out.span **{ class: "zzMoveToFollowing" } do |s|
    s  << "&lt;#{node.at(ns("//callout[@target='#{node['id']}']")).text}&gt; "
  end
  node.children.each { |n| parse(n, out) }
  @annotation = false
end

#appendix_names(clause, num) ⇒ Object



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

def appendix_names(clause, num)
  clause.xpath(ns("./appendix")).each_with_index do |c, i|
    @anchors[c["id"]] = anchor_struct(i + 1, nil, @appendix_lbl)
    @anchors[c["id"]][:level] = 2
    @anchors[c["id"]][:container] = clause["id"]
  end
end

#attr_code(attributes) ⇒ Object



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

def attr_code(attributes)
  attributes = attributes.reject { |_, val| val.nil? }.map
  attributes.map do |k, v|
    [k, (v.is_a? String) ? HTMLEntities.new.decode(v) : v]
  end.to_h
end

#author(xml, _out) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/isodoc/metadata.rb', line 25

def author(xml, _out)
  tc(xml)
  sc(xml)
  wg(xml)
  secretariat(xml)
  agency(xml)
end

#back_anchor_names(docxml) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/isodoc/xref_sect_gen.rb', line 3

def back_anchor_names(docxml)
  docxml.xpath(ns("//annex")).each_with_index do |c, i|
    annex_names(c, (65 + i).chr.to_s)
  end
  docxml.xpath(ns("//bibitem")).each do |ref|
    reference_names(ref)
  end
end

#bibdate(isoxml, _out) ⇒ Object



79
80
81
82
83
# File 'lib/isodoc/metadata.rb', line 79

def bibdate(isoxml, _out)
  isoxml.xpath(ns("//bibdata/date")).each do |d|
    ("#{d['type']}date".to_sym, date_range(d))
  end
end

#biblio_list(f, div, bibliography) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/isodoc/references.rb', line 96

def biblio_list(f, div, bibliography)
  bibitems = split_bibitems(f)
  bibitems[:iso].each_with_index do |b, i|
    iso_bibitem_entry(div, b, (i + 1), bibliography)
  end
  bibitems[:noniso].each_with_index do |b, i|
    noniso_bibitem(div, b, (i + 1 + bibitems[:iso].size), bibliography)
  end
end

#bibliography(isoxml, out) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/isodoc/references.rb', line 133

def bibliography(isoxml, out)
  f = isoxml.at(ns(BIBLIOGRAPHY_XPATH)) || return
  page_break(out)
  out.div do |div|
    div.h1 @bibliography_lbl, **{ class: "Section3" }
    f.elements.reject do |e|
      ["reference", "title", "bibitem"].include? e.name
    end.each { |e| parse(e, div) }
    biblio_list(f, div, true)
  end
end

#bibliography_parse(node, out) ⇒ Object



145
146
147
148
149
150
151
152
153
154
# File 'lib/isodoc/references.rb', line 145

def bibliography_parse(node, out)
  title = node&.at(ns("./title"))&.text || ""
  out.div do |div|
    div.h2 title, **{ class: "Section3" }
    node.elements.reject do |e|
      ["reference", "title", "bibitem"].include? e.name
    end.each { |e| parse(e, div) }
    biblio_list(node, div, true)
  end
end

#clause_names(docxml, sect_num) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/isodoc/xref_sect_gen.rb', line 35

def clause_names(docxml, sect_num)
  q = "//clause[parent::sections][not(xmlns:title = 'Scope')]"\
    "[not(descendant::terms)]"
  docxml.xpath(ns(q)).each_with_index do |c, i|
    section_names(c, (i + sect_num), 1)
  end
end

#cleanup(docxml) ⇒ Object



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

def cleanup(docxml)
  comment_cleanup(docxml)
  footnote_cleanup(docxml)
  inline_header_cleanup(docxml)
  figure_cleanup(docxml)
  table_cleanup(docxml)
  symbols_cleanup(docxml)
  example_cleanup(docxml)
  admonition_cleanup(docxml)
end

#comment_attributes(docxml, x) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/isodoc/comments.rb', line 85

def comment_attributes(docxml, x)
  fromlink = docxml.at("//*[@id='#{x['from']}']")
  return(nil) if fromlink.nil?
  tolink = docxml.at("//*[@id='#{x['to']}']") || fromlink
  target = docxml.at("//*[@id='#{x['target']}']")
  { from: fromlink, to: tolink, target: target }
end

#comment_cleanup(docxml) ⇒ Object



60
61
62
63
64
# File 'lib/isodoc/comments.rb', line 60

def comment_cleanup(docxml)
  move_comment_link_to_from(docxml)
  reorder_comments_by_comment_link(docxml)
  embed_comment_in_comment_list(docxml)
end


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

def comment_link_attrs(fn, node)
  { style: "MsoCommentReference", target: fn,
    class: "commentLink", from: node["from"],
    to: node["to"] }
end

#comments(div) ⇒ Object



9
10
11
12
13
14
# File 'lib/isodoc/comments.rb', line 9

def comments(div)
  return if @comments.empty?
  div.div **{ style: "mso-element:comment-list" } do |div1|
    @comments.each { |fn| div1.parent << fn }
  end
end

#compose_title(main, intro, part, partnum, subpartnum, lang) ⇒ Object



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

def compose_title(main, intro, part, partnum, subpartnum, lang)
  main = main.nil? ? "" : @c.encode(main.text, :hexadecimal)
  intro &&
    main = "#{@c.encode(intro.text, :hexadecimal)}&nbsp;&mdash; #{main}"
  if part
    suffix = @c.encode(part.text, :hexadecimal)
    partnum = "#{partnum}&ndash;#{subpartnum}" if partnum && subpartnum
    suffix = "#{part_label(lang)}&nbsp;#{partnum}: " + suffix if partnum
    main = "#{main}&nbsp;&mdash; #{suffix}"
  end
  main
end

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



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

def convert(filename, file = nil, debug = false)
  file = File.read(filename, encoding: "utf-8") if file.nil?
  @openmathdelim, @closemathdelim = extract_delims(file)
  docxml, filename, dir = convert_init(file, filename, debug)
  result = convert1(docxml, filename, dir)
  return result if debug
  postprocess(result, filename, dir)
end

#convert1(docxml, filename, dir) ⇒ Object



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

def convert1(docxml, filename, dir)
  anchor_names docxml
  noko do |xml|
    xml.html do |html|
      html.parent.add_namespace("epub", "http://www.idpf.org/2007/ops")
      define_head html, filename, dir
      make_body(html, docxml)
    end
  end.join("\n")
end

#convert_init(file, filename, debug) ⇒ Object



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

def convert_init(file, filename, debug)
  docxml = Nokogiri::XML(file)
  filename, dir = init_file(filename, debug)
  docxml.root.default_namespace = ""
  i18n_init(docxml&.at(ns("//bibdata/language"))&.text || "en",
            docxml&.at(ns("//bibdata/script"))&.text || "Latn")
  [docxml, filename, dir]
end

#date_note_process(b, ref) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/isodoc/references.rb', line 16

def date_note_process(b, ref)
  date_note = b.at(ns("./note[text()][contains(.,'ISO DATE:')]"))
  return if date_note.nil?
  date_note.content = date_note.content.gsub(/ISO DATE: /, "")
  date_note.children.first.replace("<p>#{date_note.content}</p>")
  footnote_parse(date_note, ref)
end

#date_range(date) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/isodoc/metadata.rb', line 69

def date_range(date)
  from = date.at(ns("./from"))
  to = date.at(ns("./to"))
  on = date.at(ns("./on"))
  return on.text if on
  ret = "#{from.text}&ndash;"
  ret += to.text if to
  ret
end

#definition_parse(node, out) ⇒ Object



3
4
5
# File 'lib/isodoc/terms.rb', line 3

def definition_parse(node, out)
  node.children.each { |n| parse(n, out) }
end

#deprecated_term_parse(node, out) ⇒ Object



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

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

#dl_parse(node, out) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/isodoc/lists.rb', line 64

def dl_parse(node, out)
  out.dl do |v|
    node.elements.select { |n| dt_dd? n }.each_slice(2) do |dt, dd|
      v.dt { |term| dt_parse(dt, term) }
      v.dd do |listitem|
        dd.children.each { |n| parse(n, listitem) }
      end
    end
  end
  node.elements.reject { |n| dt_dd? n }.each { |n| parse(n, out) }
end

#docid(isoxml, _out) ⇒ Object



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

def docid(isoxml, _out)
  dn = docnumber(isoxml)
  docstatus = [:stage]
  if docstatus
    abbr = [:stageabbr]
    docstatus = [:stage]
    (docstatus.to_i < 60) && dn = abbr + " " + dn
  end
  (:docnumber, dn)
end

#docid_l10n(x) ⇒ Object



3
4
5
6
# File 'lib/isodoc/references.rb', line 3

def docid_l10n(x)
  return x if x.nil?
  x.gsub(/All Parts/, @all_parts_lbl)
end

#docnumber(isoxml) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/isodoc/metadata.rb', line 111

def docnumber(isoxml)
  docnumber = isoxml.at(ns("//project-number"))
  partnumber = isoxml.at(ns("//project-number/@part"))
  subpartnumber = isoxml.at(ns("//project-number/@subpart"))
  dn = docnumber&.text || ""
  dn += "-#{partnumber.text}" if partnumber
  dn += "-#{subpartnumber.text}" if subpartnumber
  dn
end

#docstatus(isoxml, _out) ⇒ Object



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

def docstatus(isoxml, _out)
  docstatus = isoxml.at(ns("//status/stage"))
  if docstatus
    (:stage, docstatus.text)
    abbr = stage_abbrev(docstatus.text, isoxml.at(ns("//status/iteration")),
                        isoxml.at(ns("//version/draft")))
    (:stageabbr, abbr)
  end
end

#doctype(isoxml, _out) ⇒ Object



85
86
87
88
89
90
# File 'lib/isodoc/metadata.rb', line 85

def doctype(isoxml, _out)
  b = isoxml.at(ns("//bibdata")) || return
  return unless b["type"]
  t = b["type"].split(/-/).map{ |w| w.capitalize }.join(" ")
  (:doctype, t)
end

#draftinfo(draft, revdate) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/isodoc/metadata.rb', line 142

def draftinfo(draft, revdate)
  draftinfo = ""
  if draft
    draftinfo = " (#{@draft_lbl} #{draft}"
    draftinfo += ", #{revdate}" if revdate
    draftinfo += ")"
  end
  l10n(draftinfo)
end

#dt_dd?(n) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/isodoc/lists.rb', line 60

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

#dt_parse(dt, term) ⇒ Object



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

def dt_parse(dt, term)
  if dt.elements.empty?
    term.p **attr_code(class: note? ? "Note" : nil) do |p|
      p << dt.text
    end
  else
    dt.children.each { |n| parse(n, term) }
  end
end

#embed_comment_in_comment_list(docxml) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/isodoc/comments.rb', line 70

def embed_comment_in_comment_list(docxml)
  docxml.xpath(COMMENT_IN_COMMENT_LIST).each do |x|
    n = x.next_element
    n&.children&.first&.add_previous_sibling(x.remove)
  end
  docxml
end

#eref_localities1(type, from, to, lang = "en") ⇒ Object



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

def eref_localities1(type, from, to, lang = "en")
  subsection = from&.text&.match?(/\./)
  return l10n(eref_localities1_zh(type, from, to)) if lang == "zh"
  ret = ","
  loc = @locality[type] || type.sub(/^locality:/, "").capitalize
  ret += " #{loc}" unless subsection && type == "clause"
  ret += " #{from.text}" if from
  ret += "&ndash;#{to.text}" if to
  l10n(ret)
end

#eref_localities1_zh(type, from, to) ⇒ Object



67
68
69
70
71
72
# File 'lib/isodoc/i18n.rb', line 67

def eref_localities1_zh(type, from, to)
  ret = ", 第#{from.text}" if from
  ret += "&ndash;#{to}" if to
  ret += (@locality[type] || type.sub(/^locality:/, "").capitalize )
  ret
end

#example_anchor_names(sections) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/isodoc/xref_gen.rb', line 63

def example_anchor_names(sections)
  sections.each do |s|
    notes = s.xpath(CHILD_EXAMPLES_XPATH)
    notes.each_with_index do |n, i|
      next if @anchors[n["id"]]
      idx = notes.size == 1 ? "" : " #{i + 1}"
      @anchors[n["id"]] = anchor_struct(idx, s, @example_xref_lbl)
    end
    example_anchor_names(s.xpath(ns("./clause | ./appendix")))
  end
end

#example_cleanup(docxml) ⇒ Object



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

def example_cleanup(docxml)
  docxml.xpath("//table[@class = 'example']//p[not(@class)]").each do |p|
    p["class"] = "example"
  end
  docxml
end

#example_div_parse(node, out) ⇒ Object

used if we are boxing examples



81
82
83
84
85
86
87
88
# File 'lib/isodoc/blocks.rb', line 81

def example_div_parse(node, out)
  out.div **attr_code(id: node["id"], class: "example") do |div|
    out.p { |p| p << example_label(node) }
    node.children.each do |n|
      parse(n, div)
    end
  end
end

#example_label(node) ⇒ Object



70
71
72
73
74
# File 'lib/isodoc/blocks.rb', line 70

def example_label(node)
  n = get_anchors[node["id"]]
  return @example_lbl if n.nil? || n[:label].empty?
  l10n("#{@example_lbl} #{n[:label]}")
end

#example_parse(node, out) ⇒ Object



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

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 << example_label(node)
      end
      tr.td **{ valign: "top", class: "example" } do |td|
        node.children.each { |n| parse(n, td) }
      end
    end
  end
end

#extract_delims(text) ⇒ Object

avoid ‘; avoid {{ (Liquid Templates); avoid [[ (Javascript)



112
113
114
115
116
117
118
119
120
# File 'lib/isodoc/utils.rb', line 112

def extract_delims(text)
  @openmathdelim = "(#("
  @closemathdelim = ")#)"
  while text.include?(@openmathdelim) || text.include?(@closemathdelim)
    @openmathdelim += "("
    @closemathdelim += ")"
  end
  [@openmathdelim, @closemathdelim]
end

#extract_symbols_list(dl) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
# File 'lib/isodoc/cleanup.rb', line 173

def extract_symbols_list(dl)
  dl_out = []
  dl.xpath("./dt | ./dd").each do |dtd|
    if dtd.name == "dt"
      dl_out << { dt: dtd.remove, key: symbol_key(dtd) }
    else
      dl_out.last[:dd] = dtd.remove
    end
  end
  dl_out
end

#figure_aside_process(f, aside, key) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/isodoc/cleanup.rb', line 48

def figure_aside_process(f, aside, key)
  # get rid of footnote link, it is in diagram
  f&.at("./a[@class='TableFootnoteRef']")&.remove
  fnref = f.at(".//a[@class='TableFootnoteRef']")
  dt = key.add_child("<dt></dt>").first
  dd = key.add_child("<dd></dd>").first
  fnref.parent = dt
  aside.xpath(".//p").each do |a|
    a.delete("class")
    a.parent = dd
  end
end

#figure_cleanup(docxml) ⇒ Object

move footnotes into key, and get rid of footnote reference since it is in diagram



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

def figure_cleanup(docxml)
  docxml.xpath(FIGURE_WITH_FOOTNOTES).each do |f|
    key = figure_get_or_make_dl(f)
    f.xpath(".//aside").each do |aside|
      figure_aside_process(f, aside, key)
    end
  end
  docxml
end

#figure_get_or_make_dl(t) ⇒ Object



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

def figure_get_or_make_dl(t)
  dl = t.at(".//dl")
  if dl.nil?
    t.add_child("<p><b>#{@key_lbl}</b></p><dl></dl>")
    dl = t.at(".//dl")
  end
  dl
end

#figure_key(out) ⇒ Object



51
52
53
54
55
# File 'lib/isodoc/blocks.rb', line 51

def figure_key(out)
  out.p do |p|
    p.b { |b| b << @key_lbl }
  end
end

#figure_name_parse(node, div, name) ⇒ Object



44
45
46
47
48
49
# File 'lib/isodoc/blocks.rb', line 44

def figure_name_parse(node, div, name)
  div.p **{ class: "FigureTitle", align: "center" } do |p|
    p << l10n("#{@figure_lbl} #{get_anchors[node['id']][:label]}")
    p << "&nbsp;&mdash; #{name.text}" if name
  end
end

#figure_parse(node, out) ⇒ Object



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

def figure_parse(node, out)
  @in_figure = true
  name = node.at(ns("./name"))
  out.div **attr_code(id: node["id"], class: "figure") do |div|
    node.children.each do |n|
      figure_key(out) if n.name == "dl"
      parse(n, div) unless n.name == "name"
    end
    figure_name_parse(node, div, name) if name
  end
  @in_figure = false
end


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

def footnote_backlinks(docxml)
  seen = {}
  docxml.xpath('//a[@epub:type = "footnote"]').each_with_index do |x, i|
    next if seen[x["href"]]
    seen[x["href"]] = true
    fn = docxml.at(%<//*[@id = '#{x['href'].sub(/^#/, '')}']>) || next
    x["id"] || x["id"] = "fnref:#{i + 1}"
    fn.elements.first.children.first.previous = x.dup
    fn.add_child "<a href='##{x['id']}'>&#x21A9;</a>"
  end
  docxml
end

#footnote_cleanup(docxml) ⇒ Object



86
87
88
89
90
91
# File 'lib/isodoc/cleanup.rb', line 86

def footnote_cleanup(docxml)
  docxml.xpath('//a[@epub:type = "footnote"]/sup').each_with_index do |x, i|
    x.content = (i + 1).to_s
  end
  docxml
end

#footnote_parse(node, out) ⇒ Object



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

def footnote_parse(node, out)
  return table_footnote_parse(node, out) if @in_table || @in_figure
  fn = node["reference"]
  attrs = { "epub:type": "footnote", rel: "footnote", href: "#fn:#{fn}" }
  out.a **attrs do |a|
    a.sup { |sup| sup << fn }
  end
  make_footnote(node, fn)
end

#footnotes(div) ⇒ Object



5
6
7
8
# File 'lib/isodoc/footnotes.rb', line 5

def footnotes(div)
  return if @footnotes.empty?
  @footnotes.each { |fn| div.parent << fn }
end

#format_ref(ref, isopub, date) ⇒ Object



156
157
158
159
160
161
162
163
164
165
# File 'lib/isodoc/references.rb', line 156

def format_ref(ref, isopub, date)
  if isopub
    return ref unless date
    on = date.at(ns("./on"))
    return ref if on&.text == "--"
    return ref + ": #{date_range(date)}"
  end
  return "[#{ref}]" if /^\d+$/.match?(ref) && !/^\[.*\]$/.match?(ref)
  ref
end

#formula_parse(node, out) ⇒ Object



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

def formula_parse(node, out)
  out.div **attr_code(id: node["id"], class: "formula") do |div|
    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



141
142
143
144
145
# File 'lib/isodoc/blocks.rb', line 141

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

#from_xhtml(xml) ⇒ Object



74
75
76
# File 'lib/isodoc/utils.rb', line 74

def from_xhtml(xml)
  xml.to_xml.sub(%r{ xmlns="http://www.w3.org/1999/xhtml"}, "")
end

#generate_css(filename, stripwordcss, fontheader) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/isodoc/convert.rb', line 73

def generate_css(filename, stripwordcss, fontheader)
  stylesheet = File.read(filename, encoding: "UTF-8")
  stylesheet.gsub!(/(\s|\{)mso-[^:]+:[^;]+;/m, "\\1") if stripwordcss
  engine = Sass::Engine.new(fontheader + stylesheet, syntax: :scss)
  outname = File.basename(filename, ".*") + ".css"
  File.open(outname, "w") { |f| f.write(engine.render) }
  @files_to_delete << outname
  outname
end

#get_anchorsObject



7
8
9
# File 'lib/isodoc/xref_gen.rb', line 7

def get_anchors
  @anchors
end

#get_clause_id(node) ⇒ Object



84
85
86
87
# File 'lib/isodoc/utils.rb', line 84

def get_clause_id(node)
  clause = node.xpath(CLAUSE_ANCESTOR)
  clause&.last&.text || nil
end

#get_comments_from_text(docxml, link_order) ⇒ Object



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

def get_comments_from_text(docxml, link_order)
  comments = []
  docxml.xpath("//div[@style='mso-element:comment']").each do |c|
    next unless c["id"] && !link_order[c["id"]].nil?
    comments << { text: c.remove.to_s, id: c["id"] }
  end
  comments.sort! { |a, b| link_order[a[:id]] <=> link_order[b[:id]] }
  # comments
end

#get_metadataObject



17
18
19
# File 'lib/isodoc/metadata.rb', line 17

def 
  @meta
end

#get_note_container_id(node) ⇒ Object



97
98
99
100
# File 'lib/isodoc/utils.rb', line 97

def get_note_container_id(node)
  container = node.xpath(NOTE_CONTAINER_ANCESTOR)
  container&.last&.text || nil
end

#get_table_ancestor_id(node) ⇒ Object



43
44
45
46
47
# File 'lib/isodoc/footnotes.rb', line 43

def get_table_ancestor_id(node)
  table = node.ancestors("table") || node.ancestors("figure")
  return UUIDTools::UUID.random_create.to_s if table.empty?
  table.last["id"]
end

#header_strip(h) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/isodoc/utils.rb', line 122

def header_strip(h)
  h = h.to_s.gsub(%r{<br/>}, " ").sub(/<\/?h[12][^>]*>/, "")
  h1 = to_xhtml_fragment(h.dup)
  h1.traverse do |x|
    x.remove if x.name == "span" && x["class"] == "MsoCommentReference"
    x.remove if x.name == "a" && x["epub:type"] == "footnote"
    if x.name == "a"
      x.replace(x.children)
    end
  end
  from_xhtml(h1)
end

#hierarchical_asset_names(clause, num) ⇒ Object



183
184
185
186
187
188
189
190
191
# File 'lib/isodoc/xref_gen.rb', line 183

def hierarchical_asset_names(clause, num)
  clause.xpath(ns(".//table")).each_with_index do |t, i|
    @anchors[t["id"]] = anchor_struct("#{num}.#{i + 1}", nil, @table_lbl)
  end
  hierarchical_figure_names(clause, num)
  clause.xpath(ns(".//formula")).each_with_index do |t, i|
    @anchors[t["id"]] = anchor_struct("#{num}.#{i + 1}", t, @formula_lbl)
  end
end

#hierarchical_figure_names(clause, num) ⇒ Object



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

def hierarchical_figure_names(clause, num)
  i = j = 0
  clause.xpath(ns(".//figure")).each do |t|
    if t.parent.name == "figure" then j += 1
    else
      j = 0
      i += 1
    end
    label = "#{num}.#{i}" + (j.zero? ? "" : "-#{j}")
    @anchors[t["id"]] = anchor_struct(label, nil, @figure_lbl)
  end
end

#html_buttonObject



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

def html_button() 
  '<button onclick="topFunction()" id="myBtn" '\
    'title="Go to top">Top</button>'.freeze
end

#html_cleanup(x) ⇒ Object



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

def html_cleanup(x)
  footnote_backlinks(html_toc(
    term_header(move_images(html_footnote_filter(html_preface(htmlstyle(x))))))
  )
end

#html_cover(docxml) ⇒ Object



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

def html_cover(docxml)
  cover = File.read(@htmlcoverpage, encoding: "UTF-8")
  coverxml = to_xhtml_fragment(cover)
  d = docxml.at('//div[@class="WordSection1"]')
  d.children.first.add_previous_sibling coverxml.to_xml(encoding: "US-ASCII")
end

#html_doc_path(file) ⇒ Object



69
70
71
# File 'lib/isodoc/convert.rb', line 69

def html_doc_path(file)
  File.join(File.dirname(__FILE__), File.join("html", file))
end

#html_footnote_filter(docxml) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/isodoc/html.rb', line 141

def html_footnote_filter(docxml)
  seen = {}
  i = 1
  docxml.xpath('//a[@epub:type = "footnote"]').each do |x|
    fn = docxml.at(%<//*[@id = '#{x['href'].sub(/^#/, '')}']>) || next
    i, seen = update_footnote_filter(fn, x, i, seen)
  end
  docxml
end

#html_headObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/isodoc/html.rb', line 56

def html_head() 
  "<script type=\"text/javascript\" src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js\"></script>\n\n<!--TOC script import-->\n<script type=\"text/javascript\"  src=\"https://cdn.rawgit.com/jgallen23/toc/0.3.2/dist/toc.min.js\"></script>\n\n<!--Google fonts-->\n<link href=\"https://fonts.googleapis.com/css?family=Overpass:300,300i,600,900\" rel=\"stylesheet\">\n<link href=\"https://fonts.googleapis.com/css?family=Lato:400,400i,700,900\" rel=\"stylesheet\">\n<!--Font awesome import for the link icon-->\n<link rel=\"stylesheet\" href=\"https://use.fontawesome.com/releases/v5.0.8/css/solid.css\" integrity=\"sha384-v2Tw72dyUXeU3y4aM2Y0tBJQkGfplr39mxZqlTBDUZAb9BGoC40+rdFCG0m10lXk\" crossorigin=\"anonymous\">\n<link rel=\"stylesheet\" href=\"https://use.fontawesome.com/releases/v5.0.8/css/fontawesome.css\" integrity=\"sha384-q3jl8XQu1OpdLgGFvNRnPdj5VIlCvgsDQTQB6owSOHWlAurxul7f+JpUOVdAiJ5P\" crossorigin=\"anonymous\">\n  HEAD\nend\n".freeze

#html_intro(docxml) ⇒ Object



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

def html_intro(docxml)
  intro = File.read(@htmlintropage, encoding: "UTF-8")
  introxml = to_xhtml_fragment(intro)
  d = docxml.at('//div[@class="WordSection2"]')
  d.children.first.add_previous_sibling introxml.to_xml(encoding: "US-ASCII")
end

#html_main(docxml) ⇒ Object



77
78
79
80
81
82
# File 'lib/isodoc/html.rb', line 77

def html_main(docxml)
  docxml.at("//head").add_child(html_head())
  d = docxml.at('//div[@class="WordSection3"]')
  d.name = "main"
  d.children.first.previous = html_button()
end

#html_preface(docxml) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/isodoc/html.rb', line 84

def html_preface(docxml)
  html_cover(docxml) if @htmlcoverpage
  html_intro(docxml) if @htmlintropage
  docxml.at("//body") << mathjax(@openmathdelim, @closemathdelim)
  if @scripts
    scripts = File.read(@scripts, encoding: "UTF-8")
    a = docxml.at("//body").add_child docxml.create_cdata(scripts) #scripts.to_xml(encoding: "US-ASCII")
  end
  html_main(docxml)
  docxml
end

#html_toc(docxml) ⇒ Object

end



203
204
205
# File 'lib/isodoc/html.rb', line 203

def html_toc(docxml)
  docxml
end

#htmlstyle(docxml) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/isodoc/html.rb', line 117

def htmlstyle(docxml)
  title = docxml.at("//*[local-name() = 'head']/*[local-name() = 'title']")
  head = docxml.at("//*[local-name() = 'head']")
  css = htmlstylesheet
  if title.nil? then head.children.first.add_previous_sibling css
  else
    title.add_next_sibling css
  end
  docxml
end

#htmlstylesheetObject



110
111
112
113
114
115
# File 'lib/isodoc/html.rb', line 110

def htmlstylesheet
  stylesheet = File.read(@htmlstylesheet, encoding: "UTF-8")
  xml = Nokogiri::XML("<style/>")
  xml.children.first << Nokogiri::XML::Comment.new(xml, "\n#{stylesheet}\n")
  xml.root.to_s
end

#i18n_init(lang, script) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/isodoc/i18n.rb', line 5

def i18n_init(lang, script)
  @lang = lang
  @script = script
  y = if @i18nyaml
        YAML.load_file(@i18nyaml)
      elsif lang == "en"
        YAML.load_file(File.join(File.dirname(__FILE__), "i18n-en.yaml"))
      elsif lang == "fr"
        YAML.load_file(File.join(File.dirname(__FILE__), "i18n-fr.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-en.yaml"))
      end
  @term_def_boilerplate = y["term_def_boilerplate"]
  @scope_lbl = y["scope"]
  @symbols_lbl = y["symbols"]
  @table_of_contents_lbl = y["table_of_contents"]
  @introduction_lbl = y["introduction"]
  @foreword_lbl = y["foreword"]
  @termsdef_lbl = y["termsdef"]
  @termsdefsymbols_lbl = y["termsdefsymbols"]
  @normref_lbl = y["normref"]
  @bibliography_lbl = y["bibliography"]
  @clause_lbl = y["clause"]
  @annex_lbl = y["annex"]
  @appendix_lbl = y["appendix"]
  @no_terms_boilerplate = y["no_terms_boilerplate"]
  @internal_terms_boilerplate = y["internal_terms_boilerplate"]
  @norm_with_refs_pref = y["norm_with_refs_pref"]
  @norm_empty_pref = y["norm_empty_pref"]
  @external_terms_boilerplate = y["external_terms_boilerplate"]
  @internal_external_terms_boilerplate =
    y["internal_external_terms_boilerplate"]
  @note_lbl = y["note"]
  @note_xref_lbl = y["note_xref"]
  @termnote_lbl = y["termnote"]
  @figure_lbl = y["figure"]
  @list_lbl = y["list"]
  @formula_lbl = y["formula"]
  @table_lbl = y["table"]
  @key_lbl = y["key"]
  @example_lbl = y["example"]
  @example_xref_lbl = y["example_xref"]
  @where_lbl = y["where"]
  @wholeoftext_lbl = y["wholeoftext"]
  @draft_lbl = y["draft"]
  @inform_annex_lbl = y["inform_annex"]
  @norm_annex_lbl = y["norm_annex"]
  @modified_lbl = y["modified"]
  @deprecated_lbl = y["deprecated"]
  @source_lbl = y["source"]
  @and_lbl = y["and"]
  @all_parts_lbl = y["all_parts"]
  @locality = y["locality"]
  @admonition = y["admonition"]
  @labels = y
  @labels["language"] = @lang
  @labels["script"] = @script
end

#image_parse(url, out, caption) ⇒ Object



207
208
209
210
# File 'lib/isodoc/blocks.rb', line 207

def image_parse(url, out, caption)
  out.img **attr_code(src: url)
  image_title_parse(out, caption)
end

#image_title_parse(out, caption) ⇒ Object



199
200
201
202
203
204
205
# File 'lib/isodoc/blocks.rb', line 199

def image_title_parse(out, caption)
  unless caption.nil?
    out.p **{ class: "FigureTitle", align: "center" } do |p|
      p.b { |b| b << caption.to_s }
    end
  end
end

#in_commentObject



5
6
7
# File 'lib/isodoc/comments.rb', line 5

def in_comment
  @in_comment
end

#init_metadataObject



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

def 
  @meta = { tc: "XXXX", sc: "XXXX", wg: "XXXX",
            editorialgroup: [],
            secretariat: "XXXX",
            obsoletes: nil,
            obsoletes_part: nil }
  DATETYPES.each { |w| @meta["#{w}date".to_sym] = "XXX" }
end

#initial_anchor_names(d) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/isodoc/xref_sect_gen.rb', line 12

def initial_anchor_names(d)
  introduction_names(d.at(ns("//introduction")))
  n = 0
  n = section_names(d.at(ns("//clause[title = 'Scope']")), n, 1)
  n = section_names(d.at(ns(
    "//references[title = 'Normative References']")), n, 1)
  n = section_names(d.at(ns("//sections/terms | "\
                            "//sections/clause[descendant::terms]")), n, 1)
  n = section_names(d.at(ns("//sections/symbols-abbrevs")), n, 1)
  middle_section_asset_names(d)
  clause_names(d, n)
  termnote_anchor_names(d)
  termexample_anchor_names(d)
end

#inline_header_cleanup(docxml) ⇒ Object



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

def inline_header_cleanup(docxml)
  docxml.xpath('//span[@class="zzMoveToFollowing"]').each do |x|
    x.delete("class")
    n = x.next_element
    if n.nil?
      x.name = "p"
    else
      n.children.first.previous = x.remove
    end
  end
  docxml
end

#insert_comment_cont(from, to, target) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/isodoc/comments.rb', line 102

def insert_comment_cont(from, to, target)
  # includes_to = from.at(".//*[@id='#{to}']")
  while !from.nil? && from["id"] != to
    following = from.xpath("./following::*")
    (from = following.shift) && incl_to = from.at(".//*[@id='#{to}']")
    while !incl_to.nil? && !from.nil? && skip_comment_wrap(from)
      (from = following.shift) && incl_to = from.at(".//*[@id='#{to}']")
    end
    wrap_comment_cont(from, target) if !from.nil?
  end
end

#insert_tab(out, n) ⇒ Object



10
11
12
# File 'lib/isodoc/utils.rb', line 10

def insert_tab(out, n)
  [1..n].each { out << "&nbsp; " }
end

#introduction_names(clause) ⇒ Object



43
44
45
46
47
48
# File 'lib/isodoc/xref_sect_gen.rb', line 43

def introduction_names(clause)
  return if clause.nil?
  clause.xpath(ns("./clause")).each_with_index do |c, i|
    section_names1(c, "0.#{i + 1}", 2)
  end
end

#iso?(org) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
# File 'lib/isodoc/metadata.rb', line 92

def iso?(org)
  name = org&.at(ns("./name"))&.text
  abbrev = org&.at(ns("./abbreviation"))&.text
  (abbrev == "ISO" || 
   name == "International Organization for Standardization" )
end

#iso_bibitem_entry(list, b, ordinal, biblio) ⇒ Object



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

def iso_bibitem_entry(list, b, ordinal, biblio)
  list.p **attr_code(iso_bibitem_entry_attrs(b, biblio)) do |ref|
    if biblio
      ref << "[#{ordinal}]"
      insert_tab(ref, 1)
    end
    ref << iso_bibitem_ref_code(b)
    date_note_process(b, ref)
    ref << ", "
    ref.i { |i| i << " #{iso_title(b)}" }
  end
end

#iso_bibitem_entry_attrs(b, biblio) ⇒ Object



24
25
26
# File 'lib/isodoc/references.rb', line 24

def iso_bibitem_entry_attrs(b, biblio)
  { id: b["id"], class: biblio ? "Biblio" : "NormRef" }
end

#iso_bibitem_ref_code(b) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/isodoc/references.rb', line 8

def iso_bibitem_ref_code(b)
  isocode = b.at(ns("./docidentifier")).text
  isodate = b.at(ns("./date[@type = 'published']"))
  reference = docid_l10n(isocode)
  reference += ": #{date_range(isodate)}" if isodate
  reference
end

#iso_title(b) ⇒ Object



28
29
30
31
32
# File 'lib/isodoc/references.rb', line 28

def iso_title(b)
  title = b.at(ns("./title[@language = '#{@language}']"))
  title = b.at(ns("./title")) unless title
  title.text
end

#l10n(x, lang = @lang, script = @script) ⇒ Object

function localising spaces and punctuation. Not clear if period needs to be localised for zh



87
88
89
90
91
92
93
94
95
96
# File 'lib/isodoc/i18n.rb', line 87

def l10n(x, lang = @lang, script = @script)
  if lang == "zh" && script == "Hans"
    x.gsub(/ /, "").gsub(/:/, ":").gsub(/,/, "、").
      gsub(/\(/, "(").gsub(/\)/, ")").
      gsub(/\[/, "【").gsub(/\]/, "】").
      gsub(/<b>/, "").gsub("</b>", "")
  else
    x
  end
end

#li_parse(node, out) ⇒ Object



44
45
46
47
48
# File 'lib/isodoc/lists.rb', line 44

def li_parse(node, out)
  out.li do |li|
    node.children.each { |n| parse(n, li) }
  end
end

#list_anchor_names(sections) ⇒ Object



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

def list_anchor_names(sections)
  sections.each do |s|
    notes = s.xpath(ns(".//ol")) - s.xpath(ns(".//clause//ol")) -
      s.xpath(ns(".//appendix//ol")) - s.xpath(ns(".//ol//ol"))
    notes.each_with_index do |n, i|
      idx = notes.size == 1 ? "" : " #{i + 1}"
      @anchors[n["id"]] = anchor_struct(idx, s, @list_lbl)
      list_item_anchor_names(n, @anchors[n["id"]], 1, "", notes.size != 1)
    end
    list_anchor_names(s.xpath(ns("./clause | ./appendix")))
  end
end

#list_item_anchor_names(list, list_anchor, depth, prev_label, refer_list) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/isodoc/xref_gen.rb', line 97

def list_item_anchor_names(list, list_anchor, depth, prev_label, refer_list)
  list.xpath(ns("./li")).each_with_index do |li, i|
    label = listlabel(depth, i + 1)
    label = "#{prev_label}.#{label}" unless prev_label.empty?
    label = "#{list_anchor[:xref]} #{label}" if refer_list
    li["id"] && @anchors[li["id"]] = { xref: "#{label})", 
                                       container: list_anchor[:container] }
    li.xpath(ns("./ol")).each do |ol|
      list_item_anchor_names(ol, list_anchor, depth + 1, label, false)
    end
  end
end

#listlabel(depth, i) ⇒ Object



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

def listlabel(depth, i)
  return i.to_s if [2, 7].include? depth
  return (96 + i).chr.to_s if [1, 6].include? depth
  return (64 + i).chr.to_s if [4, 9].include? depth
  return RomanNumerals.to_roman(i).downcase if [3, 8].include? depth
  return RomanNumerals.to_roman(i).upcase if [5, 10].include? depth
  return i.to_s
end

add in from and to links to move the comment into place



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

def make_comment_link(out, fn, node)
  out.span(**comment_link_attrs(fn, node)) do |s1|
    s1.span **{ lang: "EN-GB", style: "font-size:9.0pt" } do |s2|
      s2.a **{ style: "mso-comment-reference:SMC_#{fn};"\
               "mso-comment-date:#{node['date'].gsub(/[-:Z]/, '')}" }
      s2.span **{ style: "mso-special-character:comment",
                  target: fn } # do |s|
    end
  end
end

#make_comment_target(out) ⇒ Object



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

def make_comment_target(out)
  out.span **{ style: "MsoCommentReference" } do |s1|
    s1.span **{ lang: "EN-GB", style: "font-size:9.0pt" } do |s2|
      s2.span **{ style: "mso-special-character:comment" }
    end
  end
end

#make_comment_text(node, fn) ⇒ Object



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

def make_comment_text(node, fn)
  noko do |xml|
    xml.div **{ style: "mso-element:comment", id: fn } do |div|
      div.span **{ style: %{mso-comment-author:"#{node['reviewer']}"} }
      make_comment_target(div)
      node.children.each { |n| parse(n, div) }
    end
  end.join("\n")
end

#make_footnote(node, fn) ⇒ Object



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

def make_footnote(node, fn)
  return if @seen_footnote.include?(fn)
  @in_footnote = true
  @footnotes << make_generic_footnote_text(node, fn)
  @in_footnote = false
  @seen_footnote << fn
end

#make_generic_footnote_text(node, fnid) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/isodoc/footnotes.rb', line 35

def make_generic_footnote_text(node, fnid)
  noko do |xml|
    xml.aside **{ id: "fn:#{fnid}", class: "footnote" } do |div|
      node.children.each { |n| parse(n, div) }
    end
  end.join("\n")
end

#make_table_attr(node) ⇒ Object



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

def make_table_attr(node)
  {
    id: node["id"],
    class: "MsoISOTable",
    border: 1,
    cellspacing: 0,
    cellpadding: 0,
  }
end


10
11
12
13
14
15
# File 'lib/isodoc/footnotes.rb', line 10

def make_table_footnote_link(out, fnid, fnref)
  attrs = { href: "##{fnid}", class: "TableFootnoteRef" }
  out.a **attrs do |a|
    a << fnref
  end
end

#make_table_footnote_target(out, fnid, fnref) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/isodoc/footnotes.rb', line 17

def make_table_footnote_target(out, fnid, fnref)
  attrs = { id: fnid, class: "TableFootnoteRef" }
  out.a **attrs do |a|
    a << fnref
    insert_tab(a, 1)
  end
end

#make_table_footnote_text(node, fnid, fnref) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/isodoc/footnotes.rb', line 25

def make_table_footnote_text(node, fnid, fnref)
  attrs = { id: "fn:#{fnid}" }
  noko do |xml|
    xml.div **attr_code(attrs) do |div|
      make_table_footnote_target(div, fnid, fnref)
      node.children.each { |n| parse(n, div) }
    end
  end.join("\n")
end

#make_tr_attr(td, row, totalrows) ⇒ Object

def make_tr_attr(td, row, totalrows, cols, totalcols, header) border-left:#? “#{SW 1.5pt;” : “none;”} border-right:#SW #== totalcols && !header ? “1.5” : “1.0”pt;



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

def make_tr_attr(td, row, totalrows)
  style = td.name == "th" ? "font-weight:bold;" : ""
  rowmax = td["rowspan"] ? row + td["rowspan"].to_i - 1 : row
  style += "    border-top:\#{row.zero? ? \"\#{SW} 1.5pt;\" : 'none;'}\n    border-bottom:\#{SW} \#{rowmax == totalrows ? '1.5' : '1.0'}pt;\n  STYLE\n  { rowspan: td[\"rowspan\"], colspan: td[\"colspan\"],\n    align: td[\"align\"], style: style.gsub(/\\n/, \"\") }\nend\n"

#mathjax(open, close) ⇒ Object



43
44
45
# File 'lib/isodoc/html.rb', line 43

def mathjax(open, close)
  MATHJAX.gsub("OPEN", open).gsub("CLOSE", close)
end

#merge_fnref_into_fn_text(a) ⇒ Object



93
94
95
96
97
# File 'lib/isodoc/cleanup.rb', line 93

def merge_fnref_into_fn_text(a)
  fn = a.at('.//a[@class="TableFootnoteRef"]')
  n = fn.next_element
  n&.children&.first&.add_previous_sibling(fn.remove)
end

#middle_section_asset_names(d) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/isodoc/xref_sect_gen.rb', line 27

def middle_section_asset_names(d)
  middle_sections = "//clause[title = 'Scope'] | "\
    "//foreword | //introduction | "\
    "//references[title = 'Normative References'] | //sections/terms | "\
    "//sections/symbols-abbrevs | //clause[parent::sections]"
  sequential_asset_names(d.xpath(ns(middle_sections)))
end

#modification_parse(node, out) ⇒ Object



7
8
9
10
11
# File 'lib/isodoc/terms.rb', line 7

def modification_parse(node, out)
  out << "[MODIFICATION]"
  para = node.at(ns("./p"))
  para.children.each { |n| parse(n, out) }
end


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

def move_comment_link_to_from(docxml)
  docxml.xpath('//span[@style="MsoCommentReference"][@from]').each do |x|
    attrs = comment_attributes(docxml, x) || next
    move_comment_link_to_from1(x, attrs[:from])
    insert_comment_cont(attrs[:from], x["to"], x["target"])
  end
end


78
79
80
81
82
83
# File 'lib/isodoc/comments.rb', line 78

def move_comment_link_to_from1(x, fromlink)
  x.remove
  link = x.at(".//a")
  fromlink.replace(x)
  link.children = fromlink
end

#move_images(docxml) ⇒ Object

presupposes that the image source is local



165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/isodoc/html.rb', line 165

def move_images(docxml)
  system "rm -r _images; mkdir _images"
  docxml.xpath("//*[local-name() = 'img']").each do |i|
    matched = /\.(?<suffix>\S+)$/.match i["src"]
    uuid = UUIDTools::UUID.random_create.to_s
    new_full_filename = File.join("_images", "#{uuid}.#{matched[:suffix]}")
    system "cp #{i['src']} #{new_full_filename}"
    i["src"] = new_full_filename
    i["width"], i["height"] = Html2Doc.image_resize(i, 800, 1200)
  end
  docxml
end

#new_fullcolspan_row(t, tfoot) ⇒ Object



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

def new_fullcolspan_row(t, tfoot)
  # how many columns in the table?
  cols = 0
  t.at(".//tr").xpath("./td | ./th").each do |td|
    cols += (td["colspan"] ? td["colspan"].to_i : 1)
  end
  style = %{border-top:0pt;border-bottom:#{SW} 1.5pt;}
  tfoot.add_child("<tr><td colspan='#{cols}' style='#{style}'/></tr>")
  tfoot.xpath(".//td").last
end

#noko(&block) ⇒ Object

block for processing XML document fragments as XHTML, to allow for HTMLentities



43
44
45
46
47
48
49
50
# File 'lib/isodoc/utils.rb', line 43

def noko(&block)
  doc = ::Nokogiri::XML.parse(NOKOHEAD)
  fragment = doc.fragment("")
  ::Nokogiri::XML::Builder.with fragment, &block
  fragment.to_xml(encoding: "US-ASCII").lines.map do |l|
    l.gsub(/\s*\n/, "")
  end
end

#noniso_bibitem(list, b, ordinal, bibliography) ⇒ Object

TODO generate formatted ref if not present



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/isodoc/references.rb', line 65

def noniso_bibitem(list, b, ordinal, bibliography)
  list.p **attr_code("id": b["id"], class: "Biblio") do |r|
    if bibliography
      id = docid_l10n(b.at(ns("./docidentifier")).text.gsub(/[\[\]]/, ""))
      ref_entry_code(r, ordinal, id)
    else
      r << "#{iso_bibitem_ref_code(b)}, "
    end
    reference_format(b, r)
  end
end

#norm_ref(isoxml, out, num) ⇒ Object



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

def norm_ref(isoxml, out, num)
  q = "//bibliography/references[title = 'Normative References']"
  f = isoxml.at(ns(q)) or return num
  out.div do |div|
    num = num + 1
    clause_name("#{num}.", @normref_lbl, div, nil)
    norm_ref_preface(f, div)
    biblio_list(f, div, false)
  end
  num
end

#norm_ref_preface(f, div) ⇒ Object



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

def norm_ref_preface(f, div)
  refs = f.elements.select do |e|
    ["reference", "bibitem"].include? e.name
  end
  pref = if refs.empty? then @norm_empty_pref
         else
           @norm_with_refs_pref
         end
  div.p pref
end

#note_anchor_names(sections) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/isodoc/xref_gen.rb', line 46

def note_anchor_names(sections)
  sections.each do |s|
    notes = s.xpath(CHILD_NOTES_XPATH)
    notes.each_with_index do |n, i|
      next if @anchors[n["id"]]
      next if n["id"].nil?
      idx = notes.size == 1 ? "" : " #{i + 1}"
      @anchors[n["id"]] = anchor_struct(idx, s, @note_xref_lbl)
    end
    note_anchor_names(s.xpath(ns("./clause | ./appendix")))
  end
end

#note_label(node) ⇒ Object



5
6
7
8
9
# File 'lib/isodoc/blocks.rb', line 5

def note_label(node)
  n = get_anchors[node["id"]]
  return @note_lbl if n.nil? || n[:label].empty?
  l10n("#{@note_lbl} #{n[:label]}")
end

#note_p_parse(node, div) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/isodoc/blocks.rb', line 11

def note_p_parse(node, div)
  div.p **{ class: "Note" } do |p|
    p.span **{ class: "note_label" } do |s|
      s << note_label(node)
    end
    insert_tab(p, 1)
    node.first_element_child.children.each { |n| parse(n, p) }
  end
  node.element_children[1..-1].each { |n| parse(n, div) }
end

#note_parse(node, out) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/isodoc/blocks.rb', line 32

def note_parse(node, out)
  @note = true
  out.div **{ id: node["id"], class: "Note" } do |div|
    if node.first_element_child.name == "p"
      note_p_parse(node, div)
    else
      note_parse1(node, div)
    end
  end
  @note = false
end

#note_parse1(node, div) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/isodoc/blocks.rb', line 22

def note_parse1(node, div)
  div.p **{ class: "Note" } do |p|
    p.span **{ class: "note_label" } do |s|
      s << note_label(node)
    end
    insert_tab(p, 1)
  end
  node.children.each { |n| parse(n, div) }
end

#ns(xpath) ⇒ Object



3
4
5
6
7
8
# File 'lib/isodoc/utils.rb', line 3

def ns(xpath)
  xpath.gsub(%r{/([a-zA-z])}, "/xmlns:\\1").
    gsub(%r{::([a-zA-z])}, "::xmlns:\\1").
    gsub(%r{\[([a-zA-z][a-z0-9A-Z@/]* ?=)}, "[xmlns:\\1").
    gsub(%r{\[([a-zA-z][a-z0-9A-Z@/]*\])}, "[xmlns:\\1")
end

#ol_depth(node) ⇒ Object

We don’t really want users to specify type of ordered list; we will use a fixed hierarchy as practiced by ISO (though not fully spelled out): a) 1) i) A) I)



26
27
28
29
30
31
32
33
34
# File 'lib/isodoc/lists.rb', line 26

def ol_depth(node)
  depth = node.ancestors("ul, ol").size + 1
  type = :alphabet
  type = :arabic if [2, 7].include? depth
  type = :roman if [3, 8].include? depth
  type = :alphabet_upper if [4, 9].include? depth
  type = :roman_upper if [5, 10].include? depth
  ol_style(type)
end

#ol_parse(node, out) ⇒ Object



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

def ol_parse(node, out)
  # style = ol_style(node["type"])
  style = ol_depth(node)
  out.ol **attr_code(type: style) do |ol|
    node.children.each { |n| parse(n, ol) }
  end
end

#ol_style(type) ⇒ Object



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

def ol_style(type)
  type = :alphabet unless type
  OL_STYLE[type.to_sym]
end

#para_attrs(node) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/isodoc/blocks.rb', line 156

def para_attrs(node)
  classtype = nil
  classtype = "Note" if @note
  classtype = "MsoCommentText" if in_comment
  classtype = "Sourcecode" if @annotation
  attrs = { class: classtype, id: node["id"] }
  unless node["align"].nil?
    attrs[:align] = node["align"] unless node["align"] == "justify"
    attrs[:style] = "text-align:#{node['align']}"
  end
  attrs
end

#para_parse(node, out) ⇒ Object



169
170
171
172
173
174
175
176
177
# File 'lib/isodoc/blocks.rb', line 169

def para_parse(node, out)
  out.p **attr_code(para_attrs(node)) do |p|
    unless @termdomain.empty?
      p << "&lt;#{@termdomain}&gt; "
      @termdomain = ""
    end
    node.children.each { |n| parse(n, p) }
  end
end

#para_then_remainder(first, node, p, div) ⇒ Object



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

def para_then_remainder(first, node, p, div)
  if first.name == "p"
    first.children.each { |n| parse(n, p) }
    node.elements.drop(1).each { |n| parse(n, div) }
  else
    node.elements.each { |n| parse(n, div) }
  end
end

#part_label(lang) ⇒ Object

we don’t leave this to i18n.rb, because we have both English and French titles in the same document



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

def part_label(lang)
  case lang
  when "en" then "Part"
  when "fr" then "Partie"
  end
end

#populate_template(docxml, _format) ⇒ Object



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

def populate_template(docxml, _format)
  meta = .merge(@labels)
  docxml = docxml.
    gsub(/\[TERMREF\]\s*/, l10n("[#{@source_lbl}: ")).
    gsub(/\s*\[\/TERMREF\]\s*/, l10n("]")).
    gsub(/\s*\[MODIFICATION\]/, l10n(", #{@modified_lbl} &mdash; "))
  template = Liquid::Template.parse(docxml)
  template.render(meta.map { |k, v| [k.to_s, v] }.to_h)
end

#postprocess(result, filename, dir) ⇒ Object



3
4
5
6
7
# File 'lib/isodoc/html.rb', line 3

def postprocess(result, filename, dir)
  result = from_xhtml(cleanup(to_xhtml(result)))
  toHTML(result, filename)
  @files_to_delete.each { |f| system "rm #{f}" }
end

#quote_attribution(node, out) ⇒ Object



179
180
181
182
183
184
185
186
# File 'lib/isodoc/blocks.rb', line 179

def quote_attribution(node, out)
  author = node.at(ns("./author"))
  source = node.at(ns("./source"))
  out.p **{ class: "QuoteAttribution" } do |p|
    p << "&mdash; #{author.text}, " if author
    eref_parse(source, p) if source
  end
end

#quote_parse(node, out) ⇒ Object



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

def quote_parse(node, out)
  attrs = para_attrs(node)
  attrs[:class] = "Quote"
  out.div **attr_code(attrs) do |p|
    node.children.each do |n|
      parse(n, p) unless ["author", "source"].include? n.name
    end
    quote_attribution(node, out)
  end
end

#ref_entry_code(r, ordinal, t) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/isodoc/references.rb', line 47

def ref_entry_code(r, ordinal, t)
  if /^\d+$/.match?(t)
    r << "[#{t}]"
    insert_tab(r, 1)
  else
    r << "[#{ordinal}]"
    insert_tab(r, 1)
    r << "#{t},"
  end
end

#reference_format(b, r) ⇒ Object



58
59
60
61
62
# File 'lib/isodoc/references.rb', line 58

def reference_format(b, r)
  title = b.at(ns("./formattedref")) || 
    b.at(ns("./title[@language = '#{@language}']")) || b.at(ns("./title"))
  title&.children&.each { |n| parse(n, r) }
end

#reference_names(ref) ⇒ Object



167
168
169
170
171
172
173
174
# File 'lib/isodoc/references.rb', line 167

def reference_names(ref)
  isopub = ref.at(ns(ISO_PUBLISHER_XPATH))
  docid = ref.at(ns("./docidentifier"))
  # return ref_names(ref) unless docid
  date = ref.at(ns("./date[@type = 'published']"))
  reference = format_ref(docid_l10n(docid.text), isopub, date)
  @anchors[ref["id"]] = { xref: reference }
end

#relations(isoxml, _out) ⇒ Object



202
203
204
205
206
207
208
# File 'lib/isodoc/metadata.rb', line 202

def relations(isoxml, _out)
  std = isoxml.at(ns("//bibdata/relation[@type = 'obsoletes']")) || return
  locality = std.at(ns(".//locality"))
  id = std.at(ns(".//docidentifier"))
  (:obsoletes, id.text) if id
  (:obsoletes_part, locality.text) if locality
end

#remove_bottom_border(td) ⇒ Object



115
116
117
118
# File 'lib/isodoc/cleanup.rb', line 115

def remove_bottom_border(td)
  td["style"] =
    td["style"].gsub(/border-bottom:[^;]+;/, "border-bottom:0pt;")
end


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

def reorder_comments_by_comment_link(docxml)
  link_order = {}
  docxml.xpath(COMMENT_TARGET_XREFS).each_with_index do |target, i|
    link_order[target.value] = i
  end
  comments = get_comments_from_text(docxml, link_order)
  list = docxml.at("//*[@style='mso-element:comment-list']") || return
  list.children = comments.map { |c| c[:text] }.join("\n")
end

#review_note_parse(node, out) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/isodoc/comments.rb', line 16

def review_note_parse(node, out)
  fn = @comments.length + 1
  make_comment_link(out, fn, node)
  @in_comment = true
  @comments << make_comment_text(node, fn)
  @in_comment = false
end

#sc(xml) ⇒ Object



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

def sc(xml)
  sc_num = xml.at(ns("//editorialgroup/subcommittee/@number"))
  sc_type = xml.at(ns("//editorialgroup/subcommittee/@type"))&.text || "SC"
  if sc_num
    scid = "#{sc_type} #{sc_num.text}"
    (:sc, scid)
    (:editorialgroup, [:editorialgroup] << scid)
  end
end

#script_cdata(result) ⇒ Object



9
10
11
12
13
14
# File 'lib/isodoc/html.rb', line 9

def script_cdata(result)
  result.gsub(%r{<script>\s*<!\[CDATA\[}m, "<script>").
    gsub(%r{\]\]>\s*</script>}, "</script>").
    gsub(%r{<!\[CDATA\[\s*<script>}m, "<script>").
    gsub(%r{</script>\s*\]\]>}, "</script>")
end

#secretariat(xml) ⇒ Object



64
65
66
67
# File 'lib/isodoc/metadata.rb', line 64

def secretariat(xml)
  sec = xml.at(ns("//editorialgroup/secretariat"))
  (:secretariat, sec.text) if sec
end

#section_names(clause, num, lvl) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/isodoc/xref_sect_gen.rb', line 50

def section_names(clause, num, lvl)
  return num if clause.nil?
  num = num + 1
  @anchors[clause["id"]] =
    { label: num.to_s, xref: l10n("#{@clause_lbl} #{num}"), level: lvl }
  clause.xpath(ns("./clause | ./term  | ./terms | ./symbols-abbrevs")).
    each_with_index do |c, i|
    section_names1(c, "#{num}.#{i + 1}", lvl + 1)
  end
  num
end

#section_names1(clause, num, level) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/isodoc/xref_sect_gen.rb', line 62

def section_names1(clause, num, level)
  @anchors[clause["id"]] =
    { label: num, level: level, xref: num }
  # subclauses are not prefixed with "Clause"
  clause.xpath(ns("./clause | ./terms | ./term | ./symbols-abbrevs")).
    each_with_index do |c, i|
    section_names1(c, "#{num}.#{i + 1}", level + 1)
  end
end

#sentence_join(array) ⇒ Object



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

def sentence_join(array)
  return "" if array.nil? || array.empty?
  if array.length == 1
    array[0]
  else
    l10n("#{array[0..-2].join(', ')} #{@and_lbl} #{array.last}")
  end
end

#sequential_asset_names(clause) ⇒ Object



160
161
162
163
164
165
166
167
168
# File 'lib/isodoc/xref_gen.rb', line 160

def sequential_asset_names(clause)
  clause.xpath(ns(".//table")).each_with_index do |t, i|
    @anchors[t["id"]] = anchor_struct(i + 1, nil, @table_lbl)
  end
  sequential_figure_names(clause)
  clause.xpath(ns(".//formula")).each_with_index do |t, i|
    @anchors[t["id"]] = anchor_struct(i + 1, t, @formula_lbl)
  end
end

#sequential_figure_names(clause) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/isodoc/xref_gen.rb', line 122

def sequential_figure_names(clause)
  i = j = 0
  clause.xpath(ns(".//figure")).each do |t|
    if t.parent.name == "figure" then j += 1
    else
      j = 0
      i += 1
    end
    label = i.to_s + (j.zero? ? "" : "-#{j}")
    @anchors[t["id"]] = anchor_struct(label, nil, @figure_lbl)
  end
end

#set_metadata(key, value) ⇒ Object



21
22
23
# File 'lib/isodoc/metadata.rb', line 21

def (key, value)
  @meta[key] = value
end

#skip_comment_wrap(from) ⇒ Object



98
99
100
# File 'lib/isodoc/comments.rb', line 98

def skip_comment_wrap(from)
  from["style"] != "mso-special-character:comment"
end

#sourcecode_name_parse(_node, div, name) ⇒ Object



103
104
105
106
107
# File 'lib/isodoc/blocks.rb', line 103

def sourcecode_name_parse(_node, div, name)
  div.p **{ class: "FigureTitle", align: "center" } do |p|
    p << name.text
  end
end

#sourcecode_parse(node, out) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/isodoc/blocks.rb', line 109

def sourcecode_parse(node, out)
  name = node.at(ns("./name"))
  out.p **attr_code(id: node["id"], class: "Sourcecode") do |div|
    @sourcecode = true
    node.children.each do |n|
      parse(n, div) unless n.name == "name"
    end
    @sourcecode = false
    sourcecode_name_parse(node, div, name) if name
  end
end

#split_bibitems(f) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/isodoc/references.rb', line 83

def split_bibitems(f)
  iso_bibitem = []
  non_iso_bibitem = []
  f.xpath(ns("./bibitem")).each do |x|
    if x.at(ns(ISO_PUBLISHER_XPATH)).nil?
      non_iso_bibitem << x
    else
      iso_bibitem << x
    end
  end
  { iso: iso_bibitem, noniso: non_iso_bibitem }
end

#stage_abbrev(stage, iter, draft) ⇒ Object



26
27
28
29
30
31
# File 'lib/isodoc/utils.rb', line 26

def stage_abbrev(stage, iter, draft)
  stage = STAGE_ABBRS[stage.to_sym] || "??"
  stage += iter.text if iter
  stage = "Pre" + stage if draft&.text =~ /^0\./
  stage
end

#subtitle(isoxml, _out) ⇒ Object



192
193
194
195
196
197
198
199
200
# File 'lib/isodoc/metadata.rb', line 192

def subtitle(isoxml, _out)
  intro = isoxml.at(ns("//title-intro[@language='fr']"))
  main = isoxml.at(ns("//title-main[@language='fr']"))
  part = isoxml.at(ns("//title-part[@language='fr']"))
  partnumber = isoxml.at(ns("//project-number/@part"))
  subpartnumber = isoxml.at(ns("//project-number/@subpart"))
  main = compose_title(main, intro, part, partnumber, subpartnumber, "fr")
  (:docsubtitle, main)
end

#symbol_key(x) ⇒ Object

We assume AsciiMath is being used in the terms & definitions. Indices sort after letter but before any following letter (x, x_m, x_1, xa); we use colon to force that sort order. Numbers sort after letters; we use thorn to force that sort order.



168
169
170
171
# File 'lib/isodoc/cleanup.rb', line 168

def symbol_key(x)
  HTMLEntities.new.decode(x.text).gsub(/_/, ":").gsub(/`/, "").
    gsub(/[0-9]+/, "þ\\1")
end

#symbols_cleanup(docxml) ⇒ Object



185
186
187
188
189
190
191
192
# File 'lib/isodoc/cleanup.rb', line 185

def symbols_cleanup(docxml)
  dl = docxml.at("//div[@class = 'Symbols']/dl")
  return docxml unless dl
  dl_out = extract_symbols_list(dl)
  dl_out.sort! { |a, b| a[:key] <=> b[:key] }
  dl.replace(dl_out.map { |d| d[:dt].to_s + d[:dd].to_s }.join("\n"))
  docxml
end

#table_cleanup(docxml) ⇒ Object



158
159
160
161
162
# File 'lib/isodoc/cleanup.rb', line 158

def table_cleanup(docxml)
  table_footnote_cleanup(docxml)
  table_note_cleanup(docxml)
  docxml
end

#table_footnote_cleanup(docxml) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/isodoc/cleanup.rb', line 99

def table_footnote_cleanup(docxml)
  docxml.xpath("//table[descendant::aside]").each do |t|
    t.xpath(".//aside").each do |a|
      merge_fnref_into_fn_text(a)
      a.name = "div"
      a["class"] = "TableFootnote"
      t << a.remove
    end
  end
  # preempt html2doc putting MsoNormal there
  docxml.xpath("//p[not(self::*[@class])]"\
               "[ancestor::*[@class = 'TableFootnote']]").each do |p|
    p["class"] = "TableFootnote"
  end
end

#table_footnote_parse(node, out) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/isodoc/footnotes.rb', line 49

def table_footnote_parse(node, out)
  fn = node["reference"]
  tid = get_table_ancestor_id(node)
  make_table_footnote_link(out, tid + fn, fn)
  # do not output footnote text if we have already seen it for this table
  return if @seen_footnote.include?(tid + fn)
  @in_footnote = true
  out.aside **{ class: "footnote" } do |a|
    a << make_table_footnote_text(node, tid + fn, fn)
  end
  @in_footnote = false
  @seen_footnote << (tid + fn)
end

#table_get_or_make_tfoot(t) ⇒ Object



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

def table_get_or_make_tfoot(t)
  tfoot = t.at(".//tfoot")
  if tfoot.nil?
    t.add_child("<tfoot></tfoot>")
    tfoot = t.at(".//tfoot")
  else
    tfoot.xpath(".//td | .//th").each { |td| remove_bottom_border(td) }
  end
  tfoot
end

#table_note_cleanup(docxml) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/isodoc/cleanup.rb', line 142

def table_note_cleanup(docxml)
  docxml.xpath("//table[div[@class = 'Note' or "\
               "@class = 'TableFootnote']]").each do |t|
    tfoot = table_get_or_make_tfoot(t)
    insert_here = new_fullcolspan_row(t, tfoot)
    t.xpath("div[@class = 'Note' or @class = 'TableFootnote']").each do |d|
      d.parent = insert_here
    end
  end
  # preempt html2doc putting MsoNormal there
  docxml.xpath("//p[not(self::*[@class])]"\
               "[ancestor::*[@class = 'Note']]").each do |p|
    p["class"] = "Note"
  end
end

#table_parse(node, out) ⇒ Object



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

def table_parse(node, out)
  @in_table = true
  table_title_parse(node, out)
  out.table **make_table_attr(node) do |t|
    thead_parse(node, t)
    tbody_parse(node, t)
    tfoot_parse(node, t)
    (dl = node.at(ns("./dl"))) && parse(dl, out)
    node.xpath(ns("./note")).each { |n| parse(n, out) }
  end
  @in_table = false
  # out.p { |p| p << "&nbsp;" }
end

#table_title_parse(node, out) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/isodoc/table.rb', line 3

def table_title_parse(node, out)
  name = node.at(ns("./name"))
  out.p **{ class: "TableTitle", align: "center" } do |p|
    p << l10n("#{@table_lbl} #{get_anchors[node['id']][:label]}")
    p << l10n("&nbsp;&mdash; #{name.text}") if name
  end
end

#tbody_parse(node, t) ⇒ Object



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

def tbody_parse(node, t)
  tbody = node.at(ns("./tbody"))
  t.tbody do |h|
    tbody.element_children.each_with_index do |n, i|
      tr_parse(n, h, i, tbody.element_children.size, false)
    end
  end
end

#tc(xml) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/isodoc/metadata.rb', line 33

def tc(xml)
  tc_num = xml.at(ns("//editorialgroup/technical-committee/@number"))
  tc_type = xml.at(ns("//editorialgroup/technical-committee/@type"))&.
    text || "TC"
  if tc_num
    tcid = "#{tc_type} #{tc_num.text}"
    (:tc,  tcid)
    (:editorialgroup, [:editorialgroup] << tcid)
  end
end

#term_header(docxml) ⇒ Object



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

def term_header(docxml)
  %w(h1 h2 h3 h4 h5 h6 h7 h8).each do |h|
    docxml.xpath("//p[@class = 'TermNum'][../#{h}]").each do |p|
      p.name = "h#{h[1].to_i + 1}"
    end
  end
  docxml
end

#term_parse(node, out) ⇒ Object



26
27
28
29
30
# File 'lib/isodoc/terms.rb', line 26

def term_parse(node, out)
  out.p **{ class: "Terms", style:"text-align:left;" } do |p|
    node.children.each { |c| parse(c, p) }
  end
end

#termdef_parse(node, out) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/isodoc/terms.rb', line 59

def termdef_parse(node, out)
  out.p **{ class: "TermNum", id: node["id"] } do |p|
    p << get_anchors[node["id"]][:label]
  end
  set_termdomain("")
  node.children.each { |n| parse(n, out) }
end

#termexample_anchor_names(docxml) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/isodoc/xref_gen.rb', line 26

def termexample_anchor_names(docxml)
  docxml.xpath(ns("//term[termexample]")).each do |t|
    t.xpath(ns("./termexample")).each_with_index do |n, i|
      @anchors[n["id"]] =
        { label: (i + 1).to_s,
          xref: l10n("#{@anchors[t['id']][:xref]}, "\
                     "#{@note_xref_lbl} #{i + 1}") }
    end
  end
end

#termnote_anchor_names(docxml) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/isodoc/xref_gen.rb', line 15

def termnote_anchor_names(docxml)
  docxml.xpath(ns("//term[termnote]")).each do |t|
    t.xpath(ns("./termnote")).each_with_index do |n, i|
      @anchors[n["id"]] =
        { label: termnote_label(i + 1),
          xref: l10n("#{@anchors[t['id']][:xref]}, "\
                     "#{@note_xref_lbl} #{i + 1}") }
    end
  end
end

#termnote_label(n) ⇒ Object



11
12
13
# File 'lib/isodoc/xref_gen.rb', line 11

def termnote_label(n)
  @termnote_lbl.gsub(/%/, n.to_s)
end

#termnote_parse(node, out) ⇒ Object



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

def termnote_parse(node, out)
  out.div **{ class: "Note" } do |div|
    first = node.first_element_child
    div.p **{ class: "Note" } do |p|
      p << "#{get_anchors[node['id']][:label]}: "
      para_then_remainder(first, node, p, div)
    end
  end
end

#termref_parse(node, out) ⇒ Object



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

def termref_parse(node, out)
  out.p do |p|
    p << "[TERMREF]"
    node.children.each { |n| parse(n, p) }
    p << "[/TERMREF]"
  end
end

#tfoot_parse(node, t) ⇒ Object



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

def tfoot_parse(node, t)
  tfoot = node.at(ns("./tfoot"))
  if tfoot
    t.tfoot do |h|
      tfoot.element_children.each_with_index do |n, i|
        tr_parse(n, h, i, tfoot.element_children.size, false)
      end
    end
  end
end

#thead_parse(node, t) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/isodoc/table.rb', line 11

def thead_parse(node, t)
  thead = node.at(ns("./thead"))
  if thead
    t.thead do |h|
      thead.element_children.each_with_index do |n, i|
        tr_parse(n, h, i, thead.element_children.size, true)
      end
    end
  end
end

#title(isoxml, _out) ⇒ Object



182
183
184
185
186
187
188
189
190
# File 'lib/isodoc/metadata.rb', line 182

def title(isoxml, _out)
  intro = isoxml.at(ns("//title-intro[@language='en']"))
  main = isoxml.at(ns("//title-main[@language='en']"))
  part = isoxml.at(ns("//title-part[@language='en']"))
  partnumber = isoxml.at(ns("//project-number/@part"))
  subpartnumber = isoxml.at(ns("//project-number/@subpart"))
  main = compose_title(main, intro, part, partnumber, subpartnumber, "en")
  (:doctitle, main)
end

#to_xhtml(xml) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/isodoc/utils.rb', line 59

def to_xhtml(xml)
  xml.gsub!(/<\?xml[^>]*>/, "")
  unless /<!DOCTYPE /.match? xml
    xml = '<!DOCTYPE html SYSTEM
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' + xml
  end
  Nokogiri::XML.parse(xml)
end

#to_xhtml_fragment(xml) ⇒ Object



68
69
70
71
72
# File 'lib/isodoc/utils.rb', line 68

def to_xhtml_fragment(xml)
  doc = ::Nokogiri::XML.parse(NOKOHEAD)
  fragment = doc.fragment(xml)
  fragment
end

#toHTML(result, filename) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/isodoc/html.rb', line 16

def toHTML(result, filename)
  result = script_cdata(from_xhtml(html_cleanup(to_xhtml(result))))
  result = populate_template(result, :html)
  File.open("#{filename}.html", "w") do |f|
    f.write(result)
  end
end

#tr_parse(node, out, ord, totalrows, _header) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/isodoc/table.rb', line 83

def tr_parse(node, out, ord, totalrows, _header)
  out.tr do |r|
    node.elements.each do |td|
      attrs = make_tr_attr(td, ord, totalrows - 1)
      # i, node.elements.size - 1, header)
      r.send td.name, **attr_code(attrs) do |entry|
        td.children.each { |n| parse(n, entry) }
      end
    end
  end
end

#ul_parse(node, out) ⇒ Object



3
4
5
6
7
# File 'lib/isodoc/lists.rb', line 3

def ul_parse(node, out)
  out.ul do |ul|
    node.children.each { |n| parse(n, ul) }
  end
end

#update_footnote_filter(fn, x, i, seen) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/isodoc/html.rb', line 128

def update_footnote_filter(fn, x, i, seen)
  if seen[fn.text]
    x.at("./sup").content = seen[fn.text][:num].to_s
    fn.remove unless x["href"] == seen[fn.text][:href]
    x["href"] = seen[fn.text][:href]
  else
    seen[fn.text] = { num: i, href: x["href"] }
    x.at("./sup").content = i.to_s
    i += 1
  end
  [i, seen]
end

#version(isoxml, _out) ⇒ Object



152
153
154
155
156
157
158
# File 'lib/isodoc/metadata.rb', line 152

def version(isoxml, _out)
  (:docyear, isoxml&.at(ns("//copyright/from"))&.text)
  (:draft, isoxml&.at(ns("//version/draft"))&.text)
  (:revdate, isoxml&.at(ns("//version/revision-date"))&.text)
  (:draftinfo,
               draftinfo([:draft], [:revdate]))
end

#wg(xml) ⇒ Object



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

def wg(xml)
  wg_num = xml.at(ns("//editorialgroup/workgroup/@number"))
  wg_type = xml.at(ns("//editorialgroup/workgroup/@type"))&.text || "WG"
  if wg_num
    wgid = "#{wg_type} #{wg_num.text}"
    (:wg, wgid)
    (:editorialgroup, [:editorialgroup] << wgid)
  end
end

#wrap_comment_cont(from, target) ⇒ Object



93
94
95
96
# File 'lib/isodoc/comments.rb', line 93

def wrap_comment_cont(from, target)
  s = from.replace("<span style='mso-comment-continuation:#{target}'>")
  s.first.children = from
end