metanorma-plugin-datastruct 0.3.4 → 0.3.6

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: ed19e5b25fb509b816c567e7e274739ce9dd2e734814ccf77c3e68f1e45775ad
4
- data.tar.gz: b57e630d156f1f604d370842cd26431e63ed68a27ec58f4cd89ca78409110372
3
+ metadata.gz: c587e187f319415023899f557238cea0c5211bf7e3f6a590b31835f36367f635
4
+ data.tar.gz: fa8aea4c6c5dea4d167492444165cc0e40acde1b56a8b81d34f29a3b8f2d6d58
5
5
  SHA512:
6
- metadata.gz: 4212768404a5a67011682cce9aadac40453dadfc34d63cf69c07fd888789a817793c1c9e0a2b16c582f6a7015f7cd58046e3a5583d992b4072912c452bd5a4e0
7
- data.tar.gz: feb6aa83d2fca789fa7044dea3579f80d2c9b8e16b989fd4c3369764c048eea1d7427b36fdb622eeadc657e45aaf6e6931e065582805101735134259aa04db59
6
+ metadata.gz: a05c5f25575383dc28d988eeac331b4ff10dde2dfd36f70a701aa8f90d18ca2458587bef808117d71f67236cdcb278529deabd3264dc10c733153c40d5d6a90c
7
+ data.tar.gz: f2776f29ff0fa1c10d7c786ee6e41b2f38d23b971854a8b54c5c075fb482addf4acdcf7ee8570d7676993c48c21626c12755ae8ec1b666a53d61e406ed6ac107
data/README.adoc CHANGED
@@ -692,3 +692,73 @@ The color is red and the shape is square.
692
692
  ----
693
693
 
694
694
  --
695
+
696
+
697
+ === Content contains json or yaml filepaths
698
+
699
+ When the content of the loaded files contains filepaths of json files or
700
+ yaml files, you can load extra data by these filepaths by `load_file` liquid
701
+ filter.
702
+
703
+ EXAMPLE:
704
+ --
705
+ Given:
706
+
707
+ strings1.json
708
+ [source,json]
709
+ ----
710
+ {
711
+ "foo": "bar",
712
+ "paths": ["a.yaml", "b.yaml"]
713
+ }
714
+ ----
715
+
716
+ Where:
717
+
718
+ * `paths` is an array of filepaths relative to the Metanorma document
719
+
720
+ a.yaml
721
+ [source,yaml]
722
+ ----
723
+ ---
724
+ shape: circle
725
+ color: red
726
+ ----
727
+
728
+ b.yaml
729
+ [source,yaml]
730
+ ----
731
+ ---
732
+ shape: square
733
+ color: blue
734
+ corners: 4
735
+ ----
736
+
737
+ And the block:
738
+ [source,asciidoc]
739
+ ------
740
+ [data2text,my_context=strings1.json]
741
+ ----
742
+ I'm heading to the {{my_context.foo}}.
743
+
744
+ {% for path in my_context.paths %}
745
+ {% assign foo = path | load_file %}
746
+ This is {{ foo[shape] }} with color {{ foo[color] }}.
747
+ {% endfor %}
748
+ ----
749
+ ------
750
+
751
+ Where:
752
+
753
+ * load_file is a liquid filter that loads the file content
754
+
755
+ Will render as:
756
+ [source,asciidoc]
757
+ ----
758
+ I'm heading to the bar.
759
+
760
+ This is circle with color red.
761
+ This is square with color blue.
762
+ ----
763
+
764
+ --
@@ -0,0 +1,12 @@
1
+ require "metanorma/plugin/datastruct/content"
2
+
3
+ module Liquid
4
+ module CustomFilters
5
+ include ::Metanorma::Plugin::Datastruct::Content
6
+
7
+ def loadfile(path, parent_folder)
8
+ resolved_file_path = File.expand_path(path, parent_folder)
9
+ load_content_from_file(resolved_file_path)
10
+ end
11
+ end
12
+ end
@@ -4,20 +4,13 @@ require "liquid"
4
4
  require "asciidoctor"
5
5
  require "asciidoctor/reader"
6
6
  require "liquid/custom_blocks/key_iterator"
7
- require "liquid/custom_blocks/with_yaml_nested_context"
8
- require "liquid/custom_blocks/with_json_nested_context"
9
7
  require "liquid/custom_filters/values"
10
8
  require "liquid/custom_filters/replace_regex"
9
+ require "liquid/custom_filters/loadfile"
11
10
  require "metanorma/plugin/datastruct/source_extractor"
12
11
 
13
12
  Liquid::Environment.default
