metanorma-plugin-datastruct 0.3.4 → 0.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ed19e5b25fb509b816c567e7e274739ce9dd2e734814ccf77c3e68f1e45775ad
4
- data.tar.gz: b57e630d156f1f604d370842cd26431e63ed68a27ec58f4cd89ca78409110372
3
+ metadata.gz: c8ea0d4bb0e67f7932f2f1e1a9e4753c55fe2e42f0dfd1f9e25711f3769dcd74
4
+ data.tar.gz: c43066c046874fa618e06b8fd62ada97ee665e795ea9a87a62fb89642796577f
5
5
  SHA512:
6
- metadata.gz: 4212768404a5a67011682cce9aadac40453dadfc34d63cf69c07fd888789a817793c1c9e0a2b16c582f6a7015f7cd58046e3a5583d992b4072912c452bd5a4e0
7
- data.tar.gz: feb6aa83d2fca789fa7044dea3579f80d2c9b8e16b989fd4c3369764c048eea1d7427b36fdb622eeadc657e45aaf6e6931e065582805101735134259aa04db59
6
+ metadata.gz: 2dfda2e9c51df6408b037dee8a07e17f6a31e51607e35932fb69a81d8a2efa7c986a847de281e06927d512d0d5322ae5e7b4de8e59f429901918ccbb36b7612c
7
+ data.tar.gz: cff48e717964db88996408c9a347f9f795dc29a3d2ed6a2b26969801f81380f35780be1a1d7e0136f8d67ab0c59071639fc15a555e87ed1e7a2df427636e9697
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
@@ -93,23 +87,16 @@ module Metanorma
93
87
 
94
88
  def collect_internal_block_lines(document, input_lines, end_mark) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
95
89
  current_block = []
96
- nested_marks = []
97
90
  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
91
+ if block_line.match?(LOAD_FILE_REGEXP)
92
+ load_file_match = block_line.match(LOAD_FILE_REGEXP)
106
93
 
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)
94
+ # Add parent folder as argument to loadfile filter
95
+ block_line = "{% assign #{load_file_match[1]} = "\
96
+ "#{load_file_match[2]} | loadfile: " \
97
+ "\"#{document.attributes['docdir']}\" %}"
112
98
  end
99
+
113
100
  current_block.push(block_line)
114
101
  end
115
102
  current_block
@@ -119,16 +106,6 @@ module Metanorma
119
106
  @config[:block_name].split("2").first
120
107
  end
121
108
 
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
109
  def parse_template(document, current_block, block_match) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
133
110
  transformed_liquid_lines = current_block.map do |x|
134
111
  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.5".freeze
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
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.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
@@ -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