metanorma-standoc 3.1.3 → 3.1.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.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/lib/metanorma/standoc/biblio-standoc.rng +0 -49
  3. data/lib/metanorma/standoc/biblio.rng +11 -1
  4. data/lib/metanorma/standoc/blocks_notes.rb +1 -0
  5. data/lib/metanorma/standoc/cleanup.rb +4 -1
  6. data/lib/metanorma/standoc/cleanup_bibdata.rb +16 -1
  7. data/lib/metanorma/standoc/cleanup_boilerplate.rb +62 -16
  8. data/lib/metanorma/standoc/cleanup_maths.rb +2 -1
  9. data/lib/metanorma/standoc/cleanup_section_names.rb +8 -8
  10. data/lib/metanorma/standoc/converter.rb +2 -3
  11. data/lib/metanorma/standoc/front.rb +0 -8
  12. data/lib/metanorma/standoc/front_committee.rb +140 -0
  13. data/lib/metanorma/standoc/front_contributor.rb +14 -3
  14. data/lib/metanorma/standoc/front_organisation.rb +24 -18
  15. data/lib/metanorma/standoc/isodoc.rng +24 -1
  16. data/lib/metanorma/standoc/macros.rb +1 -3
  17. data/lib/metanorma/standoc/ref_queue.rb +1 -1
  18. data/lib/metanorma/standoc/ref_utility.rb +27 -2
  19. data/lib/metanorma/standoc/regex.rb +6 -5
  20. data/lib/metanorma/standoc/sectiontype.rb +1 -0
  21. data/lib/metanorma/standoc/utils.rb +11 -1
  22. data/lib/metanorma/standoc/validate.rb +1 -1
  23. data/lib/metanorma/standoc/validate_section.rb +12 -2
  24. data/lib/metanorma/standoc/validate_table.rb +3 -1
  25. data/lib/metanorma/standoc/version.rb +1 -1
  26. data/metanorma-standoc.gemspec +1 -0
  27. metadata +17 -12
  28. data/lib/asciidoctor/standoc/datamodel/attributes_table_preprocessor.rb +0 -3
  29. data/lib/asciidoctor/standoc/datamodel/diagram_preprocessor.rb +0 -3
  30. data/lib/asciidoctor/standoc/datamodel/plantuml_renderer.rb +0 -8
  31. data/lib/asciidoctor/standoc/macros_plantuml.rb +0 -2
  32. data/lib/metanorma/standoc/datamodel/attributes_table_preprocessor.rb +0 -57
  33. data/lib/metanorma/standoc/datamodel/diagram_preprocessor.rb +0 -103
  34. data/lib/metanorma/standoc/datamodel/plantuml_renderer.rb +0 -409
  35. data/lib/metanorma/standoc/macros_plantuml.rb +0 -143
  36. data/lib/metanorma/standoc/views/datamodel/model_representation.adoc.erb +0 -30
  37. data/lib/metanorma/standoc/views/datamodel/plantuml_representation.adoc.erb +0 -20
@@ -1,6 +1,6 @@
1
1
  <?xml version="1.0" encoding="UTF-8"?>
2
2
  <grammar xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0" xmlns="http://relaxng.org/ns/structure/1.0" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
3
- <!-- VERSION v2.1.0 -->
3
+ <!-- VERSION v2.1.2 -->
4
4
 
5
5
  <!--
6
6
  ALERT: cannot have root comments, because of https://github.com/metanorma/metanorma/issues/437
@@ -123,6 +123,10 @@ the type attribute defaults to `review` for reviews</a:documentation>
123
123
  <a:documentation>Notes whose scope is the current block</a:documentation>
124
124
  </ref>
125
125
  </zeroOrMore>
126
+ <ref name="BlockSource">
127
+ <a:documentation>Bibliographic source for the information in the list.
128
+ Sources are currently only rendered in metanorma-plateau</a:documentation>
129
+ </ref>
126
130
  </define>
127
131
  <define name="OlBody">
128
132
  <optional>