14
13
  .register_tag("keyiterator", Liquid::CustomBlocks::KeyIterator)
15
- Liquid::Environment.default
16
- .register_tag("with_yaml_nested_context",
17
- Liquid::CustomBlocks::WithYamlNestedContext)
18
- Liquid::Environment.default
19
- .register_tag("with_json_nested_context",
20
- Liquid::CustomBlocks::WithJsonNestedContext)
21
14
  Liquid::Environment.default.register_filter(Liquid::CustomFilters)
22
15
 
23
16
  module Asciidoctor
@@ -36,6 +29,7 @@ module Metanorma
36
29
  Asciidoctor::Extensions::Preprocessor
37
30
  BLOCK_START_REGEXP = /\{(.+?)\.\*,(.+),(.+)\}/.freeze
38
31
  BLOCK_END_REGEXP = /\A\{[A-Z]+\}\z/.freeze
32
+ LOAD_FILE_REGEXP = /{% assign (.*) = (.*) \| load_file %}/.freeze
39
33
 
40
34
  def process(document, reader)
41
35
  r = ::Asciidoctor::PreprocessorNoIfdefsReader
@@ -80,7 +74,8 @@ module Metanorma
80
74
 
81
75
  def process_text_blocks(document, input_lines)
82
76
  line = input_lines.next
