metanorma-plugin-lutaml 0.4.9 → 0.4.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  {% for klass in classes %}
2
- {% include "packages_data_dictionary_class" %}
2
+ {% include "packages_data_dictionary_class", depth: depth %}
3
3
  {% endfor %}
@@ -4,7 +4,7 @@
4
4
  {% endif %}
5
5
  {% assign klass_name = klass.name | downcase | replace: ':', '' | replace: ' ', '_' %}
6
6
  [[section-{{ klass.xmi_id }}]]
7
- .Elements of {{ package.name }}::{{ klass.name }}” (data_type)
7
+ .Definition table of “{{ package.name }}::{{ klass.name }}” ({{ klass.stereotype }})
8
8
  [width="100%",cols="a,a,a,a,a,a,a,a"]
9
9
  |===
10
10
 
@@ -2,17 +2,33 @@
2
2
  {% assign package_name = package.name | downcase | replace: ":", "" | replace: " ", "_" %}
3
3
  {% if additional_context.before and additional_context.before.size > 0 %}
4
4
  {% for before in additional_context.before %}
5
+ {% if before.liquid %}
6
+ {{ before.text | interpolate }}
7
+ {% else %}
5
8
  {{ before.text }}
9
+ {% endif %}
10
+ {% endfor %}
11
+ {% endif %}
12
+ {% assign before_package_key = 'before;' | append: package.name %}
13
+ {% if additional_context[before_package_key] %}
14
+ {% for before in additional_context[before_package_key] %}
15
+ {% if before.liquid %}
16
+ {{ before.text | interpolate }}
17
+ {% else %}
18
+ {{ before.text }}
19
+ {% endif %}
6
20
  {% endfor %}
7
21
  {% endif %}
8
22
  {% if depth %}
23
+ {% assign nested_depth = depth | plus: 1 %}
24
+ {% capture equalsigns %}{% for count in (1..depth) %}={% endfor %}{% endcapture %}
9
25
  [[rc_{{ package_name }}-model_section]]
10
- {% capture equalsigns %}{% for count in (1..depth) %}={% endfor %}{% endcapture %}{{equalsigns}} {{ package.name }}
26
+ {{equalsigns}} {{ package.name }}
11
27
  {% endif %}
12
28
 
13
- {% if additional_context.all_macroses.size > 0 %}
14
- {% assign sorted_all_macroses = additional_context.all_macroses | where: "position", "before" | sort: 'index' %}
15
- {% for block in sorted_all_macroses %}
29
+ {% if additional_context.all_macros.size > 0 %}
30
+ {% assign sorted_all_macros = additional_context.all_macros | where: "position", "before" | sort: 'index' %}
31
+ {% for block in sorted_all_macros %}
16
32
  {% case block.type %}
17
33
  {% when 'include_block' %}
18
34
  {% unless block.package and block.package != package.name %}
@@ -21,8 +37,12 @@
21
37
  {% unless block_content contains "Liquid error" %}
22
38
  {% if block.text %}
23
39
 
40
+ {% if block.liquid %}
41
+ {{ block.text | interpolate }}
42
+ {% else %}
24
43
  {{ block.text }}
25
44
  {% endif %}
45
+ {% endif %}
26
46
 
27
47
  {{ block_content }}
28
48
  {% endunless %}
@@ -30,14 +50,18 @@
30
50
  {% when 'package_text' %}
31
51
  {% unless block.package and block.package != package.name %}
32
52
 
53
+ {% if block.liquid %}
54
+ {{ block.text | interpolate }}
55
+ {% else %}
33
56
  {{ block.text }}
57
+ {% endif %}
34
58
  {% endunless %}
35
59
  {% else %}
36
60
  {% endcase %}
37
61
  {% endfor %}
38
62
  {% endif %}
39
63
 
40
- // TODO: move to all_macroses block
64
+ {% comment %} // TODO: move to all_macros block {% endcomment %}
41
65
  {% if additional_context.diagram_include_block %}
42
66
  {% for diagram_include_block in additional_context.diagram_include_block %}
43
67
  {% include "diagrams_block", package_name: package_name, image_base_path: diagram_include_block.base_path, text: diagram_include_block.text %}
@@ -45,57 +69,66 @@
45
69
  {% endif %}
46
70
 
47
71
  {% if package.classes.size > 0 %}
72
+ {% unless package_skip_sections[package.name]['classes'] %}
48
73
  {{equalsigns}}= Class definitions
49
74
 
75
+ {% endunless %}
50
76
  {% comment %} {% assign data_types = package.classes | where: "stereotype", "DataType" %} {% endcomment %}