@@ -140,6 +144,10 @@ the type attribute defaults to `review` for reviews</a:documentation>
140
144
  <a:documentation>Notes whose scope is the current block</a:documentation>
141
145
  </ref>
142
146
  </zeroOrMore>
147
+ <ref name="BlockSource">
148
+ <a:documentation>Bibliographic source for the information in the list.
149
+ Sources are currently only rendered in metanorma-plateau</a:documentation>
150
+ </ref>
143
151
  </define>
144
152
  <define name="OlAttributes">
145
153
  <a:documentation>NOTE: `start` attribute is not included by default, because of problems it raises with DOC output</a:documentation>
@@ -188,6 +196,10 @@ the type attribute defaults to `review` for reviews</a:documentation>
188
196
  <a:documentation>Notes whose scope is the current block</a:documentation>
189
197
  </ref>
190
198
  </zeroOrMore>
199
+ <ref name="BlockSource">
200
+ <a:documentation>Bibliographic source for the information in the list.
201
+ Sources are currently only rendered in metanorma-plateau</a:documentation>
202
+ </ref>
191
203
  </define>
192
204
  <define name="dt">
193
205
  <element name="dt">
@@ -871,6 +883,12 @@ titlecase, or lowercase</a:documentation>
871
883
  </oneOrMore>
872
884
  </element>
873
885
  </define>
886
+ <define name="ParagraphFnBody" combine="interleave">
887
+ <ref name="BlockSource">
888
+ <a:documentation>Bibliographic source for the information in the paragraph
889
+ parargaph sources are currently only rendered in metanorma-plateau</a:documentation>
890
+ </ref>
891
+ </define>
874
892
  <define name="BasicBlock" combine="choice">
875
893
  <ref name="columnbreak"/>
876
894
  </define>
@@ -2233,6 +2251,11 @@ used in document amendments</a:documentation>
2233
2251
  <ref name="RequiredId"/>
2234
2252
  <ref name="NumberingAttributes"/>
2235
2253
  <ref name="BlockAttributes"/>
2254
+ <optional>
2255
+ <attribute name="type">
2256
+ <a:documentation>Semantic classification of note</a:documentation>
2257
+ </attribute>
2258
+ </optional>
2236
2259
  <oneOrMore>
2237
2260
  <choice>
2238
2261
  <a:documentation>Content of the term note</a:documentation>
@@ -2,16 +2,14 @@ require "uuidtools"
2
2
  require "yaml"
3
3
  require "csv"
4
4
  require_relative "macros_inline"
5
- require_relative "macros_plantuml"
6
5
  require_relative "macros_terms"
7
6
  require_relative "macros_form"
8
7
  require_relative "macros_note"
9
8
  require_relative "macros_embed"
10
9
  require_relative "macros_link"
11
- require_relative "datamodel/attributes_table_preprocessor"
12
- require_relative "datamodel/diagram_preprocessor"
13
10
  require "metanorma-plugin-glossarist"
14
11
  require "metanorma-plugin-lutaml"
12
+ require "metanorma-plugin-plantuml"
15
13
 
16
14
  module Metanorma
17
15
  module Standoc
@@ -131,7 +131,7 @@ module Metanorma
131
131
 
132
132
  def fetch_ref(xml, code, year, **opts)
133
133
  opts[:no_year] and return nil
134
- code = code.sub(/^\([^)]+\)/, "")
134
+ _, code = extract_balanced_parentheses(code)
135
135
  hit = fetch_ref1(code, year, opts) or return nil
136
136
  xml.parent.add_child(smart_render_xml(hit, code, opts))
137
137
  xml
@@ -42,7 +42,7 @@ module Metanorma
42
42
  end
43
43
 
44
44
  def docid(bib, code, codetype = nil)
45
- type, code1 = if /^\[\d+\]$|^\([^)]+\).*$/.match?(code)
45
+ type, code1 = if /^\[\d+\]$|^\(.+\).*$/.match?(code)
46
46
  ["metanorma", mn_code(code)]
