Class: IsoDoc::Convert

Inherits:
Object
  • Object
show all
Defined in:
lib/isodoc.rb,
lib/isodoc/html.rb,
lib/isodoc/i18n.rb,
lib/isodoc/lists.rb,
lib/isodoc/notes.rb,
lib/isodoc/table.rb,
lib/isodoc/utils.rb,
lib/isodoc/blocks.rb,
lib/isodoc/cleanup.rb,
lib/isodoc/metadata.rb,
lib/isodoc/xref_gen.rb,
lib/isodoc/references.rb,
lib/isodoc/postprocessing.rb

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
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
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
SECTIONS_XPATH =
"//foreword | //introduction | //sections/terms | //annex | "\
"//sections/clause | //references[not(ancestor::references)]".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[name = 'ISO' or xmlns:name = 'IEC']".freeze
WORD_TOC_PREFACE =
<<~TOC.freeze
  <span lang="EN-GB"><span
    style='mso-element:field-begin'></span><span
    style='mso-spacerun:yes'>&#xA0;</span>TOC
    \\o &quot;1-2&quot; \\h \\z \\u <span
    style='mso-element:field-separator'></span></span>
TOC
WORD_TOC_SUFFIX =
<<~TOC.freeze
  <p class="MsoToc1"><span lang="EN-GB"><span
    style='mso-element:field-end'></span></span><span
    lang="EN-GB"><o:p>&nbsp;</o:p></span></p>
TOC

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



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
68
69
70
71
72
73
74
# File 'lib/isodoc.rb', line 43

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

#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.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



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

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

#anchor_names(docxml) ⇒ Object

extract names for all anchors, xref and label



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

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)))
end

#anchor_struct(lbl, container, elem) ⇒ Object



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

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



198
199
200
201
202
203
204
205
206
207
208
# File 'lib/isodoc/xref_gen.rb', line 198

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



210
211
212
213
214
215
# File 'lib/isodoc/xref_gen.rb', line 210

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



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

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



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

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



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

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

#biblio_list(f, div, bibliography) ⇒ Object



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

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



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

def bibliography(isoxml, out)
  q = "./*/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



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

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

def clause_names(docxml, sect_num)
  q = "//clause[parent::sections][not(xmlns:title = 'Scope')]"
  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



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

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



133
134
135
136
137
# File 'lib/isodoc/notes.rb', line 133

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


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

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

#comments(div) ⇒ Object



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

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



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

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



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

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

#convert1(docxml, filename, dir) ⇒ Object



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

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



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

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



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

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

#define_head(html, filename, _dir) ⇒ Object

isodoc.css overrides any CSS injected by Html2Doc, which is inserted before this CSS.



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

def define_head(html, filename, _dir)
  html.head do |head|
    head.title { |t| t << filename }
    return unless @standardstylesheet
    head.style do |style|
      stylesheet = File.read(@standardstylesheet).
        gsub("FILENAME", filename)
      style.comment "\n#{stylesheet}\n"
    end
  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



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

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



105
106
107
108
109
110
111
112
113
# File 'lib/isodoc/metadata.rb', line 105

def draftinfo(draft, revdate)
  draftinfo = ""
  if draft
    draftinfo = " (#{@draft_lbl} #{draft.text}"
    draftinfo += ", #{revdate.text}" 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



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

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



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

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



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

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



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/isodoc/xref_gen.rb', line 81

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)



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

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

#extract_symbols_list(dl) ⇒ Object



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

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



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

def figure_cleanup(docxml)
  # move footnotes into key, and get rid of footnote reference
  # since it is in diagram
  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
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


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

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



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

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



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/isodoc/notes.rb', line 69

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



13
14
15
16
# File 'lib/isodoc/notes.rb', line 13

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

#format_ref(ref, isopub) ⇒ Object



145
146
147
148
149
# File 'lib/isodoc/references.rb', line 145

