metanorma-utils 1.2.7 → 1.3.0

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: 43530083e199af0abeff8df1b697507ed4f08f5039de4acba24409008c51e534
4
- data.tar.gz: 19a237c0c4134abbf35daeeff19e0aeea715a528c9f55cd6150908460f21bed1
3
+ metadata.gz: 06b8911f4c03cb97f4a358a3d5bfeb3931f49bf205a06ff486f1897cf4413ab7
4
+ data.tar.gz: a1d15ab7db12270bec314abff49b6cd16e6150c13142e6ac338df19f46c5e2a1
5
5
  SHA512:
6
- metadata.gz: a910ee73c2ee55d26cae92489643cd41b6de237f663910030475f4de1e295b45e6199f0722bc1dd8665007ccb0a3fdd1117fdb6dcb9a1dada1afc3998cd8e346
7
- data.tar.gz: 8942457b7b8a19372a9a559c7db552e5c63675d2f26f24a17d47962c65c4cddd491eadfc56223533b8412f34c52cafbb3f818f16246c24ce76ed8b62810206d9
6
+ metadata.gz: 0b69767957d3f54acd8f85b023d876097561f83e6c606051dbde0889ff7e37cf368839424576164a7fe81698eef0417cfe2a6ea019ee04f591e8e10295c00d7f
7
+ data.tar.gz: c3203ed0411f37c048c558777d51955866e507c79454d686a80f4d9ab392e11f6f5332e03541d5d34cba77f779ad01275102a52127f517f4dd8eb06e626e501b
@@ -10,23 +10,6 @@ on:
10
10
 
11
11
  jobs:
12
12
  rake:
13
- name: Test on Ruby ${{ matrix.ruby }} ${{ matrix.os }}
14
- runs-on: ${{ matrix.os }}
15
- continue-on-error: ${{ matrix.experimental }}
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 }}
@@ -2,3 +2,4 @@ require_relative "utils/version"
2
2
  require_relative "utils/main"
3
3
  require_relative "utils/image"
4
4
  require_relative "utils/log"
5
+ require_relative "utils/xml"
data/lib/utils/image.rb CHANGED
@@ -115,8 +115,10 @@ module Metanorma
115
115
  svg.xpath("//m:style", "m" => SVG_NS).each do |s|
116
116
  c = s.children.to_xml
117
117
  ids.each do |i|