47
47
  elsif %w(attachment repo path).include?(codetype)
48
48
  [nil, code]
@@ -62,7 +62,10 @@ module Metanorma
62
62
  end
63
63
 
64
64
  def mn_code(code)
65
- code.sub(/^\(/, "[").sub(/^([^)]+)\).*$/, "\\1]")
65
+ # Handle balanced parentheses at the start of the string
66
+ balance, remainder = extract_balanced_parentheses(code)
67
+ balance and return "[#{balance}]"
68
+ remainder
66
69
  .sub(/^dropid\((.+)\)$/, "\\1")
67
70
  .sub(/^hidden\((.+)\)$/, "\\1")
68
71
  .sub(/^nofetch\((.+)\)$/, "\\1")
@@ -218,6 +221,28 @@ module Metanorma
218
221
  @type = 'DOI' or @type = 'doi' or @type = 'ISSN' or @type = 'issn' or @type = 'ISBN' or @type = 'isbn' or starts-with(@type, 'ISSN.') or starts-with(@type, 'ISBN.') or starts-with(@type, 'issn.') or starts-with(@type, 'isbn.')
219
222
  XPATH
220
223
  end
224
+
225
+ private
226
+
227
+ def extract_balanced_parentheses(code)
228
+ code.start_with?("(") or return [nil, code]
229
+ paren_count = 0
230
+ # Find the matching closing parenthesis
231
+ code.each_char.with_index do |char, index|
232
+ case char
233
+ when "(" then paren_count += 1
234
+ when ")"
235
+ paren_count -= 1
236
+ paren_count.zero? or next
237
+ # Found the matching closing parenthesis
238
+ content = code[1...index] # Extract content between parentheses
239
+ remaining = code[(index + 1)..] || "" # Get remaining string
240
+ return [content, remaining]
241
+ end
242
+ end
243
+ # If we get here, parentheses are unbalanced - return original
244
+ [nil, code]
245
+ end
221
246
  end
222
247
  end
223
248
  end
@@ -52,27 +52,28 @@ module Metanorma
52
52
 
53
53
  ISO_REF =
54
54
  %r{^<ref\sid="(?<anchor>[^"]+)">
55
- \[(?<usrlbl>\([^)]+\))?(?<code>(?:ISO|IEC)[^0-9]*\s[0-9-]+|IEV)
55
+ \[(?<usrlbl>\(.+\))?(?<code>(?:ISO|IEC)[^0-9]*\s[0-9-]+|IEV)
56
56
  (?::(?<year>[0-9][0-9-]+))?\]</ref>,?\s*(?<text>.*)$}xm
57
57
 
58
58
  ISO_REF_NO_YEAR =
59
59
  %r{^<ref\sid="(?<anchor>[^"]+)">
60
- \[(?<usrlbl>\([^)]+\))?(?<code>(?:ISO|IEC)[^0-9]*\s[0-9-]+):
60
+ \[(?<usrlbl>\(.+\))?(?<code>(?:ISO|IEC)[^0-9]*\s[0-9-]+):
61
61
  (?:--|–|—|&\#821[12];)\]</ref>,?\s*
62
62
  (?:<fn[^>]*>\s*<p>(?<fn>[^\]]+)</p>\s*</fn>)?,?\s?(?<text>.*)$}xm
63
63
 
64
64
  ISO_REF_ALL_PARTS =
65
65
  %r{^<ref\sid="(?<anchor>[^"]+)">
66
- \[(?<usrlbl>\([^)]+\))?(?<code>(?:ISO|IEC)[^0-9]*\s[0-9]+)
66
+ \[(?<usrlbl>\(.+\))?(?<code>(?:ISO|IEC)[^0-9]*\s[0-9]+)
67
67
  (?::(?<year>--|–|—|&\#821[12];|[0-9][0-9-]+))?\s
68
68
  \(all\sparts\)\]</ref>,?\s*
69
69
  (?:<fn[^>]*>\s*<p>(?<fn>[^\]]+)</p>\s*</fn>,?\s?)?(?<text>.*)$}xm
