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

WordConvert

Constant Summary collapse

MATHJAX_ADDR =
"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js".freeze
MATHJAX =
<<~"MATHJAX".freeze
  <script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      asciimath2jax: {
        delimiters: [['OPEN', 'CLOSE']]
      }
   });
  </script>
  <script src="#{MATHJAX_ADDR}?config=AM_HTMLorMML"></script>
MATHJAX
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_abbreviation(stage)
  STAGE_ABBRS[stage.to_sym] || "??"
end

NOKOHEAD = <<~HERE.freeze
<!DOCTYPE html SYSTEM
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head> <title></title> <meta charset="UTF-8" /> </head>
<body> </body> </html>
HERE
CLAUSE_ANCESTOR =
".//ancestor::*[local-name() = 'subsection' or "\
"local-name() = 'foreword' or "\
"local-name() = 'introduction' or local-name() = 'terms' or "\
"local-name() = 'clause' or local-name() = 'references' or "\
"local-name() = 'annex']/@id".freeze
NOTE_CONTAINER_ANCESTOR =
".//ancestor::*[local-name() = 'subsection' or "\
"local-name() = 'foreword' or "\
"local-name() = 'introduction' or local-name() = 'terms' or "\
"local-name() = 'clause' or local-name() = 'references' or "\
"local-name() = 'annex' or local-name() = 'formula' or "\
"local-name() = 'table' or local-name() = 'example' or "\
"local-name() = 'figure']/@id".freeze
EXAMPLE_TBL_ATTR =
{ width: "110pt", valign: "top",
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
SECTIONS_XPATH =
"//foreword | //introduction | //sections/terms | //annex | "\
"//sections/clause | //references[not(ancestor::clause)] | "\
"//clause[descendant::references][not(ancestor::clause)]".freeze
CHILD_NOTES_XPATH =
"./*[not(self::xmlns:subsection)]//xmlns:note | ./xmlns:note".freeze
CHILD_EXAMPLES_XPATH =
"./*[not(self::xmlns:subsection)]//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

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



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
# File 'lib/isodoc/convert.rb', line 33

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]
  @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"
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" } do |p|
    node.children.each { |c| parse(c, p) }
  end
end

#admonition_cleanup(docxml) ⇒ Object



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

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 << name.upcase } if name
    node.children.each do |n|
      parse(n, t)
    end
  end
end

#agency(xml) ⇒ Object



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

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



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

def anchor_names(docxml)
  initial_anchor_names(docxml)
  middle_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



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

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

#annex_names(clause, num) ⇒ Object



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

def annex_names(clause, num)
  obl = l10n("(#{@inform_annex_lbl})")
  obl = l10n("(#{@norm_annex_lbl})") if clause["obligation"] == "normative"
  label = l10n("<b>#{@annex_lbl} #{num}</b><br/>#{obl}")
  @anchors[clause["id"]] =
    { label: label, xref: "#{@annex_lbl} #{num}", level: 1 }
  clause.xpath(ns("./subsection")).each_with_index do |c, i|
    annex_names1(c, "#{num}.#{i + 1}", 2)
  end
  hierarchical_asset_names(clause, num)
end

#annex_names1(clause, num, level) ⇒ Object



75
76
77
78
79
80
# File 'lib/isodoc/xref_sect_gen.rb', line 75

def annex_names1(clause, num, level)
  @anchors[clause["id"]] = { label: num, xref: num, level: level }
  clause.xpath(ns(".//subsection")).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

#attr_code(attributes) ⇒ Object



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

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



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

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



76
77
78
79
80
# File 'lib/isodoc/metadata.rb', line 76

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



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

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



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

def bibliography(isoxml, out)
  q = "//clause[title = 'Bibliography'][descendant::references] | //references[title = 'Bibliography']"
  f = isoxml.at(ns(q)) || 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



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

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



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

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).to_s, 1)
  end
end

#cleanup(docxml) ⇒ Object



8
9
10
11
12
13
14
15
16
# 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)
  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



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

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, debug = false) ⇒ Object



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

