asciidoctor-iso 0.10.1 → 0.10.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5ae350c2e4edf9c90dcba4d2ad5c318800a8d26b199c6b7c90375847f58fc46b
4
- data.tar.gz: af561d03b111302e8a7dad8c714e93ae335bf7d768d67fd328a2e57282da8582
3
+ metadata.gz: 037ae405ca1ab7a6f18676326cb76f89f708ae1991434c91144d3e431149519d
4
+ data.tar.gz: 4dba9c034ef24e4595e5105c8a6c9a643924c892dd229a7682fc56cbf8bf01da
5
5
  SHA512:
6
- metadata.gz: 83c629ce75f58024f2d096634cfe9111862470250500218f5face86849e8eb14567076f439f9907445880767dcd4d42bfb4c7351e437b0d56b95c057e5f0d2ff
7
- data.tar.gz: 242f6a1f91a4e3b86776f60fb13ff5cc806651bd2e56ae4bd4e7e24fb84b8d120fe0ff52519ae3b8e37edf710e959c5fc7d11817be8fb30066c262c5b5a36dfa
6
+ metadata.gz: 30302f78ee8fc4ee5b3cd7a3f6aa3cd52dda9090844ccced275f9cc3d25af7cc6c3f871a00b20e28abf6a3eeea7d78362529efd2f13527e088c50ef1bd220417
7
+ data.tar.gz: 77367db460c5bc2f009a0760f18fe100ae8da27f3c22dc03a26c5397c0ee75db5012aba54eeed7a0617d08315b5433c87b2c2861042135d4686d108d675592d4
@@ -32,6 +32,7 @@ Gem::Specification.new do |spec|
32
32
  spec.add_dependency "isodoc", ">= 0.8"
33
33
  spec.add_dependency "iev", "~> 0.2.0"
34
34
  spec.add_dependency "relaton", "~> 0.1.3"
35
+ spec.add_dependency "metanorma-standoc", "~> 0.0.1"
35
36
 
36
37
  spec.add_development_dependency "bundler", "~> 1.15"
37
38
  spec.add_development_dependency "byebug"
@@ -1,35 +1,14 @@
1
1
  require "date"
2
2
  require "nokogiri"
3
- require "htmlentities"
4
3
  require "json"
5
4
  require "pathname"
6
5
  require "open-uri"
7
6
  require "pp"
8
- require "sass"
9
7
  require "isodoc"
10
- require "relaton"
11
8
 
12
9
  module Asciidoctor
13
10
  module ISO
14
- module Base
15
- Asciidoctor::Extensions.register do
16
- inline_macro Asciidoctor::ISO::AltTermInlineMacro
17
- inline_macro Asciidoctor::ISO::DeprecatedTermInlineMacro
18
- inline_macro Asciidoctor::ISO::DomainTermInlineMacro
19
- block Asciidoctor::ISO::PlantUMLBlockMacro
20
- end
21
-
22
- def content(node)
23
- node.content
24
- end
25
-
26
- def skip(node, name = nil)
27
- name = name || node.node_name
28
- w = "converter missing for #{name} node in ISO backend"
29
- Utils::warning(node, w, nil)
30
- nil
31
- end
32
-
11
+ class Converter < Standoc::Converter
33
12
  def html_converter(node)
34
13
  IsoDoc::Iso::HtmlConvert.new(
35
14
  script: node.attr("script"),
@@ -61,59 +40,6 @@ module Asciidoctor
61
40
  )
62
41
  end
63
42
 
