metanorma-plugin-datastruct 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fe622d105e9790da8f5d61265addc954c5718827464c6b7069bfcf4f28b854e2
4
- data.tar.gz: aa6fd9ec16c14b9d558631ce6dd3f87a4a5547d7cd917a8651489018ca7b9bdc
3
+ metadata.gz: 506a0c248f1bcd37ddc11b9927517a0e64244a56e19f482fd2b49e7cf753d1f9
4
+ data.tar.gz: 933f231c3e43cce3f0d7058a036c4f3d80c028c5a463bc7823d5ff8b938fa417
5
5
  SHA512:
6
- metadata.gz: 57afedfbda26e055e3b09cc7ea5fd84978c95862039ce7894438ad6acead8dff3551fc9c7980fa006ad98eadfc6d3ed5d1c76dbe465c89df487aaa69be44c642
7
- data.tar.gz: 23171c33074450db80cd6c6ab310cfdab19153428bf147beb9a7478edc3d7a61c55a89e29274debece82663ffc9d98d9d886a3193cce259b37e78a1bc40fe046
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*
@@ -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.3".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
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.3
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-04-09 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
@@ -234,6 +234,7 @@ files:
234
234
  - lib/metanorma-plugin-datastruct.rb
235
235
  - lib/metanorma/plugin/datastruct/base_structured_text_preprocessor.rb
236
236
  - lib/metanorma/plugin/datastruct/json2_text_preprocessor.rb
237
+ - lib/metanorma/plugin/datastruct/source_extractor.rb
237
238
  - lib/metanorma/plugin/datastruct/version.rb
238
239
  - lib/metanorma/plugin/datastruct/yaml2_text_preprocessor.rb
239
240
  - metanorma-plugin-datastruct.gemspec