def convert(filename, debug = false)
  convert_file(File.read(filename), filename, debug)
end

#convert1(docxml, filename, dir) ⇒ Object



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

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

#convert_file(file, filename, debug) ⇒ Object



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

def convert_file(file, filename, debug)
  @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

#convert_init(file, filename, debug) ⇒ Object



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

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



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

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



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

def date_range(date)
  from = date.at(ns("./from"))
  to = date.at(ns("./to"))
  ret = from.text
  ret += "&ndash;#{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" } do |p|
    p << l10n("#{@deprecated_lbl}: ")
    node.children.each { |c| parse(c, p) }
  end
end

#dl_parse(node, out) ⇒ Object



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

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

#docid_l10n(x) ⇒ Object



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

def docid_l10n(x)
  x.gsub(/All Parts/, @all_parts_lbl)
end

#docnumber(isoxml) ⇒ Object



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

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

#draftinfo(draft, revdate) ⇒ Object



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

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

#dt_parse(dt, term) ⇒ Object



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

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



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

def eref_localities1(type, from, to, lang = "en")
  subsection = from&.text&.match?(/\./)
  return l10n(eref_localities1_zh(type, from, to)) if lang == "zh"
  ret = ","
  ret += " #{@locality[type]}" 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



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

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

#example_anchor_names(sections) ⇒ Object



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

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("./subsection")))
  end
end

#example_div_parse(node, out) ⇒ Object

used if we are boxing examples



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

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



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

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



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

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" } do |td|
        node.children.each { |n| parse(n, td) }
      end
    end
  end
end

#extract_delims(text) ⇒ Object

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



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

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

#extract_symbols_list(dl) ⇒ Object



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

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



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/isodoc/cleanup.rb', line 40

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



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

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



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

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



49
50
51
52
53
# File 'lib/isodoc/blocks.rb', line 49

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

#figure_name_parse(node, div, name) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/isodoc/blocks.rb', line 40

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

#figure_parse(node, out) ⇒ Object



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

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


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

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"] = "_footnote#{i + 1}"
    fn.elements.first.children.first.previous =
      "<a href='##{x['id']}'>#{x.at('./sup').text}) </a>"
  end
  docxml
end

#footnote_cleanup(docxml) ⇒ Object



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

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



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

def footnote_parse(node, out)
  return table_footnote_parse(node, out) if @in_table || @in_figure
  fn = node["reference"]
  out.a **{ "epub:type": "footnote", href: "#ftn#{fn}" } do |a|
    a.sup { |sup| sup << fn }
  end
  return if @seen_footnote.include?(fn)
  @in_footnote = true
  @footnotes << make_generic_footnote_text(node, fn)
  @in_footnote = false
  @seen_footnote << 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



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

def format_ref(ref, isopub, date)
  if isopub
    return ref unless date
    from = date.at(ns("./from"))
    return ref if from&.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



71
72
73
# File 'lib/isodoc/utils.rb', line 71

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

#get_anchorsObject



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

def get_anchors
  @anchors
end

#get_clause_id(node) ⇒ Object



82
83
84
85
# File 'lib/isodoc/utils.rb', line 82

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



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

def 
  @meta
end

#get_note_container_id(node) ⇒ Object



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

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



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

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



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

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



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

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_cleanup(x) ⇒ Object



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

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

#html_cover(docxml) ⇒ Object



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

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

#html_footnote_filter(docxml) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/isodoc/html.rb', line 90

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_intro(docxml) ⇒ Object



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

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

#html_preface(docxml) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/isodoc/html.rb', line 39

def html_preface(docxml)
  html_cover(docxml) if @htmlcoverpage
  html_intro(docxml) if @htmlintropage
  docxml.at("//*[local-name() = 'body']") << mathjax(@openmathdelim,
                                                     @closemathdelim)
  docxml
end

#html_toc(docxml) ⇒ Object



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