51
77
  {% assign basic_types = package.classes | where: "stereotype", "BasicType" %}
52
78
  {% comment %} {% assign enumerations = package.classes | where: "stereotype", "Enumeration" %} {% endcomment %}
53
79
  {% assign code_lists = package.classes | where: "stereotype", "CodeList" %}
80
+ {% assign unions = package.data_types | where: "stereotype", "Union" %}
54
81
 
55
82
  {% assign not_classes_length = plus: basic_types.size | plus: code_lists.size %}
56
- {% assign not_classes_stereoptypes = 'DataType,BasicType,Enumeration,CodeList' | split: ','%}
57
- {% unless not_classes_length == package.classes.size %}
58
- .Classes used in {{ package.name }}
83
+ {% assign not_classes_stereotypes = 'DataType,BasicType,Enumeration,CodeList' | split: ','%}
84
+ {% unless not_classes_length == package.classes.size or package_skip_sections[package.name]['classes'] %}
85
+ .Classes defined in {{ package.name }} ({{ package.stereotype }})
59
86
  [cols="2a,6a",options="header"]
60
87
  |===
61
88
  |Name |Description
89
+
62
90
  {% for klass in package.classes %}
63
- {% unless not_classes_stereoptypes contains klass.stereotype %}
64
- |<<{{ klass.name }}-section,{{ klass.name }}>> «{{ klass.stereotype }}»
65
- |{{ klass.definition }}
91
+ {% unless not_classes_stereotypes contains klass.stereotype %}
92
+ | <<{{ klass.name }}-section,{{ klass.name }}>> «{{ klass.stereotype }}»
93
+ | {{ klass.definition | replace: '|', '\|' }}
66
94
 
67
95
  {% endunless %}
68
96
  {% endfor %}
69
97
  |===
70
98
  {% endunless %}
71
99
 
72
- {% if package.data_types.size > 0 %}
73
- {% include "packages_entity_list_classes", classes: package.data_types, type: "Data Types" %}
100
+ {% assign non_unions = package.data_types | where: "stereotype", "DataType" %}
101
+ {% if non_unions.size > 0 %}
102
+ {% include "packages_entity_list_classes", classes: non_unions, type: "Data types", depth: nested_depth %}
74
103
  {% endif %}
75
104
 
76
105
  {% if basic_types.size > 0 %}
77
- {% include "packages_entity_list_classes", classes: basic_types, type: "Primitive Data Types" %}
106
+ {% include "packages_entity_list_classes", classes: basic_types, type: "Primitive data types", depth: nested_depth %}
78
107
  {% endif %}
79
108
 
80
109
  {% if package.enums.size > 0 %}
81
- {% include "packages_entity_list_classes", classes: package.enums, type: "Enumerated Classes" %}
110
+ {% include "packages_entity_list_classes", classes: package.enums, type: "Enumerated classes", depth: nested_depth %}
111
+ {% endif %}
112
+
113
+ {% if unions.size > 0 %}
114
+ {% include "packages_entity_list_classes", classes: unions, type: "Union types", depth: nested_depth %}
82
115
  {% endif %}
83
116
 
84
117
  {% if code_lists.size > 0 %}
85
- {% include "packages_entity_list_classes", classes: code_lists, type: "CodeList Classes" %}
118
+ {% include "packages_entity_list_classes", classes: code_lists, type: "Code list classes", depth: nested_depth %}
86
119
  {% endif %}
87
120
 
88
121
  {% endif %}
89
122
 
90
123
  {% comment %} {% if package.data_types.size > 0 %}
91
124
  .Data types used in {{ package.name }}
92
- [cols="2,6",options="header"]
125
+ [cols="2a,6a",options="header"]
93
126
  |===
94
127
  |Name |Description
95
128
 
96
129
  {% for klass in package.data_types %}
97
130
  |<<{{ klass.name }}-section,{{ klass.name }}>> «{{ klass.stereotype }}»
98
- |{{ klass.definition }}
131
+ |{{ klass.definition | replace: '|', '\|' }}
99
132
 
100
133
  {% endfor %}
101
134
 
@@ -110,27 +143,69 @@
110
143
 
111
144
  {% for klass in package.enums %}
112
145
  |<<{{ klass.name }}-section,{{ klass.name }}>> «{{ klass.stereotype }}»
113
- |{{ klass.definition }}
146
+ |{{ klass.definition | replace: '|', '\|' }}
114
147
 
115
148
  {% endfor %}
116
149
 
117
150
  |===
118
151
  {% endif %} {% endcomment %}
119
152
 