70
70
 
71
+ # These regexes allow () inside usrlbl but not inside code
71
72
  NON_ISO_REF = %r{^<ref\sid="(?<anchor>[^"]+)">
72
- \[(?<usrlbl>\([^)]+\))?(?<code>.+?)\]</ref>,?\s*(?<text>.*)$}xm
73
+ \[(?<usrlbl>\(.+\))?(?<code>.+?)\]</ref>,?\s*(?<text>.*)$}xm
73
74
 
74
75
  NON_ISO_REF1 = %r{^<ref\sid="(?<anchor>[^"]+)">
75
- (?<usrlbl>\([^)]+\))?(?<code>.+?)</ref>,?\s*(?<text>.*)$}xm
76
+ (?<usrlbl>\(.+\))?(?<code>.+?)</ref>,?\s*(?<text>.*)$}xm
76
77
  end
77
78
  end
78
79
  end
@@ -73,6 +73,7 @@ module Metanorma
73
73
  end
74
74
 
75
75
  def preface_main_filter(ret, node)
76
+ node.level > 1 and return ret
76
77
  start_main_section(ret, node)
77
78
  @preface && self.class::MAIN_CLAUSE_NAMES.include?(ret) and return nil
78
79
  !@preface && self.class::PREFACE_CLAUSE_NAMES.include?(ret) and
@@ -154,7 +154,17 @@ module Metanorma
154
154
  #{text}
155
155
  ADOC
156
156
  c = Asciidoctor.convert(doc, backend: flavour, header_footer: true)
157
- Nokogiri::XML(c).at("//xmlns:sections")
157
+ ret = Nokogiri::XML(c).at("//xmlns:sections")
158
+ separate_numbering_footnotes(ret)
159
+ end
160
+
161
+ # separate numbering of externally sourced footnotes
162
+ # from that of current doc
163
+ def separate_numbering_footnotes(docxml)
164
+ docxml.xpath("//xmlns:fn").each do |f|
165
+ f["reference"] = "_#{UUIDTools::UUID.random_create}_#{f['reference']}"
166
+ end
167
+ docxml
158
168
  end
159
169
 
160
170
  def asciimath_key(sym)
@@ -28,7 +28,7 @@ module Metanorma
28
28
  math_validate(doc)
29
29
  fatalerrors = @log.abort_messages
30
30
  fatalerrors.empty? or
31
- clean_abort("\n\nFATAL ERRROS:\n\n#{fatalerrors.join("\n\n")}", doc)
31
+ clean_abort("\n\nFATAL ERRORS:\n\n#{fatalerrors.join("\n\n")}", doc)
32
32
  end
33
33
 
34
34
  MATHML_NS = "http://www.w3.org/1998/Math/MathML".freeze
@@ -31,11 +31,21 @@ module Metanorma
31
31
  @log.add("Style", node, w)
32
32
  end
33
33
 
34
+ def reject_metanorma_extension
35
+ ->(node) {
36
+ node.ancestors.detect do |x|
37
+ x.name == "metanorma-extension"
38
+ end
39
+ }
40
+ end
41
+
34
42
  def asset_title_style(root)
35
- root.xpath("//figure[image][not(name)]").each do |node|
43
+ root.xpath("//figure[image][not(name)]")
44
+ .reject(&reject_metanorma_extension).each do |node|
36
45
  style_warning(node, "Figure should have title", nil)
37
46
  end
38
- root.xpath("//table[not(name)]").each do |node|
47
+ root.xpath("//table[not(name)]")
48
+ .reject(&reject_metanorma_extension).each do |node|
39
49
  style_warning(node, "Table should have title", nil)
40
50
  end
41
51
  end
@@ -15,7 +15,8 @@ module Metanorma
15
15
  end
16
16
 
17
17
  def empty_table_validate(doc)