64
- def init(node)
65
- @fn_number = 0
66
- @draft = false
67
- @refids = Set.new
68
- @anchors = {}
69
- @draft = node.attributes.has_key?("draft")
70
- @novalid = node.attr("novalid")
71
- @fontheader = default_fonts(node)
72
- @files_to_delete = []
73
- @filename = node.attr("docfile") ?
74
- node.attr("docfile").gsub(/\.adoc$/, "").gsub(%r{^.*/}, "") : ""
75
- @no_isobib_cache = node.attr("no-isobib-cache")
76
- @no_isobib = node.attr("no-isobib")
77
- @bibdb = nil
78
- init_bib_caches(node)
79
- init_iev_caches(node)
80
- end
81
-
82
- def init_bib_caches(node)
83
- unless (@no_isobib_cache || @no_isobib)
84
- globalname = bibliocache_name(true)
85
- localname = bibliocache_name(false) if node.attr("local-cache")
86
- if node.attr("flush-caches")
87
- system("rm -f #{globalname}") unless globalname.nil?
88
- system("rm -f #{localname}") unless localname.nil?
89
- end
90
- end
91
- @bibdb = Relaton::Db.new(globalname, localname) unless @no_isobib
92
- end
93
-
94
- def init_iev_caches(node)
95
- unless (@no_isobib_cache || @no_isobib)
96
- globalname = ievcache_name(true)
97
- localname = ievcache_name(false) if node.attr("local-cache")
98
- if node.attr("flush-caches")
99
- system("rm -f #{globalname}") unless globalname.nil?
100
- system("rm -f #{localname}") unless localname.nil?
101
- end
102
- end
103
- @iev = Iev::Db.new(globalname, localname) unless @no_isobib
104
- end
105
-
106
- def default_fonts(node)
107
- b = node.attr("body-font") ||
108
- (node.attr("script") == "Hans" ? '"SimSun",serif' :
109
- '"Cambria",serif')
110
- h = node.attr("header-font") ||
111
- (node.attr("script") == "Hans" ? '"SimHei",sans-serif' :
112
- '"Cambria",serif')
113
- m = node.attr("monospace-font") || '"Courier New",monospace'
114
- "$bodyfont: #{b};\n$headerfont: #{h};\n$monospacefont: #{m};\n"
115
- end
116
-
117
43
  def document(node)
118
44
  init(node)
119
45
  ret = makexml(node).to_xml(indent: 2)
@@ -135,77 +61,6 @@ module Asciidoctor
135
61
  result << "</iso-standard>"
136
62
  textcleanup(result.flatten * "\n")
137
63
  end
138
-
139
- def makexml(node)
140
- result = makexml1(node)
141
- ret1 = cleanup(Nokogiri::XML(result))
142
- ret1.root.add_namespace(nil, "http://riboseinc.com/isoxml")
143
- validate(ret1) unless @novalid
144
- ret1
145
- end
146
-
147
- def draft?
148
- @draft
149
- end
150
-
151
- def doctype(node)
152
- node.attr("doctype")
153
- end
154
-
155
- def front(node, xml)
156
- xml.bibdata **attr_code(type: doctype(node)) do |b|
157
- metadata node, b
158
- end
159
- metadata_version(node, xml)
160
- end
161
-
162
- def middle(node, xml)
163
- xml.sections do |s|
164
- s << node.content if node.blocks?
165
- end
166
- end
167
-
168
- def term_source_attr(seen_xref)
169
- { bibitemid: seen_xref.children[0]["target"],
170
- format: seen_xref.children[0]["format"],
171
- type: "inline" }
172
- end
173
-
174
- def add_term_source(xml_t, seen_xref, m)
175
- xml_t.origin seen_xref.children[0].content,
176
- **attr_code(term_source_attr(seen_xref))
177
- m[:text] && xml_t.modification do |mod|
178
- mod.p { |p| p << m[:text].sub(/^\s+/, "") }
179
- end
180
- end
181
-
182
- TERM_REFERENCE_RE_STR = <<~REGEXP.freeze
183
- ^(?<xref><xref[^>]+>([^<]*</xref>)?)
184
- (,\s(?<text>.*))?
185
- $
186
- REGEXP
187
- TERM_REFERENCE_RE =
188
- Regexp.new(TERM_REFERENCE_RE_STR.gsub(/\s/, "").gsub(/_/, "\\s"),
189
- Regexp::IGNORECASE | Regexp::MULTILINE)
190
-
191
- def extract_termsource_refs(text, node)
192
- matched = TERM_REFERENCE_RE.match text
193
- if matched.nil?
194
- Utils::warning(node, "term reference not in expected format", text)
195
- end
196
- matched
197
- end
198
-
199
- def termsource(node)
200
- matched = extract_termsource_refs(node.content, node) || return
201
- noko do |xml|
202
- attrs = { status: matched[:text] ? "modified" : "identical" }
203
- xml.termsource **attrs do |xml_t|
204
- seen_xref = Nokogiri::XML.fragment(matched[:xref])
205
- add_term_source(xml_t, seen_xref, matched)
206
- end
207
- end.join("\n")
208
- end
209
64
  end
