isodoc 1.5.5 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/isodoc/css.rb CHANGED
@@ -14,17 +14,22 @@ module IsoDoc
14
14
  compiled_path
15
15
  end
16
16
 
17
+ def localpath(path)
18
+ return path if %r{^[A-Z]:|^/|^file:/}.match(path)
19
+ return path unless (@sourcedir || @localdir) && path
20
+ File.expand_path(File.join((@sourcedir || @localdir), path))
21
+ end
22
+
17
23
  # run this after @meta is populated
18
24
  def populate_css
19
- @htmlstylesheet = generate_css(@htmlstylesheet_name, true)
20
- @wordstylesheet = generate_css(@wordstylesheet_name, false)
21
- @standardstylesheet = generate_css(@standardstylesheet_name, false)
22
- if @htmlstylesheet_override_name
23
- @htmlstylesheet_override = File.open(@htmlstylesheet_override_name)
24
- end
25
- if @wordstylesheet_override_name
26
- @wordstylesheet_override = File.open(@wordstylesheet_override_name)
27
- end
25
+ @htmlstylesheet = generate_css(localpath(@htmlstylesheet_name), true)
26
+ @wordstylesheet = generate_css(localpath(@wordstylesheet_name), false)
27
+ @standardstylesheet =
28
+ generate_css(localpath(@standardstylesheet_name), false)
29
+ @htmlstylesheet_override_name and @htmlstylesheet_override =
30
+ File.open(localpath(@htmlstylesheet_override_name))
31
+ @wordstylesheet_override_name and @wordstylesheet_override =
32
+ File.open(localpath(@wordstylesheet_override_name))
28
33
  end
29
34
 
30
35
  def default_fonts(_options)
@@ -23,21 +23,26 @@ module IsoDoc::Function
23
23
  end
24
24
 
25
25
  def xref_parse(node, out)
