metanorma-standoc 1.11.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/rake.yml +3 -31
  3. data/.gitignore +23 -0
  4. data/Gemfile +2 -0
  5. data/lib/asciidoctor/standoc/base.rb +2 -241
  6. data/lib/asciidoctor/standoc/blocks.rb +2 -238
  7. data/lib/asciidoctor/standoc/blocks_notes.rb +2 -100
  8. data/lib/asciidoctor/standoc/cleanup.rb +2 -207
  9. data/lib/asciidoctor/standoc/cleanup_amend.rb +2 -53
  10. data/lib/asciidoctor/standoc/cleanup_block.rb +2 -173
  11. data/lib/asciidoctor/standoc/cleanup_boilerplate.rb +2 -204
  12. data/lib/asciidoctor/standoc/cleanup_footnotes.rb +2 -108
  13. data/lib/asciidoctor/standoc/cleanup_image.rb +2 -69
  14. data/lib/asciidoctor/standoc/cleanup_inline.rb +2 -179
  15. data/lib/asciidoctor/standoc/cleanup_maths.rb +2 -221
  16. data/lib/asciidoctor/standoc/cleanup_ref.rb +2 -169
  17. data/lib/asciidoctor/standoc/cleanup_ref_dl.rb +2 -93
  18. data/lib/asciidoctor/standoc/cleanup_reqt.rb +2 -110
  19. data/lib/asciidoctor/standoc/cleanup_section.rb +2 -184
  20. data/lib/asciidoctor/standoc/cleanup_section_names.rb +2 -91
  21. data/lib/asciidoctor/standoc/cleanup_symbols.rb +2 -47
  22. data/lib/asciidoctor/standoc/cleanup_table.rb +2 -67
  23. data/lib/asciidoctor/standoc/cleanup_terms.rb +2 -113
  24. data/lib/asciidoctor/standoc/cleanup_terms_designations.rb +2 -161
  25. data/lib/asciidoctor/standoc/cleanup_text.rb +2 -95
  26. data/lib/asciidoctor/standoc/cleanup_toc.rb +3 -0
  27. data/lib/asciidoctor/standoc/cleanup_xref.rb +2 -106
  28. data/lib/asciidoctor/standoc/converter.rb +2 -123
  29. data/lib/asciidoctor/standoc/datamodel/attributes_table_preprocessor.rb +2 -56
  30. data/lib/asciidoctor/standoc/datamodel/diagram_preprocessor.rb +2 -102
  31. data/lib/asciidoctor/standoc/datamodel/plantuml_renderer.rb +3 -404
  32. data/lib/asciidoctor/standoc/deprecated.rb +5 -0
  33. data/lib/asciidoctor/standoc/front.rb +2 -219
  34. data/lib/asciidoctor/standoc/front_contributor.rb +2 -191
  35. data/lib/asciidoctor/standoc/inline.rb +2 -229
  36. data/lib/asciidoctor/standoc/lists.rb +2 -119
  37. data/lib/asciidoctor/standoc/macros.rb +2 -203
  38. data/lib/asciidoctor/standoc/macros_form.rb +2 -62
  39. data/lib/asciidoctor/standoc/macros_note.rb +2 -44
  40. data/lib/asciidoctor/standoc/macros_plantuml.rb +2 -112
  41. data/lib/asciidoctor/standoc/macros_terms.rb +2 -180
  42. data/lib/asciidoctor/standoc/ref.rb +2 -225
  43. data/lib/asciidoctor/standoc/ref_sect.rb +2 -143
  44. data/lib/asciidoctor/standoc/ref_utility.rb +2 -0
  45. data/lib/asciidoctor/standoc/render.rb +3 -0
  46. data/lib/asciidoctor/standoc/reqt.rb +2 -89
  47. data/lib/asciidoctor/standoc/section.rb +2 -190
  48. data/lib/asciidoctor/standoc/table.rb +2 -84
  49. data/lib/asciidoctor/standoc/term_lookup_cleanup.rb +2 -178
  50. data/lib/asciidoctor/standoc/terms.rb +2 -153
  51. data/lib/asciidoctor/standoc/utils.rb +2 -116
  52. data/lib/asciidoctor/standoc/validate.rb +2 -157
  53. data/lib/asciidoctor/standoc/validate_section.rb +2 -54
  54. data/lib/isodoc/html/htmlstyle.css +20 -11
  55. data/lib/isodoc/html/htmlstyle.scss +11 -11
  56. data/lib/metanorma/standoc/base.rb +149 -0
  57. data/lib/{asciidoctor → metanorma}/standoc/basicdoc.rng +0 -0
  58. data/lib/{asciidoctor → metanorma}/standoc/biblio.rng +0 -0
  59. data/lib/metanorma/standoc/blocks.rb +239 -0
  60. data/lib/metanorma/standoc/blocks_notes.rb +101 -0
  61. data/lib/metanorma/standoc/cleanup.rb +146 -0
  62. data/lib/metanorma/standoc/cleanup_amend.rb +54 -0
  63. data/lib/metanorma/standoc/cleanup_block.rb +173 -0
  64. data/lib/metanorma/standoc/cleanup_boilerplate.rb +213 -0
  65. data/lib/metanorma/standoc/cleanup_footnotes.rb +109 -0
  66. data/lib/metanorma/standoc/cleanup_image.rb +70 -0
  67. data/lib/metanorma/standoc/cleanup_inline.rb +190 -0
  68. data/lib/metanorma/standoc/cleanup_maths.rb +222 -0
  69. data/lib/metanorma/standoc/cleanup_ref.rb +170 -0
  70. data/lib/metanorma/standoc/cleanup_ref_dl.rb +104 -0
  71. data/lib/metanorma/standoc/cleanup_reqt.rb +111 -0
  72. data/lib/metanorma/standoc/cleanup_section.rb +212 -0
  73. data/lib/metanorma/standoc/cleanup_section_names.rb +92 -0
  74. data/lib/metanorma/standoc/cleanup_symbols.rb +48 -0
  75. data/lib/metanorma/standoc/cleanup_table.rb +68 -0
  76. data/lib/metanorma/standoc/cleanup_terms.rb +140 -0
  77. data/lib/metanorma/standoc/cleanup_terms_designations.rb +199 -0
  78. data/lib/metanorma/standoc/cleanup_text.rb +96 -0
  79. data/lib/metanorma/standoc/cleanup_toc.rb +98 -0
  80. data/lib/metanorma/standoc/cleanup_xref.rb +107 -0
  81. data/lib/metanorma/standoc/converter.rb +124 -0
  82. data/lib/metanorma/standoc/datamodel/attributes_table_preprocessor.rb +57 -0
  83. data/lib/metanorma/standoc/datamodel/diagram_preprocessor.rb +103 -0
  84. data/lib/metanorma/standoc/datamodel/plantuml_renderer.rb +409 -0
  85. data/lib/metanorma/standoc/front.rb +224 -0
  86. data/lib/metanorma/standoc/front_contributor.rb +192 -0
  87. data/lib/metanorma/standoc/inline.rb +232 -0
  88. data/lib/{asciidoctor → metanorma}/standoc/isodoc.rng +90 -18
  89. data/lib/metanorma/standoc/lists.rb +120 -0
  90. data/lib/metanorma/standoc/macros.rb +204 -0
  91. data/lib/metanorma/standoc/macros_form.rb +63 -0
  92. data/lib/metanorma/standoc/macros_note.rb +45 -0
  93. data/lib/metanorma/standoc/macros_plantuml.rb +113 -0
  94. data/lib/metanorma/standoc/macros_terms.rb +181 -0
  95. data/lib/metanorma/standoc/ref.rb +243 -0
  96. data/lib/metanorma/standoc/ref_sect.rb +153 -0
  97. data/lib/metanorma/standoc/ref_utility.rb +129 -0
  98. data/lib/metanorma/standoc/render.rb +115 -0
  99. data/lib/metanorma/standoc/reqt.rb +90 -0
  100. data/lib/{asciidoctor → metanorma}/standoc/reqt.rng +0 -0
  101. data/lib/metanorma/standoc/section.rb +209 -0
  102. data/lib/metanorma/standoc/table.rb +85 -0
  103. data/lib/metanorma/standoc/term_lookup_cleanup.rb +179 -0
  104. data/lib/metanorma/standoc/terms.rb +160 -0
  105. data/lib/metanorma/standoc/utils.rb +101 -0
  106. data/lib/metanorma/standoc/validate.rb +158 -0
  107. data/lib/metanorma/standoc/validate_section.rb +55 -0
  108. data/lib/metanorma/standoc/version.rb +1 -1
  109. data/lib/{asciidoctor → metanorma}/standoc/views/datamodel/model_representation.adoc.erb +0 -0
  110. data/lib/{asciidoctor → metanorma}/standoc/views/datamodel/plantuml_representation.adoc.erb +0 -0
  111. data/lib/metanorma-standoc.rb +1 -1
  112. data/metanorma-standoc.gemspec +4 -4
  113. data/spec/{asciidoctor → metanorma}/base_spec.rb +73 -8
  114. data/spec/{asciidoctor → metanorma}/blank_spec.rb +1 -1
  115. data/spec/{asciidoctor → metanorma}/blocks_spec.rb +49 -20
  116. data/spec/{asciidoctor → metanorma}/cleanup_blocks_spec.rb +25 -1
  117. data/spec/{asciidoctor → metanorma}/cleanup_sections_spec.rb +2 -2
  118. data/spec/{asciidoctor → metanorma}/cleanup_spec.rb +9 -9
  119. data/spec/{asciidoctor → metanorma}/cleanup_terms_spec.rb +528 -91
  120. data/spec/{asciidoctor → metanorma}/datamodel/attributes_table_preprocessor_spec.rb +22 -22
  121. data/spec/{asciidoctor → metanorma}/datamodel/diagram_preprocessor_spec.rb +17 -17
  122. data/spec/{asciidoctor → metanorma}/inline_spec.rb +175 -6
  123. data/spec/{asciidoctor → metanorma}/isobib_cache_spec.rb +5 -9
  124. data/spec/{asciidoctor → metanorma}/lists_spec.rb +1 -1
  125. data/spec/{asciidoctor → metanorma}/macros_json2text_spec.rb +0 -0
  126. data/spec/{asciidoctor → metanorma}/macros_plantuml_spec.rb +3 -3
  127. data/spec/{asciidoctor → metanorma}/macros_spec.rb +8 -8
  128. data/spec/{asciidoctor → metanorma}/macros_yaml2text_spec.rb +0 -0
  129. data/spec/metanorma/refs_dl_spec.rb +863 -0
  130. data/spec/{asciidoctor → metanorma}/refs_spec.rb +1277 -687
  131. data/spec/{asciidoctor → metanorma}/section_spec.rb +90 -3
  132. data/spec/{asciidoctor → metanorma}/table_spec.rb +1 -1
  133. data/spec/{asciidoctor → metanorma}/validate_spec.rb +2 -2
  134. data/spec/spec_helper.rb +0 -1
  135. data/spec/vcr_cassettes/dated_iso_ref_joint_iso_iec.yml +179 -179
  136. data/spec/vcr_cassettes/dated_iso_ref_joint_iso_iec1.yml +12 -12
  137. data/spec/vcr_cassettes/isobib_get_123.yml +13 -13
  138. data/spec/vcr_cassettes/isobib_get_123_1.yml +98 -98
  139. data/spec/vcr_cassettes/isobib_get_123_1_fr.yml +111 -111
  140. data/spec/vcr_cassettes/isobib_get_123_2001.yml +13 -13
  141. data/spec/vcr_cassettes/isobib_get_124.yml +14 -14
  142. data/spec/vcr_cassettes/rfcbib_get_rfc8341.yml +14 -14
  143. data/spec/vcr_cassettes/separates_iev_citations_by_top_level_clause.yml +46 -46
  144. metadata +89 -38
  145. data/lib/asciidoctor/standoc/ref_date_id.rb +0 -62
  146. data/spec/asciidoctor/refs_dl_spec.rb +0 -864
