metanorma-plugin-datastruct 0.2.1 → 0.2.4

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: e2df7a54019a9c960234097bdf347213643900712c88c38237e45e8188aa1ab4
4
- data.tar.gz: ac5d3580256a1f059354faa6d53cfa32b0e7a14ea573e2a74f58582c69149a91
3
+ metadata.gz: 506a0c248f1bcd37ddc11b9927517a0e64244a56e19f482fd2b49e7cf753d1f9
4
+ data.tar.gz: 933f231c3e43cce3f0d7058a036c4f3d80c028c5a463bc7823d5ff8b938fa417
5
5
  SHA512:
6
- metadata.gz: 0cc9a6f6ea77e72beed6253bb260b3edb01eab07568148e6482f8777c49636806cb22ed0d7a418c9b69c0905065998890df28172edbdbfd088eb0fea9de7d83e
7
- data.tar.gz: cd545f742fff9b0c0d77294eed728738d6b7564fd3a7aa77c763013472b0abf108076fc35de05ac80a80e2a5b008de07f94f6743d1d90d54c2e140d0bd601401
6
+ metadata.gz: 49014c580c342ceab3e6a86b2ab9e056ba78f03b76e7e260c5ce44f7de3e139e668670ce99266db3aa2f6fd39a8d71749dcaa8863f60a566890153db81fffebe
7
+ data.tar.gz: 92c523b5a89060a8754073c5f5fd776a167f160bb2219254325c4ecb1d0f79e180da9cdbf1a75a35169068d55294f1675d2db409a2a9be2e1e0c5d2f607e5859
data/.gitignore CHANGED
@@ -9,4 +9,8 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
- Gemfile.lock
12
+ Gemfile.lock
13
+
14
+ .byebug_history
15
+ *.err
16
+ .rubocop*
data/Gemfile CHANGED
@@ -2,3 +2,4 @@ source "https://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in metanorma-plugin-datastruct.gemspec
4
4
  gemspec
5
+ gem "debug"
@@ -8,6 +8,7 @@ require "liquid/custom_blocks/with_yaml_nested_context"
8
8
  require "liquid/custom_blocks/with_json_nested_context"
9
9
  require "liquid/custom_filters/values"
10
10
  require "liquid/custom_filters/replace_regex"
11
+ require "metanorma/plugin/datastruct/source_extractor"
11
12
 
12
13
  Liquid::Template.register_tag("keyiterator", Liquid::CustomBlocks::KeyIterator)
13
14
  Liquid::Template
@@ -28,8 +29,15 @@ module Metanorma
28
29
  BLOCK_END_REGEXP = /\A\{[A-Z]+\}\z/.freeze
29
30
 
30
31
  def process(document, reader)
31
- input_lines = reader.readlines.to_enum
32
- Asciidoctor::Reader.new(processed_lines(document, input_lines))
32
+ input_lines = reader.readlines
33
+ Metanorma::Plugin::Datastruct::SourceExtractor.extract(
34
+ document,
35
+ input_lines,
36
+ )
37
+
38
+ Asciidoctor::Reader.new(
39
+ processed_lines(document, input_lines.to_enum),
40
+ )
33
41
  end
34
42
 
35
43
  protected
@@ -38,6 +46,10 @@ module Metanorma
38
46
  raise ArgumentError, "Implement `content_from_file` in your class"
39
47
  end
40
48
 
49
+ def content_from_anchor(_document, _file_path)
50
+ raise ArgumentError, "Implement `content_from_anchor` in your class"
51
+ end
52
+
41
53
  private
42
54
 
43
55
  def processed_lines(document, input_lines)
@@ -109,7 +121,13 @@ module Metanorma
109
121
  def parse_template(document, current_block, block_match)
110
122
  transformed_liquid_lines = current_block
111
123
  .map(&method(:transform_line_liquid))
