metanorma-utils 1.2.8 → 1.3.1
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 +4 -4
- data/.github/workflows/rake.yml +3 -20
- data/lib/metanorma-utils.rb +1 -0
- data/lib/utils/main.rb +51 -52
- data/lib/utils/version.rb +1 -1
- data/lib/utils/xml.rb +66 -0
- data/metanorma-utils.gemspec +4 -4
- data/spec/img_spec.rb +297 -0
- data/spec/utils_spec.rb +83 -309
- data/spec/xml_spec.rb +75 -0
- metadata +16 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fd89605d15a5973a46c4fbc9b88c024191e6a809e4255f34db32d57bab198cc3
|
4
|
+
data.tar.gz: 167f7c09e8b6347c0f5990656775cba5ec5cdc05a2b715690451e3c02f80fb65
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1baca64c93f1598bd4f6285ac62c912a519a36c9a9ee08dfe883fe5af0cd721983cc3be513d2dd3d8329ff9efbcc940f0f207ed40791e93556a477ac230c6bb0
|
7
|
+
data.tar.gz: f4a13c5846b93d6fa7262736a5e177e809ddf72340713c890a7bcddad8d2ba2526e71ee5eff0dd3900e10a6c45b0f00cf7944a30ab1343feef34d3679428e795
|
data/.github/workflows/rake.yml
CHANGED
@@ -10,23 +10,6 @@ on:
|
|
10
10
|
|
11
11
|
jobs:
|
12
12
|
rake:
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
strategy:
|
17
|
-
fail-fast: false
|
18
|
-
matrix:
|
19
|
-
ruby: [ '3.0', '2.7', '2.6', '2.5' ]
|
20
|
-
os: [ ubuntu-latest, windows-latest, macos-latest ]
|
21
|
-
experimental: [ false ]
|
22
|
-
steps:
|
23
|
-
- uses: actions/checkout@v2
|
24
|
-
with:
|
25
|
-
submodules: true
|
26
|
-
|
27
|
-
- uses: ruby/setup-ruby@v1
|
28
|
-
with:
|
29
|
-
ruby-version: ${{ matrix.ruby }}
|
30
|
-
bundler-cache: true
|
31
|
-
|
32
|
-
- run: bundle exec rake
|
13
|
+
uses: metanorma/metanorma-build-scripts/.github/workflows/generic-rake.yml@main
|
14
|
+
secrets:
|
15
|
+
pat_token: ${{ secrets.METANORMA_CI_PAT_TOKEN }}
|
data/lib/metanorma-utils.rb
CHANGED
data/lib/utils/main.rb
CHANGED
@@ -1,34 +1,35 @@
|
|
1
1
|
require "asciidoctor"
|
2
2
|
require "tempfile"
|
3
3
|
require "sterile"
|
4
|
-
require "uuidtools"
|
5
4
|
require "htmlentities"
|
5
|
+
require "nokogiri"
|
6
6
|
|
7
7
|
module Metanorma
|
8
8
|
module Utils
|
9
|
-
NAMECHAR = "\u0000-\u002c\u002f\u003a-\u0040\\u005b-\u005e"\
|
10
|
-
"\u0060\u007b-\u00b6\u00b8-\u00bf\u00d7\u00f7\u037e"\
|
11
|
-
"\u2000-\u200b"\
|
12
|
-
"\u200e-\u203e\u2041-\u206f\u2190-\u2bff\u2ff0-\u3000".freeze
|
13
|
-
NAMESTARTCHAR = "\\u002d\u002e\u0030-\u0039\u00b7\u0300-\u036f"\
|
14
|
-
"\u203f-\u2040".freeze
|
15
|
-
|
16
9
|
class << self
|
17
|
-
def
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
"_"
|
22
|
-
else
|
23
|
-
(%r([#{NAMESTARTCHAR}#])o.match?(start) ? "_#{start}" : start)
|
24
|
-
end
|
25
|
-
ret2 = tag[1..-1] || ""
|
26
|
-
(ret1 || "") + ret2.gsub(%r([#{NAMECHAR}#])o, "_")
|
10
|
+
def attr_code(attributes)
|
11
|
+
attributes.compact.transform_values do |v|
|
12
|
+
v.is_a?(String) ? HTMLEntities.new.decode(v) : v
|
13
|
+
end
|
27
14
|
end
|
28
15
|
|
29
|
-
|
30
|
-
|
31
|
-
|
16
|
+
# , " => ," : CSV definition does not deal with space followed by quote
|
17
|
+
# at start of field
|
18
|
+
def csv_split(text, delim = ";")
|
19
|
+
return if text.nil?
|
20
|
+
|
21
|
+
CSV.parse_line(text&.gsub(/#{delim} "(?!")/, "#{delim}\""),
|
22
|
+
liberal_parsing: true,
|
23
|
+
col_sep: delim)&.compact&.map(&:strip)
|
24
|
+
end
|
25
|
+
|
26
|
+
# if the contents of node are blocks, output them to out;
|
27
|
+
# else, wrap them in <p>
|
28
|
+
def wrap_in_para(node, out)
|
29
|
+
if node.blocks? then out << node.content
|
30
|
+
else
|
31
|
+
out.p { |p| p << node.content }
|
32
|
+
end
|
32
33
|
end
|
33
34
|
|
34
35
|
def asciidoc_sub(text, flavour = :standoc)
|
@@ -50,9 +51,13 @@ module Metanorma
|
|
50
51
|
|
51
52
|
# TODO needs internationalisation
|
52
53
|
def smartformat(text)
|
53
|
-
|
54
|
-
.
|
55
|
-
|
54
|
+
HTMLEntities.new.encode(
|
55
|
+
HTMLEntities.new.decode(
|
56
|
+
text.gsub(/ --? /, " — ")
|
57
|
+
.gsub(/--/, "—"),
|
58
|
+
)
|
59
|
+
.smart_format, :basic
|
60
|
+
)
|
56
61
|
end
|
57
62
|
|
58
63
|
def endash_date(elem)
|
@@ -136,35 +141,29 @@ module Metanorma
|
|
136
141
|
%w(Arab Aran Hebr).include? script
|
137
142
|
end
|
138
143
|
|
139
|
-
#
|
140
|
-
def
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
# not currently used
|
154
|
-
# if node contains blocks, flatten them into a single line;
|
155
|
-
# and extract only raw text
|
156
|
-
def flatten_rawtext(node)
|
157
|
-
result = []
|
158
|
-
if node.respond_to?(:blocks) && node.blocks?
|
159
|
-
node.blocks.each { |b| result << flatten_rawtext(b) }
|
160
|
-
elsif node.respond_to?(:lines)
|
161
|
-
result = flatten_rawtext_lines(node, result)
|
162
|
-
elsif node.respond_to?(:text)
|
163
|
-
result << node.text.gsub(/<[^>]*>+/, "")
|
164
|
-
else
|
165
|
-
result << node.content.gsub(/<[^>]*>+/, "")
|
144
|
+
# convert definition list term/value pair into Nokogiri XML attribute
|
145
|
+
def dl_to_attrs(elem, dlist, name)
|
146
|
+
e = dlist.at("./dt[text()='#{name}']") or return
|
147
|
+
val = e.at("./following::dd/p") || e.at("./following::dd") or return
|
148
|
+
elem[name] = val.text
|
149
|
+
end
|
150
|
+
|
151
|
+
# convert definition list term/value pairs into Nokogiri XML elements
|
152
|
+
def dl_to_elems(ins, elem, dlist, name)
|
153
|
+
a = elem.at("./#{name}[last()]")
|
154
|
+
ins = a if a
|
155
|
+
dlist.xpath("./dt[text()='#{name}']").each do |e|
|
156
|
+
ins = dl_to_elems1(e, name, ins)
|
166
157
|
end
|
167
|
-
|
158
|
+
ins
|
159
|
+
end
|
160
|
+
|
161
|
+
def dl_to_elems1(term, name, ins)
|
162
|
+
v = term.at("./following::dd")
|
163
|
+
e = v.elements and e.size == 1 && e.first.name == "p" and v = e.first
|
164
|
+
v.name = name
|
165
|
+
ins.next = v
|
166
|
+
ins.next
|
168
167
|
end
|
169
168
|
end
|
170
169
|
end
|
data/lib/utils/version.rb
CHANGED
data/lib/utils/xml.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require "asciidoctor"
|
2
|
+
require "tempfile"
|
3
|
+
require "uuidtools"
|
4
|
+
require "htmlentities"
|
5
|
+
require "nokogiri"
|
6
|
+
|
7
|
+
module Metanorma
|
8
|
+
module Utils
|
9
|
+
NAMECHAR = "\u0000-\u002c\u002f\u003a-\u0040\\u005b-\u005e"\
|
10
|
+
"\u0060\u007b-\u00b6\u00b8-\u00bf\u00d7\u00f7\u037e"\
|
11
|
+
"\u2000-\u200b"\
|
12
|
+
"\u200e-\u203e\u2041-\u206f\u2190-\u2bff\u2ff0-\u3000".freeze
|
13
|
+
NAMESTARTCHAR = "\\u002d\u002e\u0030-\u0039\u00b7\u0300-\u036f"\
|
14
|
+
"\u203f-\u2040".freeze
|
15
|
+
|
16
|
+
class << self
|
17
|
+
def to_ncname(tag, asciionly: true)
|
18
|
+
asciionly and tag = HTMLEntities.new.encode(tag, :basic, :hexadecimal)
|
19
|
+
start = tag[0]
|
20
|
+
ret1 = if %r([#{NAMECHAR}#])o.match?(start)
|
21
|
+
"_"
|
22
|
+
else
|
23
|
+
(%r([#{NAMESTARTCHAR}#])o.match?(start) ? "_#{start}" : start)
|
24
|
+
end
|
25
|
+
ret2 = tag[1..-1] || ""
|
26
|
+
(ret1 || "") + ret2.gsub(%r([#{NAMECHAR}#])o, "_")
|
27
|
+
end
|
28
|
+
|
29
|
+
def anchor_or_uuid(node = nil)
|
30
|
+
uuid = UUIDTools::UUID.random_create
|
31
|
+
node.nil? || node.id.nil? || node.id.empty? ? "_#{uuid}" : node.id
|
32
|
+
end
|
33
|
+
|
34
|
+
NOKOHEAD = <<~HERE.freeze
|
35
|
+
<!DOCTYPE html SYSTEM
|
36
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
37
|
+
<html xmlns="http://www.w3.org/1999/xhtml">
|
38
|
+
<head> <title></title> <meta charset="UTF-8" /> </head>
|
39
|
+
<body> </body> </html>
|
40
|
+
HERE
|
41
|
+
|
42
|
+
# block for processing XML document fragments as XHTML,
|
43
|
+
# to allow for HTMLentities
|
44
|
+
# Unescape special chars used in Asciidoctor substitution processing
|
45
|
+
def noko(&block)
|
46
|
+
doc = ::Nokogiri::XML.parse(NOKOHEAD)
|
47
|
+
fragment = doc.fragment("")
|
48
|
+
::Nokogiri::XML::Builder.with fragment, &block
|
49
|
+
fragment.to_xml(encoding: "US-ASCII", indent: 0,
|
50
|
+
save_with: Nokogiri::XML::Node::SaveOptions::AS_XML)
|
51
|
+
.lines.map do |l|
|
52
|
+
l.gsub(/>\n$/, ">").gsub(/\s*\n$/m, " ").gsub("–", "\u0096")
|
53
|
+
.gsub("—", "\u0097").gsub("–", "\u0096")
|
54
|
+
.gsub("—", "\u0097")
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def ns(xpath)
|
59
|
+
xpath.gsub(%r{/([a-zA-z])}, "/xmlns:\\1")
|
60
|
+
.gsub(%r{::([a-zA-z])}, "::xmlns:\\1")
|
61
|
+
.gsub(%r{\[([a-zA-z][a-z0-9A-Z@/-]* ?=)}, "[xmlns:\\1")
|
62
|
+
.gsub(%r{\[([a-zA-z][a-z0-9A-Z@/-]*[/\[\]])}, "[xmlns:\\1")
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/metanorma-utils.gemspec
CHANGED
@@ -24,12 +24,12 @@ Gem::Specification.new do |spec|
|
|
24
24
|
spec.test_files = `git ls-files -- {spec}/*`.split("\n")
|
25
25
|
spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
|
26
26
|
|
27
|
-
spec.add_dependency "asciidoctor", "
|
27
|
+
spec.add_dependency "asciidoctor", ">= 2"
|
28
28
|
spec.add_dependency "concurrent-ruby"
|
29
29
|
spec.add_dependency "htmlentities", "~> 4.3.4"
|
30
30
|
spec.add_dependency "marcel", "~> 1.0.0"
|
31
31
|
spec.add_dependency "mime-types"
|
32
|
-
spec.add_dependency "nokogiri", "
|
32
|
+
spec.add_dependency "nokogiri", ">= 1.11"
|
33
33
|
spec.add_dependency "sterile", "~> 1.0.14"
|
34
34
|
spec.add_dependency "uuidtools"
|
35
35
|
|
@@ -37,12 +37,12 @@ Gem::Specification.new do |spec|
|
|
37
37
|
spec.add_development_dependency "equivalent-xml", "~> 0.6"
|
38
38
|
spec.add_development_dependency "guard", "~> 2.14"
|
39
39
|
spec.add_development_dependency "guard-rspec", "~> 4.7"
|
40
|
-
spec.add_development_dependency "metanorma-standoc", "~>
|
40
|
+
spec.add_development_dependency "metanorma-standoc", "~> 2.0"
|
41
41
|
spec.add_development_dependency "rake", "~> 13.0"
|
42
42
|
spec.add_development_dependency "rspec", "~> 3.6"
|
43
43
|
spec.add_development_dependency "rubocop", "~> 1.5.2"
|
44
44
|
spec.add_development_dependency "simplecov", "~> 0.15"
|
45
45
|
spec.add_development_dependency "timecop", "~> 0.9"
|
46
|
-
spec.add_development_dependency "vcr", "~>
|
46
|
+
spec.add_development_dependency "vcr", "~> 6.1.0"
|
47
47
|
spec.add_development_dependency "webmock"
|
48
48
|
end
|