118
- c = c.gsub(%r[##{i}\b], sprintf("#%s_%09d", i, idx))
119
- .gsub(%r(\[id\s*=\s*['"]?#{i}['"]?\]), sprintf("[id='%s_%09d']", i, idx))
118
+ c = c.gsub(%r[##{i}\b],
119
+ sprintf("#%<id>s_%<idx>09d", id: i, idx: idx))
120
+ .gsub(%r(\[id\s*=\s*['"]?#{i}['"]?\]),
121
+ sprintf("[id='%<id>s_%<idx>09d']", id: i, idx: idx))
120
122
  end
121
123
  s.children = c
122
124
  end
data/lib/utils/log.rb CHANGED
@@ -29,7 +29,7 @@ module Metanorma
29
29
  (!node.respond_to?(:level) || node.level.positive?) &&
30
30
  (!node.respond_to?(:context) || node.context != :section)
31
31
  node = node.parent
32
- return "Section: #{node.title}" if node&.respond_to?(:context) &&
32
+ return "Section: #{node.title}" if node.respond_to?(:context) &&
33
33
  node&.context == :section
34
34
  end
35
35
  "??"
@@ -50,13 +50,19 @@ module Metanorma
50
50
  @log.each_key do |key|
51
51
  f.puts "\n\n== #{key}\n\n"
52
52
  @log[key].sort_by { |a| a[:location] }.each do |n|
53
- loc = n[:location] ? "(#{n[:location]}): " : ""
54
- f.puts "#{loc}#{n[:message]}"
55
- n[:context]&.split(/\n/)&.first(5)&.each { |l| f.puts "\t#{l}" }
53
+ write1(f, n)
56
54
  end
57
55
  end
58
56
  end
59
57
  end
58
+
59
+ def write1(file, entry)
60
+ loc = entry[:location] ? "(#{entry[:location]}): " : ""
61
+ file.puts "#{loc}#{entry[:message]}"
62
+ entry[:context]&.split(/\n/)&.first(5)&.each do |l|
63
+ file.puts "\t#{l}"
64
+ end
65
+ end
60
66
  end
61
67
  end
62
68
  end
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 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, "_")
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
- def anchor_or_uuid(node = nil)
30
- uuid = UUIDTools::UUID.random_create
31
- node.nil? || node.id.nil? || node.id.empty? ? "_#{uuid}" : node.id
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
- text.gsub(/ --? /, "&#8201;&#8212;&#8201;")
54
- .gsub(/--/, "&#8212;").smart_format.gsub(/</, "&lt;")
55
- .gsub(/>/, "&gt;")
54
+ HTMLEntities.new.encode(
55
+ HTMLEntities.new.decode(
56
+ text.gsub(/ --? /, "&#8201;&#8212;&#8201;")
57
+ .gsub(/--/, "&#8212;"),
58
+ )
59
+ .smart_format, :basic
60
+ )
56
61
  end
57
62
 
58
63
  def endash_date(elem)
@@ -68,10 +73,8 @@ module Metanorma
68
73
  def set_nested_value(hash, keys, new_val)
69
74
  key = keys[0]
70
75
  if keys.length == 1
71
- hash[key] = if hash[key].is_a?(Array)
72
- (hash[key] << new_val)
73
- else
74
- hash[key].nil? ? new_val : [hash[key], new_val]
76
+ hash[key] = if hash[key].is_a?(Array) then (hash[key] << new_val)
77
+ else hash[key].nil? ? new_val : [hash[key], new_val]
75
78
  end
76
79
  elsif hash[key].is_a?(Array)
77
80
  hash[key][-1] = {} if !hash[key].empty? && hash[key][-1].nil?
@@ -118,35 +121,49 @@ module Metanorma
118
121
  end
119
122
  end
120
123
 
121
- # not currently used
122
- def flatten_rawtext_lines(node, result)
123
- node.lines.each do |x|
124
- result << if node.respond_to?(:context) &&
125
- (node.context == :literal || node.context == :listing)
126
- x.gsub(/</, "&lt;").gsub(/>/, "&gt;")
127
- else
128
- # strip not only HTML <tag>, and Asciidoc xrefs <<xref>>
129
- x.gsub(/<[^>]*>+/, "")
130
- end
131
- end
132
- result
133
- end
134
-
135
- # not currently used
136
- # if node contains blocks, flatten them into a single line;
137
- # and extract only raw text
138
- def flatten_rawtext(node)
139
- result = []
140
- if node.respond_to?(:blocks) && node.blocks?
141
- node.blocks.each { |b| result << flatten_rawtext(b) }
142
- elsif node.respond_to?(:lines)
143
- result = flatten_rawtext_lines(node, result)
144
- elsif node.respond_to?(:text)
145
- result << node.text.gsub(/<[^>]*>+/, "")
124
+ def default_script(lang)
125
+ case lang
126
+ when "ar", "fa" then "Arab"
127
+ when "ur" then "Aran"
128
+ when "ru", "bg" then "Cyrl"
129
+ when "hi" then "Deva"
130
+ when "el" then "Grek"
131
+ when "zh" then "Hans"
132
+ when "ko" then "Kore"
133
+ when "he" then "Hebr"
134
+ when "ja" then "Jpan"
146
135
  else
147
- result << node.content.gsub(/<[^>]*>+/, "")
136
+ "Latn"
148
137
  end
149
- result.reject(&:empty?)
138
+ end
139
+
140
+ def rtl_script?(script)
141
+ %w(Arab Aran Hebr).include? script
142
+ end
143
+
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)
157
+ end
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
150
167
  end
151
168
  end
152
169
  end
data/lib/utils/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Metanorma
2
2
  module Utils
3
- VERSION = "1.2.7".freeze
3
+ VERSION = "1.3.0".freeze
4
4
  end
5
5
  end
data/lib/utils/xml.rb ADDED
@@ -0,0 +1,59 @@
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("&#150;", "\u0096")
53
+ .gsub("&#151;", "\u0097").gsub("&#x96;", "\u0096")
54
+ .gsub("&#x97;", "\u0097")
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -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", "~> 2.0.0"
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", "~> 1.11"
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", "~> 1.10"
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", "~> 5.0.0"
46
+ spec.add_development_dependency "vcr", "~> 6.1.0"
47
47
  spec.add_development_dependency "webmock"
48
48
  end