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.
@@ -1,193 +0,0 @@
1
- require "date"
2
- require "nokogiri"
3
- require "htmlentities"
4
- require "json"
5
- require "pathname"
6
- require "open-uri"
7
- require "pp"
8
-
9
- module Asciidoctor
10
- module ISO
11
- module Cleanup
12
- def para_cleanup(xmldoc)
13
- xmldoc.xpath("//p[not(@id)]").each do |x|
14
- x["id"] = Utils::anchor_or_uuid
15
- end
16
- xmldoc.xpath("//note[not(@id)][not(ancestor::bibitem)]"\
17
- "[not(ancestor::table)]").each do |x|
18
- x["id"] = Utils::anchor_or_uuid
19
- end
20
- end
21
-
22
- # move Key dl after table footer
23
- def dl_table_cleanup(xmldoc)
24
- q = "//table/following-sibling::*[1]"\
25
- "[self::p and normalize-space() = 'Key']"
26
- xmldoc.xpath(q).each do |s|
27
- if !s.next_element.nil? && s.next_element.name == "dl"
28
- s.previous_element << s.next_element.remove
29
- s.remove
30
- end
31
- end
32
- end
33
-
34
- def insert_thead(s)
35
- thead = s.at("./thead")
36
- return thead unless thead.nil?
37
- if tname = s.at("./name")
38
- thead = tname.add_next_sibling("<thead/>").first
39
- return thead
40
- end
41
- s.children.first.add_previous_sibling("<thead/>").first
42
- end
43
-
44
- def header_rows_cleanup(xmldoc)
45
- xmldoc.xpath("//table[@headerrows]").each do |s|
46
- thead = insert_thead(s)
47
- (thead.xpath("./tr").size...s["headerrows"].to_i).each do
48
- row = s.at("./tbody/tr")
49
- row.parent = thead
50
- end
51
- s.delete("headerrows")
52
- end
53
- end
54
-
55
- def table_cleanup(xmldoc)
56
- dl_table_cleanup(xmldoc)
57
- notes_table_cleanup(xmldoc)
58
- header_rows_cleanup(xmldoc)
59
- end
60
-
61
- # move notes into table
62
- def notes_table_cleanup(xmldoc)
63
- nomatches = false
64
- until nomatches
65
- q = "//table/following-sibling::*[1][self::note]"
66
- nomatches = true
67
- xmldoc.xpath(q).each do |n|
68
- n.previous_element << n.remove
69
- nomatches = false
70
- end
71
- end
72
- end
73
-
74
- # include where definition list inside stem block
75
- def formula_cleanup(x)
76
- q = "//formula/following-sibling::*[1]"\
77
- "[self::p and text() = 'where']"
78
- x.xpath(q).each do |s|
79
- if !s.next_element.nil? && s.next_element.name == "dl"
80
- s.previous_element << s.next_element.remove
81
- s.remove
82
- end
83
- end
84
- end
85
-
86
- # include key definition list inside figure
87
- def figure_dl_cleanup(xmldoc)
88
- q = "//figure/following-sibling::*"\
89
- "[self::p and normalize-space() = 'Key']"
90
- xmldoc.xpath(q).each do |s|
91
- if !s.next_element.nil? && s.next_element.name == "dl"
92
- s.previous_element << s.next_element.remove
93
- s.remove
94
- end
95
- end
96
- end
97
-
98
- # examples containing only figures become subfigures of figures
99
- def subfigure_cleanup(xmldoc)
100
- nodes = xmldoc.xpath("//example/figure")
101
- while !nodes.empty?
102
- nodes[0].parent.name = "figure"
103
- nodes = xmldoc.xpath("//example/figure")
104
- end
105
- end
106
-
107
- def figure_cleanup(xmldoc)
108
- figure_footnote_cleanup(xmldoc)
109
- figure_dl_cleanup(xmldoc)
110
- subfigure_cleanup(xmldoc)
111
- end
112
-
113
- def make_preface(x, s)
114
- if x.at("//foreword | //introduction")
115
- preface = s.add_previous_sibling("<preface/>").first
116
- foreword = x.at("//foreword")
117
- preface.add_child foreword.remove if foreword
118
- introduction = x.at("//introduction")
119
- preface.add_child introduction.remove if introduction
120
- end
121
- end
122
-
123
- def make_bibliography(x, s)
124
- if x.at("//sections/references")
125
- biblio = s.add_next_sibling("<bibliography/>").first
126
- x.xpath("//sections/references").each do |r|
127
- biblio.add_child r.remove
128
- end
129
- end
130
- end
131
-
132
- def sections_order_cleanup(x)
133
- s = x.at("//sections")
134
- make_preface(x, s)
135
- make_bibliography(x, s)
136
- x.xpath("//sections/annex").reverse_each { |r| s.next = r.remove }
137
- end
138
-
139
- def maxlevel(x)
140
- max = 5
141
- x.xpath("//clause[@level]").each do |c|
142
- max = c["level"].to_i if max < c["level"].to_i
143
- end
144
- max
145
- end
146
-
147
- def sections_level_cleanup(x)
148
- m = maxlevel(x)
149
- return if m < 6
150
- m.downto(6).each do |l|
151
- x.xpath("//clause[@level = '#{l}']").each do |c|
152
- c.delete("level")
153
- c.previous_element << c.remove
154
- end
155
- end
156
- end
157
-
158
- def sections_cleanup(x)
159
- sections_order_cleanup(x)
160
- sections_level_cleanup(x)
161
- end
162
-
163
- def obligations_cleanup(x)
164
- obligations_cleanup_info(x)
165
- obligations_cleanup_norm(x)
166
- obligations_cleanup_inherit(x)
167
- end
168
-
169
- def obligations_cleanup_info(x)
170
- (s = x.at("//foreword")) && s["obligation"] = "informative"
171
- (s = x.at("//introduction")) && s["obligation"] = "informative"
172
- x.xpath("//references").each { |r| r["obligation"] = "informative" }
173
- end
174
-
175
- def obligations_cleanup_norm(x)
176
- (s = x.at("//clause[title = 'Scope']")) && s["obligation"] = "normative"
177
- (s = x.at("//clause[title = 'Symbols and Abbreviated Terms']")) &&
178
- s["obligation"] = "normative"
179
- x.xpath("//terms").each { |r| r["obligation"] = "normative" }
180
- x.xpath("//symbols-abbrevs").each { |r| r["obligation"] = "normative" }
181
- end
182
-
183
- def obligations_cleanup_inherit(x)
184
- x.xpath("//annex | //clause").each do |r|
185
- r["obligation"] = "normative" unless r["obligation"]
186
- end
187
- x.xpath(Utils::SUBCLAUSE_XPATH).each do |r|
188
- r["obligation"] = r.at("./ancestor::*/@obligation").text
189
- end
190
- end
191
- end
192
- end
193
- end
@@ -1,96 +0,0 @@
1
- require "date"
2
- require "nokogiri"
3
- require "htmlentities"
4
- require "json"
5
- require "pathname"
6
- require "open-uri"
7
- require "pp"
8
-
9
- module Asciidoctor
10
- module ISO
11
- module Cleanup
12
- # include footnotes inside figure
13
- def figure_footnote_cleanup(xmldoc)
14
- nomatches = false
15
- until nomatches
16
- q = "//figure/following-sibling::*[1][self::p and *[1][self::fn]]"
17
- nomatches = true
18
- xmldoc.xpath(q).each do |s|
19
- s.previous_element << s.first_element_child.remove
20
- s.remove
21
- nomatches = false
22
- end
23
- end
24
- end
25
-
26
- def table_footnote_renumber1(fn, i, seen)
27
- if seen[fn.text] then outnum = seen[fn.text]
28
- else
29
- i += 1
30
- outnum = i
31
- seen[fn.text] = outnum
32
- end
33
- fn["reference"] = (outnum - 1 + "a".ord).chr
34
- fn["table"] = true
35
- [i, seen]
36
- end
37
-
38
- def table_footnote_renumber(xmldoc)
39
- xmldoc.xpath("//table | //figure").each do |t|
40
- seen = {}
41
- i = 0
42
- t.xpath(".//fn").each do |fn|
43
- i, seen = table_footnote_renumber1(fn, i, seen)
44
- end
45
- end
46
- end
47
-
48
- def other_footnote_renumber1(fn, i, seen)
49
- unless fn["table"]
50
- if seen[fn.text] then outnum = seen[fn.text]
51
- else
52
- i += 1
53
- outnum = i
54
- seen[fn.text] = outnum
55
- end
56
- fn["reference"] = outnum.to_s
57
- end
58
- [i, seen]
59
- end
60
-
61
- PRE_NORMREF_FOOTNOTES = "//foreword//fn | //introduction//fn |"\
62
- "//clause[title = 'Scope']//fn" .freeze
63
-
64
- NORMREF_FOOTNOTES =
65
- "//references[title = 'Normative References']//fn |"\
66
- "//references[title = 'Normative References']//bibitem/note".freeze
67
-
68
- POST_NORMREF_FOOTNOTES =
69
- "//clause[not(title = 'Scope')]//fn | "\
70
- "//references[title = 'Bibliography']//fn | "\
71
- "//references[title = 'Bibliography']//bibitem/note".freeze
72
-
73
- def other_footnote_renumber(xmldoc)
74
- seen = {}
75
- i = 0
76
- xmldoc.xpath(PRE_NORMREF_FOOTNOTES).each do |fn|
77
- i, seen = other_footnote_renumber1(fn, i, seen)
78
- end
79
- xmldoc.xpath(NORMREF_FOOTNOTES).each do |fn|
80
- i, seen = other_footnote_renumber1(fn, i, seen)
81
- end
82
- xmldoc.xpath(POST_NORMREF_FOOTNOTES).each do |fn|
83
- i, seen = other_footnote_renumber1(fn, i, seen)
84
- end
85
- end
86
-
87
- def footnote_renumber(xmldoc)
88
- table_footnote_renumber(xmldoc)
89
- other_footnote_renumber(xmldoc)
90
- xmldoc.xpath("//fn").each do |fn|
91
- fn.delete("table")
92
- end
93
- end
94
- end
95
- end
96
- end
@@ -1,125 +0,0 @@
1
- module Asciidoctor
2
- module ISO
3
- module Cleanup
4
- # extending localities to cover ISO referencing
5
- LOCALITY_REGEX_STR = <<~REGEXP.freeze
6
- ^((?<locality>section|clause|part|paragraph|chapter|page|
7
- table|annex|figure|example|note|formula|
8
- locality:[^ \\t\\n\\r:,]+)(\\s+|=)
9
- (?<ref>[^"][^ \\t\\n,:-]*|"[^"]+")
10
- (-(?<to>[^"][^ \\t\\n,:-]*|"[^"]"))?|
11
- (?<locality2>whole|locality:[^ \\t\\n\\r:,]+))[,:]?\\s*
12
- (?<text>.*)$
13
- REGEXP
14
- LOCALITY_RE = Regexp.new(LOCALITY_REGEX_STR.gsub(/\s/, ""),
15
- Regexp::IGNORECASE | Regexp::MULTILINE)
16
-
17
- def tq(x)
18
- x.sub(/^"/, "").sub(/"$/, "")
19
- end
20
-
21
- def extract_localities(x)
22
- text = x.children.first.remove.text
23
- while (m = LOCALITY_RE.match text)
24
- ref = m[:ref] ? "<referenceFrom>#{tq m[:ref]}</referenceFrom>" : ""
25
- refto = m[:to] ? "<referenceTo>#{tq m[:to]}</referenceTo>" : ""
26
- loc = m[:locality]&.downcase || m[:locality2]&.downcase
27
- x.add_child("<locality type='#{loc}'>#{ref}#{refto}</locality>")
28
- text = m[:text]
29
- end
30
- x.add_child(text)
31
- end
32
-
33
- def xref_to_eref(x)
34
- x["bibitemid"] = x["target"]
35
- x["citeas"] = @anchors&.dig(x["target"], :xref) ||
36
- warn("ISO: #{x['target']} is not a real reference!")
37
- x.delete("target")
38
- extract_localities(x) unless x.children.empty?
39
- end
40
-
41
- def xref_cleanup(xmldoc)
42
- xmldoc.xpath("//xref").each do |x|
43
- if refid? x["target"]
44
- x.name = "eref"
45
- xref_to_eref(x)
46
- else
47
- x.delete("type")
48
- end
49
- end
50
- end
51
-
52
- # allows us to deal with doc relation localities,
53
- # temporarily stashed to "bpart"
54
- def bpart_cleanup(xmldoc)
55
- xmldoc.xpath("//relation/bpart").each do |x|
56
- extract_localities(x)
57
- x.replace(x.children)
58
- end
59
- end
60
-
61
- def quotesource_cleanup(xmldoc)
62
- xmldoc.xpath("//quote/source | //terms/source").each do |x|
63
- xref_to_eref(x)
64
- end
65
- end
66
-
67
- def origin_cleanup(xmldoc)
68
- xmldoc.xpath("//origin").each do |x|
69
- x["citeas"] = @anchors&.dig(x["bibitemid"], :xref) ||
70
- warn("ISO: #{x['bibitemid']} is not a real reference!")
71
- extract_localities(x) unless x.children.empty?
72
- end
73
- end
74
-
75
- # move ref before p
76
- def ref_cleanup(xmldoc)
77
- xmldoc.xpath("//p/ref").each do |r|
78
- parent = r.parent
79
- parent.previous = r.remove
80
- end
81
- end
82
-
83
- def normref_cleanup(xmldoc)
84
- q = "//references[title = 'Normative References']"
85
- r = xmldoc.at(q) || return
86
- r.elements.each do |n|
87
- n.remove unless ["title", "bibitem"].include? n.name
88
- end
89
- end
90
-
91
- def format_ref(ref, isopub)
92
- return ref if isopub
93
- return "[#{ref}]" if /^\d+$/.match(ref) && !/^\[.*\]$/.match(ref)
94
- ref
95
- end
96
-
97
- ISO_PUBLISHER_XPATH =
98
- "./contributor[role/@type = 'publisher']/"\
99
- "organization[abbreviation = 'ISO' or abbreviation = 'IEC' or "\
100
- "name = 'International Organization for Standardization' or "\
101
- "name = 'International Electrotechnical Commission']".freeze
102
-
103
- def date_range(date)
104
- from = date.at("./from")
105
- to = date.at("./to")
106
- on = date.at("./on")
107
- return on.text if on
108
- ret = "#{from.text}&ndash;"
109
- ret += to.text if to
110
- ret
111
- end
112
-
113
- def reference_names(xmldoc)
114
- xmldoc.xpath("//bibitem[not(ancestor::bibitem)]").each do |ref|
115
- isopub = ref.at(ISO_PUBLISHER_XPATH)
116
- docid = ref.at("./docidentifier")
117
- date = ref.at("./date[@type = 'published']")
118
- reference = format_ref(docid.text, isopub)
119
- reference += ":#{date_range(date)}" if date && isopub
120
- @anchors[ref["id"]] = { xref: reference }
121
- end
122
- end
123
- end
124
- end
125
- end
@@ -1,134 +0,0 @@
1
- require "asciidoctor/extensions"
2
- require "htmlentities"
3
-
4
- module Asciidoctor
5
- module ISO
6
- module Inline
7
- def refid?(x)
8
- @refids.include? x
9
- end
10
-
11
- def inline_anchor(node)
12
- case node.type
13
- when :ref
14
- inline_anchor_ref node
15
- when :xref
16
- inline_anchor_xref node
17
- when :link
18
- inline_anchor_link node
19
- when :bibref
20
- inline_anchor_bibref node
21
- end
22
- end
23
-
24
- def inline_anchor_ref(node)
25
- noko do |xml|
26
- xml.bookmark nil, **attr_code(id: node.id)
27
- end.join
28
- end
29
-
30
- def inline_anchor_xref(node)
31
- matched = /^fn(:\s*(?<text>.*))?$/.match node.text
32
- f = matched.nil? ? "inline" : "footnote"
33
- c = matched.nil? ? node.text : matched[:text]
34
- t = node.target.gsub(/^#/, "").gsub(%r{(.)(\.xml)?#.*$}, "\\1")
35
- noko do |xml|
36
- xml.xref **attr_code(target: t, type: f) do |x|
37
- x << c
38
- end
39
- end.join
40
- end
41
-
42
- def inline_anchor_link(node)
43
- contents = node.text
44
- contents = "" if node.target.gsub(%r{^mailto:}, "") == node.text
45
- attributes = { "target": node.target }
46
- noko do |xml|
47
- xml.link **attr_code(attributes) do |l|
48
- l << contents
49
- end
50
- end.join
51
- end
52
-
53
- def inline_anchor_bibref(node)
54
- eref_contents = node.target == node.text ? nil : node.text
55
- eref_attributes = { id: node.target }
56
- @refids << node.target
57
- noko do |xml|
58
- xml.ref **attr_code(eref_attributes) do |r|
59
- r << eref_contents
60
- end
61
- end.join
62
- end
63
-
64
- def inline_callout(node)
65
- noko do |xml|
66
- xml.callout node.text
67
- end.join
68
- end
69
-
70
- def inline_footnote(node)
71
- noko do |xml|
72
- @fn_number += 1
73
- xml.fn **{ reference: @fn_number } do |fn|
74
- fn.p { |p| p << node.text }
75
- end
76
- end.join("\n")
77
- end
78
-
79
- def inline_break(node)
80
- noko do |xml|
81
- xml << node.text
82
- xml.br
83
- end.join("\n")
84
- end
85
-
86
- def page_break(_node)
87
- noko { |xml| xml.pagebreak }.join("\n")
88
- end
89
-
90
- def thematic_break(_node)
91
- noko { |xml| xml.hr }.join("\n")
92
- end
93
-
94
- def stem_parse(text, xml)
95
- if /&lt;([^:>&]+:)?math(\s+[^>&]+)?&gt; |
96
- <([^:>&]+:)?math(\s+[^>&]+)?>/x.match text
97
- math = HTMLEntities.new.encode(text, :basic, :hexadecimal).
98
- gsub(/&amp;gt;/, ">").gsub(/\&amp;lt;/, "<").gsub(/&amp;amp;/, "&").
99
- gsub(/&gt;/, ">").gsub(/&lt;/, "<").gsub(/&amp;/, "&")
100
- xml.stem math, **{ type: "MathML" }
101
- else
102
- xml.stem text, **{ type: "AsciiMath" }
103
- end
104
- end
105
-
106
- def inline_quoted(node)
107
- noko do |xml|
108
- case node.type
109
- when :emphasis then xml.em node.text
110
- when :strong then xml.strong node.text
111
- when :monospaced then xml.tt node.text
112
- when :double then xml << "\"#{node.text}\""
113
- when :single then xml << "'#{node.text}'"
114
- when :superscript then xml.sup node.text
115
- when :subscript then xml.sub node.text
116
- when :asciimath then stem_parse(node.text, xml)
117
- else
118
- case node.role
119
- # the following three are legacy, they are now handled by macros
120
- when "alt" then xml.admitted { |a| a << node.text }
121
- when "deprecated" then xml.deprecates { |a| a << node.text }
122
- when "domain" then xml.domain { |a| a << node.text }
123
-
124
- when "strike" then xml.strike node.text
125
- when "smallcap" then xml.smallcap node.text
126
- else
127
- xml << node.text
128
- end
129
- end
130
- end.join
131
- end
132
- end
133
- end
134
- end