metanorma-plugin-lutaml 0.2.3 → 0.2.4.alpha

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: c060a247f01300f4fe46b58671a07a8f0e3a80f53e64974ccd07208e181e0eaa
4
+ data.tar.gz: dcf58ee6f298d24ad2493bd65c62a549a2673717ce8a9404695caad20d32e069
5
5
  SHA512:
6
- metadata.gz: 4b14175e12a83ab2e38e336bd0a71858764caf04db55c47814bdbcfb5560771b7f5cd9f066b90ded162ff8ec4af24172d50290daafeb8fdcc3e26375fcea20fc
7
- data.tar.gz: 0d6f2e23ffcd10ce9a84bb1c4fe10f3190ade34c0ff5b317ad0f25c584ce651d88d212329acaf524942e4f6ed7027e73f57a3b07c8ed0aa443c52a6926b01776
6
+ metadata.gz: 5061cc833de5a8d4742095e5491f66ab608bdac3042ecc601cdc26e89f5ff258aa34650b3b19e3eb3b38b50be01b3f866e18f15adda02000278e6a5864cf1b34
7
+ data.tar.gz: 3be917c68c0fd6b0157f6578e40f61da433b262629950f76d09b2b2cf9e4cc5f7955d9066a0930f337ec7beb9426790d650bfb632cf9abf570f3431360855ca6
@@ -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,73 @@
1
+ module Metanorma
2
+ module Plugin
3
+ module Lutaml
4
+ class ExpressRemarksDecorator
5
+ RELATIVE_PREFIX_MACRO_REGEXP = /^(\* <<express.+?>>;|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
+ "#{$1}#{$2}#{prefixed_path}#{$4}"
49
+ end
50
+ end
51
+
52
+ def process_remark_offsets(string, offset)
53
+ string
54
+ .split("\n")
55
+ .map do |line|
56
+ if line.match?(/^=/)
57
+ set_string_offsets(line, offset)
58
+ else
59
+ line
60
+ end
61
+ end
62
+ .join("\n")
63
+ end
64
+
65
+ def set_string_offsets(string, offset)
66
+ return "#{'=' * offset}#{string}" if offset.positive?
67
+
68
+ string.gsub(/^={#{offset * -1}}/, "")
69
+ end
70
+ end
71
+ end
72
+ end
73
+ 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,16 +5,24 @@ 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
@@ -27,17 +35,22 @@ module Metanorma
27
35
 
28
36
  private
29
37
 
30
- def processed_lines(document, input_lines)
38
+ def processed_lines(document, input_lines, express_indexes)
31
39
  result = []
32
40
  loop do
33
- result.push(*process_text_blocks(document, input_lines))
41
+ result
42
+ .push(*process_text_blocks(
43
+ document,
44
+ input_lines,
45
+ express_indexes
46
+ ))
34
47
  end
35
48
  result
36
49
  end
37
50
 
38
- def process_text_blocks(document, input_lines)
51
+ def process_text_blocks(document, input_lines, express_indexes)
39
52
  line = input_lines.next
40
- block_match = line.match(/^\[lutaml,(.+?),(.+?)\]/)
53
+ block_match = line.match(/^\[lutaml,([^,]+)?,?([^,]+)?,?([^,]+)?\]/)
41
54
  return [line] if block_match.nil?
42
55
 
43
56
  end_mark = input_lines.next
@@ -45,7 +58,8 @@ module Metanorma
45
58
  collect_internal_block_lines(document,
46
59
  input_lines,
47
60
  end_mark),
48
- block_match)
61
+ block_match,
62
+ express_indexes)
49
63
  end
50
64
 
51
65
  def collect_internal_block_lines(_document, input_lines, end_mark)
@@ -56,19 +70,62 @@ module Metanorma
56
70
  current_block
57
71
  end
58
72
 
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)
73
+ def contexts_items(block_match, document, express_indexes)
74
+ contexts_names = block_match[1].split(";").map(&:strip)
75
+ contexts_names.each_with_object([]) do |path, res|
76
+ if express_indexes[path]
77
+ res.push(*express_indexes[path])
78
+ else
79
+ res.push(content_from_file(document, path).to_liquid)
80
+ end
81
+ end
82
+ end
83
+
84
+ def parse_template(document, current_block, block_match, express_indexes)
85
+ options = parse_options(block_match[3])
86
+ .merge("relative_path_prefix" => File.dirname(block_match[1]))
87
+ contexts_items(block_match, document, express_indexes)
88
+ .map do |context_items|
89
+ parse_context_block(document: document,
90
+ context_lines: current_block,
91
+ context_items: decorate_context_items(context_items, options),
92
+ context_name: block_match[2].strip)
93
+ end.flatten
65
94
  rescue StandardError => e
