metanorma-standoc 3.0.4 → 3.0.5

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.
@@ -0,0 +1,104 @@
1
+ require "jing"
2
+
3
+ module Metanorma
4
+ module Standoc
5
+ module Validate
6
+ def schema_location
7
+ self.class.respond_to?(:_file) and ret = self.class::_file
8
+ ret ||= caller_locations(1..1).first.absolute_path
9
+ ret ||= __FILE__
10
+ File.join(File.dirname(ret), schema_file)
11
+ end
12
+
13
+ def schema_file
14
+ "isodoc-compile.rng"
15
+ end
16
+
17
+ def schema_validate(doc, schema)
18
+ Tempfile.open(["tmp", ".xml"], encoding: "UTF-8") do |f|
19
+ schema_validate1(f, doc, schema)
20
+ rescue Jing::Error => e
21
+ clean_abort("Jing failed with error: #{e}", doc)
22
+ ensure
23
+ f.close!
24
+ end
25
+ end
26
+
27
+ def schema_validate1(file, doc, schema)
28
+ file.write(to_xml(doc))
29
+ file.close
30
+ errors = Jing.new(schema, encoding: "UTF-8").validate(file.path)
31
+ warn "Syntax Valid!" if errors.none?
32
+ errors.each do |e|
33
+ @log.add("Metanorma XML Syntax",
34
+ "XML Line #{'%06d' % e[:line]}:#{e[:column]}", e[:message])
35
+ end
36
+ end
37
+
38
+ def validate_document_fragment(xml_fragment)
39
+ f = add_ns_to_fragment(xml_fragment) or
40
+ return [true,
41
+ "Fragment is not well-formed XML, not validating"]
42
+ begin
43
+ temp_schema, schema = fragment_schema(f.root.name)
44
+ schema or return [false, "Did not expect element #{f.root.name}"]
45
+ validation_errors = schema.validate(f)
46
+ [validation_errors.none? do |x|
47
+ x.to_s.include?("Did not expect element")
48
+ end, validation_errors]
49
+ ensure
50
+ temp_schema.unlink
51
+ end
52
+ end
53
+
54
+ def add_ns_to_fragment(xml_fragment)
55
+ f = Nokogiri::XML(xml_fragment, &:strict)
56
+ f.errors.any? || f.root.nil? and return nil
57
+ root_tag = f.root.name
58
+ f.root.namespace or
59
+ f = Nokogiri::XML(xml_fragment
60
+ .sub(/<#{root_tag}([^>]*)>/,
61
+ "<#{root_tag}\\1 xmlns='#{xml_namespace}'>"))
62
+ f
63
+ rescue StandardError
64
+ nil
65
+ end
66
+
67
+ def fragment_schema(root_element)
68
+ temp_schema = Tempfile.new(["dynamic_schema", ".rng"])
69
+ temp_schema.write(<<~SCHEMA)
70
+ <grammar xmlns="http://relaxng.org/ns/structure/1.0">
71
+ <include href="#{schema_location}">
72
+ <start combine="choice">
73
+ <ref name="#{root_element}"/>
74
+ </start>
75
+ </include>
76
+ </grammar>
77
+ SCHEMA
78
+ temp_schema.close
79
+ [temp_schema, Nokogiri::XML::RelaxNG(File.open(temp_schema.path))]
80
+ rescue StandardError # error because root_element is not in schema
81
+ [temp_schema, nil]
82
+ end
83
+
84
+ SVG_NS = "http://www.w3.org/2000/svg".freeze
85
+
86
+ WILDCARD_ATTRS = "//stem | //metanorma-extension".freeze
87
+
88
+ # RelaxNG cannot cope well with wildcard attributes. So we strip
89
+ # any attributes from FormattedString instances (which can contain
90
+ # xs:any markup, and are signalled with @format) before validation.
91
+ def formattedstr_strip(doc)
92
+ doc.xpath(WILDCARD_ATTRS, "m" => SVG_NS).each do |n|
93
+ n.elements.each do |e|
94
+ e.traverse do |e1|
95
+ e1.element? and e1.each { |k, _v| e1.delete(k) } # rubocop:disable Style/HashEachMethods
96
+ end
97
+ end
98
+ end
99
+ doc.xpath("//m:svg", "m" => SVG_NS).each { |n| n.replace("<svg/>") }
100
+ doc
101
+ end
102
+ end
103
+ end
104
+ end
@@ -19,6 +19,6 @@ module Metanorma
19
19
  end
20
20
 
21
21
  module Standoc
22
- VERSION = "3.0.4".freeze
22
+ VERSION = "3.0.5".freeze
23
23
  end
24
24
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metanorma-standoc
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.4
4
+ version: 3.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-03-31 00:00:00.000000000 Z
11
+ date: 2025-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -517,6 +517,7 @@ files:
517
517
  - lib/metanorma/standoc/cleanup_mathvariant.rb
518
518
  - lib/metanorma/standoc/cleanup_ref.rb
519
519
  - lib/metanorma/standoc/cleanup_reqt.rb
520
+ - lib/metanorma/standoc/cleanup_review.rb
520
521
  - lib/metanorma/standoc/cleanup_section.rb
521
522
  - lib/metanorma/standoc/cleanup_section_names.rb
522
523
  - lib/metanorma/standoc/cleanup_symbols.rb
@@ -554,6 +555,7 @@ files:
554
555
  - lib/metanorma/standoc/ref_queue.rb
555
556
  - lib/metanorma/standoc/ref_sect.rb
556
557
  - lib/metanorma/standoc/ref_utility.rb
558
+ - lib/metanorma/standoc/regex.rb
557
559
  - lib/metanorma/standoc/render.rb
558
560
  - lib/metanorma/standoc/reqt.rb
559
561
  - lib/metanorma/standoc/reqt.rng
@@ -565,6 +567,7 @@ files:
565
567
  - lib/metanorma/standoc/terms.rb
566
568
  - lib/metanorma/standoc/utils.rb
567
569
  - lib/metanorma/standoc/validate.rb
570
+ - lib/metanorma/standoc/validate_schema.rb
568
571
  - lib/metanorma/standoc/validate_section.rb
569
572
  - lib/metanorma/standoc/validate_table.rb
570
573
  - lib/metanorma/standoc/validate_term.rb