@@ -1,208 +1,3 @@
1
- require "nokogiri"
2
- require "pathname"
3
- require "html2doc"
4
- require_relative "./cleanup_block"
5
- require_relative "./cleanup_table"
6
- require_relative "./cleanup_footnotes"
7
- require_relative "./cleanup_ref"
8
- require_relative "./cleanup_ref_dl"
9
- require_relative "./cleanup_boilerplate"
10
- require_relative "./cleanup_section"
11
- require_relative "./cleanup_terms"
12
- require_relative "./cleanup_symbols"
13
- require_relative "./cleanup_xref"
14
- require_relative "./cleanup_inline"
15
- require_relative "./cleanup_amend"
16
- require_relative "./cleanup_maths"
17
- require_relative "./cleanup_image"
18
- require_relative "./cleanup_reqt"
19
- require_relative "./cleanup_text"
20
- require "relaton_iev"
1
+ require "asciidoctor/standoc/deprecated"
2
+ require "metanorma/standoc/cleanup"
21
3
 
22
- module Asciidoctor
23
- module Standoc
24
- module Cleanup
25
- def cleanup(xmldoc)
26
- element_name_cleanup(xmldoc)
27
- sections_cleanup(xmldoc)
28
- obligations_cleanup(xmldoc)
29
- table_cleanup(xmldoc)
30
- formula_cleanup(xmldoc)
31
- form_cleanup(xmldoc)
32
- sourcecode_cleanup(xmldoc)
33
- figure_cleanup(xmldoc)
34
- element_name_cleanup(xmldoc)
35
- ref_cleanup(xmldoc)
36
- note_cleanup(xmldoc)
37
- clausebefore_cleanup(xmldoc)
38
- bibitem_cleanup(xmldoc)
39
- normref_cleanup(xmldoc)
40
- biblio_cleanup(xmldoc)
41
- reference_names(xmldoc)
42
- symbols_cleanup(xmldoc)
43
- xref_cleanup(xmldoc)
44
- concept_cleanup(xmldoc)
45
- related_cleanup(xmldoc)
46
- origin_cleanup(xmldoc)
47
- bookmark_cleanup(xmldoc)
48
- termdef_cleanup(xmldoc)
49
- RelatonIev::iev_cleanup(xmldoc, @bibdb)
50
- element_name_cleanup(xmldoc)
51
- bpart_cleanup(xmldoc)
52
- quotesource_cleanup(xmldoc)
53
- callout_cleanup(xmldoc)
54
- footnote_cleanup(xmldoc)
55
- mathml_cleanup(xmldoc)
56
- script_cleanup(xmldoc)
57
- docidentifier_cleanup(xmldoc)
58
- requirement_cleanup(xmldoc)
59
- bibdata_cleanup(xmldoc)
60
- svgmap_cleanup(xmldoc)
61
- boilerplate_cleanup(xmldoc)
62
- toc_cleanup(xmldoc)
63
- smartquotes_cleanup(xmldoc)
64
- variant_cleanup(xmldoc)
65
- para_cleanup(xmldoc)
66
- empty_element_cleanup(xmldoc)
67
- img_cleanup(xmldoc)
68
- anchor_cleanup(xmldoc)
69
- xmldoc
70
- end
71
-
72
- def docidentifier_cleanup(xmldoc); end
73
-
74
- TEXT_ELEMS =
75
- %w{status language script version author name callout phone email
76
- street city state country postcode identifier referenceFrom surname
77
- referenceTo docidentifier docnumber prefix initial addition forename
78
- title draft secretariat title-main title-intro title-part
79
- verbaldefinition nonverbalrepresentation}.freeze
80
-
81
- # it seems Nokogiri::XML is treating the content of <script> as cdata,
82
- # because of its use in HTML. Bad nokogiri. Undoing that, since we use
83
- # script as a normal tag
84
- def script_cleanup(xmldoc)
85
- xmldoc.xpath("//script").each { |x| x.content = x.to_str }
86
- end
87
-
88
- def empty_element_cleanup(xmldoc)
89
- xmldoc.xpath("//#{TEXT_ELEMS.join(' | //')}").each do |x|
90
- next if x.name == "name" && x.parent.name == "expression"
91
-
92
- x.remove if x.children.empty?
93
- end
94
- end
95
-
96
- def element_name_cleanup(xmldoc)
97
- xmldoc.traverse { |n| n.name = n.name.gsub(/_/, "-") }
98
- end
99
-
100
- # allows us to deal with doc relation localities,
101
- # temporarily stashed to "bpart"
102
- def bpart_cleanup(xmldoc)
103
- xmldoc.xpath("//relation/bpart").each do |x|
104
- extract_localities(x)
105
- x.replace(x.children)
106
- end
107
- end
108
-
109
- def variant_cleanup(xmldoc)
110
- variant_space_cleanup(xmldoc)
111
- xmldoc.xpath("//*[variant]").each do |c|
112
- next unless c.children.any? do |n|
113
- n.name != "variant" && (!n.text? || !n.text.gsub(/\s/, "").empty?)
114
- end
115
-
116
- variant_cleanup1(c)
117
- end
118
- xmldoc.xpath("//variantwrap").each { |n| n.name = "variant" }
119
- end
120
-
121
- def variant_cleanup1(elem)
122
- elem.xpath("./variant").each do |n|
123
- if n.at_xpath("preceding-sibling::node()"\
124
- "[not(self::text()[not(normalize-space())])][1]"\
125
- "[self::variantwrap]")
126
- n.previous_element << n
127
- else
128
- n.replace("<variantwrap/>").first << n
129
- end
130
- end
131
- end
132
-
133
- def variant_space_cleanup(xmldoc)
134
- xmldoc.xpath("//*[variant]").each do |c|
135
- if c&.next&.text? && c&.next&.next&.name == "variant"
136
- c.next.text.gsub(/\s/, "").empty? and
137
- c.next.remove
138
- end
139
- end
140
- end
141
-
142
- def toc_cleanup(xmldoc)
143
- toc_cleanup_para(xmldoc)
144
- xmldoc.xpath("//toc").each { |t| toc_cleanup1(t, xmldoc) }
145
- toc_cleanup_clause(xmldoc)
146
- end
147
-
148
- def toc_cleanup_para(xmldoc)
149
- xmldoc.xpath("//p[toc]").each do |x|
150
- x.xpath("./toc").reverse.each do |t|
151
- x.next = t
152
- end
153
- x.remove if x.text.strip.empty?
154
- end
155
- end
156
-
157
- def toc_index(toc, xmldoc)
158
- depths = toc_index_depths(toc)
159
- depths.keys.each_with_object([]) do |key, arr|
160
- xmldoc.xpath(key).each do |x|
161
- t = x.at("./following-sibling::variant-title[@type = 'toc']") and
162
- x = t
163
- arr << { text: x.children.to_xml, depth: depths[key].to_i,
164
- target: x.xpath("(./ancestor-or-self::*/@id)[last()]")[0].text,
165
- line: x.line }
166
- end
167
- end.sort_by { |a| a[:line] }
168
- end
169
-
170
- def toc_index_depths(toc)
171
- toc.xpath("./toc-xpath").each_with_object({}) do |x, m|
172
- m[x.text] = x["depth"]
173
- end
174
- end
175
-
176
- def toc_cleanup1(toc, xmldoc)
177
- depth = 1
178
- ret = ""
179
- toc_index(toc, xmldoc).each do |x|
180
- if depth > x[:depth] then ret += "</ul></li>" * (depth - x[:depth])
181
- elsif depth < x[:depth] then ret += "<li><ul>" * (x[:depth] - depth)
182
- end
183
- ret += "<li><xref target='#{x[:target]}'>#{x[:text]}</xref></li>"
184
- depth = x[:depth]
185
- end
186
- toc.children = "<ul>#{ret}</ul>"
187
- end
188
-
189
- def toc_cleanup_clause(xmldoc)
190
- xmldoc
191
- .xpath("//clause[@type = 'toc'] | //annex[@type = 'toc']").each do |c|
192
- c.xpath(".//ul[not(ancestor::ul)]").each do |ul|
193
- toc_cleanup_clause_entry(xmldoc, ul)
194
- ul.replace("<toc>#{ul.to_xml}</toc>")
195
- end
196
- end
197
- end
198
-
199
- def toc_cleanup_clause_entry(xmldoc, list)
200
- list.xpath(".//xref[not(text())]").each do |x|
201
- c1 = xmldoc.at("//*[@id = '#{x['target']}']")
202
- t = c1.at("./variant-title[@type = 'toc']") || c1.at("./title")
203
- x << t.dup.children
204
- end
205
- end
206
- end
207
- end
208
- end
@@ -1,54 +1,3 @@
1
- module Asciidoctor
2
- module Standoc
3
- module Cleanup
4
- def change_clauses(x)
5
- x.xpath("//clause[@change]").each do |c|
6
- a = create_amend(c)
7
- end
8
- end
1
+ require "asciidoctor/standoc/deprecated"
2
+ require "metanorma/standoc/cleanup_amend"
9
3
 