210
65
  end
211
66
  end
@@ -1,244 +1,37 @@
1
1
  require "date"
2
2
  require "nokogiri"
3
+ require "htmlentities"
4
+ require "json"
3
5
  require "pathname"
4
6
  require "open-uri"
5
7
  require "pp"
6
- require_relative "./cleanup_block.rb"
7
- require_relative "./cleanup_footnotes.rb"
8
- require_relative "./cleanup_ref.rb"
9
8
 
10
9
  module Asciidoctor
11
10
  module ISO
12
- module Cleanup
13
- def textcleanup(text)
14
- text.gsub(/\s+<fn /, "<fn ")
15
- end
16
-
17
- def cleanup(xmldoc)
18
- termdef_cleanup(xmldoc)
19
- sections_cleanup(xmldoc)
20
- obligations_cleanup(xmldoc)
21
- table_cleanup(xmldoc)
22
- formula_cleanup(xmldoc)
23
- figure_cleanup(xmldoc)
24
- ref_cleanup(xmldoc)
25
- note_cleanup(xmldoc)
26
- normref_cleanup(xmldoc)
27
- biblio_cleanup(xmldoc)
28
- reference_names(xmldoc)
29
- xref_cleanup(xmldoc)
30
- bpart_cleanup(xmldoc)
31
- quotesource_cleanup(xmldoc)
32
- para_cleanup(xmldoc)
33
- callout_cleanup(xmldoc)
34
- origin_cleanup(xmldoc)
35
- element_name_cleanup(xmldoc)
36
- footnote_renumber(xmldoc)
37
- empty_element_cleanup(xmldoc)
38
- mathml_cleanup(xmldoc)
39
- script_cleanup(xmldoc)
40
- docidentifier_cleanup(xmldoc)
41
- bookmark_cleanup(xmldoc)
42
- xmldoc
43
- end
44
-
45
- def docidentifier_cleanup(xmldoc)
46
- id = xmldoc.at("//bibdata/docidentifier/project-number")
47
- return unless id
48
-
49
- prefix = []
50
- xmldoc.xpath("//bibdata/contributor[role/@type = 'publisher']"\
51
- "/organization").each do |x|
52
- x1 = x.at("abbreviation")&.text || x.at("name")&.text
53
- x1 == "ISO" and prefix.unshift("ISO") or prefix << x1
54
- end
55
-
56
- id.content = prefix.join("/") + " " + id.text
57
- end
58
-
59
- TEXT_ELEMS =
60
- %w{status language script version author name callout phone
61
- email street city state country postcode identifier referenceFrom
62
- referenceTo docidentifier prefix initial addition surname forename
63
- title draft secretariat title-main title-intro title-part}.freeze
64
-
65
- # it seems Nokogiri::XML is treating the content of <script> as cdata,
66
- # because of its use in HTML. Bad nokogiri. Undoing that, since we use
67
- # script as a normal tag
68
- def script_cleanup(xmldoc)
69
- xmldoc.xpath("//script").each do |x|
70
- x.content = x.to_str
71
- end
72
- end
73
-
74
- def empty_element_cleanup(xmldoc)
75
- xmldoc.xpath("//" + TEXT_ELEMS.join(" | //")).each do |x|
76
- x.remove if x.children.empty?
77
- end
78
- end
79
-
80
- def element_name_cleanup(xmldoc)
81
- xmldoc.traverse { |n| n.name = n.name.gsub(/_/, "-") }
82
- end
83
-
84
- def link_callouts_to_annotations(callouts, annotations)
85
- callouts.each_with_index do |c, i|
86
- c["target"] = "_" + UUIDTools::UUID.random_create
87
- annotations[i]["id"] = c["target"]
88
- end
89
- end
90
-
91
- def align_callouts_to_annotations(xmldoc)
92
- xmldoc.xpath("//sourcecode").each do |x|
93
- callouts = x.elements.select { |e| e.name == "callout" }
94
- annotations = x.elements.select { |e| e.name == "annotation" }
95
- if callouts.size == annotations.size
96
- link_callouts_to_annotations(callouts, annotations)
97
- end
98
- end
99
- end
100
-
101
- def merge_annotations_into_sourcecode(xmldoc)
102
- xmldoc.xpath("//sourcecode").each do |x|
103
- while x&.next_element&.name == "annotation"
104
- x.next_element.parent = x
105
- end
106
- end
107
- end
108
-
109
- def callout_cleanup(xmldoc)
110
- merge_annotations_into_sourcecode(xmldoc)
111
- align_callouts_to_annotations(xmldoc)
112
- end
113
-
114
- def termdef_stem_cleanup(xmldoc)
115
- xmldoc.xpath("//term/p/stem").each do |a|
116
- if a.parent.elements.size == 1
117
- # para containing just a stem expression
118
- t = Nokogiri::XML::Element.new("admitted", xmldoc)
119
- parent = a.parent
120
- t.children = a.remove
121
- parent.replace(t)
122
- end
123
- end
124
- end
125
-
126
- def termdomain_cleanup(xmldoc)
127
- xmldoc.xpath("//p/domain").each do |a|
128
- prev = a.parent.previous
129
- prev.next = a.remove
130
- end
131
- end
132
-
133
- def termdefinition_cleanup(xmldoc)
134
- xmldoc.xpath("//term").each do |d|
135
- first_child = d.at("./p | ./figure | ./formula") || return
136
- t = Nokogiri::XML::Element.new("definition", xmldoc)
137
- first_child.replace(t)
138
- t << first_child.remove
139
- d.xpath("./p | ./figure | ./formula").each { |n| t << n.remove }
140
- end
141
- end
142
-
143
- def termdef_unnest_cleanup(xmldoc)
144
- # release termdef tags from surrounding paras
145
- nodes = xmldoc.xpath("//p/admitted | //p/deprecates")
146
- while !nodes.empty?
147
- nodes[0].parent.replace(nodes[0].parent.children)
148
- nodes = xmldoc.xpath("//p/admitted | //p/deprecates")
149
- end
150
- end
151
-
152
- def termdef_boilerplate_cleanup(xmldoc)
153
- xmldoc.xpath("//terms/p | //terms/ul").each(&:remove)
154
- end
155
-
156
- def termdef_subclause_cleanup(xmldoc)
157
- xmldoc.xpath("//terms[terms]").each do |t|
158
- t.name = "clause"
159
- end
160
- end
161
-
162
- def termdocsource_cleanup(xmldoc)
163
- f = xmldoc.at("//preface | //sections")
164
- xmldoc.xpath("//terms/termdocsource | "\
165
- "//clause/termdocsource").each do |s|
166
- f.previous = s.remove
167
- end
168
- end
169
-
170
- def termdef_cleanup(xmldoc)
171
- termdef_unnest_cleanup(xmldoc)
172
- termdef_stem_cleanup(xmldoc)
173
- termdomain_cleanup(xmldoc)
174
- termdefinition_cleanup(xmldoc)
175
- termdef_boilerplate_cleanup(xmldoc)
176
- termdef_subclause_cleanup(xmldoc)
177
- termdocsource_cleanup(xmldoc)
178
- end
179
-
180
- def biblio_cleanup(xmldoc)
181
- xmldoc.xpath("//references[references]").each do |t|
182
- t.name = "clause"
183
- end
184
- end
185
-
186
- ELEMS_ALLOW_NOTES =
187
- # %w[p formula quote sourcecode example admonition ul ol dl figure]
188
- %w[p formula ul ol dl figure].freeze
189
-
190
- # if a note is at the end of a section, it is left alone
191
- # if a note is followed by a non-note block,
192
- # it is moved inside its preceding block if it is not delimited
193
- # (so there was no way of making that block include the note)
194
- def note_cleanup(xmldoc)
195
- q = "//note[following-sibling::*[not(local-name() = 'note')]]"
196
- xmldoc.xpath(q).each do |n|
197
- next unless n.ancestors("table").empty?
198
- prev = n.previous_element || next
199
- n.parent = prev if ELEMS_ALLOW_NOTES.include? prev.name
200
- end
201
- end
202
-
203
- def empty_text_before_first_element(x)
204
- x.children.each do |c|
205
- if c.text?
206
- return false if /\S/.match(c.text)
207
- end
208
- return true if c.element?
209
- end
210
- true
211
- end
212
-
213
- def strip_initial_space(x)
214
- if x.children[0].text?
215
- if !/\S/.match(x.children[0].text)
216
- x.children[0].remove
217
- else
218
- x.children[0].content = x.children[0].text.gsub(/^ /, "")
219
- end
220
- end
221
- end
222
-
223
- def bookmark_cleanup(xmldoc)
224
- xmldoc.xpath("//li[descendant::bookmark]").each do |x|
225
- if x&.elements&.first&.name == "p" &&
226
- x&.elements&.first&.elements&.first&.name == "bookmark"
227
- if empty_text_before_first_element(x.elements[0])
228
- x["id"] = x.elements[0].elements[0].remove["id"]
229
- strip_initial_space(x.elements[0])
230
- end
231
- end
232
- end
233
- end
234
-
235
- def mathml_cleanup(xmldoc)
236
- xmldoc.xpath("//stem[@type = 'MathML']").each do |x|
237
- math = x.text.gsub(/&lt;/, "<").gsub(/&gt;/, ">").gsub(/&quot;/, '"').
238
- gsub(/&amp;/, "&").gsub(/<[^:\/]+:/, "<").gsub(/<\/[^:]+:/, "</").
239
- gsub(/ xmlns[^>]+/, "").
240
- gsub(/<math>/, '<math xmlns="http://www.w3.org/1998/Math/MathML">')
241
- x.children = math
11
+ class Converter < Standoc::Converter
12
+ PRE_NORMREF_FOOTNOTES = "//foreword//fn | //introduction//fn |"\
13
+ "//clause[title = 'Scope']//fn" .freeze
14
+
15
+ NORMREF_FOOTNOTES =
16
+ "//references[title = 'Normative References']//fn |"\
17
+ "//references[title = 'Normative References']//bibitem/note".freeze
18
+
19
+ POST_NORMREF_FOOTNOTES =
20
+ "//clause[not(title = 'Scope')]//fn | "\
21
+ "//references[title = 'Bibliography']//fn | "\
22
+ "//references[title = 'Bibliography']//bibitem/note".freeze
23
+
24
+ def other_footnote_renumber(xmldoc)
25
+ seen = {}
26
+ i = 0
27
+ xmldoc.xpath(PRE_NORMREF_FOOTNOTES).each do |fn|
28
+ i, seen = other_footnote_renumber1(fn, i, seen)
29
+ end
30
+ xmldoc.xpath(NORMREF_FOOTNOTES).each do |fn|
31
+ i, seen = other_footnote_renumber1(fn, i, seen)
32
+ end
33
+ xmldoc.xpath(POST_NORMREF_FOOTNOTES).each do |fn|
34
+ i, seen = other_footnote_renumber1(fn, i, seen)
242
35
  end
243
36
  end
244
37
  end