def html_toc(docxml)
  return docxml unless @htmlintropage
  ret = ""
  prevname = ""
  docxml.xpath("//h1 | //h2").each do |h|
    ret = html_toc1(h, ret, prevname) unless h["class"] == "toc-contents"
    prevname = h.name
  end
  ret += "<ul>" if prevname == "h2"
  docxml.at("//*[@id='toc-list']").replace("<ul>#{ret}</ret>")
  docxml
end

#html_toc1(h, ret, prevname) ⇒ Object



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

def html_toc1(h, ret, prevname)
  h["id"] = UUIDTools::UUID.random_create.to_s unless h["id"]
  li = "<li><a href='##{h["id"]}'>#{header_strip(h)}</a></li>"
  if h.name == "h1"
    ret += "</ul>" if prevname == "h2"
  else
    ret += "<ul>" if prevname == "h1"
  end
  ret + li
end

#htmlstyle(docxml) ⇒ Object



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

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



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

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
# 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"]
  @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"]
  @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"]
end

#id(isoxml, _out) ⇒ Object



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

def id(isoxml, _out)
  dn = docnumber(isoxml)
  documentstatus = isoxml.at(ns("//status/stage"))
  if documentstatus
    (:stage, documentstatus.text)
    abbr = stage_abbreviation(documentstatus.text)
    (:stageabbr, abbr)
    (documentstatus.text.to_i < 60) && dn = abbr + " " + dn
  end
  (:docnumber, dn)
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



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

def 
  @meta = { tc: "XXXX", sc: "XXXX", wg: "XXXX",
            editorialgroup: [],
            secretariat: "XXXX",
            obsoletes: nil,
            obsoletes_part: nil }
  %w{published accessed created activated obsoleted}.each do |w|
    @meta["#{w}date".to_sym] = "XXX"
  end
end

#initial_anchor_names(d) ⇒ Object



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

def initial_anchor_names(d)
  introduction_names(d.at(ns("//introduction")))
  section_names(d.at(ns("//clause[title = 'Scope']")), "1", 1)
  section_names(d.at(ns(
    "//references[title = 'Normative References']")), "2", 1)
  section_names(d.at(ns("//sections/terms | //sections/clause[descendant::terms]")), "3", 1)
  middle_section_asset_names(d)
end

#inline_header_cleanup(docxml) ⇒ Object



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

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



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

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

#iso?(org) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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 << " #{b.at(ns('./title')).text}" }
  end
end

#iso_bibitem_entry_attrs(b, biblio) ⇒ Object



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

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

#iso_bibitem_ref_code(b) ⇒ Object



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

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

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

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



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

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



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

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

#list_anchor_names(sections) ⇒ Object



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

def list_anchor_names(sections)
  sections.each do |s|
    notes = s.xpath(ns(".//ol")) - s.xpath(ns(".//subsection//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("./subsection")))
  end
end

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



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

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



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

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']}" }
      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_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: "ftn#{fnid}" } do |div|
      node.children.each { |n| parse(n, div) }
    end
  end.join("\n")
end

#make_table_attr(node) ⇒ Object



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

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: "ftn#{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;



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

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 += <<~STYLE
    border-top:#{row.zero? ? "#{SW} 1.5pt;" : 'none;'}
    border-bottom:#{SW} #{rowmax == totalrows ? '1.5' : '1.0'}pt;
  STYLE
  { rowspan: td["rowspan"], colspan: td["colspan"],
    align: td["align"], style: style.gsub(/\n/, "") }
end

#mathjax(open, close) ⇒ Object



35
36
37
# File 'lib/isodoc/html.rb', line 35

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

#merge_fnref_into_fn_text(a) ⇒ Object



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

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_anchor_names(docxml) ⇒ Object



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

def middle_anchor_names(docxml)
  symbols_abbrevs = docxml.at(ns("//sections/symbols-abbrevs"))
  sect_num = 4
  if symbols_abbrevs
    section_names(symbols_abbrevs, sect_num.to_s, 1)
    sect_num += 1
  end
  clause_names(docxml, sect_num)
  termnote_anchor_names(docxml)
  termexample_anchor_names(docxml)
end

#middle_section_asset_names(d) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/isodoc/xref_sect_gen.rb', line 21

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



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

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



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

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



40
41
42
43
44
45
46
47
# File 'lib/isodoc/utils.rb', line 40

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



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

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) ⇒ Object



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

