Class: IsoDoc::Convert

Inherits:
Object
  • Object
show all
Defined in:
lib/isodoc.rb,
lib/isodoc/html.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

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"]'
COMMENT_TARGET_XREFS =
"//span[@style='mso-special-character:comment']/@target"
SW =
"solid windowtext"
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 = "<!DOCTYPE html SYSTEM\n\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head> <title></title> <meta charset=\"UTF-8\" /> </head>\n<body> </body> </html>\n"
FIGURE_WITH_FOOTNOTES =
"//div[@class = 'figure'][descendant::aside]"\
"[not(descendant::div[@class = 'figure'])]".freeze
TABLE_WITH_FOOTNOTES =
"//table[descendant::aside]".freeze
ISO_PUBLISHER_XPATH =
"./contributor[xmlns:role/@type = 'publisher']/organization[name = 'ISO']"
NORM_WITH_REFS_PREF =
"id": b["id"], class: "Biblio") do |r|
    ref_entry_code(r, ordinal, ref.text.gsub(/[\[\]]/, ""))
    para.children.each { |n| parse(n, r) }
  end
end

ISO_PUBLISHER_XPATH =
  "./contributor[xmlns:role/@type = 'publisher']/organization[name = 'ISO']"

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

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

NORM_WITH_REFS_PREF = "The following documents are referred to in the text in such a way\nthat some or all of their content constitutes requirements of this\ndocument. For dated references, only the edition cited applies.\nFor undated references, the latest edition of the referenced\ndocument (including any amendments) applies.\n"
NORM_EMPTY_PREF =
"There are no normative references in this document."
WORD_TOC_PREFACE =
"<span lang=\"EN-GB\"><span\n  style='mso-element:field-begin'></span><span \n  style='mso-spacerun:yes'>&#xA0;</span>TOC\n  \\\\o &quot;1-2&quot; \\\\h \\\\z \\\\u <span \n  style='mso-element:field-separator'></span></span>\n"
WORD_TOC_SUFFIX =
"<p class=\"MsoToc1\"><span lang=\"EN-GB\"><span \n  style='mso-element:field-end'></span></span><span \n  lang=\"EN-GB\"><o:p>&nbsp;</o:p></span></p>\n"

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/isodoc.rb', line 39

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

Instance Method Details

#admonition_cleanup(docxml) ⇒ Object



18
19
20
21
22
23
24
# 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
end

#admonition_parse(node, out) ⇒ Object



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

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



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

def agency(xml)
  agency = ""
  pub = 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



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

def anchor_names(docxml)
  initial_anchor_names(docxml)
  middle_anchor_names(docxml)
  back_anchor_names(docxml)
  table_note_anchor_names(docxml)
end

#annex_names(clause, num) ⇒ Object



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

