isodoc 1.6.0 → 1.6.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
- require_relative "html_function/comments.rb"
2
- require_relative "html_function/footnotes.rb"
3
- require_relative "html_function/html.rb"
4
- require_relative "html_function/postprocess.rb"
1
+ require_relative "html_function/comments"
2
+ require_relative "html_function/footnotes"
3
+ require_relative "html_function/html"
4
+ require_relative "html_function/postprocess"
5
5
 
6
6
  module IsoDoc
7
7
  class HtmlConvert < ::IsoDoc::Convert
@@ -1,4 +1,5 @@
1
1
  require "isodoc/html_function/mathvariant_to_plain"
2
+ require_relative "postprocess_footnotes"
2
3
 
3
4
  module IsoDoc::HtmlFunction
4
5
  module Html
@@ -46,6 +47,7 @@ module IsoDoc::HtmlFunction
46
47
 
47
48
  def htmlstylesheet(file)
48
49
  return if file.nil?
50
+
49
51
  file.open if file.is_a?(Tempfile)
50
52
  stylesheet = file.read
51
53
  xml = Nokogiri::XML("<style/>")
@@ -57,6 +59,7 @@ module IsoDoc::HtmlFunction
57
59
 
58
60
  def htmlstyle(docxml)
59
61
  return docxml unless @htmlstylesheet
62
+
60
63
  title = docxml.at("//*[local-name() = 'head']/*[local-name() = 'title']")
61
64
  head = docxml.at("//*[local-name() = 'head']")
62
65
  head << htmlstylesheet(@htmlstylesheet)
@@ -77,7 +80,7 @@ module IsoDoc::HtmlFunction
77
80
  def authority_cleanup1(docxml, klass)
78
81
  dest = docxml.at("//div[@id = 'boilerplate-#{klass}-destination']")
79
82
  auth = docxml.at("//div[@id = 'boilerplate-#{klass}' or @class = 'boilerplate-#{klass}']")
80
- auth&.xpath(".//h1[not(text())] | .//h2[not(text())]")&.each { |h| h.remove }
83
+ auth&.xpath(".//h1[not(text())] | .//h2[not(text())]")&.each(&:remove)
81
84
  auth&.xpath(".//h1 | .//h2")&.each { |h| h["class"] = "IntroTitle" }
82
85
  dest and auth and dest.replace(auth.remove)
83
86
  end
@@ -92,14 +95,18 @@ module IsoDoc::HtmlFunction
92
95
  doc = to_xhtml_fragment(File.read(@htmlcoverpage, encoding: "UTF-8"))
93
96
  d = docxml.at('//div[@class="title-section"]')
94
97
  # d.children.first.add_previous_sibling doc.to_xml(encoding: "US-ASCII")
95
- d.children.first.add_previous_sibling populate_template(doc.to_xml(encoding: "US-ASCII"), :html)
98
+ d.children.first.add_previous_sibling(
99
+ populate_template(doc.to_xml(encoding: "US-ASCII"), :html)
100
+ )
96
101
  end
97
102
 
98
103
  def html_intro(docxml)
99
104
  doc = to_xhtml_fragment(File.read(@htmlintropage, encoding: "UTF-8"))
100
105
  d = docxml.at('//div[@class="prefatory-section"]')
101
106
  # d.children.first.add_previous_sibling doc.to_xml(encoding: "US-ASCII")
102
- d.children.first.add_previous_sibling populate_template(doc.to_xml(encoding: "US-ASCII"), :html)
107
+ d.children.first.add_previous_sibling(
108
+ populate_template(doc.to_xml(encoding: "US-ASCII"), :html)
109
+ )
103
110
  end
104
111
 
105
112
  def html_toc_entry(level, header)
@@ -112,7 +119,9 @@ module IsoDoc::HtmlFunction
112
119
  end
113
120
 
114
121
  def toclevel
115
- ret = toclevel_classes.map { |l| "#{l}:not(:empty):not(.TermNum):not(.noTOC)" }
122
+ ret = toclevel_classes.map do |l|
123
+ "#{l}:not(:empty):not(.TermNum):not(.noTOC)"
124
+ end
116
125
  <<~HEAD.freeze
117
126
  function toclevel() { return "#{ret.join(',')}";}