83
- block_match = line.match(/^\[#{config[:block_name]},(.+?),(.+?)\]/)
77
+ block_match = line.match(/^\[#{config[:block_name]},(.+?),(.+?)\]/) ||
78
+ line.match(/^\[#{config[:block_name]},(.+?)\]/)
84
79
  return [line] if block_match.nil?
85
80
 
86
81
  end_mark = input_lines.next
@@ -93,23 +88,16 @@ module Metanorma
93
88
 
94
89
  def collect_internal_block_lines(document, input_lines, end_mark) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
95
90
  current_block = []
96
- nested_marks = []
97
91
  while (block_line = input_lines.next) != end_mark
98
- if nested_match = block_line
99
- .match(/^\[#{config[:block_name]},(.+?),(.+?)\]/)
100
- current_block
101
- .push(*nested_context_tag(document,
102
- nested_match[1],
103
- nested_match[2]).split("\n"))
104
- next nested_marks.push(input_lines.next)
105
- end
92
+ if block_line.match?(LOAD_FILE_REGEXP)
93
+ load_file_match = block_line.match(LOAD_FILE_REGEXP)
106
94
 
107
- if nested_marks.include?(block_line)
108
- current_block.push(
109
- "{% endwith_#{data_file_type}_nested_context %}",
110
- )
111
- next nested_marks.delete(block_line)
95
+ # Add parent folder as argument to loadfile filter
96
+ block_line = "{% assign #{load_file_match[1]} = "\
97
+ "#{load_file_match[2]} | loadfile: " \
98
+ "\"#{document.attributes['docdir']}\" %}"
112
99
  end
100
+
113
101
  current_block.push(block_line)
114
102
  end
115
103
  current_block
@@ -119,16 +107,6 @@ module Metanorma
119
107
  @config[:block_name].split("2").first
120
108
  end
121
109
 
122
- def nested_context_tag(document, file_path, context_name)
123
- absolute_file_path = relative_file_path(document, file_path)
124
- <<~TEMPLATE
125
- {% capture nested_file_path %}
126
- #{absolute_file_path}
127
- {% endcapture %}
128
- {% with_#{data_file_type}_nested_context nested_file_path, #{context_name} %}
129
- TEMPLATE
130
- end
131
-
132
110
  def parse_template(document, current_block, block_match) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
133
111
  transformed_liquid_lines = current_block.map do |x|
134
112
  transform_line_liquid(x)
@@ -10,9 +10,7 @@ module Metanorma
10
10
  protected
11
11
 
12
12
  # https://ruby-doc.org/stdlib-2.5.1/libdoc/psych/rdoc/Psych.html#method-c-safe_load
13
- def yaml_content_from_file(document, file_path) # rubocop:disable Metrics/MethodLength
14
- resolved_file_path = relative_file_path(document, file_path)
15
-
13
+ def yaml_content_from_file(resolved_file_path) # rubocop:disable Metrics/MethodLength
16
14
  unless File.exist?(resolved_file_path)
17
15
  ::Metanorma::Util.log(
18
16
  "YAML file referenced in [yaml2text] block not found: " \
@@ -23,7 +21,7 @@ module Metanorma
23
21
 
24
22
  YAML.safe_load(
25
23
  File.read(resolved_file_path, encoding: "UTF-8"),
26
- permitted_classes: [Date, Time],
24
+ permitted_classes: [Date, Time, Symbol],
27
25
  permitted_symbols: [],
28
26
  aliases: true,
29
27
  )
@@ -32,15 +30,14 @@ module Metanorma
32
30
  def yaml_content_from_anchor(document, anchor)
33
31
  YAML.safe_load(
34
32
  document.attributes["source_blocks"][anchor],
35
- permitted_classes: [Date, Time],
33
+ permitted_classes: [Date, Time, Symbol],
36
34
  permitted_symbols: [],
37
35
  aliases: true,
38
36
  )
39
37
  end
40
38
 
41
- def json_content_from_file(document, file_path)
42
- JSON.parse(File.read(relative_file_path(document, file_path),
43
- encoding: "UTF-8"))
39
+ def json_content_from_file(resolved_file_path)
40
+ JSON.parse(File.read(resolved_file_path, encoding: "UTF-8"))
44
41
  end
45
42
 
46
43
  def json_content_from_anchor(document, anchor)
@@ -48,21 +45,31 @@ module Metanorma
48
45
  end
49
46
 
50
47
  def content_from_file(document, file_path)
51
- if json_file?(file_path)
52
- json_content_from_file(document, file_path)
48
+ resolved_file_path = relative_file_path(document, file_path)
49
+ load_content_from_file(resolved_file_path)
50
+ end
51
+
52
+ def load_content_from_file(resolved_file_path)
53
+ if json_file?(resolved_file_path)
54
+ json_content_from_file(resolved_file_path)
53
55
  else
54
- yaml_content_from_file(document, file_path)
56
+ yaml_content_from_file(resolved_file_path)
55
57
  end
56
58
  end
57
59
 
58
60
  def content_from_anchor(document, anchor)
59
- if json_content?(document.attributes["source_blocks"][anchor])
61
+ source_block = document.attributes["source_blocks"][anchor]
62
+ if json_content?(source_block)
60
63
  json_content_from_anchor(document, anchor)
61
64
  else
62
65
  yaml_content_from_anchor(document, anchor)
63
66
  end
64
67
  end
65
68
 
69
+ def json_or_yaml_filepath?(file_path)
70
+ file_path.end_with?(".json", ".yaml", ".yml")
71
+ end
72
+
66
73
  def json_file?(file_path)
67
74
  file_path.end_with?(".json")
68
75
  end
@@ -1,7 +1,7 @@
1
1
  module Metanorma
2
2
  module Plugin
3
3
  module Datastruct
4
- VERSION = "0.3.4".freeze
4
+ VERSION = "0.3.6".freeze
5
5
  end
6
6
  end
7
7
  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.3.4
4
+ version: 0.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-05-06 00:00:00.000000000 Z
11
+ date: 2025-05-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: asciidoctor
@@ -255,8 +255,7 @@ files:
255
255
  - bin/console
256
256
  - bin/setup
257
257
  - lib/liquid/custom_blocks/key_iterator.rb
258
- - lib/liquid/custom_blocks/with_json_nested_context.rb
259
- - lib/liquid/custom_blocks/with_yaml_nested_context.rb
258
+ - lib/liquid/custom_filters/loadfile.rb
260
259
  - lib/liquid/custom_filters/replace_regex.rb
261
260
  - lib/liquid/custom_filters/values.rb
262
261
  - lib/metanorma-plugin-datastruct.rb
@@ -1,18 +0,0 @@
1
- module Liquid
2
- module CustomBlocks
3
- class WithJsonNestedContext < Block
4
- def initialize(tag_name, markup, tokens)
5
- super
6
- @context_file_variable, @context_name = markup.split(",").map(&:strip)
7
- end
8
-
9
- def render(context)
10
- context_file = context[@context_file_variable].to_s.strip
11
- context[@context_name] = JSON.parse(
12
- File.read(context_file, encoding: "utf-8"),
13
- )
14
- super
15
- end
16
- end
17
- end
18
- end
@@ -1,21 +0,0 @@
1
- module Liquid
2
- module CustomBlocks
3
- class WithYamlNestedContext < Block
4
- def initialize(tag_name, markup, tokens)
5
- super
6
- @context_file_variable, @context_name = markup.split(",").map(&:strip)
7
- end
8
-
9
- def render(context)
10
- context_file = context[@context_file_variable].to_s.strip
11
- context[@context_name] = YAML.safe_load(
12
- File.read(context_file, encoding: "utf-8"),
13
- permitted_classes: [Date, Time],
14
- permitted_symbols: [],
15
- aliases: true,
16
- )
17
- super
18
- end
19
- end
20
- end
21
- end