66
- document.logger
67
- .warn("Failed to parse lutaml \
68
- block: #{e.message}")
95
+ document.logger.warn("Failed to parse lutaml block: #{e.message}")
69
96
  []
70
97
  end
71
98
 
99
+ def parse_options(options_string)
100
+ options_string
101
+ .to_s
102
+ .scan(/(.+?)=(\s?[^\s]+)/)
103
+ .map { |elem| elem.map(&:strip) }
104
+ .to_h
105
+ end
106
+
107
+ def decorate_context_items(context_items, options)
108
+ return context_items if !context_items.is_a?(Hash)
109
+
110
+ context_items
111
+ .map do |(key, val)|
112
+ if val.is_a?(Hash)
113
+ [key, decorate_context_items(val, options)]
114
+ elsif key == REMARKS_ATTRIBUTE
115
+ [key,
116
+ val&.map do |remark|
117
+ Metanorma::Plugin::Lutaml::ExpressRemarksDecorator
118
+ .call(remark, options)
119
+ end]
120
+ elsif val.is_a?(Array)
121
+ [key, val.map { |n| decorate_context_items(n, options) }]
122
+ else
123
+ [key, val]
124
+ end
125
+ end
126
+ .to_h
127
+ end
128
+
72
129
  def parse_context_block(context_lines:,
73
130
  context_items:,
74
131
  context_name:,
@@ -45,12 +45,12 @@ module Metanorma
45
45
 
46
46
  def parse_marco(lutaml_path, entity_name, document)
47
47
  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
48
+ .serialized_document
49
+ entities = [lutaml_document["classes"], lutaml_document["enums"]]
50
+ .compact
51
+ .flatten
52
+ entity_definition = entities.detect do |klass|
53
+ klass["name"] == entity_name.strip
54
54
  end
55
55
  model_representation(entity_definition, document)
56
56
  end
@@ -59,12 +59,13 @@ module Metanorma
59
59
  render_result, errors = Utils.render_liquid_string(
60
60
  template_string: table_template,
61
61
  context_items: entity_definition,
62
- context_name: 'definition'
62
+ context_name: "definition"
63
63
  )
64
64
  Utils.notify_render_errors(document, errors)
65
65
  render_result.split("\n")
66
66
  end
67
67
 
68
+ # rubocop:disable Layout/IndentHeredoc
68
69
  def table_template
69
70
  <<~TEMPLATE
70
71
  === {{ definition.name }}
@@ -94,6 +95,7 @@ module Metanorma
94
95
 
95
96
  TEMPLATE
96
97
  end
98
+ # rubocop:enable Layout/IndentHeredoc
97
99
  end
98
100
  end
99
101
  end
@@ -31,7 +31,44 @@ 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
+ idxs[name]
42
+ .push(::Lutaml::Parser.parse(File.new(path, encoding: "UTF-8")).to_liquid)
43
+ rescue StandardError => e
44
+ document.logger.warn("Failed to load #{path}: #{e.message}")
45
+ end
46
+ end
47
+ end
48
+
49
+ def parse_document_express_indexes(document, input_lines)
50
+ express_indexes = {}
51
+ loop do
52
+ line = input_lines.next
53
+ break if line.length.zero?
54
+
55
+ _, name, folder = line.match(/^:lutaml-express-index:(.+?);(.+?)$/)&.to_a
56
+ if folder && name
57
+ process_express_index(
58
+ folder.strip.gsub(";", ""),
59
+ name.strip,
60
+ express_indexes,
61
+ document
62
+ )
63
+ end
64
+ end
65
+ express_indexes
66
+ rescue StopIteration
67
+ express_indexes
68
+ ensure
69
+ input_lines.rewind
70
+ end
34
71
  end
35
72
  end
36
73
  end
37
- end
74
+ 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".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"
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.alpha
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-02-09 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
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
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