153
+ {% if additional_context.all_macros.size > 0 %}
154
+ {% assign sorted_all_macros = additional_context.all_macros | where: "position", "after" | sort: 'index' %}
155
+ {% for block in sorted_all_macros %}
156
+ {% case block.type %}
157
+ {% when 'include_block' %}
158
+ {% unless block.package and block.package != package.name %}
159
+ {% capture block_filename %}{{ block.base_path }}{{ package_name }}{% endcapture %}
160
+ {% capture block_content %}{% include block_filename %}{% endcapture %}
161
+ {% unless block_content contains "Liquid error" %}
162
+ {% if block.text %}
163
+
164
+ {% if block.liquid %}
165
+ {{ block.text | interpolate }}
166
+ {% else %}
167
+ {{ block.text }}
168
+ {% endif %}
169
+ {% endif %}
170
+
171
+ {{ block_content }}
172
+ {% endunless %}
173
+ {% endunless %}
174
+ {% when 'package_text' %}
175
+ {% unless block.package and block.package != package.name %}
176
+
177
+ {% if block.liquid %}
178
+ {{ block.text | interpolate }}
179
+ {% else %}
180
+ {{ block.text }}
181
+ {% endif %}
182
+ {% endunless %}
183
+ {% else %}
184
+ {% endcase %}
185
+ {% endfor %}
186
+ {% endif %}
187
+
120
188
  {% assign after_package_key = 'after;' | append: package.name %}
121
189
  {% if additional_context[after_package_key] %}
122
- {{equalsigns}}= Additional information
123
190
  {% for after in additional_context[after_package_key] %}
191
+ {% if after.liquid %}
192
+ {{ after.text | interpolate }}
193
+ {% else %}
124
194
  {{ after.text }}
195
+ {% endif %}
125
196
  {% endfor %}
126
197
  {% endif %}
127
198
  {% if package.packages.size > 0 and render_nested_packages %}
128
- {% assign nested_depth = depth | plus: 1 %}{% include "packages_entity_list", depth: nested_depth, context: package %}
199
+ {% include "packages_entity_list", depth: nested_depth, context: package %}
129
200
  {% endif %}
130
201
  {% endfor %}
131
202
 
132
203
  {% if additional_context.after and additional_context.after.size > 0 %}
133
204
  {% for after in additional_context.after %}
205
+ {% if after.liquid %}
206
+ {{ after.text | interpolate }}
207
+ {% else %}
134
208
  {{ after.text }}
209
+ {% endif %}
135
210
  {% endfor %}
136
211
  {% endif %}
@@ -0,0 +1,19 @@
1
+ {% for package in context.packages %}
2
+ {% assign not_classes_stereotypes = 'DataType,BasicType,Enumeration,CodeList' | split: ','%}
3
+ [cols="2a,6a",options="header"]
4
+ |===
5
+ |Class |Description
6
+
7
+ {% for klass in package.classes %}
8
+ {% assign klass_name = klass.name %}
9
+ {% assign package_name = package.name %}
10
+ {% unless package_entities and package_entities[package_name][klass_name] != true %}
11
+ {% unless not_classes_stereotypes contains klass.stereotype %}
12
+ | <<{{ klass.name }}-section,{{ klass.name }}>> «{{ klass.stereotype }}»
13
+ | {{ klass.definition | replace: '|', '\|' }}
14
+
15
+ {% endunless %}
16
+ {% endunless %}
17
+ {% endfor %}
18
+ |===
19
+ {% endfor %}
@@ -1,11 +1,11 @@
1
- .{{ type }} used in {{ package.name }}
1
+ .{{ type }} defined in {{ package.name }} ({{ package.stereotype }})
2
2
  [cols="2a,6a",options="header"]
3
3
  |===
4
4
  |Name |Description
5
5
 
6
6
  {% for klass in classes %}
7
- |<<{{ klass.name }}-section,{{ klass.name }}>> «{{ klass.stereotype | default: "Enumeration" }}»
8
- |{{ klass.definition }}
7
+ | <<{{ klass.name }}-section,{{ klass.name }}>> «{{ klass.stereotype | default: "Enumeration" }}»
8
+ | {{ klass.definition | replace: '|', '\|' }}
9
9
 
10
10
  {% endfor %}
11
11
  |===
@@ -5,7 +5,7 @@
5
5
  {% assign klass_name = klass.name | downcase | replace: ':', '' | replace: ' ', '_' %}
6
6
 
7
7
  [[section-{{ klass.xmi_id }}]]
8
- .Elements of {{ package.name }}::{{ klass.name }}&#8221; (enum)
8
+ .Definition table of &#8220;{{ package.name }}::{{ klass.name }}&#8221; ({{ klass.stereotype }})
9
9
  [width="100%",cols="a,a,a,a,a,a,a,a"]