def norm_ref(isoxml, out)
  q = "./*/references[title = 'Normative References']"
  f = isoxml.at(ns(q)) || return
  out.div do |div|
    clause_name("2.", @normref_lbl, div, nil)
    norm_ref_preface(f, div)
    biblio_list(f, div, false)
  end
end

#norm_ref_preface(f, div) ⇒ Object



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

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



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

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("./subsection")))
  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
# File 'lib/isodoc/blocks.rb', line 11

def note_p_parse(node, div)
  div.p **{ class: "Note" } do |p|
    p << note_label(node)
    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



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

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



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

def note_parse1(node, div)
  div.p **{ class: "Note" } do |p|
    p << note_label(node)
    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]+ ?=)}, "[xmlns:\\1").
    gsub(%r{\[([a-zA-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)



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

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



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

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



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

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

#populate_template(docxml, _format) ⇒ Object



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

def populate_template(docxml, _format)
  meta = 
  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
# File 'lib/isodoc/html.rb', line 3

def postprocess(result, filename, dir)
  result = from_xhtml(cleanup(to_xhtml(result)))
  toHTML(result, filename)
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



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

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



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

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

#reference_names(ref) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'lib/isodoc/references.rb', line 154

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)
  # reference += ": #{date_range(date)}" if date && isopub && from.text != "--"
  @anchors[ref["id"]] = { xref: reference }
end

#relations(isoxml, _out) ⇒ Object



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

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



102
103
104
105
# File 'lib/isodoc/cleanup.rb', line 102

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



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

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

#secretariat(xml) ⇒ Object



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

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

#section_names(clause, num, lvl) ⇒ Object



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

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

#section_names1(clause, num, level) ⇒ Object



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

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

#sentence_join(array) ⇒ Object



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

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



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

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



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

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



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

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



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

def sourcecode_name_parse(_node, div, name)
  div.p **{ class: "FigureTitle", align: "center" } do |p|
    p.b do |b|
      b << name.text
    end
  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



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

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_abbreviation(stage) ⇒ Object



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

def stage_abbreviation(stage)
  STAGE_ABBRS[stage.to_sym] || "??"
end

#subtitle(isoxml, _out) ⇒ Object



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

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.



152
153
154
155
# File 'lib/isodoc/cleanup.rb', line 152

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

#symbols_cleanup(docxml) ⇒ Object



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

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



142
143
144
145
146
# File 'lib/isodoc/cleanup.rb', line 142

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

#table_footnote_cleanup(docxml) ⇒ Object



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

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"] = "Note"
      t << a.remove
    end
  end
end

#table_footnote_parse(node, out) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# 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 { |a| a << make_table_footnote_text(node, tid + fn, fn) }
  @in_footnote = false
  @seen_footnote << (tid + fn)
end

#table_get_or_make_tfoot(t) ⇒ Object



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

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



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

def table_note_cleanup(docxml)
  docxml.xpath("//table[div[@class = 'Note']]").each do |t|
    tfoot = table_get_or_make_tfoot(t)
    insert_here = new_fullcolspan_row(t, tfoot)
    t.xpath("div[@class = 'Note']").each { |d| d.parent = insert_here }
  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



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

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
10
11
# 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.b do |b|
      b << l10n("#{@table_lbl} #{get_anchors[node['id']][:label]}")
      b << l10n("&nbsp;&mdash; #{name.text}") if name
    end
  end
end

#tbody_parse(node, t) ⇒ Object



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

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



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

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_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" } 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



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

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



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

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



163
164
165
166
167
168
169
170
171
# File 'lib/isodoc/metadata.rb', line 163

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



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

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



65
66
67
68
69
# File 'lib/isodoc/utils.rb', line 65

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

#toHTML(result, filename) ⇒ Object



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

def toHTML(result, filename)
  result = 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



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

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



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

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



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

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



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

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