def format_ref(ref, isopub)
  return "ISO #{ref}" if isopub
  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



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

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

#generate_header(filename, _dir) ⇒ Object



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

def generate_header(filename, _dir)
  template = Liquid::Template.parse(File.read(@header, encoding: "UTF-8"))
  meta = 
  meta[:filename] = filename
  params = meta.map { |k, v| [k.to_s, v] }.to_h
  File.open("header.html", "w") do |f|
    f.write(template.render(params))
  end
end

#get_anchorsObject



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

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



195
196
197
198
199
200
201
202
203
# File 'lib/isodoc/notes.rb', line 195

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



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

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

#get_table_ancestor_id(node) ⇒ Object



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

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



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/isodoc/postprocessing.rb', line 153

def header_strip(h)
  h = h.to_s.gsub(%r{<br/>}, " ").
    sub(/<h[12][^>]*>/, "").sub(%r{</h[12]>}, "")
  h1 = to_xhtml_fragment(h)
  h1.xpath(".//*").each do |x|
    if x.name == "span" && x["style"] == "MsoCommentReference"
      x.children.remove
      x.content = ""
    end
  end
  from_xhtml(h1)
end

#hierarchical_asset_names(clause, num) ⇒ Object



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

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



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

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



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

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

#html_footnote_filter(docxml) ⇒ Object



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

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_header(html, docxml, filename, dir) ⇒ Object

these are in fact preprocess, but they are extraneous to main HTML file



92
93
94
95
# File 'lib/isodoc/postprocessing.rb', line 92

def html_header(html, docxml, filename, dir)
  anchor_names docxml
  define_head html, filename, dir
end

#html_preface(docxml) ⇒ Object



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