10
10
  |===
11
11
 
@@ -60,5 +60,4 @@ h| Values:
60
60
  7+| (none)
61
61
  {% endif %}
62
62
 
63
-
64
63
  |===
@@ -52,7 +52,7 @@ module Metanorma
52
52
 
53
53
  def process_text_blocks(document, input_lines, express_indexes)
54
54
  line = input_lines.next
55
- block_match = line.match(/^\[lutaml_express,([^,]+)?,?([^,]+)?,?([^,]+)?\]/)
55
+ block_match = line.match(/^\[(?:\blutaml\b|\blutaml_express\b),([^,]+)?,?([^,]+)?,?([^,]+)?\]/)
56
56
  return [line] if block_match.nil?
57
57
 
58
58
  end_mark = input_lines.next
@@ -87,7 +87,7 @@ module Metanorma
87
87
  {% else %}
88
88
  .{{ definition.name }} attributes
89
89
  |===
90
- |Name |Definition |Mandatory/ Optional/ Conditional |Max Occur |Data Type
90
+ |Name |Definition |Mandatory / Optional / Conditional |Max Occur |Data Type
91
91
 
92
92
  {% for item in definition.attributes %}
93
93
  |{{ item.name }} |{% if item.definition %}{{ item.definition }}{% endif %} |{% if item.cardinality.min == "0" %}O{% else %}M{% endif %} |{% if item.cardinality.max == "*" %}N{% else %}1{% endif %} |{% if item.origin %}<<{{ item.origin }}>>{% endif %} `{{ item.type }}`
@@ -24,6 +24,7 @@ module Metanorma
24
24
  RENDER_STYLES_INCLUDES = {
25
25
  'default' => 'packages',
26
26
  'entity_list' => 'packages_entity_list',
27
+ 'entity_list_class' => 'packages_entity_list_class',
27
28
  'data_dictionary' => 'packages_data_dictionary'
28
29
  }.freeze
29
30
  RENDER_STYLE_ATTRIBUTE = 'render_style'.freeze
@@ -39,11 +40,19 @@ module Metanorma
39
40
 
40
41
  private
41
42
 
42
- def lutaml_document_from_file(document, file_path)
43
- ::Lutaml::Parser
44
- .parse(File.new(Utils.relative_file_path(document, file_path),
45
- encoding: "UTF-8"))
46
- .first
43
+ def lutaml_document_from_file_or_cache(document, file_path)
44
+ full_path = Utils.relative_file_path(document, file_path)
45
+ if document.attributes['lutaml_xmi_cache'] &&
46
+ document.attributes['lutaml_xmi_cache'][full_path]
47
+ return document.attributes['lutaml_xmi_cache'][full_path]
48
+ end
49
+ result_document = ::Lutaml::Parser.parse(File.new(full_path,
50
+ encoding: "UTF-8"))
51
+ .first
52
+ result_document
53
+ document.attributes['lutaml_xmi_cache'] ||= {}
54
+ document.attributes['lutaml_xmi_cache'][full_path] = result_document
55
+ result_document
47
56
  end
48
57
 
49
58
  def parse_yaml_config_file(document, file_path)
@@ -66,12 +75,12 @@ module Metanorma
66
75
  block_match = line.match(MARCO_REGEXP)
67
76
  return [line] if block_match.nil?
68
77
 
69
- lutaml_document = lutaml_document_from_file(document, block_match[1])
78
+ lutaml_document = lutaml_document_from_file_or_cache(document, block_match[1])
70
79
  fill_in_diagrams_attributes(document, lutaml_document)
71
80
  model_representation(
72
81
  lutaml_document,
73
82
  document,
74
- collect_additional_context(input_lines, input_lines.next),
83
+ collect_additional_context(document, input_lines, input_lines.next),
75
84
  parse_yaml_config_file(document, block_match[2]))
76
85
  end
77
86
 
@@ -116,14 +125,17 @@ module Metanorma
116
125
  .merge(children_pks_daigs)
117
126
  end
118
127
 
119
- def collect_additional_context(input_lines, end_mark)
128
+ def collect_additional_context(document, input_lines, end_mark)
120
129
  additional_context = Hash.new { |hash, key| hash[key] = [] }
121
- additional_context['all_macroses'] = []
130
+ additional_context['all_macros'] = []
122
131
  block_lines = []
123
132
  while (block_line = input_lines.next) != end_mark
124
133
  block_lines.push(block_line)
125
134
  end
