metanorma-standoc 2.2.2 → 2.2.3

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: e1259358ae365bba52ab94c40d88b6cc9d4b1eb9b65a9f7fc8edef4bd77138e0
4
- data.tar.gz: 6c348c7e3b1706287332368251d90fa277fee7e9a54a0fc0cd11e19f5464506b
3
+ metadata.gz: 91681e7073cc074159e4aab0c10043fd86c8cffeba1c1fef9b1db85d11894a42
4
+ data.tar.gz: 0b6047784c5a7ba431ddf6ddbdfd6fb6042289b0947eaa15563a5c3d9e731fba
5
5
  SHA512:
6
- metadata.gz: 602f12339e31a98c061ce7dc46d9fad6888b18e962942f110ebeea86f6739311b093578dbc8886a795bfcd01de790c40c134072449ff1e701779f2e29139a743
7
- data.tar.gz: 39b93eb600aa0f18e21982174fc204eb36a9f28ae7e1e3b5a4a78ddfa9a05a440e6e3d6b94cdf7824dbd71b62880a193d5c88028ecd78d074c7c9361961966f6
6
+ metadata.gz: 68dcf12b8ba888e074970b6cea1918ec8223dd9ae5297a3bac0f337bed63731a8bf1579028915dfa07ce0ec7924677c603c81427b3bccb99640b07f2e148da19
7
+ data.tar.gz: 89458b82d0111581cc81a9cb19ad1c6dcecbdf64916c958e729d4a59424ab61f5c4ab9b7ac73ecbcab242e1e41624e28335bf94edcd7dd50dc80a6f74fd1245e
@@ -160,8 +160,11 @@ module Metanorma
160
160
  end
161
161
 
162
162
  def clean_abort(msg, file = nil)
163
- file and
164
- File.open("#{@filename}.xml.abort", "w:UTF-8") { |f| f.write(file) }
163
+ if file
164
+ doc = file.to_xml(encoding: "UTF-8", indent: 2,
165
+ save_with: Nokogiri::XML::Node::SaveOptions::AS_XML)
166
+ File.open("#{@filename}.xml.abort", "w:UTF-8") { |f| f.write(doc) }
167
+ end
165
168
  clean_exit
166
169
  abort(msg)
167
170
  end
@@ -1,18 +1,17 @@
1
1
  require "asciidoctor"
2
- require "metanorma/util"
3
- require "metanorma/standoc/version"
4
- require "metanorma/standoc/base"
5
- require "metanorma/standoc/front"
6
- require "metanorma/standoc/lists"
7
- require "metanorma/standoc/ref"
8
- require "metanorma/standoc/inline"
9
- require "metanorma/standoc/blocks"
10
- require "metanorma/standoc/section"
11
- require "metanorma/standoc/table"
12
- require "metanorma/standoc/validate"
13
- require "metanorma/standoc/utils"
14
- require "metanorma/standoc/cleanup"
15
- require "metanorma/standoc/reqt"
2
+ require_relative "version"
3
+ require_relative "base"
4
+ require_relative "front"
5
+ require_relative "lists"
6
+ require_relative "ref"
7
+ require_relative "inline"
8
+ require_relative "blocks"
9
+ require_relative "section"
10
+ require_relative "table"
11
+ require_relative "validate"
12
+ require_relative "utils"
13
+ require_relative "cleanup"
14
+ require_relative "reqt"
16
15
  require_relative "./macros"
17
16
 
18
17
  module Metanorma
@@ -5,7 +5,6 @@ require "nokogiri"
5
5
  require "jing"
6
6
  require "iev"
7
7
  require "pngcheck"
8
- require "png"
9
8
 
10
9
  module Metanorma
11
10
  module Standoc
@@ -55,7 +54,8 @@ module Metanorma
55
54
  table_validate(doc)
56
55
  requirement_validate(doc)
57
56
  image_validate(doc)
58
- @fatalerror.empty? or clean_abort(@fatalerror.join("\n"), doc.to_xml)
57
+ @fatalerror.empty? or
58
+ clean_abort(@fatalerror.join("\n"), doc)
59
59
  end
60
60
 