112
- context_items = content_from_file(document, block_match[1])
124
+
125
+ context_items = if block_match[1].start_with?("#")
126
+ content_from_anchor(document, block_match[1][1..-1])
127
+ else
128
+ content_from_file(document, block_match[1])
129
+ end
130
+
113
131
  parse_context_block(document: document,
114
132
  context_lines: transformed_liquid_lines,
115
133
  context_items: context_items,
@@ -41,6 +41,10 @@ module Metanorma
41
41
  JSON.parse(File.read(relative_file_path(document, file_path),
42
42
  encoding: "UTF-8"))
43
43
  end
44
+
45
+ def content_from_anchor(document, anchor)
46
+ JSON.parse(document.attributes["source_blocks"][anchor])
47
+ end
44
48
  end
45
49
  end
46
50
  end
@@ -0,0 +1,99 @@
1
+ module Metanorma
2
+ module Plugin
3
+ module Datastruct
4
+ class SourceExtractor
5
+ # example:
6
+ # - [[abc]]
7
+ ANCHOR_REGEX_1 = /^\[\[(?<id>[^\]]*)\]\]\s*$/.freeze
8
+
9
+ # examples:
10
+ # - [#abc]
11
+ # - [source#abc,ruby]
12
+ ANCHOR_REGEX_2 = /^\[[^#,]*#(?<id>[^,\]]*)[,\]]/.freeze
13
+
14
+ # examples:
15
+ # - [id=abc]
16
+ # - [source,id="abc"]
17
+ ANCHOR_REGEX_3 = /^\[(?:.+,)?id=['"]?(?<id>[^,\]'"]*)['"]?[,\]]/.freeze
18
+
19
+ def initialize(document, input_lines)
20
+ @document = document
21
+ @input_lines = input_lines
22
+
23
+ @document.attributes["source_blocks"] ||= {}
24
+ end
25
+
26
+ def self.extract(document, input_lines)
27
+ new(document, input_lines).extract
28
+ end
29
+
30
+ def extract
31
+ lines = @input_lines.to_enum
32
+
33
+ loop do
34
+ line = lines.next
35
+
36
+ if /^embed::|^include::/.match?(line.strip)
37
+ file_lines = read(filename(@document, line))
38
+ SourceExtractor.extract(@document, file_lines)
39
+ elsif m = match_anchor(line)
40
+ @document.attributes["source_blocks"][m[:id]] = read_section lines
41
+ end
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def match_anchor(line)
48
+ line.match(ANCHOR_REGEX_1) ||
49
+ line.match(ANCHOR_REGEX_2) ||
50
+ line.match(ANCHOR_REGEX_3)
51
+ end
52
+
53
+ def readlines_safe(file)
54
+ return [] if file.eof?
55
+
56
+ file.readlines
57
+ end
58
+
59
+ def read(inc_path)
60
+ ::File.open inc_path, "r" do |fd|
61
+ readlines_safe(fd).map(&:chomp)
62
+ end
63
+ end
64
+
65
+ def filename(document, line)
66
+ m = /(^include::|^embed::)([^\[]+)\[/.match(line)
67
+ return nil unless m
68
+
69
+ file_path = relative_file_path(document, m[2])
70
+
71
+ File.exist?(file_path) ? file_path : nil
72
+ end
73
+
74
+ def relative_file_path(document, file_path)
75
+ docfile_directory = File.dirname(
76
+ document.attributes["docfile"] || ".",
77
+ )
78
+ document
79
+ .path_resolver
80
+ .system_path(file_path, docfile_directory)
81
+ end
82
+
83
+ def read_section(lines)
84
+ m = lines.next.match(/^--+/)
85
+ return "" unless m
86
+
87
+ end_mark = m[0]
88
+ current_section = []
89
+
90
+ while (line = lines.next) != end_mark
91
+ current_section << line
92
+ end
93
+
94
+ current_section.join("\n")
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
@@ -1,7 +1,7 @@
1
1
  module Metanorma
2
2
  module Plugin
3
3
  module Datastruct
4
- VERSION = "0.2.1".freeze
4
+ VERSION = "0.2.4".freeze
5
5
  end
6
6
  end
7
7
  end
@@ -43,7 +43,16 @@ module Metanorma
43
43
  File.read(relative_file_path(document, file_path), encoding: "UTF-8"),
44
44
  permitted_classes: [Date, Time],
45
45
  permitted_symbols: [],
46
- aliases: true
46
+ aliases: true,
47
+ )
48
+ end
49
+
50
+ def content_from_anchor(document, anchor)
51
+ YAML.safe_load(
52
+ document.attributes["source_blocks"][anchor],
53
+ permitted_classes: [Date, Time],
54
+ permitted_symbols: [],
55
+ aliases: true,
47
56
  )
48
57
  end
49
58
  end
@@ -17,16 +17,17 @@ Gem::Specification.new do |spec|
17
17
  # Specify which files should be added to the gem when it is released.
18
18
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
19
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ `git ls-files -z`.split("\x0").reject do |f|
21
+ f.match(%r{^(test|spec|features)/})
22
+ end
21
23
  end
22
24
  spec.bindir = "exe"
23
25
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
26
  spec.require_paths = ["lib"]
25
27
 
26
28
  spec.add_dependency "asciidoctor", "~> 2.0.0"
27
- spec.add_dependency "relaton-cli"
28
29
  spec.add_dependency "isodoc"
29
- spec.add_dependency "liquid", "~> 4"
30
+ spec.add_dependency "relaton-cli"
30
31
 
31
32
  spec.add_development_dependency "byebug"
32
33
  spec.add_development_dependency "equivalent-xml"
@@ -39,4 +40,5 @@ Gem::Specification.new do |spec|
39
40
  spec.add_development_dependency "timecop", "~> 0.9"
40
41
  spec.add_development_dependency "vcr", "~> 6.1.0"
41
42
  spec.add_development_dependency "webmock"
43
+ spec.metadata["rubygems_mfa_required"] = "false"
42
44
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metanorma-plugin-datastruct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-03-02 00:00:00.000000000 Z
11
+ date: 2023-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: asciidoctor
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: 2.0.0
27
27
  - !ruby/object:Gem::Dependency
28
- name: relaton-cli
28
+ name: isodoc
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: isodoc
42
+ name: relaton-cli
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -52,20 +52,6 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: liquid
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '4'
62
- type: :runtime
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '4'
69
55
  - !ruby/object:Gem::Dependency
70
56
  name: byebug
71
57
  requirement: !ruby/object:Gem::Requirement
@@ -248,13 +234,15 @@ files:
248
234
  - lib/metanorma-plugin-datastruct.rb
249
235
  - lib/metanorma/plugin/datastruct/base_structured_text_preprocessor.rb
250
236
  - lib/metanorma/plugin/datastruct/json2_text_preprocessor.rb
237
+ - lib/metanorma/plugin/datastruct/source_extractor.rb
251
238
  - lib/metanorma/plugin/datastruct/version.rb
252
239
  - lib/metanorma/plugin/datastruct/yaml2_text_preprocessor.rb
253
240
  - metanorma-plugin-datastruct.gemspec
254
241
  homepage: https://github.com/metanorma/metanorma-plugin-datastruct
255
242
  licenses:
256
243
  - BSD-2-Clause
257
- metadata: {}
244
+ metadata:
245
+ rubygems_mfa_required: 'false'
258
246
  post_install_message:
259
247
  rdoc_options: []
260
248
  require_paths: