metanorma-plugin-lutaml 0.2.3 → 0.2.4.pre.alpha.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: d91024f06594fbe6e771ce88c50ad1685385dfc43c709d8b3e559ca447f593d7
4
- data.tar.gz: 7945a746eaddac195273299ea4b8f0acb152aafc3896b48ebfc3f26119e2c292
3
+ metadata.gz: 74c485317a97c4ab8244b48e78551570a2fcd3f5946735c918a91a5e522af8cb
4
+ data.tar.gz: de93940324773fafdeb4d841e5fdf509c8499e6fbd0ac426b74747d240ecc6b6
5
5
  SHA512:
6
- metadata.gz: 4b14175e12a83ab2e38e336bd0a71858764caf04db55c47814bdbcfb5560771b7f5cd9f066b90ded162ff8ec4af24172d50290daafeb8fdcc3e26375fcea20fc
7
- data.tar.gz: 0d6f2e23ffcd10ce9a84bb1c4fe10f3190ade34c0ff5b317ad0f25c584ce651d88d212329acaf524942e4f6ed7027e73f57a3b07c8ed0aa443c52a6926b01776
6
+ metadata.gz: 3aff4cfd2d9201b14937c7b6d6764133b2a09309df16b3368a5f373a092b0a28e23b4a2ab30e44f91e498c6a7a21d2c51c462aef2226b3bf598373203c083622
7
+ data.tar.gz: 2d1cc114c2c5f3b49d953c29c88880b80ca13e892095629f99fb9635120a21c7b98cec34dee182f0af1425bbeb7369ab67f398f8dd418e5c2d188bdb15df6182
@@ -1,5 +1,3 @@
1
- # Auto-generated by Cimas: Do not edit it manually!
2
- # See https://github.com/metanorma/cimas
3
1
  name: rake
4
2
 
5
3
  on:
@@ -56,7 +54,7 @@ jobs:
56
54
  polling_interval_seconds: 5
57
55
  timeout_minutes: 5
58
56
  max_attempts: 3
59
- command: choco install --no-progress graphviz --version 2.38.0.20190211
57
+ command: choco install --no-progress graphviz
60
58
 
61
59
  - name: Check dot command
62
60
  if: matrix.os == 'windows-latest'
@@ -65,17 +63,3 @@ jobs:
65
63
 
66
64
  - name: Run specs
67
65
  run: bundle exec rake
68
-
69
- notify:
70
- name: Trigger notify workflow
71
- needs: rake
72
- runs-on: ubuntu-latest
73
- steps:
74
- - name: Trigger notify workflow
75
- uses: Sibz/github-status-action@v1
76
- with:
77
- authToken: ${{ secrets.GITHUB_TOKEN }}
78
- context: 'tests-passed-successfully'
79
- description: 'Tests passed successfully'
80
- state: 'success'
81
- sha: ${{ github.event.pull_request.head.sha || github.sha }}
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  Gemfile.lock
2
2
  .rspec_status
3
- spec/assets/lutaml
3
+ spec/assets/lutaml
4
+ test.err
data/Gemfile CHANGED
@@ -2,4 +2,3 @@ source "https://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in metanorma-plugin-lutaml.gemspec
4
4
  gemspec
5
-
@@ -0,0 +1,77 @@
1
+ module Metanorma
2
+ module Plugin
3
+ module Lutaml
4
+ class ExpressRemarksDecorator
5
+ RELATIVE_PREFIX_MACRO_REGEXP = /^(link|image|video|audio|include)(:+)?(?![^\/:]+:\/\/|[A-Z]:\/|\/)([^:\[]+)(\[.*\])?$/.freeze
6
+
7
+ attr_reader :remark, :options
8
+
9
+ def self.call(remark, options)
10
+ new(remark, options).call
11
+ end
12
+
13
+ def initialize(remark, options)
14
+ @remark = remark
15
+ @options = options
16
+ end
17
+
18
+ def call
19
+ result = remark
20
+ if options["leveloffset"]
21
+ result = process_remark_offsets(result, options["leveloffset"].to_i)
22
+ end
23
+ if options["relative_path_prefix"]
24
+ result = update_relative_paths(result,
25
+ options["relative_path_prefix"])
26
+ end
27
+ result
28
+ end
29
+
30
+ private
31
+
32
+ def update_relative_paths(string, path_prefix)
33
+ string
34
+ .split("\n")
35
+ .map do |line|
36
+ if line.match?(RELATIVE_PREFIX_MACRO_REGEXP)
37
+ prefix_relative_paths(line, path_prefix)
38
+ else
39
+ line
40
+ end
41
+ end
42
+ .join("\n")
43
+ end
44
+
45
+ def prefix_relative_paths(line, path_prefix)
46
+ line.gsub(RELATIVE_PREFIX_MACRO_REGEXP) do |_match|
47
+ prefixed_path = File.join(path_prefix, $3.strip)
48
+ # When we are dealing with arelative path of a template: ../path/to/file we need to transform it into
49
+ # the absolute one because `image::` macro wont understand it other way
50
+ prefixed_path = File.absolute_path(prefixed_path) if prefixed_path.start_with?('../')
51
+ full_path = File.expand_path(prefixed_path)
52
+ "#{$1}#{$2}#{full_path}#{$4}"
53
+ end
54
+ end
55
+
56
+ def process_remark_offsets(string, offset)
57
+ string
58
+ .split("\n")
59
+ .map do |line|
60
+ if line.match?(/^=/)
61
+ set_string_offsets(line, offset)
62
+ else
63
+ line
64
+ end
65
+ end
66
+ .join("\n")
67
+ end
68
+
69
+ def set_string_offsets(string, offset)
70
+ return "#{'=' * offset}#{string}" if offset.positive?
71
+
72
+ string.gsub(/^={#{offset * -1}}/, "")
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -22,7 +22,8 @@ module Metanorma
22
22
  create_listing_block(
23
23
  parent,
24
24
  reader.source,
25
- attrs.reject { |k, v| k == 1 })
25
+ attrs.reject { |k, _v| k == 1 }
26
+ )
26
27
  end
27
28
 
28
29
  def process(parent, reader, attrs)
@@ -32,35 +33,35 @@ module Metanorma
32
33
  through_attrs["target"] = filename
33
34
  through_attrs["title"] = uml_document.caption
34
35
  create_image_block(parent, through_attrs)
35
- rescue => e
36
+ rescue StandardError => e
36
37
  abort(parent, reader, attrs, e.message)
37
38
  end
38
39
 
39
40
  private
40
41
 
41
42
  def lutaml_temp(reader)
42
- temp_file = Tempfile.new(['lutaml', '.lutaml'])
43
+ temp_file = Tempfile.new(["lutaml", ".lutaml"])
43
44
  temp_file.puts(reader.read)
44
45
  temp_file.rewind
45
46
  temp_file
46
47
  end
47
48
 
48
49
  # if no :imagesdir: leave image file in lutaml
49
- def generate_file(parent, reader, uml_document)
50
+ def generate_file(parent, _reader, uml_document)
50
51
  formatter = ::Lutaml::Uml::Formatter::Graphviz.new
51
52
  formatter.type = :png
52
53
 
53
- imagesdir = if parent.document.attr('imagesdir')
54
- File.join(parent.document.attr('imagesdir'), 'lutaml')
54
+ imagesdir = if parent.document.attr("imagesdir")
55
+ File.join(parent.document.attr("imagesdir"), "lutaml")
55
56
  else
56
- 'lutaml'
57
+ "lutaml"
57
58
  end
58
59
  result_path = Utils.relative_file_path(parent.document, imagesdir)
59
60
  result_pathname = Pathname.new(result_path)
60
61
  result_pathname.mkpath
61
- File.writable?(result_pathname) or raise "Destination path #{result_path} not writable for Lutaml!"
62
+ File.writable?(result_pathname) || raise("Destination path #{result_path} not writable for Lutaml!")
62
63
 
63
- outfile = Tempfile.new(['lutaml', '.png'])
64
+ outfile = Tempfile.new(["lutaml", ".png"])
64
65
  outfile.binmode
65
66
  outfile.puts(formatter.format(uml_document))
66
67
 
@@ -74,11 +75,11 @@ module Metanorma
74
75
  end
75
76
 
76
77
  def generate_attrs(attrs)
77
- through_attrs = %w(id align float title role width height alt).
78
- inject({}) do |memo, key|
79
- memo[key] = attrs[key] if attrs.has_key? key
80
- memo
81
- end
78
+ %w(id align float title role width height alt)
79
+ .reduce({}) do |memo, key|
80
+ memo[key] = attrs[key] if attrs.has_key? key
81
+ memo
82
+ end
82
83
  end
83
84
  end
84
85
  end
@@ -5,39 +5,53 @@ require "asciidoctor"
5
5
  require "asciidoctor/reader"
6
6
  require "lutaml"
7
7
  require "metanorma/plugin/lutaml/utils"
8
+ require "metanorma/plugin/lutaml/utils"
9
+ require "metanorma/plugin/lutaml/express_remarks_decorator"
8
10
 
9
11
  module Metanorma
10
12
  module Plugin
11
13
  module Lutaml
12
14
  # Class for processing Lutaml files
13
15
  class LutamlPreprocessor < Asciidoctor::Extensions::Preprocessor
16
+ REMARKS_ATTRIBUTE = "remarks".freeze
14
17
 
15
18
  def process(document, reader)
16
19
  input_lines = reader.readlines.to_enum
17
- Asciidoctor::Reader.new(processed_lines(document, input_lines))
20
+ express_indexes = Utils.parse_document_express_indexes(
21
+ document,
22
+ input_lines
23
+ )
24
+ Asciidoctor::Reader
25
+ .new(processed_lines(document, input_lines, express_indexes))
18
26
  end
19
27
 
20
28
  protected
21
29
 
22
- def content_from_file(document, file_path)
23
- ::Lutaml::Parser
24
- .parse(File.new(Utils.relative_file_path(document, file_path),
25
- encoding: "UTF-8"))
30
+ def content_from_files(document, file_paths)
31
+ file_list = file_paths.map do |file_path|
32
+ File.new(Utils.relative_file_path(document, file_path), encoding: "UTF-8")
33
+ end
34
+ ::Lutaml::Parser.parse(file_list)
26
35
  end
27
36
 
28
37
  private
29
38
 
30
- def processed_lines(document, input_lines)
39
+ def processed_lines(document, input_lines, express_indexes)
31
40
  result = []
32
41
  loop do
33
- result.push(*process_text_blocks(document, input_lines))
42
+ result
43
+ .push(*process_text_blocks(
44
+ document,
45
+ input_lines,
46
+ express_indexes
47
+ ))
34
48
  end
35
49
  result
36
50
  end
37
51
 
38
- def process_text_blocks(document, input_lines)
52
+ def process_text_blocks(document, input_lines, express_indexes)
39
53
  line = input_lines.next
40
- block_match = line.match(/^\[lutaml,(.+?),(.+?)\]/)
54
+ block_match = line.match(/^\[lutaml,([^,]+)?,?([^,]+)?,?([^,]+)?\]/)
41
55
  return [line] if block_match.nil?
42
56
 
43
57
  end_mark = input_lines.next
@@ -45,7 +59,8 @@ module Metanorma
45
59
  collect_internal_block_lines(document,
46
60
  input_lines,
47
61
  end_mark),
48
- block_match)
62
+ block_match,
63
+ express_indexes)
49
64
  end
50
65
 
51
66
  def collect_internal_block_lines(_document, input_lines, end_mark)
@@ -56,17 +71,70 @@ module Metanorma
56
71
  current_block
57
72
  end
58
73
 
59
- def parse_template(document, current_block, block_match)
60
- context_items = content_from_file(document, block_match[1])
61
- parse_context_block(document: document,
62
- context_lines: current_block,
63
- context_items: context_items,
64
- context_name: block_match[2].strip)
65
- rescue StandardError => e
66
- document.logger
67
- .warn("Failed to parse lutaml \
68
- block: #{e.message}")
69
- []
74
+ def contexts_items(block_match, document, express_indexes)
75
+ contexts_names = block_match[1].split(";").map(&:strip)
76
+ file_paths = []
77
+ result = contexts_names.each_with_object([]) do |path, res|
78
+ if express_indexes[path]
79
+ res.push(*express_indexes[path])
80
+ else
81
+ file_paths.push(path)
82
+ end
83
+ end
84
+ if !file_paths.empty?
85
+ from_files = content_from_files(document, file_paths)
86
+ .map do |n|
87
+ n.to_liquid.map { |j| j.merge('relative_path_prefix' => Utils.relative_file_path(document, File.dirname(j['file']))) }
88
+ end
89
+ .flatten
90
+ result += from_files
91
+ end
92
+ result
93
+ end
94
+
95
+ def parse_template(document, current_block, block_match, express_indexes)
96
+ options = parse_options(block_match[3])
97
+ contexts_items(block_match, document, express_indexes)
98
+ .map do |items|
99
+ opts = options.merge('relative_path_prefix' => items['relative_path_prefix'])
100
+ parse_context_block(document: document,
101
+ context_lines: current_block,
102
+ context_items: decorate_context_items(items, opts),
103
+ context_name: block_match[2].strip)
104
+ end.flatten
105
+ # rescue StandardError => e
106
+ # document.logger.warn("Failed to parse lutaml block: #{e.message}")
107
+ # []
108
+ end
109
+
110
+ def parse_options(options_string)
111
+ options_string
112
+ .to_s
113
+ .scan(/(.+?)=(\s?[^\s]+)/)
114
+ .map { |elem| elem.map(&:strip) }
115
+ .to_h
116
+ end
117
+
118
+ def decorate_context_items(context_items, options)
119
+ return context_items if !context_items.is_a?(Hash)
120
+
121
+ context_items
122
+ .map do |(key, val)|
123
+ if val.is_a?(Hash)
124
+ [key, decorate_context_items(val, options)]
125
+ elsif key == REMARKS_ATTRIBUTE
126
+ [key,
127
+ val&.map do |remark|
128
+ Metanorma::Plugin::Lutaml::ExpressRemarksDecorator
129
+ .call(remark, options)
130
+ end]
131
+ elsif val.is_a?(Array)
132
+ [key, val.map { |n| decorate_context_items(n, options) }]
133
+ else
134
+ [key, val]
135
+ end
136
+ end
137
+ .to_h
70
138
  end
71
139
 
72
140
  def parse_context_block(context_lines:,
@@ -29,6 +29,7 @@ module Metanorma
29
29
  ::Lutaml::Parser
30
30
  .parse(File.new(Utils.relative_file_path(document, file_path),
31
31
  encoding: "UTF-8"))
32
+ .first
32
33
  end
33
34
 
34
35
  def processed_lines(document, input_lines)
@@ -45,12 +46,12 @@ module Metanorma
45
46
 
46
47
  def parse_marco(lutaml_path, entity_name, document)
47
48
  lutaml_document = lutaml_document_from_file(document, lutaml_path)
48
- .serialized_document
49
- entities = [lutaml_document['classes'], lutaml_document['enums']]
50
- .compact
51
- .flatten
52
- entity_definition = entities.find do |klass|
53
- klass['name'] == entity_name.strip
49
+ .serialized_document
50
+ entities = [lutaml_document["classes"], lutaml_document["enums"]]
51
+ .compact
52
+ .flatten
53
+ entity_definition = entities.detect do |klass|
54
+ klass["name"] == entity_name.strip
54
55
  end
55
56
  model_representation(entity_definition, document)
56
57
  end
@@ -59,12 +60,13 @@ module Metanorma
59
60
  render_result, errors = Utils.render_liquid_string(
60
61
  template_string: table_template,
61
62
  context_items: entity_definition,
62
- context_name: 'definition'
63
+ context_name: "definition"
63
64
  )
64
65
  Utils.notify_render_errors(document, errors)
65
66
  render_result.split("\n")
66
67
  end
67
68
 
69
+ # rubocop:disable Layout/IndentHeredoc
68
70
  def table_template
69
71
  <<~TEMPLATE
70
72
  === {{ definition.name }}
@@ -94,6 +96,7 @@ module Metanorma
94
96
 
95
97
  TEMPLATE
96
98
  end
99
+ # rubocop:enable Layout/IndentHeredoc
97
100
  end
98
101
  end
99
102
  end
@@ -31,7 +31,46 @@ module Metanorma
31
31
  .warn("Liquid render error: #{error_obj.message}")
32
32
  end
33
33
  end
34
+
35
+ def process_express_index(folder, name, idxs, document)
36
+ idxs[name] = []
37
+ Dir["#{Utils.relative_file_path(document, folder)}/*"].each do |path|
38
+ next if [".", ".."].include?(path)
39
+
40
+ begin
41
+ parsed = ::Lutaml::Parser.parse(File.new(path, encoding: "UTF-8"))
42
+ parsed = parsed.map { |n| n.to_liquid.map { |j| j.merge('relative_path_prefix' => Utils.relative_file_path(document, File.dirname(j['file']))) } }.flatten
43
+ idxs[name]
44
+ .push(*parsed)
45
+ rescue StandardError => e
46
+ document.logger.warn("Failed to load #{path}: #{e.message}")
47
+ end
48
+ end
49
+ end
50
+
51
+ def parse_document_express_indexes(document, input_lines)
52
+ express_indexes = {}
53
+ loop do
54
+ line = input_lines.next
55
+ break if line.length.zero?
56
+
57
+ _, name, folder = line.match(/^:lutaml-express-index:(.+?);(.+?)$/)&.to_a
58
+ if folder && name
59
+ process_express_index(
60
+ folder.strip.gsub(";", ""),
61
+ name.strip,
62
+ express_indexes,
63
+ document
64
+ )
65
+ end
66
+ end
67
+ express_indexes
68
+ rescue StopIteration
69
+ express_indexes
70
+ ensure
71
+ input_lines.rewind
72
+ end
34
73
  end
35
74
  end
36
75
  end
37
- end
76
+ end
@@ -1,7 +1,7 @@
1
1
  module Metanorma
2
2
  module Plugin
3
3
  module Lutaml
4
- VERSION = "0.2.3".freeze
4
+ VERSION = "0.2.4-alpha.4".freeze
5
5
  end
6
6
  end
7
7
  end
@@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
24
24
  spec.require_paths = ["lib"]
25
25
 
26
26
  spec.add_dependency "liquid"
27
- spec.add_dependency "lutaml", "~> 0.3.0"
27
+ spec.add_dependency "lutaml", "0.4.1.pre.alpha.2"
28
28
  spec.add_dependency "lutaml-uml", "~> 0.2.0"
29
29
  spec.add_dependency "metanorma"
30
30
  spec.add_dependency "relaton-cli"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metanorma-plugin-lutaml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4.pre.alpha.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: 2020-11-29 00:00:00.000000000 Z
11
+ date: 2021-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: liquid
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: lutaml
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - '='
32
32
  - !ruby/object:Gem::Version
33
- version: 0.3.0
33
+ version: 0.4.1.pre.alpha.2
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: 0.3.0
40
+ version: 0.4.1.pre.alpha.2
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: lutaml-uml
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -251,6 +251,7 @@ files:
251
251
  - bin/console
252
252
  - bin/setup
253
253
  - lib/metanorma-plugin-lutaml.rb
254
+ - lib/metanorma/plugin/lutaml/express_remarks_decorator.rb
254
255
  - lib/metanorma/plugin/lutaml/lutaml_diagram_block.rb
255
256
  - lib/metanorma/plugin/lutaml/lutaml_preprocessor.rb
256
257
  - lib/metanorma/plugin/lutaml/lutaml_uml_attributes_table_preprocessor.rb
@@ -273,11 +274,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
273
274
  version: '0'
274
275
  required_rubygems_version: !ruby/object:Gem::Requirement
275
276
  requirements:
276
- - - ">="
277
+ - - ">"
277
278
  - !ruby/object:Gem::Version
278
- version: '0'
279
+ version: 1.3.1
279
280
  requirements: []
280
- rubygems_version: 3.0.6
281
+ rubygems_version: 3.0.3
281
282
  signing_key:
282
283
  specification_version: 4
283
284
  summary: Metanorma plugin for LutaML