metanorma 1.3.1 → 1.3.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,45 +1,51 @@
1
1
  module Metanorma
2
2
  class Compile
3
+ def validate_options(options)
4
+ validate_type(options)
5
+ validate_format(options)
6
+ end
7
+
3
8
  def validate_type(options)
4
9
  unless options[:type]
5
10
  Util.log("[metanorma] Error: Please specify a standard type: "\
6
- "#{@registry.supported_backends}.", :error)
7
- return nil
11
+ "#{@registry.supported_backends}.", :fatal)
8
12
  end
9
13
  stdtype = options[:type].to_sym
10
- metanorma_flavor = "metanorma-#{stdtype}"
14
+ load_flavor(stdtype)
15
+ end
16
+
17
+ def validate_format(options)
18
+ unless options[:format] == :asciidoc
19
+ Util.log("[metanorma] Error: Only source file format currently "\
20
+ "supported is 'asciidoc'.", :fatal)
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def load_flavor(stdtype)
27
+ flavor = "metanorma-#{stdtype}"
11
28
  unless @registry.supported_backends.include? stdtype
12
- Util.log("[metanorma] Info: Loading `#{metanorma_flavor}` gem "\
29
+ Util.log("[metanorma] Info: Loading `#{flavor}` gem "\
13
30
  "for standard type `#{stdtype}`.", :info)
14
31
  end
15
- begin
16
- require "metanorma-#{stdtype}"
17
- Util.log("[metanorma] Info: gem `#{metanorma_flavor}` loaded.", :info)
18
- rescue Gem::ConflictError
19
- Util.log("[metanorma] Error: Couldn't resolve dependencies for "\
20
- "`metanorma-#{stdtype}`, Please add it to your Gemfile "\
21
- "and run bundle install first", :error)
22
- return false
23
- rescue LoadError
24
- Util.log("[metanorma] Error: loading gem `#{metanorma_flavor}` "\
25
- "failed. Exiting.", :error)
26
- return false
27
- end
32
+ require_flavor(flavor, stdtype)
28
33
  unless @registry.supported_backends.include? stdtype
29
- Util.log("[metanorma] Error: The `#{metanorma_flavor}` gem "\
30
- "still doesn't support `#{stdtype}`. Exiting.", :error)
31
- return false
34
+ Util.log("[metanorma] Error: The `#{flavor}` gem "\
35
+ "still doesn't support `#{stdtype}`. Exiting.", :fatal)
32
36
  end
33
- true
34
37
  end
35
38
 
36
- def validate_format(options)
37
- unless options[:format] == :asciidoc
38
- Util.log("[metanorma] Error: Only source file format currently "\
39
- "supported is 'asciidoc'.", :error)
40
- return false
41
- end
42
- true
39
+ def require_flavor(flavor, stdtype)
40
+ require flavor
41
+ Util.log("[metanorma] Info: gem `#{flavor}` loaded.", :info)
42
+ rescue Gem::ConflictError
43
+ Util.log("[metanorma] Error: Couldn't resolve dependencies for "\
44
+ "`metanorma-#{stdtype}`, Please add it to your Gemfile "\
45
+ "and run bundle install first", :fatal)
46
+ rescue LoadError
47
+ Util.log("[metanorma] Error: loading gem `#{flavor}` "\
48
+ "failed. Exiting.", :fatal)
43
49
  end
44
50
  end
45
51
  end
@@ -15,7 +15,7 @@ module Metanorma
15
15
  attr_accessor :logs
16
16
 
17
17
  def initialize
18
- @logs ||= [:warning, :error, :fatal]
18
+ @logs ||= %i[warning error fatal]
19
19
  end
20
20
  end
21
21
 
@@ -1,7 +1,7 @@
1
1
  module Metanorma
2
2
  class Document
3
3
  # @return [Strin]
4
- attr_reader :file, :attachment
4
+ attr_reader :file, :attachment, :bibitem
5
5
 
6
6
  # @param bibitem [RelatonBib::BibliographicItem]
7
7
  def initialize(bibitem, file, options = {})
@@ -14,9 +14,12 @@ module Metanorma
14
14
  class << self
15
15
  # @param file [String] file path
16
16
  # @param attachment [Bool] is an attachment
17
+ # @param identifier [String] is the identifier assigned the file
18
+ # in the collection file
17
19
  # @return [Metanorma::Document]
18
- def parse_file(file, attachment)
19
- new bibitem(file), file, { attachment: attachment }
20
+ def parse_file(file, attachment, identifier = nil)
21
+ new(bibitem(file, attachment, identifier), file,
22
+ { attachment: attachment })
20
23
  end
21
24
 
22
25
  # #param xml [Nokogiri::XML::Document, Nokogiri::XML::Element]
@@ -27,10 +30,18 @@ module Metanorma
27
30
 
28
31
  # raw XML file, can be used to put in entire file instead of just bibitem
29
32
  def raw_file(filename)
30
- doc = Nokogiri::XML(File.read(filename, encoding: "UTF-8"))
33
+ doc = Nokogiri::XML(File.read(filename, encoding: "UTF-8")) do |config|
34
+ config.huge
35
+ end
31
36
  new(doc, filename, raw: true)
32
37
  end
33
38
 
39
+ def attachment_bibitem(identifier)
40
+ Nokogiri::XML(
41
+ "<bibdata><docidentifier>#{identifier}</docidentifier></bibdata>",
42
+ )
43
+ end
44
+
34
45
  private
35
46
 
36
47
  # #param xml [Nokogiri::XML::Document, Nokogiri::XML::Element]
@@ -51,13 +62,16 @@ module Metanorma
51
62
  # @param file [String]
52
63
  # @return [RelatonBib::BibliographicItem,
53
64
  # RelatonIso::IsoBibliographicItem]
54
- def bibitem(file)
55
- case format(file)
56
- when :xml
57
- from_xml Nokogiri::XML(File.read(file, encoding: "UTF-8"))
58
- when :yaml
59
- yaml = File.read(file, encoding: "UTF-8")
60
- Relaton::Cli::YAMLConvertor.convert_single_file(yaml)
65
+ def bibitem(file, attachment, identifier)
66
+ if attachment then attachment_bibitem(identifier)
67
+ else
68
+ case format(file)
69
+ when :xml
70
+ from_xml Nokogiri::XML(File.read(file, encoding: "UTF-8"))
71
+ when :yaml
72
+ yaml = File.read(file, encoding: "UTF-8")
73
+ Relaton::Cli::YAMLConvertor.convert_single_file(yaml)
74
+ end
61
75
  end
62
76
  end
63
77
  end
@@ -88,7 +102,9 @@ module Metanorma
88
102
  if @raw
89
103
  builder << @bibitem.root.to_xml
90
104
  else
91
- builder.send(type + "-standard") { |b| b << @bibitem.to_xml(bibdata: true) }
105
+ builder.send("#{type}-standard") do |b|
106
+ b << @bibitem.to_xml(bibdata: true)
107
+ end
92
108
  end
93
109
  end
94
110
  end
@@ -3,6 +3,5 @@ require_relative "./input/asciidoc"
3
3
 
4
4
  module Metanorma
5
5
  module Input
6
-
7
6
  end
8
7
  end
@@ -2,20 +2,14 @@ require "nokogiri"
2
2
 
3
3
  module Metanorma
4
4
  module Input
5
-
6
5
  class Asciidoc < Base
7
-
8
6
  def process(file, filename, type, options = {})
9
7
  require "asciidoctor"
10
8
  out_opts = {
11
- to_file: false,
12
- safe: :safe,
13
- backend: type,
14
- header_footer: true,
15
- attributes: [
16
- "nodoc", "stem", "xrefstyle=short", "docfile=#{filename}",
17
- "output_dir=#{options[:output_dir]}"
18
- ]
9
+ to_file: false, safe: :safe, backend: type, header_footer: true,
10
+ attributes: ["nodoc", "stem", "xrefstyle=short",
11
+ "docfile=#{filename}",
12
+ "output_dir=#{options[:output_dir]}"]
19
13
  }
20
14
  unless asciidoctor_validate(file, filename, out_opts)
21
15
  warn "Cannot continue compiling Asciidoctor document"
@@ -27,11 +21,12 @@ module Metanorma
27
21
  def asciidoctor_validate(file, filename, options)
28
22
  err = nil
29
23
  begin
30
- previous_stderr, $stderr = $stderr, StringIO.new
24
+ previous_stderr = $stderr
25
+ $stderr = StringIO.new
31
26
  ::Asciidoctor.load(file, options)
32
- %r{(\n|^)asciidoctor: ERROR: ['"]?#{Regexp.escape(filename ||
33
- "<empty>")}['"]?: line \d+: include file not found: }.match($stderr.string) and
34
- err = $stderr.string
27
+ %r{(\n|^)asciidoctor: ERROR: ['"]?#{Regexp.escape(filename ||
28
+ "<empty>")}['"]?: line \d+: include file not found: }
29
+ .match($stderr.string) and err = $stderr.string
35
30
  ensure
36
31
  $stderr = previous_stderr
37
32
  end
@@ -45,8 +40,9 @@ module Metanorma
45
40
  /\n:mn-output-extensions: (?<extensions>[^\n]+)\n/ =~ headerextract
46
41
  /\n:mn-relaton-output-file: (?<relaton>[^\n]+)\n/ =~ headerextract
47
42
  /\n(?<asciimath>:mn-keep-asciimath:[^\n]*)\n/ =~ headerextract
48
- asciimath = defined?(asciimath) ?
49
- (!asciimath.nil? && asciimath != ":mn-keep-asciimath: false") : nil
43
+ asciimath = if defined?(asciimath)
44
+ (!asciimath.nil? && asciimath != ":mn-keep-asciimath: false")
45
+ end
50
46
  asciimath = nil if asciimath == false
51
47
  {
52
48
  type: defined?(type) ? type : nil,
@@ -60,38 +56,39 @@ module Metanorma
60
56
  attr&.sub(/^#{name}:\s*$/, "#{name}: true")&.sub(/^#{name}:\s+/, "")
61
57
  end
62
58
 
59
+ ADOC_OPTIONS = %w(htmlstylesheet htmlcoverpage htmlintropage scripts
60
+ scripts-override scripts-pdf wordstylesheet bare i18nyaml
61
+ standardstylesheet header wordcoverpage wordintropage
62
+ ulstyle olstyle htmlstylesheet-override
63
+ htmltoclevels doctoclevels sectionsplit
64
+ body-font header-font monospace-font title-font
65
+ wordstylesheet-override).freeze
66
+
63
67
  def extract_options(file)
64
68
  header = file.sub(/\n\n.*$/m, "\n")
65
- ret = %w(htmlstylesheet htmlcoverpage htmlintropage scripts
66
- scripts-pdf wordstylesheet
67
- standardstylesheet header wordcoverpage wordintropage i18nyaml
68
- ulstyle olstyle htmlstylesheet-override
69
- htmltoclevels doctoclevels sectionsplit
70
- body-font header-font monospace-font title-font
71
- wordstylesheet-override).each_with_object({}) do |w, acc|
69
+ ret = ADOC_OPTIONS.each_with_object({}) do |w, acc|
72
70
  m = /\n:#{w}: ([^\n]+)\n/.match(header) or next
73
71
  acc[w.gsub(/-/, "").sub(/override$/, "_override")
74
72
  .sub(/pdf$/, "_pdf").to_sym] = m[1]
75
73
  end
76
74
  /\n:data-uri-image: (?<datauriimage>[^\n]+)\n/ =~ header
77
- /\n:(?<hierarchical_assets>hierarchical-assets:[^\n]*)\n/ =~ header
75
+ /\n:(?<hier_assets>hierarchical-assets:[^\n]*)\n/ =~ header
78
76
  /\n:(?<use_xinclude>use-xinclude:[^\n]*)\n/ =~ header
79
77
  /\n:(?<break_up>break-up-urls-in-tables:[^\n]*)\n/ =~ header
80
78
 
81
- defined?(hierarchical_assets) and
82
- hierarchical_assets = empty_attr(hierarchical_assets, "hierarchical-assets")
79
+ defined?(hier_assets) and
80
+ hier_assets = empty_attr(hier_assets, "hierarchical-assets")
83
81
  defined?(use_xinclude) and
84
82
  use_xinclude = empty_attr(use_xinclude, "use-xinclude")
85
83
  defined?(break_up) and
86
84
  break_up = empty_attr(break_up, "break-up-urls-in-tables")
87
- ret.merge({
85
+ ret.merge(
88
86
  datauriimage: defined?(datauriimage) ? datauriimage != "false" : nil,
89
- hierarchical_assets: defined?(hierarchical_assets) ? hierarchical_assets : nil,
87
+ hierarchical_assets: defined?(hier_assets) ? hier_assets : nil,
90
88
  use_xinclude: defined?(use_xinclude) ? use_xinclude : nil,
91
89
  break_up_urls_in_tables: defined?(break_up) ? break_up : nil,
92
- }).reject { |_, val| val.nil? }
90
+ ).reject { |_, val| val.nil? }
93
91
  end
94
-
95
92
  end
96
93
  end
97
94
  end
@@ -4,7 +4,5 @@ require_relative "./output/xslfo"
4
4
 
5
5
  module Metanorma
6
6
  module Output
7
-
8
7
  end
9
8
  end
10
-
@@ -7,7 +7,8 @@ module Metanorma
7
7
  def file_path(url_path)
8
8
  file_url = url_path
9
9
  file_url = "file://#{url_path}" if Pathname.new(file_url).absolute?
10
- file_url = "file://#{Dir.pwd}/#{url_path}" unless %r{^file://} =~ file_url
10
+ %r{^file://}.match?(file_url) or
11
+ file_url = "file://#{Dir.pwd}/#{url_path}"
11
12
  file_url
12
13
  end
13
14
  end
@@ -11,5 +11,50 @@ module Metanorma
11
11
  exit(1)
12
12
  end
13
13
  end
14
+
15
+ # dependency ordering
16
+ def self.sort_extensions_execution_ord(ext)
17
+ case ext
18
+ when :xml then 0
19
+ when :rxl then 1
20
+ when :presentation then 2
21
+ else
22
+ 99
23
+ end
24
+ end
25
+
26
+ def self.sort_extensions_execution(ext)
27
+ ext.sort do |a, b|
28
+ sort_extensions_execution_ord(a) <=> sort_extensions_execution_ord(b)
29
+ end
30
+ end
31
+
32
+ class DisambigFiles
33
+ def initialize
34
+ @seen_filenames = []
35
+ end
36
+
37
+ def source2dest_filename(name, disambig = true)
38
+ n = name.sub(%r{^(\./)?(\.\./)+}, "")
39
+ dir = File.dirname(n)
40
+ base = File.basename(n)
41
+ if disambig && @seen_filenames.include?(base)
42
+ base = disambiguate_filename(base)
43
+ end
44
+ @seen_filenames << base
45
+ dir == "." ? base : File.join(dir, base)
46
+ end
47
+
48
+ def disambiguate_filename(base)
49
+ m = /^(?<start>.+\.)(?!0)(?<num>\d+)\.(?<suff>[^.]*)$/.match(base) ||
50
+ /^(?<start>.+\.)(?<suff>[^.]*)/.match(base) ||
51
+ /^(?<start>.+)$/.match(base)
52
+ i = m.names.include?("num") ? m["num"].to_i + 1 : 1
53
+ while @seen_filenames.include? base = "#{m['start']}#{i}.#{m['suff']}"
54
+ i += 1
55
+ end
56
+ base
57
+ end
58
+ end
14
59
  end
15
60
  end
@@ -1,3 +1,3 @@
1
1
  module Metanorma
2
- VERSION = "1.3.1".freeze
2
+ VERSION = "1.3.6".freeze
3
3
  end
data/metanorma.gemspec CHANGED
@@ -20,10 +20,10 @@ Gem::Specification.new do |spec|
20
20
  spec.bindir = "bin"
21
21
  # spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
22
  spec.require_paths = ["lib"]
23
- spec.required_ruby_version = ">= 2.4.0"
23
+ spec.required_ruby_version = ">= 2.5.0"
24
24
 
25
25
  spec.add_runtime_dependency "asciidoctor"
26
- spec.add_runtime_dependency "fontist", "~> 1.8"
26
+ spec.add_runtime_dependency "fontist", "~> 1.9"
27
27
  spec.add_runtime_dependency "htmlentities"
28
28
  spec.add_runtime_dependency "metanorma-utils", "~> 1.2.0"
29
29
  spec.add_runtime_dependency "mn2pdf", "~> 1"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metanorma
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-26 00:00:00.000000000 Z
11
+ date: 2021-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: asciidoctor
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.8'
33
+ version: '1.9'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.8'
40
+ version: '1.9'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: htmlentities
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -286,7 +286,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
286
286
  requirements:
287
287
  - - ">="
288
288
  - !ruby/object:Gem::Version
289
- version: 2.4.0
289
+ version: 2.5.0
290
290
  required_rubygems_version: !ruby/object:Gem::Requirement
291
291
  requirements:
292
292
  - - ">="