118
127
  HEAD
@@ -141,100 +150,47 @@ module IsoDoc::HtmlFunction
141
150
  i["width"], i["height"] = Html2Doc.image_resize(i, image_localfile(i),
142
151
  @maxheight, @maxwidth)
143
152
  next if /^data:/.match i["src"]
153
+
144
154
  @datauriimage ? datauri(i) : move_image1(i)
145
155
  end
146
156
  docxml
147
157
  end
148
158
 
149
- def datauri(i)
150
- type = i["src"].split(".")[-1]
159
+ def datauri(img)
160
+ type = img["src"].split(".")[-1]
151
161
  supertype = type == "xml" ? "application" : "image"
152
- bin = IO.binread(image_localfile(i))
162
+ bin = IO.binread(image_localfile(img))
153
163
  data = Base64.strict_encode64(bin)
154
- i["src"] = "data:#{supertype}/#{type};base64,#{data}"
164
+ img["src"] = "data:#{supertype}/#{type};base64,#{data}"
155
165
  end
156
166
 
157
- def image_suffix(i)
158
- type = i["mimetype"]&.sub(%r{^[^/*]+/}, "")
159
- matched = /\.(?<suffix>[^. \r\n\t]+)$/.match i["src"]
167
+ def image_suffix(img)
168
+ type = img["mimetype"]&.sub(%r{^[^/*]+/}, "")
169
+ matched = /\.(?<suffix>[^. \r\n\t]+)$/.match img["src"]
160
170
  type and !type.empty? and return type
171
+
161
172
  !matched.nil? and matched[:suffix] and return matched[:suffix]
162
173
  "png"
163
174
  end
164
175
 
165
- def move_image1(i)
166
- suffix = image_suffix(i)
176
+ def move_image1(img)
177
+ suffix = image_suffix(img)
167
178
  uuid = UUIDTools::UUID.random_create.to_s
168
179
  fname = "#{uuid}.#{suffix}"
169
180
  new_full_filename = File.join(tmpimagedir, fname)
170
- local_filename = image_localfile(i)
181
+ local_filename = image_localfile(img)
171
182
  FileUtils.cp local_filename, new_full_filename
172
- i["src"] = File.join(rel_tmpimagedir, fname)
183
+ img["src"] = File.join(rel_tmpimagedir, fname)
173
184
  end
174
185
 
175
186
  def inject_script(doc)
176
187
  return doc unless @scripts
188
+
177
189
  scripts = File.read(@scripts, encoding: "UTF-8")
178
190
  a = doc.split(%r{</body>})
179
191
  a[0] + scripts + "</body>" + a[1]
180
192
  end
181
193
 
182
- def update_footnote_filter(fn, x, i, seen)
183
- if seen[fn.text]
184
- x.at("./sup").content = seen[fn.text][:num].to_s
185
- fn.remove unless x["href"] == seen[fn.text][:href]
186
- x["href"] = seen[fn.text][:href]
187
- else
188
- seen[fn.text] = { num: i, href: x["href"] }
189
- x.at("./sup").content = i.to_s
190
- i += 1
191
- end
192
- [i, seen]
193
- end
194
-
195
- def html_footnote_filter(docxml)
196
- seen = {}
197
- i = 1
198
- docxml.xpath('//a[@class = "FootnoteRef"]').each do |x|
199
- fn = docxml.at(%<//*[@id = '#{x['href'].sub(/^#/, '')}']>) || next
200
- i, seen = update_footnote_filter(fn, x, i, seen)
201
- end
202
- docxml
203
- end
204
-
205
- def footnote_backlinks1(x, fn)
206
- xdup = x.dup
207
- xdup.remove["id"]
208
- if fn.elements.empty?
209
- fn.children.first.previous = xdup
210
- else
211
- fn.elements.first.children.first.previous = xdup
212
- end
213
- end
214
-
215
- def footnote_backlinks(docxml)
216
- seen = {}
217
- docxml.xpath('//a[@class = "FootnoteRef"]').each_with_index do |x, i|
218
- seen[x["href"]] and next or seen[x["href"]] = true
219
- fn = docxml.at(%<//*[@id = '#{x['href'].sub(/^#/, '')}']>) || next
220
- footnote_backlinks1(x, fn)
221
- x["id"] ||= "fnref:#{i + 1}"
222
- fn.add_child "<a href='##{x['id']}'>&#x21A9;</a>"
223
- end
224
- docxml
225
- end
226
-
227
- def footnote_format(docxml)
228
- docxml.xpath("//a[@class = 'FootnoteRef']/sup").each do |x|
229
- footnote_reference_format(x)
230
- end
231
- docxml.xpath("//a[@class = 'TableFootnoteRef'] | "\
232
- "//span[@class = 'TableFootnoteRef']").each do |x|
233
- table_footnote_reference_format(x)
234
- end
235
- docxml
236
- end
237
-
238
194
  def sourcecode_highlighter
239
195
  '<script src="https://cdn.rawgit.com/google/code-prettify/master/'\
240
196
  'loader/run_prettify.js"></script>'
@@ -0,0 +1,59 @@
1
+ module IsoDoc::HtmlFunction
2
+ module Html
3
+ def update_footnote_filter(fn, x, i, seen)
4
+ if seen[fn.text]
5
+ x.at("./sup").content = seen[fn.text][:num].to_s
6
+ fn.remove unless x["href"] == seen[fn.text][:href]
7
+ x["href"] = seen[fn.text][:href]
8
+ else
9
+ seen[fn.text] = { num: i, href: x["href"] }
10
+ x.at("./sup").content = i.to_s
11
+ i += 1
12
+ end
13
+ [i, seen]
14
+ end
15
+
16
+ def html_footnote_filter(docxml)
17
+ seen = {}
18
+ i = 1
19
+ docxml.xpath('//a[@class = "FootnoteRef"]').each do |x|
20
+ fn = docxml.at(%<//*[@id = '#{x['href'].sub(/^#/, '')}']>) || next
21
+ i, seen = update_footnote_filter(fn, x, i, seen)
22
+ end
23
+ docxml
24
+ end
25
+
26
+ def footnote_backlinks1(x, fn)
27
+ xdup = x.dup
28
+ xdup.remove["id"]
29
+ if fn.elements.empty?
30
+ fn.children.first.previous = xdup
31
+ else
32
+ fn.elements.first.children.first.previous = xdup
33
+ end
34
+ end
35
+
36
+ def footnote_backlinks(docxml)
37
+ seen = {}
38
+ docxml.xpath('//a[@class = "FootnoteRef"]').each_with_index do |x, i|
39
+ seen[x["href"]] and next or seen[x["href"]] = true
40
+ fn = docxml.at(%<//*[@id = '#{x['href'].sub(/^#/, '')}']>) || next
41
+ footnote_backlinks1(x, fn)
42
+ x["id"] ||= "fnref:#{i + 1}"
43
+ fn.add_child "<a href='##{x['id']}'>&#x21A9;</a>"
44
+ end
45
+ docxml
46
+ end
47
+
48
+ def footnote_format(docxml)
49
+ docxml.xpath("//a[@class = 'FootnoteRef']/sup").each do |x|
50
+ footnote_reference_format(x)
51
+ end
52
+ docxml.xpath("//a[@class = 'TableFootnoteRef'] | "\
53
+ "//span[@class = 'TableFootnoteRef']").each do |x|
54
+ table_footnote_reference_format(x)
55
+ end
56
+ docxml
57
+ end
58
+ end
59
+ end
data/lib/isodoc/i18n.rb CHANGED
@@ -5,19 +5,20 @@ module IsoDoc
5
5
  def load_yaml(lang, script, i18nyaml = nil)
6
6
  ret = load_yaml1(lang, script)
7
7
  return normalise_hash(ret.merge(YAML.load_file(i18nyaml))) if i18nyaml
8
+
8
9
  normalise_hash(ret)
9
10
  end
10
11
 
11
12
  def normalise_hash(ret)
12
- if ret.is_a? Hash
13
+ case ret
14
+ when Hash
13
15
  ret.each do |k, v|
14
16
  ret[k] = normalise_hash(v)
15
17
  end
16
18
  ret
17
- elsif ret.is_a? Array then ret.map { |n| normalise_hash(n) }
18
- elsif ret.is_a? String then ret.unicode_normalize(:nfc)
19
- else
20
- ret
19
+ when Array then ret.map { |n| normalise_hash(n) }
20
+ when String then ret.unicode_normalize(:nfc)
21
+ else ret
21
22
  end
22
23
  end
23
24
 
@@ -41,8 +42,8 @@ module IsoDoc
41
42
  @labels
42
43
  end
43
44
 
44
- def set(x, y)
45
- @labels[x] = y
45
+ def set(key, val)
46
+ @labels[key] = val
46
47
  end
47
48
 
48
49
  def initialize(lang, script, i18nyaml = nil)
@@ -57,37 +58,36 @@ module IsoDoc
57
58
  end
58
59
  end
59
60
 
60
- def self.l10n(x, lang = @lang, script = @script)
61
- l10n(x, lang, script)
61
+ def self.l10n(text, lang = @lang, script = @script)
62
+ l10n(text, lang, script)
62
63
  end
63
64
 
64
65
  # TODO: move to localization file
65
66
  # function localising spaces and punctuation.
66
67
  # Not clear if period needs to be localised for zh
67
- def l10n(x, lang = @lang, script = @script)
68
+ def l10n(text, lang = @lang, script = @script)
68
69
  if lang == "zh" && script == "Hans"
69
- xml = Nokogiri::HTML::DocumentFragment.parse(x)
70
+ xml = Nokogiri::HTML::DocumentFragment.parse(text)
70
71
  xml.traverse do |n|
71
72
  next unless n.text?
72
- n.replace(n.text.gsub(/ /, "").gsub(/:/, ":").gsub(/,/, "、").
73
- gsub(/\(/, "").gsub(/\)/, "").
74
- gsub(/\[/, "【").gsub(/\]/, "】"))
73
+
74
+ n.replace(n.text.gsub(/ /, "").gsub(/:/, ":").gsub(/,/, "")
75
+ .gsub(/\(/, "(").gsub(/\)/, ")").gsub(/\[/, "【").gsub(/\]/, "】"))
75
76
  end
76
77
  xml.to_xml.gsub(/<b>/, "").gsub("</b>", "").gsub(/<\?[^>]+>/, "")
77
- else
78
- x
78
+ else text
79
79
  end
80
80
  end
81
81
 
82
82
  def multiple_and(names, andword)
83
- return '' if names.empty?
83
+ return "" if names.empty?
84
84
  return names[0] if names.length == 1
85
+
85
86
  (names.length == 2) &&
86
87
  (return l10n("#{names[0]} #{andword} #{names[1]}", @lang, @script))
87
- l10n(names[0..-2].join(', ') + " #{andword} #{names[-1]}", @lang, @script)
88
+ l10n(names[0..-2].join(", ") + " #{andword} #{names[-1]}", @lang, @script)
88
89
  end
89
90
 
90
- #module_function :l10n
91
-
91
+ # module_function :l10n
92
92
  end
93
93
  end
@@ -3,7 +3,8 @@ require "base64"
3
3
  module IsoDoc
4
4
  class PresentationXMLConvert < ::IsoDoc::Convert
5
5
  def lower2cap(s)
6
- return s if /^[[:upper:]][[:upper:]]/.match(s)
6
+ return s if /^[[:upper:]][[:upper:]]/.match?(s)
7
+
7
8
  s.capitalize
8
9
  end
9
10
 
@@ -19,8 +20,10 @@ module IsoDoc
19
20
  end
20
21
 
21
22
  def svg_extract(f)
22
- return unless %r{^data:image/svg\+xml;base64,}.match(f["src"])
23
- svg = Base64.strict_decode64(f["src"].sub(%r{^data:image/svg\+xml;base64,}, ""))
23
+ return unless %r{^data:image/svg\+xml;base64,}.match?(f["src"])
24
+
25
+ svg = Base64.strict_decode64(f["src"]
26
+ .sub(%r{^data:image/svg\+xml;base64,}, ""))
24
27
  f.replace(svg.sub(/<\?xml[^>]*>/, ""))
25
28
  end
26
29
 
@@ -28,12 +31,15 @@ module IsoDoc
28
31
  return sourcecode1(f) if f["class"] == "pseudocode" || f["type"] == "pseudocode"
29
32
  return if labelled_ancestor(f) && f.ancestors("figure").empty?
30
33
  return if f.at(ns("./figure")) and !f.at(ns("./name"))
34
+
31
35
  lbl = @xrefs.anchor(f['id'], :label, false) or return
32
- prefix_name(f, "&nbsp;&mdash; ", l10n("#{lower2cap @i18n.figure} #{lbl}"), "name")
36
+ prefix_name(f, "&nbsp;&mdash; ",
37
+ l10n("#{lower2cap @i18n.figure} #{lbl}"), "name")
33
38
  end
34
39
 
35
40
  def prefix_name(f, delim, number, elem)
36
41
  return if number.nil? || number.empty?
42
+
37
43
  unless name = f.at(ns("./#{elem}"))
38
44
  f.children.empty? and f.add_child("<#{elem}></#{elem}>") or
39
45
  f.children.first.previous = "<#{elem}></#{elem}>"
@@ -52,6 +58,7 @@ module IsoDoc
52
58
  def sourcecode1(f)
53
59
  return if labelled_ancestor(f)
54
60
  return unless f.ancestors("example").empty?
61
+
55
62
  lbl = @xrefs.anchor(f['id'], :label, false) or return
56
63
  prefix_name(f, "&nbsp;&mdash; ", l10n("#{lower2cap @i18n.figure} #{lbl}"), "name")
57
64
  end
@@ -96,6 +103,7 @@ module IsoDoc
96
103
  # introduce name element
97
104
  def note1(f)
98
105
  return if f.parent.name == "bibitem"
106
+
99
107
  n = @xrefs.get[f["id"]]
100
108
  lbl = (@i18n.note if n.nil? || n[:label].nil? || n[:label].empty?) ?
101
109
  @i18n.note: l10n("#{@i18n.note} #{n[:label]}")
@@ -110,7 +118,7 @@ module IsoDoc
110
118
 
111
119
  # introduce name element
112
120
  def termnote1(f)
113
- lbl = l10n(@xrefs.anchor(f['id'], :label) || '???')
121
+ lbl = l10n(@xrefs.anchor(f["id"], :label) || "???")
114
122
  prefix_name(f, "", lower2cap(lbl), "name")
115
123
  end
116
124
 
@@ -134,7 +142,7 @@ module IsoDoc
134
142
 
135
143
  # introduce name element
136
144
  def recommendation1(f, type)
137
- n = @xrefs.anchor(f['id'], :label, false)
145
+ n = @xrefs.anchor(f["id"], :label, false)
138
146
  lbl = (n.nil? ? type : l10n("#{type} #{n}"))
139
147
  prefix_name(f, "", lbl, "name")
140
148
  end
@@ -148,7 +156,8 @@ module IsoDoc
148
156
  def table1(f)
149
157
  return if labelled_ancestor(f)
150
158
  return if f["unnumbered"] && !f.at(ns("./name"))
151
- n = @xrefs.anchor(f['id'], :label, false)
159
+
160
+ n = @xrefs.anchor(f["id"], :label, false)
152
161
  prefix_name(f, "&nbsp;&mdash; ", l10n("#{lower2cap @i18n.table} #{n}"), "name")
153
162
  end
154
163
 
@@ -160,7 +169,7 @@ module IsoDoc
160
169
  end
161
170
 
162
171
  def amend1(f)
163
- f.xpath(ns("./autonumber")).each { |a| a.remove }
172
+ f.xpath(ns("./autonumber")).each(&:remove)
164
173
  f.xpath(ns("./newcontent")).each { |a| a.name = "quote" }
165
174
  f.xpath(ns("./description")).each { |a| a.replace(a.children) }
166
175
  f.replace(f.children)
@@ -243,6 +243,8 @@ module IsoDoc
243
243
  end
244
244
  end
245
245
 
246
+ private
247
+
246
248
  def found_matching_variant_sibling(node)
247
249
  prev = node.xpath("./preceding-sibling::xmlns:variant")
248
250
  foll = node.xpath("./following-sibling::xmlns:variant")
@@ -1,3 +1,3 @@
1
1
  module IsoDoc
2
- VERSION = "1.6.0".freeze
2
+ VERSION = "1.6.1".freeze
3
3
  end
@@ -2,7 +2,7 @@ module IsoDoc::WordFunction
2
2
  module Footnotes
3
3
  def bookmarkid
4
4
  ret = "X"
5
- until !@bookmarks_allocated[ret] do
5
+ until !@bookmarks_allocated[ret]
6
6
  ret = Random.rand(1000000000)
7
7
  end
8
8
  @bookmarks_allocated[ret] = true
@@ -11,6 +11,7 @@ module IsoDoc::WordFunction
11
11
 
12
12
  def footnotes(div)
13
13
  return if @footnotes.empty?
14
+
14
15
  @footnotes.each { |fn| div.parent << fn }
15
16
  end
16
17
 
@@ -52,6 +53,7 @@ module IsoDoc::WordFunction
52
53
  def get_table_ancestor_id(node)
53
54
  table = node.ancestors("table") || node.ancestors("figure")
54
55
  return UUIDTools::UUID.random_create.to_s if table.empty?
56
+
55
57
  table.last["id"]
56
58
  end
57
59
 
@@ -61,30 +63,34 @@ module IsoDoc::WordFunction
61
63
  make_table_footnote_link(out, tid + fn, fn)
62
64
  # do not output footnote text if we have already seen it for this table
63
65
  return if @seen_footnote.include?(tid + fn)
66
+
64
67
  @in_footnote = true
65
68
  out.aside { |a| a << make_table_footnote_text(node, tid + fn, fn) }
66
69
  @in_footnote = false
67
70
  @seen_footnote << (tid + fn)
68
71
  end
69
72
 
70
- def seen_footnote_parse(node, out, fn)
71
- out.span **{style: "mso-element:field-begin"}
72
- out << " NOTEREF _Ref#{@fn_bookmarks[fn]} \\f \\h"
73
- out.span **{style: "mso-element:field-separator"}
74
- out.span **{class: "MsoFootnoteReference"} do |s|
75
- s << fn
73
+ def seen_footnote_parse(_node, out, footnote)
74
+ out.span **{ style: "mso-element:field-begin" }
75
+ out << " NOTEREF _Ref#{@fn_bookmarks[footnote]} \\f \\h"
76
+ out.span **{ style: "mso-element:field-separator" }
77
+ out.span **{ class: "MsoFootnoteReference" } do |s|
78
+ s << footnote
76
79
  end
77
- out.span **{style: "mso-element:field-end"}
80
+ out.span **{ style: "mso-element:field-end" }
78
81
  end
79
82
 
80
83
  def footnote_parse(node, out)
81
84
  return table_footnote_parse(node, out) if (@in_table || @in_figure) &&
82
- !node.ancestors.map {|m| m.name }.include?("name")
85
+ !node.ancestors.map { |m| m.name }.include?("name")
86
+
83
87
  fn = node["reference"] || UUIDTools::UUID.random_create.to_s
84
88
  return seen_footnote_parse(node, out, fn) if @seen_footnote.include?(fn)
89
+
85
90
  @fn_bookmarks[fn] = bookmarkid
86
- out.span **{style: "mso-bookmark:_Ref#{@fn_bookmarks[fn]}"} do |s|
87
- s.a **{ "class": "FootnoteRef", "epub:type": "footnote", href: "#ftn#{fn}" } do |a|
91
+ out.span **{ style: "mso-bookmark:_Ref#{@fn_bookmarks[fn]}" } do |s|
92
+ s.a **{ "class": "FootnoteRef", "epub:type": "footnote",
93
+ href: "#ftn#{fn}" } do |a|
88
94
  a.sup { |sup| sup << fn }
89
95
  end
90
96
  end
@@ -94,12 +100,13 @@ module IsoDoc::WordFunction
94
100
  @seen_footnote << fn
95
101
  end
96
102
 
97
- def make_footnote(node, fn)
98
- return if @seen_footnote.include?(fn)
103
+ def make_footnote(node, footnote)
104
+ return if @seen_footnote.include?(footnote)
105
+
99
106
  @in_footnote = true
100
- @footnotes << make_generic_footnote_text(node, fn)
107
+ @footnotes << make_generic_footnote_text(node, footnote)
101
108
  @in_footnote = false
102
- @seen_footnote << fn
109
+ @seen_footnote << footnote
103
110
  end
104
111
  end
105
112
  end