61
61
  def norm_ref_validate(doc)
@@ -108,7 +108,7 @@ module Metanorma
108
108
  Tempfile.open(["tmp", ".xml"], encoding: "UTF-8") do |f|
109
109
  schema_validate1(f, doc, schema)
110
110
  rescue Jing::Error => e
111
- clean_abort("Jing failed with error: #{e}", doc.to_xml)
111
+ clean_abort("Jing failed with error: #{e}", doc)
112
112
  ensure
113
113
  f.close!
114
114
  end
@@ -159,17 +159,46 @@ module Metanorma
159
159
  end
160
160
 
161
161
  def image_validate(doc)
162
+ image_exists(doc)
163
+ png_validate(doc)
164
+ end
165
+
166
+ def image_exists(doc)
167
+ doc.xpath("//image").each do |i|
168
+ Metanorma::Utils::url?(i["src"]) and next
169
+ Metanorma::Utils::datauri?(i["src"]) and next
170
+ expand_path(i["src"]) and next
171
+ @log.add("Images", i.parent,
172
+ "Image not found: #{i['src']}")
173
+ @fatalerror << "Image not found: #{i['src']}"
174
+ end
175
+ end
176
+
177
+ def expand_path(loc)
178
+ relative_path = File.join(@localdir, loc)
179
+ [loc, relative_path].detect do |p|
180
+ File.exist?(p) ? p : nil
181
+ end
182
+ end
183
+
184
+ def png_validate(doc)
162
185
  doc.xpath("//image[@mimetype = 'image/png']").each do |i|
163
- d = Metanorma::Utils::datauri(i["src"], @localdir)
164
- png_validate1(i, Base64.strict_decode64(d.sub(/^.+?base64,/, "")))
186
+ Metanorma::Utils::url?(i["src"]) and next
187
+ decoded = if Metanorma::Utils::datauri?(i["src"])
188
+ Metanorma::Utils::decode_datauri(i["src"])[:data]
189
+ else
190
+ path = expand_path(i["src"]) or next
191
+ File.binread(path)
192
+ end
193
+ png_validate1(i, decoded)
165
194
  end
166
195
  end
167
196
 
168
197
  def png_validate1(img, buffer)
169
198
  PngCheck.check_buffer(buffer)
170
199
  rescue PngCheck::CorruptPngError => e
171
- @log.add("Images", img.parent, "Corrupt PNG image")
172
- @fatalerror << "Exception #{e.message}"
200
+ @log.add("Images", img.parent,
201
+ "Corrupt PNG image detected: #{e.message}")
173
202
  end
174
203
 
175
204
  def validate(doc)
@@ -19,6 +19,6 @@ module Metanorma
19
19
  end
20
20
 
21
21
  module Standoc
22
- VERSION = "2.2.2".freeze
22
+ VERSION = "2.2.3".freeze
23
23
  end
24
24
  end
@@ -3,6 +3,7 @@ require_relative "isodoc/pdf_convert"
3
3
  require_relative "metanorma/standoc/converter"
4
4
  require_relative "metanorma/standoc/version"
5
5
  require "asciidoctor/extensions"
6
+ require "metanorma"
6
7
 
7
8
  if defined? Metanorma::Registry
8
9
  require_relative "metanorma/standoc"
@@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
28
28
  spec.add_dependency "asciidoctor", "~> 2.0.0"
29
29
  spec.add_dependency "iev", "~> 0.3.0"
30
30
  spec.add_dependency "isodoc", "~> 2.2.3"
31
+ spec.add_dependency "metanorma"
31
32
  spec.add_dependency "metanorma-plugin-datastruct", "~> 0.2.0"
32
33
  spec.add_dependency "metanorma-plugin-lutaml"
33
34
  spec.add_dependency "ruby-jing"
@@ -37,7 +38,6 @@ Gem::Specification.new do |spec|
37
38
  spec.add_dependency "latexmath"
38
39
  spec.add_dependency "mathml2asciimath"
39
40
  spec.add_dependency "mn-requirements", "~> 0.1.3"
40
- spec.add_dependency "png"
41
41
  spec.add_dependency "pngcheck"