def annex_names(clause, num)
  obligation = "(Informative)"
  obligation = "(Normative)" if clause["subtype"] == "normative"
  label = "<b>Annex #{num}</b><br/>#{obligation}"
  @anchors[clause["id"]] = { label: label,
                             xref: "Annex #{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



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

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



113
114
115
116
117
# File 'lib/isodoc/blocks.rb', line 113

def annotation_parse(node, out)
  out.p **{ class: "Sourcecode" } do |li|
    node.children.each { |n| parse(n, li) }
  end
end

#attr_code(attributes) ⇒ Object



54
55
56
57
58
59
# File 'lib/isodoc/utils.rb', line 54

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



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

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

#back_anchor_names(docxml) ⇒ Object



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

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



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

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



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

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



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

def bibliography(isoxml, out)
  q = "./*/references[title = 'Bibliography']"
  f = isoxml.at(ns(q)) or return
  page_break(out)
  out.div do |div|
    div.h1 "Bibliography", **{ 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

#clause_names(docxml, sect_num) ⇒ Object



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

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)
  admonition_cleanup(docxml)
  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

#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, lang) ⇒ Object



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

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

#convert(filename) ⇒ Object



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

def convert(filename)
  docxml = Nokogiri::XML(File.read(filename))
  filename, dir = init_file(filename)
  docxml.root.default_namespace = ""
  result = 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")
  postprocess(result, filename, dir)
end

#date_note_process(b, ref) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/isodoc/references.rb', line 11

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.



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

def define_head(html, filename, dir)
  html.head do |head|
    head.title { |t| t << filename }
    head.style do |style|
      stylesheet = File.read(@standardstylesheet).
        gsub("FILENAME", filename)
      style.comment "\n#{stylesheet}\n"
    end
  end
end

#dl_parse(node, out) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/isodoc/lists.rb', line 35

def dl_parse(node, out)
  out.dl do |v|
    node.elements.each_slice(2) do |dt, dd|
      v.dt do |term|
        if dt.elements.empty?
          term.p **attr_code(class: is_note ? "Note" : nil) do
            |p| p << dt.text
          end
        else
          dt.children.each { |n| parse(n, term) }
        end
      end
      v.dd do |listitem|
        dd.children.each { |n| parse(n, listitem) }
      end
    end
  end
end

#draftinfo(draft, revdate) ⇒ Object



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

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

#example_parse(node, out) ⇒ Object



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

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

#figure_aside_process(f, aside, key) ⇒ Object



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

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



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

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



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

def figure_get_or_make_dl(t)
  dl = t.at(".//dl")
  if dl.nil?
    t.add_child("<p><b>Key</b></p><dl></dl>")
    dl = t.at(".//dl")
  end
  dl
end

#figure_key(out) ⇒ Object



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

def figure_key(out)
  out.p do |p| 
    p.b { |b| b << "Key" }
  end
end

#figure_name_parse(node, div, name) ⇒ Object



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

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

#figure_parse(node, out) ⇒ Object



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

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


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

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
    sup = x.at("./sup").text
    fn = docxml.at(%<//*[@id = '#{x['href'].sub(/^#/, '')}']>) || next
    x["id"] || x["id"] = "_footnote#{i + 1}"
    fn.elements.first.children.first.
      add_previous_sibling("<a href='##{x['id']}'>#{sup}) </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, 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



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

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



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

def formula_parse(node, out)
  dl = node.at(ns("./dl"))
  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(dl, out) if dl
end

#formula_where(dl, out) ⇒ Object



129
130
131
132
# File 'lib/isodoc/blocks.rb', line 129

def formula_where(dl, out)
         out.p { |p| p << "where" }
    parse(dl, out)
end

#from_xhtml(xml) ⇒ Object



76
77
78
# File 'lib/isodoc/utils.rb', line 76

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

#generate_header(filename, dir) ⇒ Object



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

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



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

def get_anchors
  @anchors
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



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

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

#get_termexampleObject



8
9
10
# File 'lib/isodoc/blocks.rb', line 8

def get_termexample
  @termexample
end

#header_strip(h) ⇒ Object



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

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(".//*[@style = 'MsoCommentReference']").each do |x|
  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



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

def hierarchical_asset_names(clause, num)
  clause.xpath(ns(".//table")).each_with_index do |t, i|
    @anchors[t["id"]] = { label: "Table #{num}.#{i + 1}",
                          xref: "Table #{num}.#{i + 1}" }
  end
  hierarchical_figure_names(clause, num)
  clause.xpath(ns(".//formula")).each_with_index do |t, i|
    @anchors[t["id"]] = { label: "#{num}.#{i + 1}",
                          xref: "Formula #{num}.#{i + 1}" }
  end
end

#hierarchical_figure_names(clause, num) ⇒ Object



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

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

#html_cleanup(x) ⇒ Object



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

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

#html_footnote_filter(docxml) ⇒ Object



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

def html_footnote_filter(docxml)
  seen = {}
  i = 1
  docxml.xpath('//a[@epub:type = "footnote"]').each do |x|
    i, seen = update_footnote_filter(docxml, 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



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

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

#htmlPreface(docxml) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/isodoc/html.rb', line 17

def htmlPreface(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')
  body = docxml.at("//*[local-name() = 'body']")
  body << '<script src="https://cdn.mathjax.org/mathjax/latest/'\
    'MathJax.js?config=AM_HTMLorMML"></script>'
  docxml
end

#htmlstyle(docxml) ⇒ Object



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

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



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

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

#id(isoxml, _out) ⇒ Object



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

def id(isoxml, _out)
  docnumber = isoxml.at(ns("//project-number"))
  partnumber = isoxml.at(ns("//project-number/@part"))
  documentstatus = isoxml.at(ns("//status/stage"))
  dn = docnumber.text
  dn += "-#{partnumber.text}" if partnumber
  if documentstatus
    (:stage, documentstatus.text)
    abbr = stage_abbreviation(documentstatus.text)
    (:stageabbr, abbr)
    documentstatus.text.to_i < 60 and
      dn = abbr + " " + dn
  end
  (:docnumber, dn)
end

#image_parse(url, out, caption) ⇒ Object



195
196
197
198
# File 'lib/isodoc/blocks.rb', line 195

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

#image_title_parse(out, caption) ⇒ Object



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

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

#in_sourcecodeObject



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

def in_sourcecode
  @sourcecode
end

#init_metadataObject



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

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

#initial_anchor_names(d) ⇒ Object



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

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

#inline_header_cleanup(docxml) ⇒ Object



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

def inline_header_cleanup(docxml)
  docxml.xpath('//span[@class="zzMoveToFollowing"]').each do |x|
    n = x.next_element
    if n.nil?
      html = Nokogiri::XML.fragment("<p></p>")
      html.parent = x.parent
      x.parent = html
    else
      n.children.first.add_previous_sibling(x.remove)
    end
  end
end

#insert_comment_cont(from, to, target, docxml) ⇒ 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, docxml)
  # 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



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

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

#introduction_names(clause) ⇒ Object



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

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

#is_noteObject



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

def is_note
  @note
end

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



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/isodoc/references.rb', line 19

def iso_bibitem_entry(list, b, ordinal, biblio)
  attrs = { id: b["id"], class: biblio ? "Biblio" : nil }
  list.p **attr_code(attrs) do |ref|
    if biblio
      ref << "[#{ordinal}]"
      insert_tab(ref, 1)
    end
    ref << iso_bibitem_ref_code(b)
    date_note_process(b, ref)
    ref << ", " if biblio
    ref.i { |i| i << " #{b.at(ns('./title')).text}" }
  end
end

#iso_bibitem_ref_code(b) ⇒ Object



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

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

#li_parse(node, out) ⇒ Object



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

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



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

def make_comment_link(out, fn, node)
  out.span **{ style: "MsoCommentReference", target: fn,
               class: "commentLink", from: node['from'],
               to: node['to']} 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|
                   #s << "&nbsp;"
                   #end
                 end
               end
end

#make_comment_target(out) ⇒ Object



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

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" } # do |s|
      # s << "&nbsp;"
      # end
    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, fn_ref) ⇒ Object



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

def make_generic_footnote_text(node, fnid, fn_ref)
  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



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

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, col, totalcols, header) ⇒ Object

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, col, totalcols, header)
  style = td.name == "th" ? "font-weight:bold;" : ""
  rowmax = td["rowspan"] ? row + td["rowspan"].to_i - 1 : row
  style += "      border-top:\#{row.zero? ? \"\#{SW} 1.5pt;\" : \"none;\"}\n      mso-border-top-alt:\#{row.zero? ? \"\#{SW} 1.5pt;\" : \"none;\"}\n      border-bottom:\#{SW} \#{rowmax == totalrows ? \"1.5\" : \"1.0\"}pt;\n      mso-border-bottom-alt:\#{SW} \#{rowmax == totalrows ? \"1.5\" : \"1.0\"}pt;\n  STYLE\n  { rowspan: td[\"rowspan\"], colspan: td[\"colspan\"],\n    align: td[\"align\"], style: style.gsub(/\\n/, \"\") }\nend\n"

#makeWordToC(docxml) ⇒ Object



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

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



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

def middle_anchor_names(docxml)
  symbols_abbrevs = docxml.at(ns("//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



31
32
33
34
35
36
# File 'lib/isodoc/xref_gen.rb', line 31

def middle_section_asset_names(d)
  middle_sections = "//clause[title = 'Scope'] | "\
    "//references[title = 'Normative References'] | //terms | "\
    "//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], docxml)
    insert_comment_cont(attrs[:from], x["to"], x["target"], docxml)
  end
end


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

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

#move_images(docxml) ⇒ Object



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

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]}")
    # presupposes that the image source is local
    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



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

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



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

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



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

def noniso_bibitem(list, b, ordinal, bibliography)
  ref = b.at(ns("./docidentifier"))
  para = b.at(ns("./formattedref"))
  list.p **attr_code("id": b["id"], class: "Biblio") do |r|
    ref_entry_code(r, ordinal, ref.text.gsub(/[\[\]]/, ""))
    para.children.each { |n| parse(n, r) }
  end
end

#norm_ref(isoxml, out) ⇒ Object



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

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

#norm_ref_preface(f, div) ⇒ Object



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

def norm_ref_preface(f, div)
  refs = f.elements.select do |e|
    ["reference", "bibitem"].include? e.name
  end
  pref = if refs.empty? then self.class::NORM_EMPTY_PREF 
         else 
           self.class::NORM_WITH_REFS_PREF
         end
  div.p pref
end

#note_label(node) ⇒ Object



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

def note_label(node)
  n = get_anchors()[node["id"]]
  return "NOTE" if n.nil?
  n[:label]
end

#note_p_parse(node, div) ⇒ Object



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

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



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

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
      div.p **{ class: "Note" } do |p|
        p << note_label(node)
        insert_tab(p, 1)
      end
      node.children.each { |n| parse(n, div) }
    end
  end
  @note = false
end

#ns(xpath) ⇒ Object



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

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_parse(node, out) ⇒ Object



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

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

#ol_style(type) ⇒ Object



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

def ol_style(type)
  OL_STYLE[type.to_sym]
end

#para_attrs(node) ⇒ Object



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

def para_attrs(node)
  classtype = nil
  classtype = "Note" if @note
  # classtype = "MsoFootnoteText" if in_footnote
  classtype = "MsoCommentText" if in_comment
  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



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

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



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

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

#populate_template(docxml, _format) ⇒ Object



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

def populate_template(docxml, _format)
  meta = 
  docxml.
    gsub(/\[TERMREF\]\s*/, "[SOURCE: ").
    gsub(/\s*\[\/TERMREF\]\s*/, "]").
    gsub(/\s*\[ISOSECTION\]/, ", ").
    gsub(/\s*\[MODIFICATION\]/, ", modified &mdash; ")
  template = Liquid::Template.parse(docxml)
  template.render(meta.map { |k, v| [k.to_s, v] }.to_h)
end

#postprocess(result, filename, dir) ⇒ Object



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

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



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

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

#quote_parse(node, out) ⇒ Object



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

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



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

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



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

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



199
200
201
202
203
# File 'lib/isodoc/xref_gen.rb', line 199

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

#reference_names(ref) ⇒ Object



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

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

#remove_bottom_border(td) ⇒ Object



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

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']") or 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



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

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



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

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

#section_names(clause, num, level) ⇒ Object



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

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

#section_names1(clause, num, level) ⇒ Object



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

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

#sequential_asset_names(clause) ⇒ Object



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

def sequential_asset_names(clause)
  clause.xpath(ns(".//table")).each_with_index do |t, i|
    @anchors[t["id"]] = { label: "Table #{i + 1}",
                          xref: "Table #{i + 1}" }
  end
  sequential_figure_names(clause)
  clause.xpath(ns(".//formula")).each_with_index do |t, i|
    @anchors[t["id"]] = { label: (i + 1).to_s,
                          xref: "Formula #{i + 1}" }
  end
end

#sequential_figure_names(clause) ⇒ Object



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

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

#set_metadata(key, value) ⇒ Object



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

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

#set_termdomain(termdomain) ⇒ Object

attr_accessor :termdomain, :termexample, :sourcecode, :note



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

def set_termdomain(termdomain)
  @termdomain = termdomain
end

#set_termexample(value) ⇒ Object



12
13
14
# File 'lib/isodoc/blocks.rb', line 12

def set_termexample(value)
  @termexample = 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



93
94
95
96
97
98
99
# File 'lib/isodoc/blocks.rb', line 93

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



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

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



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

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



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

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

#subtitle(isoxml, _out) ⇒ Object



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

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"))
  main = compose_title(main, intro, part, partnumber, "fr")
  (:docsubtitle, main)
end

#table_cleanup(docxml) ⇒ Object



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

def table_cleanup(docxml)
  table_footnote_cleanup(docxml)
  table_note_cleanup(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_WITH_FOOTNOTES).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



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

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



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

def table_note_anchor_names(docxml)
  docxml.xpath(ns("//table[note]")).each do |t|
    t.xpath(ns("./note")).each_with_index do |n, i|
      @anchors[n["id"]] = { label: "NOTE #{i + 1}",
                            xref: "#{@anchors[t["id"]][:xref]},"\
                            "Note #{i + 1}" }
    end
  end
end

#table_note_cleanup(docxml) ⇒ Object



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

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



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

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")) and 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
12
13
# File 'lib/isodoc/table.rb', line 3

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

#tbody_parse(node, t) ⇒ Object



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

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



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

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



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

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: "Note #{i + 1} to entry",
                            xref: "#{@anchors[t["id"]][:xref]},"\
                            "Note #{i + 1}" }
    end
  end
end

#tfoot_parse(node, t) ⇒ Object



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

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



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

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



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

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"))
  main = compose_title(main, intro, part, partnumber, "en")
  (:doctitle, main)
end

#titlepage(_docxml, div) ⇒ Object



90
91
92
93
# File 'lib/isodoc/postprocessing.rb', line 90

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

#to_xhtml(xml) ⇒ Object



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

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



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

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

#toHTML(result, filename) ⇒ Object



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

def toHTML(result, filename)
  # result = html_cleanup(Nokogiri::HTML(result)).to_xml
  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



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

def toWord(result, filename, dir)
  result = from_xhtml(wordCleanup(to_xhtml(result)))
  result = populate_template(result, :word)
  Html2Doc.process(result, filename, @wordstylesheet, "header.html", 
                   dir, ['`', '`'])
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_with_index do |td, i|
      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(docxml, x, i, seen) ⇒ Object



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

def update_footnote_filter(docxml, x, i, seen)
  fn = docxml.at(%<//*[@id = '#{x['href'].sub(/^#/, '')}']>) || return
  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



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

def version(isoxml, _out)
  yr = isoxml.at(ns("//copyright/from"))
  (:docyear, yr.text)
  draft = isoxml.at(ns("//version/draft"))
  (:draft, draft.nil? ? nil : draft.text)
  revdate = isoxml.at(ns("//version/revision-date"))
  (:revdate, revdate.nil? ? nil : revdate.text)
  draftinfo = draftinfo(draft, revdate)
  (:draftinfo, draftinfo(draft, revdate))
end

#wg(xml) ⇒ Object



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

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

#wordAnnexCleanup(docxml) ⇒ Object

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



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

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

#wordCleanup(docxml) ⇒ Object



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

def wordCleanup(docxml)
  wordPreface(docxml)
  wordAnnexCleanup(docxml)
  docxml
end

#wordPreface(docxml) ⇒ Object



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

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

#wordTocEntry(toclevel, heading) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/isodoc/postprocessing.rb', line 95

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

#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