26
- target = /#/.match(node["target"]) ? node["target"].sub(/#/, ".html#") :
27
- "##{node["target"]}"
28
- out.a(**{ "href": target }) { |l| no_locality_parse(node, l) }
26
+ target = if /#/.match?(node["target"])
27
+ node["target"].sub(/#/, ".html#")
28
+ else
29
+ "##{node['target']}"
30
+ end
31
+ out.a(**{ "href": target }) { |l| no_locality_parse(node, l) }
29
32
  end
30
33
 
31
34
  def suffix_url(url)
32
- return url if %r{^http[s]?://}.match(url)
35
+ return url if %r{^https?://}.match?(url)
36
+
33
37
  url.sub(/#{File.extname(url)}$/, ".html")
34
38
  end
35
39
 
36
40
  def eref_target(node)
37
- href = "#" + node["bibitemid"]
41
+ href = "##{node['bibitemid']}"
38
42
  url = node.at(ns("//bibitem[@id = '#{node['bibitemid']}']/"\
39
43
  "uri[@type = 'citation']"))
40
44
  return href unless url
45
+
41
46
  href = suffix_url(url.text)
42
47
  anchor = node&.at(ns(".//locality[@type = 'anchor']"))&.text&.strip
43
48
  anchor and href += "##{anchor}"
@@ -68,10 +73,11 @@ module IsoDoc::Function
68
73
  end
69
74
 
70
75
  def stem_parse(node, out)
71
- ooml = if node["type"] == "AsciiMath"
76
+ ooml = case node["type"]
77
+ when "AsciiMath"
72
78
  "#{@openmathdelim}#{HTMLEntities.new.encode(node.text)}"\
73
79
  "#{@closemathdelim}"
74
- elsif node["type"] == "MathML" then node.first_element_child.to_s
80
+ when "MathML" then node.first_element_child.to_s
75
81
  else
76
82
  HTMLEntities.new.encode(node.text)
77
83
  end
@@ -93,7 +99,7 @@ module IsoDoc::Function
93
99
  height: node["height"] || "auto",
94
100
  width: node["width"] || "auto",
95
101
  title: node["title"],
96
- alt: node["alt"] }
102
+ alt: node["alt"] }
97
103
  out.img **attr_code(attrs)
98
104
  image_title_parse(out, caption)
99
105
  end
@@ -106,12 +112,27 @@ module IsoDoc::Function
106
112
 
107
113
  def text_parse(node, out)
108
114
  return if node.nil? || node.text.nil?
115
+
109
116
  text = node.to_s
110
- text = text.gsub("\n", "<br/>").gsub("<br/> ", "<br/>&nbsp;").
111
- gsub(/[ ](?=[ ])/, "&nbsp;") if in_sourcecode
117
+ if in_sourcecode
118
+ text = text.gsub("\n", "<br/>").gsub("<br/> ", "<br/>&nbsp;")
119
+ .gsub(/ (?= )/, "&nbsp;")
120
+ end
112
121
  out << text
113
122
  end
114
123
 
124
+ def add_parse(node, out)
125
+ out.span **{class: "addition"} do |e|
126
+ node.children.each { |n| parse(n, e) }
127
+ end
128
+ end
129
+
130
+ def del_parse(node, out)
131
+ out.span **{class: "deletion"} do |e|
132
+ node.children.each { |n| parse(n, e) }
133
+ end
134
+ end
135
+
115
136
  def error_parse(node, out)
116
137
  text = node.to_xml.gsub(/</, "&lt;").gsub(/>/, "&gt;")
117
138
  out.para do |p|
@@ -19,14 +19,21 @@ module IsoDoc::Function
19
19
  def init_file(filename, debug)
20
20
  filepath = Pathname.new(filename)
21
21
  filename = filepath.sub_ext('').sub(/\.presentation$/, "").to_s
22
+ dir = init_dir(filename, debug)
23
+ @filename = filename
24
+ @localdir = filepath.parent.to_s + '/'
25
+ @sourcedir = @localdir
26
+ @sourcefilename and @sourcedir = Pathname.new(@sourcefilename).parent.to_s + '/'
27
+ [filename, dir]
28
+ end
29
+
30
+ def init_dir(filename, debug)
22
31
  dir = "#{filename}_files"
23
32
  unless debug
24
33
  Dir.mkdir(dir, 0777) unless File.exists?(dir)
25
34
  FileUtils.rm_rf "#{dir}/*"
26
35
  end
27
- @filename = filename
28
- @localdir = filepath.parent.to_s + '/'
29
- [filename, dir]
36
+ dir
30
37
  end
31
38
 
32
39
  # tmp image dir is same directory as @filename
@@ -44,15 +51,16 @@ module IsoDoc::Function
44
51
  if @standardstylesheet
45
52
  head.style do |style|
46
53
  @standardstylesheet.open
47
- stylesheet = @standardstylesheet.read.
48
- gsub("FILENAME", File.basename(filename).sub(/\.presentation$/, ""))
54
+ stylesheet = @standardstylesheet.read.gsub(
55
+ "FILENAME", File.basename(filename).sub(/\.presentation$/, "")
56
+ )
49
57
  style.comment "\n#{stylesheet}\n"
50
58
  end
51
59
  end
52
60
  end
53
61
 
54
62
  def body_attr
55
- { lang: "#{@lang}" }
63
+ { lang: @lang.to_s }
56
64
  end
57
65
 
58
66
  def make_body(xml, docxml)
@@ -125,7 +133,7 @@ module IsoDoc::Function
125
133
  i = scope isoxml, out, 0
126
134
  i = norm_ref isoxml, out, i
127
135
  i = terms_defs isoxml, out, i
128
- i = symbols_abbrevs isoxml, out, i
136
+ symbols_abbrevs isoxml, out, i
129
137
  clause isoxml, out
130
138
  annex isoxml, out
131
139
  bibliography isoxml, out
@@ -133,7 +141,7 @@ module IsoDoc::Function
133
141
 
134
142
  def boilerplate(node, out)
135
143
  boilerplate = node.at(ns("//boilerplate")) or return
136
- out.div **{class: "authority"} do |s|
144
+ out.div **{ class: "authority" } do |s|
137
145
  boilerplate.children.each do |n|
138
146
  if n.name == "title"
139
147
  s.h1 do |h|
@@ -225,7 +233,9 @@ module IsoDoc::Function
225
233
  when "passthrough" then passthrough_parse(node, out)
226
234
  when "amend" then amend_parse(node, out)
227
235
  when "tab" then clausedelimspace(out) # in Presentation XML only
228
- when "svg" then svg_parse(node, out) # introduced in Presentation XML only
236
+ when "svg" then svg_parse(node, out) # in Presentation XML only
237
+ when "add" then add_parse(node, out)
238
+ when "del" then del_parse(node, out)
229
239
  else
230
240
  error_parse(node, out)
231
241
  end
@@ -123,6 +123,7 @@ module IsoDoc::Function
123
123
  h1 = to_xhtml_fragment(h.dup)
124
124
  h1.traverse do |x|
125
125
  x.replace(' ') if x.name == 'span' && /mso-tab-count/.match(x['style'])
126
+ x.remove if x.name == 'img'
126
127
  x.remove if x.name == 'span' && x['class'] == 'MsoCommentReference'
127
128
  x.remove if x.name == 'a' && x['class'] == 'FootnoteRef'
128
129
  x.remove if x.name == 'span' && /mso-bookmark/.match(x['style'])
@@ -42,9 +42,7 @@ module IsoDoc
42
42
  end
43
43
 
44
44
  def xref_parse(node, out)
45
- target = /#/.match(node["target"]) ? node["target"].sub(/#/, ".pdf#") :
46
- "##{node["target"]}"
47
- out.a(**{ "href": target }) { |l| l << get_linkend(node) }
45
+ out.a(**{ "href": target_pdf(node) }) { |l| l << get_linkend(node) }
48
46
  end
49
47
  end
50
48
  end
@@ -50,67 +50,79 @@ module IsoDoc
50
50
  end
51
51
  end
52
52
 
53
- def get_linkend(n)
54
- contents = non_locality_elems(n).select { |c| !c.text? || /\S/.match(c) }
53
+ def get_linkend(node)
54
+ contents = non_locality_elems(node).select { |c| !c.text? || /\S/.match(c) }
55
55
  return unless contents.empty?
56
- link = anchor_linkend(n, docid_l10n(n["target"] || n["citeas"]))
57
- link += eref_localities(n.xpath(ns("./locality | ./localityStack")), link)
58
- non_locality_elems(n).each { |n| n.remove }
59
- n.add_child(link)
56
+ link = anchor_linkend(node, docid_l10n(node["target"] || node["citeas"]))
57
+ link += eref_localities(node.xpath(ns("./locality | ./localityStack")), link, node)
58
+ non_locality_elems(node).each { |n| n.remove }
59
+ node.add_child(link)
60
60
  end
61
61
  # so not <origin bibitemid="ISO7301" citeas="ISO 7301">
62
62
  # <locality type="section"><reference>3.1</reference></locality></origin>
63
63
 
64
- def eref_localities(refs, target)
64
+ def eref_localities(refs, target, n)
65
65
  ret = ""
66
66
  refs.each_with_index do |r, i|
67
67
  delim = ","
68
68
  delim = ";" if r.name == "localityStack" && i>0
69
- ret = eref_locality_stack(r, i, target, delim, ret)
69
+ ret = eref_locality_stack(r, i, target, delim, ret, n)
70
70
  end
71
71
  ret
72
72
  end
73
73
 
74
- def eref_locality_stack(r, i, target, delim, ret)
74
+ def eref_locality_stack(r, i, target, delim, ret, n)
75
75
  if r.name == "localityStack"
76
76
  r.elements.each_with_index do |rr, j|
77
- ret += eref_localities0(rr, j, target, delim)
77
+ ret += eref_localities0(rr, j, target, delim, n)
78
78
  delim = ","
79
79
  end
80
80
  else
81
- ret += eref_localities0(r, i, target, delim)
81
+ ret += eref_localities0(r, i, target, delim, n)
82
82
  end
83
83
  ret
84
84
  end
85
85
 
86
- def eref_localities0(r, i, target, delim)
86
+ def eref_localities0(r, i, target, delim, n)
87
87
  if r["type"] == "whole" then l10n("#{delim} #{@i18n.wholeoftext}")
88
88
  else
89
- eref_localities1(target, r["type"], r.at(ns("./referenceFrom")), r.at(ns("./referenceTo")), delim, @lang)
89
+ eref_localities1(target, r["type"], r.at(ns("./referenceFrom")),
90
+ r.at(ns("./referenceTo")), delim, n, @lang)
90
91
  end
91
92
  end
92
93
 
93
94
  # TODO: move to localization file
94
- def eref_localities1_zh(target, type, from, to, delim)
95
+ def eref_localities1_zh(target, type, from, to, n, delim)
95
96
  ret = "#{delim} 第#{from.text}" if from
96
97
  ret += "&ndash;#{to.text}" if to
97
98
  loc = (@i18n.locality[type] || type.sub(/^locality:/, "").capitalize )
98
- ret += " #{loc}"
99
+ ret += " #{loc}" unless n["droploc"] == "true"
99
100
  ret
100
101
  end
101
102
 
102
103
  # TODO: move to localization file
103
- def eref_localities1(target, type, from, to, delim, lang = "en")
104
+ def eref_localities1(target, type, from, to, delim, n, lang = "en")
104
105
  return "" if type == "anchor"
105
- lang == "zh" and return l10n(eref_localities1_zh(target, type, from, to, delim))
106
+ lang == "zh" and return l10n(eref_localities1_zh(target, type, from, to, n, delim))
106
107
  ret = delim
107
- loc = @i18n.locality[type] || type.sub(/^locality:/, "").capitalize
108
- ret += " #{loc}"
108
+ ret += eref_locality_populate(type, n)
109
109
  ret += " #{from.text}" if from
110
110
  ret += "&ndash;#{to.text}" if to
111
111
  l10n(ret)
112
112
  end
113
113
 
114
+ def eref_locality_populate(type, n)
115
+ return "" if n["droploc"] == "true"
116
+ loc = @i18n.locality[type] || type.sub(/^locality:/, "")
117
+ loc = case n["case"]
118
+ when "capital" then loc.capitalize
119
+ when "lowercase" then loc.downcase
120
+ else
121
+ loc.capitalize
122
+ end
123
+ " #{loc}"
124
+ end
125
+
114
126
  def xref(docxml)
115
127
  docxml.xpath(ns("//xref")).each { |f| xref1(f) }
116
128
  end
@@ -59,7 +59,7 @@ module IsoDoc
59
59
  variant docxml
60
60
  end
61
61
 
62
- def postprocess(result, filename, dir)
62
+ def postprocess(result, filename, _dir)
63
63
  toXML(result, filename)
64
64
  @files_to_delete.each { |f| FileUtils.rm_rf f }
65
65
  end
@@ -1,3 +1,3 @@
1
1
  module IsoDoc
2
- VERSION = "1.5.5".freeze
2
+ VERSION = "1.6.0".freeze
3
3
  end
@@ -2,52 +2,61 @@ require "metanorma"
2
2
 
3
3
  module IsoDoc
4
4
  class XslfoPdfConvert < ::IsoDoc::Convert
5
+ MN2PDF_OPTIONS = :mn2pdf
6
+ MN2PDF_FONT_MANIFEST = :font_manifest_file
5
7
 
6
8
  def initialize(options)
9
+ @format = :pdf
10
+ @suffix = "pdf"
7
11
  super
8
- @maxwidth = 500
9
- @maxheight = 800
10
12
  end
11
13
 
12
14
  def tmpimagedir_suffix
13
15
  "_pdfimages"
14
16
  end
15
17
 
16
- def initialize(options)
17
- @format = :pdf
18
- @suffix = "pdf"
19
- super
20
- end
21
-
22
- def pdf_stylesheet(docxml)
18
+ def pdf_stylesheet(_docxml)
23
19
  nil
24
20
  end
25
21
 
26
- def pdf_options(docxml)
27
- font_manifest_file = @options.dig(:mn2pdf, :font_manifest_file)
28
- "--font_manifest #{font_manifest_file}" if font_manifest_file
29
- ""
22
+ def pdf_options(_docxml)
23
+ if font_manifest_file = @options.dig(MN2PDF_OPTIONS,
24
+ MN2PDF_FONT_MANIFEST)
25
+ "--font-manifest #{font_manifest_file}"
26
+ else
27
+ ""
28
+ end
30
29
  end
31
30
 
32
- def convert(input_filename, file = nil, debug = false, output_filename = nil)
31
+ def convert(input_filename, file = nil, debug = false,
32
+ output_filename = nil)
33
33
  file = File.read(input_filename, encoding: "utf-8") if file.nil?
34
- docxml, filename, dir = convert_init(file, input_filename, debug)
35
- /\.xml$/.match(input_filename) or
36
- input_filename = Tempfile.open([filename, ".xml"], encoding: "utf-8") do |f|
37
- f.write file
34
+ input_filename, docxml, filename = input_xml_path(input_filename,
35
+ file, debug)
36
+ ::Metanorma::Output::XslfoPdf.new.convert(
37
+ input_filename,
38
+ output_filename || "#{filename}.#{@suffix}",
39
+ File.join(@libdir, pdf_stylesheet(docxml)),
40
+ pdf_options(docxml)
41
+ )
42
+ end
43
+
44
+ def xref_parse(node, out)
45
+ out.a(**{ "href": target_pdf(node) }) { |l| l << get_linkend(node) }
46
+ end
47
+
48
+ def input_xml_path(input_filename, xml_file, debug)
49
+ docxml, filename, dir = convert_init(xml_file, input_filename, debug)
50
+ unless /\.xml$/.match?(input_filename)
51
+ input_filename = Tempfile.open([filename, ".xml"],
52
+ encoding: "utf-8") do |f|
53
+ f.write xml_file
38
54
  f.path
39
55
  end
56
+ end
40
57
  FileUtils.rm_rf dir
41
- ::Metanorma::Output::XslfoPdf.new.convert(input_filename,
42
- output_filename || "#{filename}.#{@suffix}",
43
- File.join(@libdir, pdf_stylesheet(docxml)),
44
- pdf_options(docxml))
45
- end
46
58
 
47
- def xref_parse(node, out)
48
- target = /#/.match(node["target"]) ? node["target"].sub(/#/, ".pdf#") :
49
- "##{node["target"]}"
50
- out.a(**{ "href": target }) { |l| l << get_linkend(node) }
59
+ [input_filename, docxml, filename]
51
60
  end
52
61
  end
53
62
  end
@@ -155,17 +155,8 @@ RSpec.describe IsoDoc do
155
155
  </preface>
156
156
  </iso-standard>
157
157
  INPUT
158
- html = File.read("test.html").sub(/^.*<body/m, "<body").sub(%r{</body>.*$}m, "</body>")
158
+ html = File.read("test.html").sub(/^.*<main/m, "<main").sub(%r{</main>.*$}m, "</main>")
159
159
  expect(xmlpp(html)).to be_equivalent_to xmlpp(<<~"OUTPUT")
160
- <body lang="en" xml:lang="en">
161
- <div class="title-section">
162
- <p>&#xA0;</p>
163
- </div>
164
- <br />
165
- <div class="prefatory-section">
166
- <p>&#xA0;</p>
167
- </div>
168
- <br />
169
160
  <main class="main-section"><button onclick="topFunction()" id="myBtn" title="Go to top">Top</button>
170
161
  <br />
171
162
  <div>
@@ -180,12 +171,6 @@ RSpec.describe IsoDoc do
180
171
  </div>
181
172
  <p class="zzSTDTitle1"></p>
182
173
  </main>
183
- <script type='text/x-mathjax-config'>
184
- MathJax.Hub.Config({ "HTML-CSS": { preferredFont: "STIX" }, asciimath2jax:
185
- { delimiters: [['(#(', ')#)']] } });
186
- </script>
187
- <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=MML_HTMLorMML-full" async="async"></script>
188
- <script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script></body>
189
174
  OUTPUT
190
175
  end
191
176