126
- block_document = (Asciidoctor::Document.new(block_lines, {})).parse
135
+ processed_lines = self
136
+ .process(document, Asciidoctor::Reader.new(block_lines))
137
+ .read_lines
138
+ block_document = (Asciidoctor::Document.new(processed_lines, {})).parse
127
139
  block_document.blocks.each do |block|
128
140
  next unless SUPPORTED_NESTED_MACRO.include?(block.attributes['role'])
129
141
 
@@ -133,7 +145,7 @@ module Metanorma
133
145
  macro_keyword = [name, package].compact.join(";")
134
146
  block_text = block.lines.length > 0 ? block.lines.join("\n") : ''
135
147
  additional_context[macro_keyword].push({ 'text' => block_text }.merge(attrs))
136
- additional_context['all_macroses'].push({ 'text' => block_text, 'type' => name, 'package' => package }.merge(attrs))
148
+ additional_context['all_macros'].push({ 'text' => block_text, 'type' => name, 'package' => package }.merge(attrs))
137
149
  end
138
150
  additional_context
139
151
  end
@@ -159,12 +171,30 @@ module Metanorma
159
171
  all_packages = [root_package, *root_package['children_packages']]
160
172
  {
161
173
  "packages" => sort_and_filter_out_packages(all_packages, options),
174
+ "package_entities" => package_entities(options),
175
+ "package_skip_sections" => package_skip_sections(options),
162
176
  "additional_context" => additional_context,
163
177
  "root_packages" => [root_package],
164
178
  "name" => root_package['name']
165
179
  }
166
180
  end
167
181
 
182
+ def package_entities(options)
183
+ return {} unless options['packages']
184
+
185
+ options['packages']
186
+ .find_all { |entity| entity.is_a?(Hash) && entity.values.first['render_entities'] }
187
+ .map { |entity| [entity.keys.first, entity.values.first['render_entities'].map { |n| [n, true] }.to_h] }.to_h
188
+ end
189
+
190
+ def package_skip_sections(options)
191
+ return {} unless options['packages']
192
+
193
+ options['packages']
194
+ .find_all { |entity| entity.is_a?(Hash) && entity.values.first['skip_tables'] }
195
+ .map { |entity| [entity.keys.first, entity.values.first['skip_tables'].map { |n| [n, true] }.to_h] }.to_h
196
+ end
197
+
168
198
  def sort_and_filter_out_packages(all_packages, options)
169
199
  return all_packages if options['packages'].nil?
170
200
 
@@ -179,8 +209,9 @@ module Metanorma
179
209
  end
180
210
  # Step two - select supplied packages by pattern
181
211
  options['packages']
182
- .find_all { |entity| entity.is_a?(String) }
183
- .each do |entity|
212
+ .find_all { |entity| entity.is_a?(String) || (entity.is_a?(Hash) && !entity['skip']) }
213
+ .each do |entity_obj|
214
+ entity = entity_obj.is_a?(String) ? entity_obj : entity_obj.keys.first
184
215
  entity_regexp = config_entity_regexp(entity)
185
216
  all_packages.each.with_index do |package|
186
217
  if package['name'] =~ entity_regexp
@@ -218,11 +249,11 @@ module Metanorma
218
249
  result = ""
219
250
  if include_root
220
251
  result += <<~LIQUID
221
- {% include "#{include_name}", context: context.root_packages, additional_context: context.additional_context, render_nested_packages: false %}
252
+ {% include "#{include_name}", package_skip_sections: context.package_skip_sections, package_entities: context.package_entities, context: context.root_packages, additional_context: context.additional_context, render_nested_packages: false %}
222
253
  LIQUID
223
254
  end
224
255
  result += <<~LIQUID
225
- {% include "#{include_name}", depth: #{section_depth}, context: context, additional_context: context.additional_context, render_nested_packages: context.render_nested_packages %}
256
+ {% include "#{include_name}", depth: #{section_depth}, package_skip_sections: context.package_skip_sections, package_entities: context.package_entities, context: context, additional_context: context.additional_context, render_nested_packages: context.render_nested_packages %}
226
257
  LIQUID
227
258
  end
228
259
  end
@@ -7,7 +7,7 @@ require "metanorma/plugin/lutaml/liquid/multiply_local_file_system"
7
7
  module Metanorma
8
8
  module Plugin
9
9
  module Lutaml
10
- # Helpers for lutaml macroses
10
+ # Helpers for lutaml macros
11
11
  module Utils
12
12
  LUTAML_EXP_IDX_TAG = /^:lutaml-express-index:(?<index_name>.+?);(?<index_path>.+?);?(\s*cache=(?<cache_path>.+))?$/.freeze
13
13