metanorma-standoc 1.11.4 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/rake.yml +3 -31
- data/.gitignore +23 -0
- data/lib/asciidoctor/standoc/base.rb +2 -145
- data/lib/asciidoctor/standoc/blocks.rb +2 -238
- data/lib/asciidoctor/standoc/blocks_notes.rb +2 -100
- data/lib/asciidoctor/standoc/cleanup.rb +2 -208
- data/lib/asciidoctor/standoc/cleanup_amend.rb +2 -53
- data/lib/asciidoctor/standoc/cleanup_block.rb +2 -172
- data/lib/asciidoctor/standoc/cleanup_boilerplate.rb +2 -212
- data/lib/asciidoctor/standoc/cleanup_footnotes.rb +2 -108
- data/lib/asciidoctor/standoc/cleanup_image.rb +2 -69
- data/lib/asciidoctor/standoc/cleanup_inline.rb +2 -189
- data/lib/asciidoctor/standoc/cleanup_maths.rb +2 -221
- data/lib/asciidoctor/standoc/cleanup_ref.rb +2 -169
- data/lib/asciidoctor/standoc/cleanup_ref_dl.rb +2 -103
- data/lib/asciidoctor/standoc/cleanup_reqt.rb +2 -110
- data/lib/asciidoctor/standoc/cleanup_section.rb +2 -184
- data/lib/asciidoctor/standoc/cleanup_section_names.rb +2 -91
- data/lib/asciidoctor/standoc/cleanup_symbols.rb +2 -47
- data/lib/asciidoctor/standoc/cleanup_table.rb +2 -67
- data/lib/asciidoctor/standoc/cleanup_terms.rb +2 -139
- data/lib/asciidoctor/standoc/cleanup_terms_designations.rb +2 -198
- data/lib/asciidoctor/standoc/cleanup_text.rb +2 -95
- data/lib/asciidoctor/standoc/cleanup_toc.rb +3 -0
- data/lib/asciidoctor/standoc/cleanup_xref.rb +2 -106
- data/lib/asciidoctor/standoc/converter.rb +2 -123
- data/lib/asciidoctor/standoc/datamodel/attributes_table_preprocessor.rb +2 -56
- data/lib/asciidoctor/standoc/datamodel/diagram_preprocessor.rb +2 -102
- data/lib/asciidoctor/standoc/datamodel/plantuml_renderer.rb +3 -404
- data/lib/asciidoctor/standoc/deprecated.rb +5 -0
- data/lib/asciidoctor/standoc/front.rb +2 -223
- data/lib/asciidoctor/standoc/front_contributor.rb +2 -191
- data/lib/asciidoctor/standoc/inline.rb +2 -231
- data/lib/asciidoctor/standoc/lists.rb +2 -119
- data/lib/asciidoctor/standoc/macros.rb +2 -203
- data/lib/asciidoctor/standoc/macros_form.rb +2 -62
- data/lib/asciidoctor/standoc/macros_note.rb +2 -44
- data/lib/asciidoctor/standoc/macros_plantuml.rb +2 -112
- data/lib/asciidoctor/standoc/macros_terms.rb +2 -180
- data/lib/asciidoctor/standoc/ref.rb +2 -251
- data/lib/asciidoctor/standoc/ref_sect.rb +2 -153
- data/lib/asciidoctor/standoc/ref_utility.rb +2 -0
- data/lib/asciidoctor/standoc/render.rb +2 -114
- data/lib/asciidoctor/standoc/reqt.rb +2 -89
- data/lib/asciidoctor/standoc/section.rb +2 -207
- data/lib/asciidoctor/standoc/table.rb +2 -84
- data/lib/asciidoctor/standoc/term_lookup_cleanup.rb +2 -178
- data/lib/asciidoctor/standoc/terms.rb +2 -159
- data/lib/asciidoctor/standoc/utils.rb +2 -100
- data/lib/asciidoctor/standoc/validate.rb +2 -157
- data/lib/asciidoctor/standoc/validate_section.rb +2 -54
- data/lib/metanorma/standoc/base.rb +149 -0
- data/lib/{asciidoctor → metanorma}/standoc/basicdoc.rng +0 -0
- data/lib/{asciidoctor → metanorma}/standoc/biblio.rng +0 -0
- data/lib/metanorma/standoc/blocks.rb +239 -0
- data/lib/metanorma/standoc/blocks_notes.rb +101 -0
- data/lib/metanorma/standoc/cleanup.rb +146 -0
- data/lib/metanorma/standoc/cleanup_amend.rb +54 -0
- data/lib/metanorma/standoc/cleanup_block.rb +173 -0
- data/lib/metanorma/standoc/cleanup_boilerplate.rb +213 -0
- data/lib/metanorma/standoc/cleanup_footnotes.rb +109 -0
- data/lib/metanorma/standoc/cleanup_image.rb +70 -0
- data/lib/metanorma/standoc/cleanup_inline.rb +190 -0
- data/lib/metanorma/standoc/cleanup_maths.rb +222 -0
- data/lib/metanorma/standoc/cleanup_ref.rb +170 -0
- data/lib/metanorma/standoc/cleanup_ref_dl.rb +104 -0
- data/lib/metanorma/standoc/cleanup_reqt.rb +111 -0
- data/lib/metanorma/standoc/cleanup_section.rb +212 -0
- data/lib/metanorma/standoc/cleanup_section_names.rb +92 -0
- data/lib/metanorma/standoc/cleanup_symbols.rb +48 -0
- data/lib/metanorma/standoc/cleanup_table.rb +68 -0
- data/lib/metanorma/standoc/cleanup_terms.rb +140 -0
- data/lib/metanorma/standoc/cleanup_terms_designations.rb +199 -0
- data/lib/metanorma/standoc/cleanup_text.rb +96 -0
- data/lib/metanorma/standoc/cleanup_toc.rb +98 -0
- data/lib/metanorma/standoc/cleanup_xref.rb +107 -0
- data/lib/metanorma/standoc/converter.rb +124 -0
- data/lib/metanorma/standoc/datamodel/attributes_table_preprocessor.rb +57 -0
- data/lib/metanorma/standoc/datamodel/diagram_preprocessor.rb +103 -0
- data/lib/metanorma/standoc/datamodel/plantuml_renderer.rb +409 -0
- data/lib/metanorma/standoc/front.rb +224 -0
- data/lib/metanorma/standoc/front_contributor.rb +192 -0
- data/lib/metanorma/standoc/inline.rb +232 -0
- data/lib/{asciidoctor → metanorma}/standoc/isodoc.rng +29 -0
- data/lib/metanorma/standoc/lists.rb +120 -0
- data/lib/metanorma/standoc/macros.rb +204 -0
- data/lib/metanorma/standoc/macros_form.rb +63 -0
- data/lib/metanorma/standoc/macros_note.rb +45 -0
- data/lib/metanorma/standoc/macros_plantuml.rb +113 -0
- data/lib/metanorma/standoc/macros_terms.rb +181 -0
- data/lib/metanorma/standoc/ref.rb +243 -0
- data/lib/metanorma/standoc/ref_sect.rb +153 -0
- data/lib/{asciidoctor/standoc/ref_date_id.rb → metanorma/standoc/ref_utility.rb} +43 -5
- data/lib/metanorma/standoc/render.rb +115 -0
- data/lib/metanorma/standoc/reqt.rb +90 -0
- data/lib/{asciidoctor → metanorma}/standoc/reqt.rng +0 -0
- data/lib/metanorma/standoc/section.rb +209 -0
- data/lib/metanorma/standoc/table.rb +85 -0
- data/lib/metanorma/standoc/term_lookup_cleanup.rb +179 -0
- data/lib/metanorma/standoc/terms.rb +160 -0
- data/lib/metanorma/standoc/utils.rb +101 -0
- data/lib/metanorma/standoc/validate.rb +158 -0
- data/lib/metanorma/standoc/validate_section.rb +55 -0
- data/lib/metanorma/standoc/version.rb +1 -1
- data/lib/{asciidoctor → metanorma}/standoc/views/datamodel/model_representation.adoc.erb +0 -0
- data/lib/{asciidoctor → metanorma}/standoc/views/datamodel/plantuml_representation.adoc.erb +0 -0
- data/lib/metanorma-standoc.rb +1 -1
- data/metanorma-standoc.gemspec +1 -1
- data/spec/{asciidoctor → metanorma}/base_spec.rb +27 -10
- data/spec/{asciidoctor → metanorma}/blank_spec.rb +1 -1
- data/spec/{asciidoctor → metanorma}/blocks_spec.rb +1 -1
- data/spec/{asciidoctor → metanorma}/cleanup_blocks_spec.rb +1 -1
- data/spec/{asciidoctor → metanorma}/cleanup_sections_spec.rb +1 -1
- data/spec/{asciidoctor → metanorma}/cleanup_spec.rb +5 -5
- data/spec/{asciidoctor → metanorma}/cleanup_terms_spec.rb +2 -2
- data/spec/{asciidoctor → metanorma}/datamodel/attributes_table_preprocessor_spec.rb +1 -1
- data/spec/{asciidoctor → metanorma}/datamodel/diagram_preprocessor_spec.rb +1 -1
- data/spec/{asciidoctor → metanorma}/inline_spec.rb +1 -1
- data/spec/{asciidoctor → metanorma}/isobib_cache_spec.rb +1 -1
- data/spec/{asciidoctor → metanorma}/lists_spec.rb +1 -1
- data/spec/{asciidoctor → metanorma}/macros_json2text_spec.rb +0 -0
- data/spec/{asciidoctor → metanorma}/macros_plantuml_spec.rb +3 -3
- data/spec/{asciidoctor → metanorma}/macros_spec.rb +6 -6
- data/spec/{asciidoctor → metanorma}/macros_yaml2text_spec.rb +0 -0
- data/spec/metanorma/refs_dl_spec.rb +863 -0
- data/spec/{asciidoctor → metanorma}/refs_spec.rb +399 -25
- data/spec/{asciidoctor → metanorma}/section_spec.rb +42 -17
- data/spec/{asciidoctor → metanorma}/table_spec.rb +1 -1
- data/spec/{asciidoctor → metanorma}/validate_spec.rb +2 -2
- data/spec/vcr_cassettes/dated_iso_ref_joint_iso_iec.yml +42 -42
- data/spec/vcr_cassettes/dated_iso_ref_joint_iso_iec1.yml +11 -11
- data/spec/vcr_cassettes/isobib_get_123.yml +12 -12
- data/spec/vcr_cassettes/isobib_get_123_1.yml +21 -21
- data/spec/vcr_cassettes/isobib_get_123_1_fr.yml +81 -81
- data/spec/vcr_cassettes/isobib_get_123_2001.yml +10 -10
- data/spec/vcr_cassettes/isobib_get_124.yml +11 -11
- data/spec/vcr_cassettes/rfcbib_get_rfc8341.yml +13 -13
- data/spec/vcr_cassettes/separates_iev_citations_by_top_level_clause.yml +46 -46
- metadata +82 -32
- data/spec/asciidoctor/refs_dl_spec.rb +0 -864
@@ -0,0 +1,111 @@
|
|
1
|
+
module Metanorma
|
2
|
+
module Standoc
|
3
|
+
module Cleanup
|
4
|
+
def requirement_cleanup(xmldoc)
|
5
|
+
requirement_metadata(xmldoc)
|
6
|
+
requirement_inherit(xmldoc)
|
7
|
+
requirement_descriptions(xmldoc)
|
8
|
+
end
|
9
|
+
|
10
|
+
REQRECPER = "//requirement | //recommendation | //permission".freeze
|
11
|
+
|
12
|
+
def requirement_inherit(xmldoc)
|
13
|
+
xmldoc.xpath(REQRECPER).each do |r|
|
14
|
+
ins = requirement_inherit_insert(r)
|
15
|
+
r.xpath("./*//inherit").each { |i| ins.previous = i }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def requirement_inherit_insert(reqt)
|
20
|
+
ins = reqt.at("./classification") || reqt.at(
|
21
|
+
"./description | ./measurementtarget | ./specification | "\
|
22
|
+
"./verification | ./import | ./description | ./component | "\
|
23
|
+
"./requirement | ./recommendation | ./permission",
|
24
|
+
) and return ins
|
25
|
+
requirement_inherit_insert1(reqt)
|
26
|
+
end
|
27
|
+
|
28
|
+
def requirement_inherit_insert1(reqt)
|
29
|
+
if t = reqt.at("./title")
|
30
|
+
t.next = " "
|
31
|
+
t.next
|
32
|
+
else
|
33
|
+
if reqt.children.empty? then reqt.add_child(" ")
|
34
|
+
else reqt.children.first.previous = " "
|
35
|
+
end
|
36
|
+
reqt.children.first
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def requirement_descriptions(xmldoc)
|
41
|
+
xmldoc.xpath(REQRECPER).each do |r|
|
42
|
+
r.xpath(".//p[not(./*)][normalize-space(.)='']").each(&:remove)
|
43
|
+
r.children.each do |e|
|
44
|
+
requirement_description_wrap(r, e)
|
45
|
+
end
|
46
|
+
requirement_description_cleanup1(r)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def requirement_description_wrap(reqt, text)
|
51
|
+
return if (text.element? && (reqt_subpart(text.name) ||
|
52
|
+
%w(requirement recommendation
|
53
|
+
permission).include?(text.name))) ||
|
54
|
+
(text.text.strip.empty? && !text.at(".//xref | .//eref | .//link"))
|
55
|
+
|
56
|
+
t = Nokogiri::XML::Element.new("description", reqt)
|
57
|
+
text.before(t)
|
58
|
+
t.children = text.remove
|
59
|
+
end
|
60
|
+
|
61
|
+
def requirement_description_cleanup1(reqt)
|
62
|
+
while d = reqt.at("./description[following-sibling::*[1]"\
|
63
|
+
"[self::description]]")
|
64
|
+
n = d.next.remove
|
65
|
+
d << n.children
|
66
|
+
end
|
67
|
+
reqt.xpath("./description[normalize-space(.)='']").each do |r|
|
68
|
+
r.replace("\n")
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def requirement_metadata(xmldoc)
|
73
|
+
xmldoc.xpath(REQRECPER).each do |r|
|
74
|
+
dl = r&.at("./dl[@metadata = 'true']")&.remove or next
|
75
|
+
requirement_metadata1(r, dl, r.at("./title"))
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def requirement_metadata1_tags
|
80
|
+
%w(label subject inherit)
|
81
|
+
end
|
82
|
+
|
83
|
+
def requirement_metadata1(reqt, dlist, ins)
|
84
|
+
unless ins
|
85
|
+
reqt.children.first.previous = " "
|
86
|
+
ins = reqt.children.first
|
87
|
+
end
|
88
|
+
%w(obligation model type).each do |a|
|
89
|
+
dl_to_attrs(reqt, dlist, a)
|
90
|
+
end
|
91
|
+
requirement_metadata1_tags.each do |a|
|
92
|
+
ins = dl_to_elems(ins, reqt, dlist, a)
|
93
|
+
end
|
94
|
+
reqt_dl_to_classif(ins, reqt, dlist)
|
95
|
+
end
|
96
|
+
|
97
|
+
def reqt_dl_to_classif(ins, reqt, dlist)
|
98
|
+
if a = reqt.at("./classification[last()]") then ins = a end
|
99
|
+
dlist.xpath("./dt[text()='classification']").each do |e|
|
100
|
+
val = e.at("./following::dd/p") || e.at("./following::dd")
|
101
|
+
req_classif_parse(val.text).each do |r|
|
102
|
+
ins.next = "<classification><tag>#{r[0]}</tag>"\
|
103
|
+
"<value>#{r[1]}</value></classification>"
|
104
|
+
ins = ins.next
|
105
|
+
end
|
106
|
+
end
|
107
|
+
ins
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
@@ -0,0 +1,212 @@
|
|
1
|
+
require "date"
|
2
|
+
require "htmlentities"
|
3
|
+
require "json"
|
4
|
+
require "mathml2asciimath"
|
5
|
+
require_relative "cleanup_section_names"
|
6
|
+
|
7
|
+
module Metanorma
|
8
|
+
module Standoc
|
9
|
+
module Cleanup
|
10
|
+
def make_preface(xml, sect)
|
11
|
+
if xml.at("//foreword | //introduction | //acknowledgements | "\
|
12
|
+
"//*[@preface]")
|
13
|
+
preface = sect.add_previous_sibling("<preface/>").first
|
14
|
+
f = xml.at("//foreword") and preface.add_child f.remove
|
15
|
+
f = xml.at("//introduction") and preface.add_child f.remove
|
16
|
+
move_clauses_into_preface(xml, preface)
|
17
|
+
f = xml.at("//acknowledgements") and preface.add_child f.remove
|
18
|
+
end
|
19
|
+
make_abstract(xml, sect)
|
20
|
+
end
|
21
|
+
|
22
|
+
def move_clauses_into_preface(xml, preface)
|
23
|
+
xml.xpath("//*[@preface]").each do |c|
|
24
|
+
c.delete("preface")
|
25
|
+
preface.add_child c.remove
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def make_abstract(xml, sect)
|
30
|
+
if xml.at("//abstract[not(ancestor::bibitem)]")
|
31
|
+
preface = sect.at("//preface") ||
|
32
|
+
sect.add_previous_sibling("<preface/>").first
|
33
|
+
abstract = xml.at("//abstract[not(ancestor::bibitem)]").remove
|
34
|
+
preface.prepend_child abstract.remove
|
35
|
+
bibabstract = bibabstract_location(xml)
|
36
|
+
dupabstract = abstract.dup
|
37
|
+
dupabstract.traverse { |n| n.remove_attribute("id") }
|
38
|
+
dupabstract.remove_attribute("language")
|
39
|
+
dupabstract.remove_attribute("script")
|
40
|
+
dupabstract&.at("./title")&.remove
|
41
|
+
bibabstract.next = dupabstract
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def bibabstract_location(xml)
|
46
|
+
xml.at("//bibdata/script") || xml.at("//bibdata/language") ||
|
47
|
+
xml.at("//bibdata/contributor[not(following-sibling::contributor)]") ||
|
48
|
+
xml.at("//bibdata/date[not(following-sibling::date)]") ||
|
49
|
+
xml.at("//docnumber") ||
|
50
|
+
xml.at("//bibdata/docidentifier"\
|
51
|
+
"[not(following-sibling::docidentifier)]") ||
|
52
|
+
xml.at("//bibdata/uri[not(following-sibling::uri)]") ||
|
53
|
+
xml.at("//bibdata/title[not(following-sibling::title)]")
|
54
|
+
end
|
55
|
+
|
56
|
+
def make_bibliography(xml, sect)
|
57
|
+
if xml.at("//sections/references")
|
58
|
+
biblio = sect.add_next_sibling("<bibliography/>").first
|
59
|
+
xml.xpath("//sections/references").each do |r|
|
60
|
+
biblio.add_child r.remove
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def make_indexsect(xml, sect)
|
66
|
+
xml.xpath("//sections/indexsect").reverse_each do |r|
|
67
|
+
sect.next = r.remove
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def sections_order_cleanup(xml)
|
72
|
+
s = xml.at("//sections")
|
73
|
+
make_preface(xml, s)
|
74
|
+
make_annexes(xml)
|
75
|
+
make_indexsect(xml, s)
|
76
|
+
make_bibliography(xml, s)
|
77
|
+
xml.xpath("//sections/annex").reverse_each { |r| s.next = r.remove }
|
78
|
+
end
|
79
|
+
|
80
|
+
def make_annexes(xml)
|
81
|
+
xml.xpath("//*[@annex]").each do |y|
|
82
|
+
y.delete("annex")
|
83
|
+
next if y.name == "annex" || !y.ancestors("annex").empty?
|
84
|
+
|
85
|
+
y.wrap("<annex/>")
|
86
|
+
y.parent["id"] = "_#{UUIDTools::UUID.random_create}"
|
87
|
+
y.parent["obligation"] = y["obligation"]
|
88
|
+
y.parent["language"] = y["language"]
|
89
|
+
y.parent["script"] = y["script"]
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def maxlevel(xml)
|
94
|
+
max = 5
|
95
|
+
xml.xpath("//clause[@level]").each do |c|
|
96
|
+
max = c["level"].to_i if max < c["level"].to_i
|
97
|
+
end
|
98
|
+
max
|
99
|
+
end
|
100
|
+
|
101
|
+
def sections_level_cleanup(xml)
|
102
|
+
m = maxlevel(xml)
|
103
|
+
return if m < 6
|
104
|
+
|
105
|
+
m.downto(6).each do |l|
|
106
|
+
xml.xpath("//clause[@level = '#{l}']").each do |c|
|
107
|
+
c.delete("level")
|
108
|
+
c.previous_element << c.remove
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def sections_cleanup(xml)
|
114
|
+
sections_order_cleanup(xml)
|
115
|
+
sections_level_cleanup(xml)
|
116
|
+
sections_names_cleanup(xml)
|
117
|
+
sections_variant_title_cleanup(xml)
|
118
|
+
change_clauses(xml)
|
119
|
+
end
|
120
|
+
|
121
|
+
def obligations_cleanup(xml)
|
122
|
+
obligations_cleanup_info(xml)
|
123
|
+
obligations_cleanup_norm(xml)
|
124
|
+
obligations_cleanup_inherit(xml)
|
125
|
+
end
|
126
|
+
|
127
|
+
def obligations_cleanup_info(xml)
|
128
|
+
s = xml.at("//foreword") and s["obligation"] = "informative"
|
129
|
+
s = xml.at("//introduction") and s["obligation"] = "informative"
|
130
|
+
s = xml.at("//acknowledgements") and s["obligation"] = "informative"
|
131
|
+
xml.xpath("//references").each { |r| r["obligation"] = "informative" }
|
132
|
+
xml.xpath("//preface//clause").each do |r|
|
133
|
+
r["obligation"] = "informative"
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
def obligations_cleanup_norm(xml)
|
138
|
+
s = xml.at("//clause[@type = 'scope']") and
|
139
|
+
s["obligation"] = "normative"
|
140
|
+
xml.xpath("//terms").each { |r| r["obligation"] = "normative" }
|
141
|
+
xml.xpath("//definitions").each { |r| r["obligation"] = "normative" }
|
142
|
+
end
|
143
|
+
|
144
|
+
def obligations_cleanup_inherit(xml)
|
145
|
+
xml.xpath("//annex | //clause[not(ancestor::boilerplate)]").each do |r|
|
146
|
+
r["obligation"] = "normative" unless r["obligation"]
|
147
|
+
end
|
148
|
+
xml.xpath(Utils::SUBCLAUSE_XPATH).each do |r|
|
149
|
+
o = r&.at("./ancestor::*/@obligation")&.text and r["obligation"] = o
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def clausebefore_cleanup(xmldoc)
|
154
|
+
preface_clausebefore_cleanup(xmldoc)
|
155
|
+
sections_clausebefore_cleanup(xmldoc)
|
156
|
+
end
|
157
|
+
|
158
|
+
def preface_clausebefore_cleanup(xmldoc)
|
159
|
+
return unless xmldoc.at("//preface")
|
160
|
+
|
161
|
+
unless ins = xmldoc.at("//preface").children.first
|
162
|
+
xmldoc.at("//preface") << " "
|
163
|
+
ins = xmldoc.at("//preface").children.first
|
164
|
+
end
|
165
|
+
xmldoc.xpath("//preface//*[@beforeclauses = 'true']").each do |x|
|
166
|
+
x.delete("beforeclauses")
|
167
|
+
ins.previous = x.remove
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
def sections_clausebefore_cleanup(xmldoc)
|
172
|
+
return unless xmldoc.at("//sections")
|
173
|
+
|
174
|
+
unless ins = xmldoc.at("//sections").children.first
|
175
|
+
xmldoc.at("//sections") << " "
|
176
|
+
ins = xmldoc.at("//sections").children.first
|
177
|
+
end
|
178
|
+
xmldoc.xpath("//sections//*[@beforeclauses = 'true']").each do |x|
|
179
|
+
x.delete("beforeclauses")
|
180
|
+
ins.previous = x.remove
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
def floatingtitle_cleanup(xmldoc)
|
185
|
+
pop_floating_title(xmldoc)
|
186
|
+
floating_title_preface2sections(xmldoc)
|
187
|
+
end
|
188
|
+
|
189
|
+
def pop_floating_title(xmldoc)
|
190
|
+
loop do
|
191
|
+
found = false
|
192
|
+
xmldoc.xpath("//floating-title").each do |t|
|
193
|
+
next unless t.next_element.nil?
|
194
|
+
next if %w(sections annex preface).include? t.parent.name
|
195
|
+
|
196
|
+
t.parent.parent << t
|
197
|
+
found = true
|
198
|
+
end
|
199
|
+
break unless found
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
def floating_title_preface2sections(xmldoc)
|
204
|
+
t = xmldoc.at("//preface/floating-title") or return
|
205
|
+
s = xmldoc.at("//sections")
|
206
|
+
unless t.next_element
|
207
|
+
s.children.first.previous = t.remove
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
module Metanorma
|
2
|
+
module Standoc
|
3
|
+
module Cleanup
|
4
|
+
def get_or_make_title(node)
|
5
|
+
unless node.at("./title")
|
6
|
+
if node.children.empty?
|
7
|
+
node << "<title/>"
|
8
|
+
else
|
9
|
+
node.children.first.previous = "<title/>"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
node.at("./title")
|
13
|
+
end
|
14
|
+
|
15
|
+
def replace_title(doc, xpath, text, first = false)
|
16
|
+
return unless text
|
17
|
+
|
18
|
+
doc.xpath(xpath).each_with_index do |node, i|
|
19
|
+
next if first && !i.zero?
|
20
|
+
|
21
|
+
title = get_or_make_title(node)
|
22
|
+
fn = title.xpath("./fn")
|
23
|
+
fn.each(&:remove)
|
24
|
+
title.children = text
|
25
|
+
fn.each { |n| title << n }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def sections_names_cleanup(xml)
|
30
|
+
replace_title(xml, "//clause[@type = 'scope']", @i18n&.scope)
|
31
|
+
replace_title(xml, "//preface//abstract", @i18n&.abstract)
|
32
|
+
replace_title(xml, "//foreword", @i18n&.foreword)
|
33
|
+
replace_title(xml, "//introduction", @i18n&.introduction)
|
34
|
+
replace_title(xml, "//acknowledgements", @i18n&.acknowledgements)
|
35
|
+
section_names_refs_cleanup(xml)
|
36
|
+
section_names_terms_cleanup(xml)
|
37
|
+
end
|
38
|
+
|
39
|
+
def section_names_refs_cleanup(xml)
|
40
|
+
replace_title(xml, "//bibliography/references[@normative = 'true']",
|
41
|
+
@i18n&.normref, true)
|
42
|
+
replace_title(xml, "//bibliography/references[@normative = 'false']",
|
43
|
+
@i18n&.bibliography, true)
|
44
|
+
end
|
45
|
+
|
46
|
+
NO_SYMABBR = "[.//definitions[not(@type)]]".freeze
|
47
|
+
SYMABBR = "[.//definitions[@type = 'symbols']]"\
|
48
|
+
"[.//definitions[@type = 'abbreviated_terms']]".freeze
|
49
|
+
SYMnoABBR = "[.//definitions[@type = 'symbols']]"\
|
50
|
+
"[not(.//definitions[@type = 'abbreviated_terms'])]".freeze
|
51
|
+
ABBRnoSYM = "[.//definitions[@type = 'abbreviated_terms']]"\
|
52
|
+
"[not(.//definitions[@type = 'symbols'])]".freeze
|
53
|
+
|
54
|
+
def section_names_terms_cleanup(xml)
|
55
|
+
replace_title(xml, "//definitions[@type = 'symbols']", @i18n&.symbols)
|
56
|
+
replace_title(xml, "//definitions[@type = 'abbreviated_terms']",
|
57
|
+
@i18n&.abbrev)
|
58
|
+
replace_title(xml, "//definitions[not(@type)]", @i18n&.symbolsabbrev)
|
59
|
+
replace_title(xml, "//terms#{SYMnoABBR} | //clause[.//terms]#{SYMnoABBR}",
|
60
|
+
@i18n&.termsdefsymbols, true)
|
61
|
+
replace_title(xml, "//terms#{ABBRnoSYM} | //clause[.//terms]#{ABBRnoSYM}",
|
62
|
+
@i18n&.termsdefabbrev, true)
|
63
|
+
replace_title(xml, "//terms#{SYMABBR} | //clause[.//terms]#{SYMABBR}",
|
64
|
+
@i18n&.termsdefsymbolsabbrev, true)
|
65
|
+
replace_title(xml, "//terms#{NO_SYMABBR} | //clause[.//terms]#{NO_SYMABBR}",
|
66
|
+
@i18n&.termsdefsymbolsabbrev, true)
|
67
|
+
replace_title(
|
68
|
+
xml,
|
69
|
+
"//terms[not(.//definitions)] | //clause[.//terms][not(.//definitions)]",
|
70
|
+
@i18n&.termsdef, true
|
71
|
+
)
|
72
|
+
end
|
73
|
+
|
74
|
+
SECTION_CONTAINERS = %w(foreword introduction acknowledgements abstract
|
75
|
+
clause clause references terms definitions annex
|
76
|
+
appendix).freeze
|
77
|
+
|
78
|
+
def sections_variant_title_cleanup(xml)
|
79
|
+
path = SECTION_CONTAINERS.map { |x| "./ancestor::#{x}" }.join(" | ")
|
80
|
+
xml.xpath("//p[@variant_title]").each do |p|
|
81
|
+
p.name = "variant-title"
|
82
|
+
p.delete("id")
|
83
|
+
p.delete("variant_title")
|
84
|
+
p.xpath("(#{path})[last()]").each do |sect|
|
85
|
+
(ins = sect.at("./title") and ins.next = p) or
|
86
|
+
sect.children.first.previous = p
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Metanorma
|
2
|
+
module Standoc
|
3
|
+
module Cleanup
|
4
|
+
# Indices sort after letter but before any following
|
5
|
+
# letter (x, x_m, x_1, xa); we use colon to force that sort order.
|
6
|
+
# Numbers sort *after* letters; we use thorn to force that sort order.
|
7
|
+
def symbol_key(sym)
|
8
|
+
key = sym.dup
|
9
|
+
key.traverse do |n|
|
10
|
+
n.name == "math" and
|
11
|
+
n.replace(grkletters(MathML2AsciiMath.m2a(n.to_xml)))
|
12
|
+
end
|
13
|
+
ret = Nokogiri::XML(key.to_xml)
|
14
|
+
HTMLEntities.new.decode(ret.text.downcase)
|
15
|
+
.gsub(/[\[\]{}<>()]/, "").gsub(/\s/m, "")
|
16
|
+
.gsub(/[[:punct:]]|[_^]/, ":\\0").gsub(/`/, "")
|
17
|
+
.gsub(/[0-9]+/, "þ\\0")
|
18
|
+
end
|
19
|
+
|
20
|
+
def grkletters(text)
|
21
|
+
text.gsub(/\b(alpha|beta|gamma|delta|epsilon|zeta|eta|theta|iota|kappa|
|
22
|
+
lambda|mu|nu|xi|omicron|pi|rho|sigma|tau|upsilon|phi|chi|
|
23
|
+
psi|omega)\b/xi, "&\\1;")
|
24
|
+
end
|
25
|
+
|
26
|
+
def extract_symbols_list(dlist)
|
27
|
+
dl_out = []
|
28
|
+
dlist.xpath("./dt | ./dd").each do |dtd|
|
29
|
+
if dtd.name == "dt"
|
30
|
+
dl_out << { dt: dtd.remove, key: symbol_key(dtd) }
|
31
|
+
else
|
32
|
+
dl_out.last[:dd] = dtd.remove
|
33
|
+
end
|
34
|
+
end
|
35
|
+
dl_out
|
36
|
+
end
|
37
|
+
|
38
|
+
def symbols_cleanup(docxml)
|
39
|
+
docxml.xpath("//definitions/dl").each do |dl|
|
40
|
+
dl_out = extract_symbols_list(dl)
|
41
|
+
dl_out.sort! { |a, b| a[:key] <=> b[:key] || a[:dt] <=> b[:dt] }
|
42
|
+
dl.children = dl_out.map { |d| d[:dt].to_s + d[:dd].to_s }.join("\n")
|
43
|
+
end
|
44
|
+
docxml
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module Metanorma
|
2
|
+
module Standoc
|
3
|
+
module Cleanup
|
4
|
+
def dl1_table_cleanup(xmldoc)
|
5
|
+
q = "//table/following-sibling::*[1][self::dl]"
|
6
|
+
xmldoc.xpath(q).each do |s|
|
7
|
+
s["key"] == "true" and s.previous_element << s.remove
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
# move Key dl after table footer
|
12
|
+
def dl2_table_cleanup(xmldoc)
|
13
|
+
q = "//table/following-sibling::*[1][self::p]"
|
14
|
+
xmldoc.xpath(q).each do |s|
|
15
|
+
if s.text =~ /^\s*key[^a-z]*$/i && s&.next_element&.name == "dl"
|
16
|
+
s.next_element["key"] = "true"
|
17
|
+
s.previous_element << s.next_element.remove
|
18
|
+
s.remove
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def insert_thead(table)
|
24
|
+
thead = table.at("./thead")
|
25
|
+
return thead unless thead.nil?
|
26
|
+
|
27
|
+
if tname = table.at("./name")
|
28
|
+
thead = tname.add_next_sibling("<thead/>").first
|
29
|
+
return thead
|
30
|
+
end
|
31
|
+
table.children.first.add_previous_sibling("<thead/>").first
|
32
|
+
end
|
33
|
+
|
34
|
+
def header_rows_cleanup(xmldoc)
|
35
|
+
xmldoc.xpath("//table[@headerrows]").each do |s|
|
36
|
+
thead = insert_thead(s)
|
37
|
+
(thead.xpath("./tr").size...s["headerrows"].to_i).each do
|
38
|
+
row = s.at("./tbody/tr")
|
39
|
+
row.parent = thead
|
40
|
+
end
|
41
|
+
thead.xpath(".//td").each { |n| n.name = "th" }
|
42
|
+
s.delete("headerrows")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def table_cleanup(xmldoc)
|
47
|
+
dl1_table_cleanup(xmldoc)
|
48
|
+
dl2_table_cleanup(xmldoc)
|
49
|
+
notes_table_cleanup(xmldoc)
|
50
|
+
header_rows_cleanup(xmldoc)
|
51
|
+
end
|
52
|
+
|
53
|
+
# move notes into table
|
54
|
+
def notes_table_cleanup(xmldoc)
|
55
|
+
nomatches = false
|
56
|
+
until nomatches
|
57
|
+
nomatches = true
|
58
|
+
xmldoc.xpath("//table/following-sibling::*[1]"\
|
59
|
+
"[self::note[not(@keep-separate = 'true')]]").each do |n|
|
60
|
+
n.delete("keep-separate")
|
61
|
+
n.previous_element << n.remove
|
62
|
+
nomatches = false
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,140 @@
|
|
1
|
+
require_relative "term_lookup_cleanup"
|
2
|
+
require_relative "cleanup_terms_designations"
|
3
|
+
|
4
|
+
module Metanorma
|
5
|
+
module Standoc
|
6
|
+
module Cleanup
|
7
|
+
def termdomain_cleanup(xmldoc)
|
8
|
+
xmldoc.xpath("//p/domain").each do |a|
|
9
|
+
parent = a.parent
|
10
|
+
prev = parent.previous
|
11
|
+
prev.next = a.remove
|
12
|
+
parent.text.strip.empty? and parent.remove
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def termdomain1_cleanup(xmldoc)
|
17
|
+
xmldoc.xpath("//term").each do |t|
|
18
|
+
d = t.xpath("./domain | ./subject").last or next
|
19
|
+
defn = d.at("../definition") and defn.previous = d.remove
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def termdefinition_cleanup(xmldoc)
|
24
|
+
generate_termdefinitions(xmldoc)
|
25
|
+
split_termdefinitions(xmldoc)
|
26
|
+
alternate_termdefinitions(xmldoc)
|
27
|
+
end
|
28
|
+
|
29
|
+
TERMDEF_BLOCKS =
|
30
|
+
"./p | ./ol | ./dl[not(@metadata = 'true')] | ./ul | ./figure | "\
|
31
|
+
"./formula | ./table".freeze
|
32
|
+
|
33
|
+
def generate_termdefinitions(xmldoc)
|
34
|
+
xmldoc.xpath("//term[not(definition)]").each do |d|
|
35
|
+
first_child = d.at(TERMDEF_BLOCKS) || next
|
36
|
+
t = Nokogiri::XML::Element.new("definition", xmldoc)
|
37
|
+
first_child.replace(t)
|
38
|
+
t << first_child.remove
|
39
|
+
d.xpath(TERMDEF_BLOCKS).each do |n|
|
40
|
+
t << n.remove
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def split_termdefinitions(xmldoc)
|
46
|
+
xmldoc.xpath("//definition").each do |d|
|
47
|
+
if d.at("./p | ./ol | ./dl | ./ul")
|
48
|
+
d.children = "<verbal-definition>#{d.children}</verbal-definition>"
|
49
|
+
else
|
50
|
+
d.children = "<non-verbal-representation>"\
|
51
|
+
"#{d.children}</non-verbal-representation>"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def alternate_termdefinitions(xmldoc)
|
57
|
+
xmldoc.xpath("//term").each do |t|
|
58
|
+
t.xpath("./definition").each do |d|
|
59
|
+
d1 = d.next_element or next
|
60
|
+
if (v = d.at("./verbal-definition")) &&
|
61
|
+
!d.at("./non-verbal-representation") &&
|
62
|
+
!d1.at("./verbal-definition") &&
|
63
|
+
nv = d1.at("./non-verbal-representation")
|
64
|
+
v.next = nv.remove
|
65
|
+
d1.remove
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def termdocsource_cleanup(xmldoc)
|
72
|
+
f = xmldoc.at("//preface | //sections")
|
73
|
+
xmldoc.xpath("//termdocsource").each { |s| f.previous = s.remove }
|
74
|
+
end
|
75
|
+
|
76
|
+
def term_children_cleanup(xmldoc)
|
77
|
+
xmldoc.xpath("//terms[terms]").each { |t| t.name = "clause" }
|
78
|
+
xmldoc.xpath("//term").each do |t|
|
79
|
+
%w(termnote termexample termsource term).each do |w|
|
80
|
+
t.xpath("./#{w}").each { |n| t << n.remove }
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def termdef_from_termbase(xmldoc)
|
86
|
+
xmldoc.xpath("//term").each do |x|
|
87
|
+
if (c = x.at("./origin/termref")) && !x.at("./definition")
|
88
|
+
x.at("./origin").previous = fetch_termbase(c["base"], c.text)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def termnote_example_cleanup(xmldoc)
|
94
|
+
%w(note example).each do |w|
|
95
|
+
xmldoc.xpath("//term#{w}[not(ancestor::term)]").each do |x|
|
96
|
+
x.name = w
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def termdef_cleanup(xmldoc)
|
102
|
+
termdef_unnest_cleanup(xmldoc)
|
103
|
+
Metanorma::Standoc::TermLookupCleanup.new(xmldoc, @log).call
|
104
|
+
term_nonverbal_designations(xmldoc)
|
105
|
+
term_dl_to_metadata(xmldoc)
|
106
|
+
term_termsource_to_designation(xmldoc)
|
107
|
+
term_designation_reorder(xmldoc)
|
108
|
+
termdef_from_termbase(xmldoc)
|
109
|
+
termdomain_cleanup(xmldoc)
|
110
|
+
termdef_stem_cleanup(xmldoc)
|
111
|
+
termdefinition_cleanup(xmldoc)
|
112
|
+
termdomain1_cleanup(xmldoc)
|
113
|
+
termnote_example_cleanup(xmldoc)
|
114
|
+
term_children_cleanup(xmldoc)
|
115
|
+
termdocsource_cleanup(xmldoc)
|
116
|
+
end
|
117
|
+
|
118
|
+
def index_cleanup(xmldoc)
|
119
|
+
return unless @index_terms
|
120
|
+
|
121
|
+
xmldoc.xpath("//preferred").each do |p|
|
122
|
+
index_cleanup1(p.at("./expression/name | ./letter-symbol/name"),
|
123
|
+
p.xpath("./field-of-application | ./usage-info")
|
124
|
+
&.map(&:text)&.join(", "))
|
125
|
+
end
|
126
|
+
xmldoc.xpath("//definitions/dl/dt").each do |p|
|
127
|
+
index_cleanup1(p, "")
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def index_cleanup1(term, fieldofappl)
|
132
|
+
return unless term
|
133
|
+
|
134
|
+
idx = term.children.dup
|
135
|
+
fieldofappl.empty? or idx << ", <#{fieldofappl}>"
|
136
|
+
term << "<index><primary>#{idx.to_xml}</primary></index>"
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|