10
- def create_amend(c)
11
- a = c.add_child("<amend id='_#{UUIDTools::UUID.random_create}'/>").first
12
- c.elements.each do |e|
13
- e.parent = a unless %w(amend title).include? e.name
14
- end
15
- create_amend1(c, a)
16
- end
17
-
18
- def create_amend1(c, a)
19
- create_amend2(c, a)
20
- d = a.at("./description")
21
- d.xpath(".//autonumber").each { |e| d.previous = e }
22
- d.xpath(".//p[normalize-space(.)='']").each { |e| e.remove }
23
- move_attrs_to_amend(c, a)
24
- a
25
- end
26
-
27
- def create_amend2(c, a)
28
- q = a.at("./quote") and q.name = "newcontent"
29
- if q.nil?
30
- a.children = "<description>#{a.children.to_xml}</description>"
31
- else
32
- pre = q&.xpath("./preceding-sibling::*")&.remove
33
- post = q&.xpath("./following-sibling::*")&.remove
34
- pre.empty? or a << "<description>#{pre.to_xml}</description>"
35
- a << q.remove
36
- post.empty? or a << "<description>#{post.to_xml}</description>"
37
- end
38
- end
39
-
40
- def move_attrs_to_amend(c, a)
41
- %w(change path path_end title).each do |e|
42
- next unless c[e]
43
- a[e] = c[e]
44
- c.delete(e)
45
- end
46
- return unless a["locality"]
47
- loc = a.children.add_previous_sibling("<location/>")
48
- extract_localities1(loc, a["locality"])
49
- loc1 = loc.at("./localityStack") and loc.replace(loc1.elements)
50
- a.delete("locality")
51
- end
52
- end
53
- end
54
- end
@@ -1,174 +1,3 @@
1
- require "date"
2
- require "htmlentities"
1
+ require "asciidoctor/standoc/deprecated"
2
+ require "metanorma/standoc/cleanup_block"
3
3
 