def html_preface(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")
  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")
  docxml.at("//*[local-name() = 'body']") << mathjax(@openmathdelim,
                                                     @closemathdelim)
  docxml
end

#htmlstyle(docxml) ⇒ Object



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

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



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

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



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/isodoc/metadata.rb', line 93

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



9
10
11
# File 'lib/isodoc/notes.rb', line 9

def in_comment
  @in_comment
end

#in_footnoteObject



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

def in_footnote
  @in_footnote
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



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

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")), "3", 1)
  middle_section_asset_names(d)
end

#inline_header_cleanup(docxml) ⇒ Object



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

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
end

#insert_comment_cont(from, to, target) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
# File 'lib/isodoc/notes.rb', line 175

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
13
14
# File 'lib/isodoc/utils.rb', line 10

def insert_tab(out, n)
  out.span **attr_code(style: "mso-tab-count:#{n}") do |span|
    [1..n].each { span << "&#xA0; " }
  end
end

#introduction_names(clause) ⇒ Object



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

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_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 += ": #{isodate.text}" 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



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

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

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



104
105
106
107
108
109
110
111
112
113
# File 'lib/isodoc/notes.rb', line 104

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



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

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



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

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



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

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


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

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



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

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



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

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

#make_WordToC(docxml) ⇒ Object



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

def make_WordToC(docxml)
  toc = ""
  docxml.xpath("//h1 | //h2[not(ancestor::*[@class = 'Section3'])]").
    each do |h|
    toc += word_toc_entry(h.name == "h1" ? 1 : 2, header_strip(h))
  end
  toc.sub(/(<p class="MsoToc1">)/,
          %{\\1#{WORD_TOC_PREFACE}}) + WORD_TOC_SUFFIX
end

#mathjax(open, close) ⇒ Object



30
31
32
# File 'lib/isodoc/html.rb', line 30

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

#merge_fnref_into_fn_text(a) ⇒ Object



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

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



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

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)
end

#middle_section_asset_names(d) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/isodoc/xref_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


187
188
189
190
191
192
193
# File 'lib/isodoc/notes.rb', line 187

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


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

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



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

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



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

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;mso-border-top-alt:0pt;
  border-bottom:#{SW} 1.5pt;mso-border-bottom-alt:#{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



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

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



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

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
    b.at(ns("./formattedref")).children.each { |n| parse(n, r) }
  end
end

#norm_ref(isoxml, out) ⇒ Object



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

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



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

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



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

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

#part_label(lang) ⇒ Object

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



130
131
132
133
134
135
# File 'lib/isodoc/metadata.rb', line 130

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

#populate_template(docxml, _format) ⇒ Object



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

def populate_template(docxml, _format)
  meta = 
  docxml = docxml.
    gsub(/\[TERMREF\]\s*/, l10n("[#{@source_lbl}: ")).
    gsub(/\s*\[\/TERMREF\]\s*/, l10n("]")).
    gsub(/\s*\[ISOSECTION\]/, 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



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

def postprocess(result, filename, dir)
  generate_header(filename, dir)
  result = from_xhtml(cleanup(to_xhtml(result)))
  toWord(result, filename, dir)
  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(list, b, ordinal, _bibliography) ⇒ Object



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

def ref_entry(list, b, ordinal, _bibliography)
  ref = b.at(ns("./ref"))
  para = b.at(ns("./p"))
  list.p **attr_code("id": ref["id"], class: "Biblio") do |r|
    ref_entry_code(r, ordinal, ref.text.gsub(/[\[\]]/, ""))
    para.children.each { |n| parse(n, r) }
  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

#ref_names(ref) ⇒ Object



161
162
163
164
165
# File 'lib/isodoc/references.rb', line 161

def ref_names(ref)
  linkend = ref.text
  linkend.gsub!(/[\[\]]/, "") unless /^\[\d+\]$/.match? linkend
  @anchors[ref["id"]] = { xref: linkend }
end

#reference_names(ref) ⇒ Object



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

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

#relations(isoxml, _out) ⇒ Object



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

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)
  (:obsoletes_part, locality.text)
end

#remove_bottom_border(td) ⇒ Object



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

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


208
209
210
211
212
213
214
215
216
# File 'lib/isodoc/notes.rb', line 208

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



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

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



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

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



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

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



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

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



138
139
140
141
142
143
144
145
146
# File 'lib/isodoc/xref_gen.rb', line 138

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



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

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



171
172
173
# File 'lib/isodoc/notes.rb', line 171

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



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

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



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

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

#subtitle(isoxml, _out) ⇒ Object



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

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. 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.



150
151
152
153
# File 'lib/isodoc/cleanup.rb', line 150

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

#symbols_cleanup(docxml) ⇒ Object



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

def symbols_cleanup(docxml)
  dl = docxml.at("//div[@class = 'Symbols']/dl") || return
  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"))
end

#table_cleanup(docxml) ⇒ Object



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

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

#table_footnote_cleanup(docxml) ⇒ Object



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

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



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

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



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

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

#termnote_anchor_names(docxml) ⇒ Object



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

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



42
43
44
# File 'lib/isodoc/xref_gen.rb', line 42

def termnote_label(n)
  @termnote_lbl.gsub(/%/, n.to_s)
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



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

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

#titlepage(_docxml, div) ⇒ Object



111
112
113
114
# File 'lib/isodoc/postprocessing.rb', line 111

def titlepage(_docxml, div)
  titlepage = File.read(@wordcoverpage, encoding: "UTF-8")
  div.parent.add_child titlepage
end

#to_xhtml(xml) ⇒ Object



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

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



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

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

#toHTML(result, filename) ⇒ Object



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

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

#toWord(result, filename, dir) ⇒ Object



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

def toWord(result, filename, dir)
  result = from_xhtml(word_cleanup(to_xhtml(result)))
  result = populate_template(result, :word)
  Html2Doc.process(result, filename: filename, stylesheet: @wordstylesheet,
                   header_file: "header.html", dir: dir,
                   asciimathdelims: [@openmathdelim, @closemathdelim],
                   liststyles: { ul: @ulstyle, ol: @olstyle })
end

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



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

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



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

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



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

def version(isoxml, _out)
  (:docyear, isoxml&.at(ns("//copyright/from"))&.text)
  # draft = isoxml.at(ns("//version/draft"))
  # set_metadata(:draft, draft.nil? ? nil : draft.text)
  (:draft, isoxml&.at(ns("//version/draft"))&.text)
  # revdate = isoxml.at(ns("//version/revision-date"))
  #set_metadata(:revdate, revdate.nil? ? nil : revdate.text)
  (:revdate, isoxml&.at(ns("//version/revision-date"))&.text)
  #set_metadata(:draftinfo, draftinfo(draft, revdate))
  (: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

#word_annex_cleanup(docxml) ⇒ Object

force Annex h2 to be p.h2Annex, so it is not picked up by ToC



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

def word_annex_cleanup(docxml)
  docxml.xpath("//h2[ancestor::*[@class = 'Section3']]").each do |h2|
    h2.name = "p"
    h2["class"] = "h2Annex"
  end
end

#word_cleanup(docxml) ⇒ Object



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

def word_cleanup(docxml)
  word_preface(docxml)
  word_annex_cleanup(docxml)
  word_dl_cleanup(docxml)
  docxml
end

#word_dl_cleanup(docxml) ⇒ Object



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

def word_dl_cleanup(docxml)
  docxml.xpath("//dl").each do |dl|
    dl.name = "table"
    extract_symbols_list(dl).each do |dtd|
      tr = dl.add_child("<tr></tr>").first
      word_dl_cleanup1(dtd, tr)
    end
  end
end

#word_dl_cleanup1(dtd, tr) ⇒ Object



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

def word_dl_cleanup1(dtd, tr)
  dtd[:dt].name = "td"
  dtd[:dt]["valign"] = "top"
  dtd[:dt].parent = tr
  dtd[:dd].name = "td"
  dtd[:dd]["valign"] = "top"
  dtd[:dd].parent = tr
end

#word_preface(docxml) ⇒ Object



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

def word_preface(docxml)
  cover = to_xhtml_fragment(File.read(@wordcoverpage, encoding: "UTF-8"))
  docxml.at('//div[@class="WordSection1"]').children.first.previous =
    cover.to_xml(encoding: "US-ASCII")
  intro = to_xhtml_fragment(File.read(@wordintropage, encoding: "UTF-8").
                            sub(/WORDTOC/, make_WordToC(docxml)))
  docxml.at('//div[@class="WordSection2"]').children.first.previous =
    intro.to_xml(encoding: "US-ASCII")
end

#word_toc_entry(toclevel, heading) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/isodoc/postprocessing.rb', line 116

def word_toc_entry(toclevel, heading)
  bookmark = Random.rand(1000000000)
  <<~TOC
    <p class="MsoToc#{toclevel}"><span class="MsoHyperlink"><span
    lang="EN-GB" style='mso-no-proof:yes'>
    <a href="#_Toc#{bookmark}">#{heading}<span lang="EN-GB"
    class="MsoTocTextSpan">
    <span style='mso-tab-count:1 dotted'>. </span>
    </span><span lang="EN-GB" class="MsoTocTextSpan">
    <span style='mso-element:field-begin'></span></span>
    <span lang="EN-GB"
    class="MsoTocTextSpan"> PAGEREF _Toc#{bookmark} \\h </span>
      <span lang="EN-GB" class="MsoTocTextSpan"><span
      style='mso-element:field-separator'></span></span><span
      lang="EN-GB" class="MsoTocTextSpan">1</span>
      <span lang="EN-GB"
      class="MsoTocTextSpan"></span><span
      lang="EN-GB" class="MsoTocTextSpan"><span
      style='mso-element:field-end'></span></span></a></span></span></p>

  TOC
end

#wrap_comment_cont(from, target) ⇒ Object



166
167
168
169
# File 'lib/isodoc/notes.rb', line 166

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