18
- doc.xpath("//table[not(.//tr)]").each do |t|
18
+ doc.xpath("//table[not(.//tr)]").reject(&reject_metanorma_extension)
19
+ .each do |t|
19
20
  @log.add("Table", t, "Empty table", severity: 0)
20
21
  end
21
22
  end
@@ -30,6 +31,7 @@ module Metanorma
30
31
  end
31
32
 
32
33
  def maxrowcols_validate(table, maxcols, mode: "row_cols")
34
+ reject_metanorma_extension.call(table) and return
33
35
  case mode
34
36
  when "row_cols"
35
37
  maxrowcols_validate0(table, maxcols, "*", mode)
@@ -19,6 +19,6 @@ module Metanorma
19
19
  end
20
20
 
21
21
  module Standoc
22
- VERSION = "3.1.3".freeze
22
+ VERSION = "3.1.5".freeze
23
23
  end
24
24
  end
@@ -36,6 +36,7 @@ Gem::Specification.new do |spec|
36
36
  spec.add_dependency "metanorma", ">= 1.6.0"
37
37
  spec.add_dependency "metanorma-plugin-glossarist", "~> 0.2.3"
38
38
  spec.add_dependency "metanorma-plugin-lutaml", "~> 0.7.31"
39
+ spec.add_dependency "metanorma-plugin-plantuml", "~> 1.0.0"
39
40
  spec.add_dependency "metanorma-utils", "~> 1.11.0"
40
41
  spec.add_dependency "ruby-jing"
41
42
  # relaton-cli not just relaton, to avoid circular reference in metanorma
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metanorma-standoc
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.3
4
+ version: 3.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-08-04 00:00:00.000000000 Z
11
+ date: 2025-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -122,6 +122,20 @@ dependencies:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
124
  version: 0.7.31
125
+ - !ruby/object:Gem::Dependency
126
+ name: metanorma-plugin-plantuml
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 1.0.0
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 1.0.0
125
139
  - !ruby/object:Gem::Dependency
126
140
  name: metanorma-utils
127
141
  requirement: !ruby/object:Gem::Requirement
@@ -447,9 +461,6 @@ files:
447
461
  - lib/asciidoctor/standoc/cleanup_toc.rb
448
462
  - lib/asciidoctor/standoc/cleanup_xref.rb
449
463
  - lib/asciidoctor/standoc/converter.rb
450
- - lib/asciidoctor/standoc/datamodel/attributes_table_preprocessor.rb
451
- - lib/asciidoctor/standoc/datamodel/diagram_preprocessor.rb
452
- - lib/asciidoctor/standoc/datamodel/plantuml_renderer.rb
453
464
  - lib/asciidoctor/standoc/deprecated.rb
454
465
  - lib/asciidoctor/standoc/front.rb
455
466
  - lib/asciidoctor/standoc/front_contributor.rb
@@ -458,7 +469,6 @@ files:
458
469
  - lib/asciidoctor/standoc/macros.rb
459
470
  - lib/asciidoctor/standoc/macros_form.rb
460
471
  - lib/asciidoctor/standoc/macros_note.rb
461
- - lib/asciidoctor/standoc/macros_plantuml.rb
462
472
  - lib/asciidoctor/standoc/macros_terms.rb
463
473
  - lib/asciidoctor/standoc/ref.rb
464
474
  - lib/asciidoctor/standoc/ref_sect.rb
@@ -515,10 +525,8 @@ files:
515
525
  - lib/metanorma/standoc/cleanup_toc.rb
516
526
  - lib/metanorma/standoc/cleanup_xref.rb
517
527
  - lib/metanorma/standoc/converter.rb
518
- - lib/metanorma/standoc/datamodel/attributes_table_preprocessor.rb
519
- - lib/metanorma/standoc/datamodel/diagram_preprocessor.rb
520
- - lib/metanorma/standoc/datamodel/plantuml_renderer.rb
521
528
  - lib/metanorma/standoc/front.rb
529
+ - lib/metanorma/standoc/front_committee.rb
522
530
  - lib/metanorma/standoc/front_contributor.rb