4
- module Asciidoctor
5
- module Standoc
6
- module Cleanup
7
- def para_cleanup(xmldoc)
8
- ["//p[not(ancestor::bibdata)]", "//ol[not(ancestor::bibdata)]",
9
- "//ul[not(ancestor::bibdata)]", "//quote[not(ancestor::bibdata)]",
10
- "//note[not(ancestor::bibitem or "\
11
- "ancestor::table or ancestor::bibdata)]"].each do |w|
12
- inject_id(xmldoc, w)
13
- end
14
- end
15
-
16
- def inject_id(xmldoc, path)
17
- xmldoc.xpath(path).each do |x|
18
- x["id"] ||= Metanorma::Utils::anchor_or_uuid
19
- end
20
- end
21
-
22
- # include where definition list inside stem block
23
- def formula_cleanup(formula)
24
- formula_cleanup_where1(formula)
25
- formula_cleanup_where2(formula)
26
- end
27
-
28
- def formula_cleanup_where1(formula)
29
- q = "//formula/following-sibling::*[1][self::dl]"
30
- formula.xpath(q).each do |s|
31
- s["key"] == "true" and s.previous_element << s.remove
32
- end
33
- end
34
-
35
- def formula_cleanup_where2(formula)
36
- q = "//formula/following-sibling::*[1][self::p]"
37
- formula.xpath(q).each do |s|
38
- if s.text =~ /^\s*where[^a-z]*$/i && s&.next_element&.name == "dl"
39
- s.next_element["key"] = "true"
40
- s.previous_element << s.next_element.remove
41
- s.remove
42
- end
43
- end
44
- end
45
-
46
- def figure_dl_cleanup1(xmldoc)
47
- q = "//figure/following-sibling::*[self::dl]"
48
- xmldoc.xpath(q).each do |s|
49
- s["key"] == "true" and s.previous_element << s.remove
50
- end
51
- end
52
-
53
- # include key definition list inside figure
54
- def figure_dl_cleanup2(xmldoc)
55
- q = "//figure/following-sibling::*[self::p]"
56
- xmldoc.xpath(q).each do |s|
57
- if s.text =~ /^\s*key[^a-z]*$/i && s&.next_element&.name == "dl"
58
- s.next_element["key"] = "true"
59
- s.previous_element << s.next_element.remove
60
- s.remove
61
- end
62
- end
63
- end
64
-
65
- # examples containing only figures become subfigures of figures
66
- def subfigure_cleanup(xmldoc)
67
- xmldoc.xpath("//example[figure]").each do |e|
68
- next unless e.elements.map(&:name).reject do |m|
69
- %w(name figure).include? m
70
- end.empty?
71
-
72
- e.name = "figure"
73
- end
74
- end
75
-
76
- def figure_cleanup(xmldoc)
77
- figure_footnote_cleanup(xmldoc)
78
- figure_dl_cleanup1(xmldoc)
79
- figure_dl_cleanup2(xmldoc)
80
- subfigure_cleanup(xmldoc)
81
- end
82
-
83
- ELEMS_ALLOW_NOTES = %w[p formula ul ol dl figure].freeze
84
-
85
- # if a note is at the end of a section, it is left alone
86
- # if a note is followed by a non-note block,
87
- # it is moved inside its preceding block if it is not delimited
88
- # (so there was no way of making that block include the note)
89
- def note_cleanup(xmldoc)
90
- q = "//note[following-sibling::*[not(local-name() = 'note')]]"
91
- xmldoc.xpath(q).each do |n|
92
- next if n["keep-separate"] == "true"
93
- next unless n.ancestors("table").empty?
94
-
95
- prev = n.previous_element || next
96
- n.parent = prev if ELEMS_ALLOW_NOTES.include? prev.name
97
- end
98
- xmldoc.xpath("//note[@keep-separate] | "\
99
- "//termnote[@keep-separate]").each do |n|
100
- n.delete("keep-separate")
101
- end
102
- end
103
-
104
- def link_callouts_to_annotations(callouts, annotations)
105
- callouts.each_with_index do |c, i|
106
- c["target"] = "_#{UUIDTools::UUID.random_create}"
107
- annotations[i]["id"] = c["target"]
108
- end
109
- end
110
-
111
- def align_callouts_to_annotations(xmldoc)
112
- xmldoc.xpath("//sourcecode").each do |x|
113
- callouts = x.elements.select { |e| e.name == "callout" }
114
- annotations = x.elements.select { |e| e.name == "annotation" }
115
- callouts.size == annotations.size and
116
- link_callouts_to_annotations(callouts, annotations)
117
- end
118
- end
119
-
120
- def merge_annotations_into_sourcecode(xmldoc)
121
- xmldoc.xpath("//sourcecode").each do |x|
122
- while x&.next_element&.name == "annotation"
123
- x.next_element.parent = x
124
- end
125
- end
126
- end
127
-
128
- def callout_cleanup(xmldoc)
129
- merge_annotations_into_sourcecode(xmldoc)
130
- align_callouts_to_annotations(xmldoc)
131
- end
132
-
133
- def sourcecode_cleanup(xmldoc)
134
- xmldoc.xpath("//sourcecode").each do |x|
135
- x.traverse do |n|
136
- next unless n.text?
137
- next unless /#{Regexp.escape(@sourcecode_markup_start)}/
138
- .match?(n.text)
139
-
140
- n.replace(sourcecode_markup(n))
141
- end
142
- end
143
- end
144
-
145
- def safe_noko(text, doc)
146
- Nokogiri::XML::Text.new(text, doc).to_xml(
147
- encoding: "US-ASCII",
148
- save_with: Nokogiri::XML::Node::SaveOptions::NO_DECLARATION,
149
- )
150
- end
151
-
152
- def sourcecode_markup(node)
153
- node.text.split(/(#{Regexp.escape(@sourcecode_markup_start)}|
154
- #{Regexp.escape(@sourcecode_markup_end)})/x)
155
- .each_slice(4).map.with_object([]) do |a, acc|
156
- acc << safe_noko(a[0], node.document)
157
- next unless a.size == 4
158
-
159
- acc << Asciidoctor.convert(
160
- a[2], doctype: :inline, backend: (self&.backend&.to_sym || :standoc)
161
- )
162
- end.join
163
- end
164
-
165
- def form_cleanup(xmldoc)
166
- xmldoc.xpath("//select").each do |s|
167
- while s&.next_element&.name == "option"
168
- s << s.next_element
169
- end
170
- end
171
- end
172
- end
173
- end
174
- end
@@ -1,205 +1,3 @@
1
- module Asciidoctor
2
- module Standoc
3
- module Cleanup
4
- def external_terms_boilerplate(sources)
5
- @i18n.l10n(
6
- @i18n.external_terms_boilerplate.gsub(/%/, sources || "???"),
7
- @lang, @script
8
- )
9
- end
1
+ require "asciidoctor/standoc/deprecated"
2
+ require "metanorma/standoc/cleanup_boilerplate"
10
3
 
11
- def internal_external_terms_boilerplate(sources)
12
- @i18n.l10n(
13
- @i18n.internal_external_terms_boilerplate.gsub(/%/, sources || "??"),
14
- @lang, @script
15
- )
16
- end
17
-
18
- def term_defs_boilerplate(div, source, term, preface, isodoc)
19
- a = @i18n.term_def_boilerplate and div.next = a
20
- source.each do |s|
21
- @anchors[s["bibitemid"]] or
22
- @log.add("Crossreferences", nil,
23
- "term source #{s['bibitemid']} not referenced")
24
- end
25
- a = source.empty? && term.nil? ? @i18n.no_terms_boilerplate :
26
- term_defs_boilerplate_cont(source, term, isodoc)
27
- a and div.next = a
28
- end
29
-
30
- def term_defs_boilerplate_cont(src, term, isodoc)
31
- sources = isodoc.sentence_join(src.map do |s|
32
- %{<eref bibitemid="#{s['bibitemid']}"/>}
33
- end)
34
- if src.empty? then @i18n.internal_terms_boilerplate
35
- elsif term.nil? then external_terms_boilerplate(sources)
36
- else
37
- internal_external_terms_boilerplate(sources)
38
- end
39
- end
40
-
41
- def norm_ref_preface(ref)
42
- if ref.at("./note[@type = 'boilerplate']")
43
- unwrap_boilerplate_clauses(ref, ".")
44
- else
45
- refs = ref.elements.select do |e|
46
- %w(references bibitem).include? e.name
47
- end
48
- pref = refs.empty? ? @i18n.norm_empty_pref : @i18n.norm_with_refs_pref
49
- ref.at("./title").next = "<p>#{pref}</p>"
50
- end
51
- end
52
-
53
- TERM_CLAUSE = "//sections/terms | "\
54
- "//sections/clause[descendant::terms]".freeze
55
-
56
- NORM_REF = "//bibliography/references[@normative = 'true'] | "\
57
- "//bibliography/clause[.//references[@normative = 'true']]".freeze
58
-
59
- def boilerplate_isodoc(xmldoc)
60
- x = xmldoc.dup
61
- x.root.add_namespace(nil, self.class::XML_NAMESPACE)
62
- xml = Nokogiri::XML(x.to_xml)
63
- @isodoc ||= isodoc(@lang, @script)
64
- @isodoc.info(xml, nil)
65
- @isodoc
66
- end
67
-
68
- def termdef_boilerplate_cleanup(xmldoc)
69
- # termdef_remove_initial_paras(xmldoc)
70
- end
71
-
72
- def termdef_remove_initial_paras(xmldoc)
73
- xmldoc.xpath("//terms/p | //terms/ul").each(&:remove)
74
- end
75
-
76
- def unwrap_boilerplate_clauses(xmldoc, xpath)
77
- xmldoc.xpath(xpath).each do |f|
78
- f.xpath(".//clause[@type = 'boilerplate'] | "\
79
- ".//note[@type = 'boilerplate']").each do |c|
80
- c&.at("./title")&.remove
81
- c.replace(c.children)
82
- end
83
- end
84
- end
85
-
86
- def termdef_boilerplate_insert(xmldoc, isodoc, once = false)
87
- xmldoc.xpath(self.class::TERM_CLAUSE).each do |f|
88
- next if f.at("./clause[@type = 'boilerplate']")
89
-
90
- term_defs_boilerplate(f.at("./title"),
91
- xmldoc.xpath(".//termdocsource"),
92
- f.at(".//term"), f.at(".//p"), isodoc)
93
- break if once
94
- end
95
- end
96
-
97
- def boilerplate_cleanup(xmldoc)
98
- isodoc = boilerplate_isodoc(xmldoc)
99
- termdef_boilerplate_cleanup(xmldoc)
100
- termdef_boilerplate_insert(xmldoc, isodoc)
101
- unwrap_boilerplate_clauses(xmldoc, self.class::TERM_CLAUSE)
102
- f = xmldoc.at(self.class::NORM_REF) and norm_ref_preface(f)
103
- initial_boilerplate(xmldoc, isodoc)
104
- end
105
-
106
- def initial_boilerplate(xml, isodoc)
107
- return if xml.at("//boilerplate")
108
-
109
- preface = xml.at("//preface") || xml.at("//sections") ||
110
- xml.at("//annex") || xml.at("//references") || return
111
- b = boilerplate(xml, isodoc) or return
112
- preface.previous = b
113
- end
114
-
115
- def boilerplate_file(_xmldoc)
116
- File.join(@libdir, "boilerplate.xml")
117
- end
118
-
119
- def boilerplate(xml, conv)
120
- file = boilerplate_file(xml)
121
- file = File.join(@localdir, @boilerplateauthority) if @boilerplateauthority
122
- !file.nil? and File.exists?(file) or return
123
- conv.populate_template(File.read(file, encoding: "UTF-8"), nil)
124
- end
125
-
126
- def bibdata_cleanup(xmldoc)
127
- bibdata_anchor_cleanup(xmldoc)
128
- bibdata_docidentifier_cleanup(xmldoc)
129
- biblio_indirect_erefs(xmldoc, @internal_eref_namespaces&.uniq)
130
- end
131
-
132
- def bibdata_anchor_cleanup(xmldoc)
133
- xmldoc.xpath("//bibdata//bibitem | //bibdata//note").each do |b|
134
- b.delete("id")
135
- end
136
- end
137
-
138
- def bibdata_docidentifier_cleanup(xmldoc)
139
- ins = xmldoc.at("//bibdata/docidentifier")
140
- xmldoc.xpath("//bibdata/docidentifier").each_with_index do |b, i|
141
- next if i.zero?
142
-
143
- ins.next = b.remove
144
- ins = ins.next
145
- end
146
- end
147
-
148
- def gather_indirect_erefs(xmldoc, prefix)
149
- xmldoc.xpath("//eref[@type = '#{prefix}']")
150
- .each_with_object({}) do |e, m|
151
- e.delete("type")
152
- m[e["bibitemid"]] = true
153
- end.keys
154
- end
155
-
156
- def insert_indirect_biblio(xmldoc, refs, prefix)
157
- ins = xmldoc.at("bibliography") or
158
- xmldoc.root << "<bibliography/>" and ins = xmldoc.at("bibliography")
159
- ins = ins.add_child("<references hidden='true' normative='false'/>").first
160
- refs.each do |x|
161
- ins << <<~BIB
162
- <bibitem id="#{x}" type="internal">
163
- <docidentifier type="repository">#{x.sub(/^#{prefix}_/, "#{prefix}/")}</docidentifier>
164
- </bibitem>
165
- BIB
166
- end
167
- end
168
-
169
- def indirect_eref_to_xref(e, id)
170
- loc = e&.at("./localityStack[locality[@type = 'anchor']]")&.remove&.text ||
171
- e&.at("./locality[@type = 'anchor']")&.remove&.text || id
172
- e.name = "xref"
173
- e.delete("bibitemid")
174
- e.delete("citeas")
175
- e["target"] = loc
176
- unless e.document.at("//*[@id = '#{loc}']")
177
- e.children = %(** Missing target #{loc})
178
- e["target"] = id
179
- end
180
- end
181
-
182
- def resolve_local_indirect_erefs(xmldoc, refs, prefix)
183
- refs.each_with_object([]) do |r, m|
184
- id = r.sub(/^#{prefix}_/, "")
185
- if n = xmldoc.at("//*[@id = '#{id}']") and n.at("./ancestor-or-self::*[@type = '#{prefix}']")
186
- xmldoc.xpath("//eref[@bibitemid = '#{r}']").each do |e|
187
- indirect_eref_to_xref(e, id)
188
- end
189
- else
190
- m << r
191
- end
192
- end
193
- end
194
-
195
- def biblio_indirect_erefs(xmldoc, prefixes)
196
- prefixes&.each do |prefix|
197
- refs = gather_indirect_erefs(xmldoc, prefix)
198
- refs = resolve_local_indirect_erefs(xmldoc, refs, prefix)
199
- refs.empty? and next
200
- insert_indirect_biblio(xmldoc, refs, prefix)
201
- end
202
- end
203
- end
204
- end
205
- end