42
42
  spec.add_dependency "relaton-cli", "~> 1.13.0"
43
43
  spec.add_dependency "relaton-iev", "~> 1.1.0"
@@ -1,5 +1,4 @@
1
1
  require "spec_helper"
2
- require "relaton_iec"
3
2
  require "fileutils"
4
3
 
5
4
  RSpec.describe Metanorma::Standoc do
@@ -783,34 +782,51 @@ RSpec.describe Metanorma::Standoc do
783
782
  .not_to include "Symbol reference in `symbol[xyz]` missing:"
784
783
  end
785
784
 
786
- it "warns and aborts if corrupt PNG" do
785
+ it "warns if corrupt PNG" do
787
786
  FileUtils.rm_f "test.xml"
788
787
  FileUtils.rm_f "test.err"
789
- begin
790
- input = <<~INPUT
791
- = Document title
792
- Author
793
- :docfile: test.adoc
794
- :no-pdf:
795
788
 
796
- == Clause
797
- image::spec/assets/corrupt.png[]
789
+ input = <<~INPUT
790
+ = Document title
791
+ Author
792
+ :docfile: test.adoc
793
+ :no-pdf:
798
794
 
799
- INPUT
800
- expect do
801
- Asciidoctor.convert(input, *OPTIONS)
802
- end.to raise_error(SystemExit)
803
- rescue SystemExit, RuntimeError
804
- end
805
- warn File.read("test.err")
795
+ == Clause
796
+ image::spec/assets/corrupt.png[]
797
+
798
+ INPUT
799
+ Asciidoctor.convert(input, *OPTIONS)
806
800
  expect(File.read("test.err"))
807
801
  .to include "Corrupt PNG image"
808
- expect(File.exist?("test.xml")).to be false
802
+ expect(File.exist?("test.xml")).to be true
803
+ end
804
+
805
+ it "does not warn if not corrupt PNG" do
806
+ FileUtils.rm_f "test.xml"
807
+ FileUtils.rm_f "test.err"
808
+
809
+ input = <<~INPUT
810
+ = Document title
811
+ Author
812
+ :docfile: test.adoc
813
+ :no-pdf:
814
+
815
+ == Clause
816
+ image::spec/assets/correct.png[]
817
+
818
+ INPUT
819
+
820
+ Asciidoctor.convert(input, *OPTIONS)
821
+ expect(File.read("test.err"))
822
+ .not_to include "Corrupt PNG image"
823
+ expect(File.exist?("test.xml")).to be true
809
824
  end
810
825
 
811
- it "does not warn and abort if not corrupt PNG" do
826
+ it "warns and aborts if images does not exist" do
812
827
  FileUtils.rm_f "test.xml"
813
828
  FileUtils.rm_f "test.err"
829
+
814
830
  begin
815
831
  input = <<~INPUT
816
832
  = Document title
@@ -819,16 +835,19 @@ RSpec.describe Metanorma::Standoc do
819
835
  :no-pdf:
820
836
 
821
837
  == Clause
822
- image::spec/assets/correct.png[]
838
+ image::spec/assets/nonexistent.png[]
823
839
 
824
840
  INPUT
841
+
825
842
  expect do
826
843
  Asciidoctor.convert(input, *OPTIONS)
827
- end.not_to raise_error
828
- rescue SystemExit, RuntimeError
844
+ end.to raise_error(SystemExit)
845
+ rescue SystemExit
829
846
  end
847
+
848
+ warn File.read("test.err")
830
849
  expect(File.read("test.err"))
831
- .not_to include "Corrupt PNG image"
832
- expect(File.exist?("test.xml")).to be true
850
+ .to include "Image not found"
851
+ expect(File.exist?("test.xml")).to be false
833
852
  end
834
853
  end
data/spec/spec_helper.rb CHANGED
@@ -20,7 +20,6 @@ require "asciidoctor"
20
20
  require "metanorma-standoc"
21
21
  require "rspec/matchers"
22
22
  require "equivalent-xml"
23
- require "metanorma"
24
23
  require "metanorma/standoc"
25
24
  require "rexml/document"
26
25