523
531
  - lib/metanorma/standoc/front_organisation.rb
524
532
  - lib/metanorma/standoc/init.rb
@@ -533,7 +541,6 @@ files:
533
541
  - lib/metanorma/standoc/macros_inline.rb
534
542
  - lib/metanorma/standoc/macros_link.rb
535
543
  - lib/metanorma/standoc/macros_note.rb
536
- - lib/metanorma/standoc/macros_plantuml.rb
537
544
  - lib/metanorma/standoc/macros_terms.rb
538
545
  - lib/metanorma/standoc/merge_bibitems.rb
539
546
  - lib/metanorma/standoc/processor.rb
@@ -560,8 +567,6 @@ files:
560
567
  - lib/metanorma/standoc/validate_table.rb
561
568
  - lib/metanorma/standoc/validate_term.rb
562
569
  - lib/metanorma/standoc/version.rb
563
- - lib/metanorma/standoc/views/datamodel/model_representation.adoc.erb
564
- - lib/metanorma/standoc/views/datamodel/plantuml_representation.adoc.erb
565
570
  - metanorma-standoc.gemspec
566
571
  homepage: https://github.com/metanorma/metanorma-standoc
567
572
  licenses:
@@ -1,3 +0,0 @@
1
- require "asciidoctor/standoc/deprecated"
2
- require "metanorma/standoc/datamodel/attributes_table_preprocessor"
3
-
@@ -1,3 +0,0 @@
1
- require "asciidoctor/standoc/deprecated"
2
- require "metanorma/standoc/datamodel/diagram_preprocessor"
3
-
@@ -1,8 +0,0 @@
1
- require "asciidoctor/standoc/deprecated"
2
- require "metanorma/standoc/datamodel/plantuml_renderer"
3
-
4
- module Asciidoctor
5
- module Datamodel
6
- PlantumlRenderer = Metanorma::Datamodel::PlantumlRenderer
7
- end
8
- end
@@ -1,2 +0,0 @@
1
- require "asciidoctor/standoc/deprecated"
2
- require "metanorma/standoc/macros_plantuml"
@@ -1,57 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "erb"
4
-
5
- module Metanorma
6
- module Standoc
7
- module Datamodel
8
- class AttributesTablePreprocessor < Asciidoctor::Extensions::Preprocessor
9
- BLOCK_START_REGEXP = /\{(.+?)\.\*,(.+),(.+)\}/
10
- BLOCK_END_REGEXP = /\A\{[A-Z]+\}\z/
11
- MARCO_REGEXP = /\[datamodel_attributes_table,([^,]+),?(.+)?\]/
12
- TEMPLATES_PATH = File.expand_path("../views/datamodel", __dir__).freeze
13
- # search document for block `datamodel_attributes_table`
14
- # read include derectives that goes after that in block and transform
15
- # into yaml2text blocks
16
- def process(document, reader)
17
- input_lines = reader.lines.to_enum
18
- Asciidoctor::PreprocessorReader.new(document, processed_lines(document, input_lines))
19
- end
20
-
21
- private
22
-
23
- def processed_lines(document, input_lines)
24
- input_lines.each_with_object([]) do |line, result|
25
- if match = line.match(MARCO_REGEXP)
26
- yaml_path = match[1]
27
- result.push(*parse_marco(yaml_path, document))
28
- else
29
- result.push(line)
30
- end
31
- end
32
- end
33
-
34
- def parse_marco(yaml_path, document)
35
- model_representation(yaml_relative_path(yaml_path, document))
36
- .split("\n")
37
- end
38
-
39
- def model_representation(model_path)
40
- template = File.read(File.join(
41
- TEMPLATES_PATH,
42
- "model_representation.adoc.erb",
43
- ))
44
- file_name = File.basename(model_path).gsub(/\.ya?ml/, "")
45
- ERB
46
- .new(template)
47
- .result(binding)
48
- end
49
-
50
- def yaml_relative_path(file_path, document)
51
- directory = File.dirname(document.attributes["docfile"] || ".")
52
- document.path_resolver.system_path(file_path, directory)
53
- end
54
- end
55
- end
56
- end
57
- end
@@ -1,103 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "erb"
4
- require "metanorma/standoc/datamodel/plantuml_renderer"
5
-
6
- module Metanorma
7
- module Standoc
8
- module Datamodel
9
- class DiagramPreprocessor < Asciidoctor::Extensions::Preprocessor
10
- BLOCK_START_REGEXP = /\{(.+?)\.\*,(.+),(.+)\}/.freeze
11
- BLOCK_END_REGEXP = /\A\{[A-Z]+\}\z/.freeze
12
- MARCO_REGEXP = /\[datamodel_diagram,([^,]+),?(.+)?\]/.freeze
13
- TEMPLATES_PATH = File.expand_path("../views/datamodel", __dir__).freeze
14
- # search document for block `datamodel_diagram`
15
- # read include derectives that goes after that in block and transform
16
- # into plantuml block
17
- def process(document, reader)
18
- input_lines = reader.lines.to_enum
19
- Asciidoctor::PreprocessorReader.new(document, processed_lines(document, input_lines))
20
- end
21
-
22
- private
23
-
24
- def processed_lines(document, input_lines)
25
- input_lines.each_with_object([]) do |line, result|
26
- if match = line.match(MARCO_REGEXP)
27
- result
28
- .push(*parse_datamodel_marco(match[1], match[2], document))
29
- else
30
- result.push(line)
31
- end
32
- end
33
- end
34
-
35
- def parse_datamodel_marco(yaml_path, include_path, document)
36
- include_path ||= File.join(File.dirname(yaml_path), "..", "models")
37
- include_path = yaml_relative_path(include_path, document)
38
- yaml_relative_to_doc_path = yaml_relative_path(yaml_path, document)
39
- view_hash = YAML.safe_load(File.read(yaml_relative_to_doc_path))
40
- plantuml_representations(view_hash,
41
- yaml_relative_to_doc_path,
42
- include_path)
43
- end
44
-
45
- def yaml_relative_path(file_path, document)
46
- docfile = document.attributes["docfile"] || "."
47
- docfile_directory = File.dirname(docfile)
48
- document.path_resolver.system_path(file_path, docfile_directory)
49
- end
50
-
51
- def import_format(include_path, import_name, values)
52
- include_content = File.read(File.join(
53
- include_path,
54
- "#{import_name}.yml",
55
- ))
56
- content = YAML.safe_load(include_content)
57
- if values
58
- content["skipSection"] = values["skipSection"]
59
- end
60
- content
61
- end
62
-
63
- def format_import_directives(imports, include_path)
64
- imports
65
- .each_with_object({}) do |(import_name, values), res|
66
- full_model_name = import_name.split("/").join
67
- content = import_format(include_path, import_name, values)
68
- res[content["name"] || full_model_name] = content
69
- end.compact
70
- end
71
-
72
- def prepare_view_hash(view_hash, all_imports)
73
- view_hash.merge!(
74
- "classes" => model_type(all_imports, "class"),
75
- "enums" => model_type(all_imports, "enum"),
76
- "relations" => view_hash["relations"] || [],
77
- "fidelity" => (view_hash["fidelity"] || {})
78
- .merge!("classes" => model_type(all_imports,
79
- "class")),
80
- )
81
- end
82
-
83
- def model_type(imports, type)
84
- imports
85
- .select do |_name, elem|
86
- elem["modelType"] == type
87
- end
88
- end
89
-
90
- def plantuml_representations(view_hash, view_path, include_path)
91
- yaml_directory = File.dirname(view_path)
92
- all_imports = format_import_directives(view_hash["imports"],
93
- include_path)
94
- prepare_view_hash(view_hash, all_imports)
95
- Metanorma::Datamodel::PlantumlRenderer
96
- .new(view_hash, File.join(yaml_directory, ".."))
97
- .render
98
- .split("\n")
99
- end
100
- end
101
- end
102
- end
103
- end