metanorma-standoc 1.11.3 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (150) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/rake.yml +3 -31
  3. data/.gitignore +23 -0
  4. data/Gemfile +0 -1
  5. data/lib/asciidoctor/standoc/base.rb +2 -145
  6. data/lib/asciidoctor/standoc/blocks.rb +2 -238
  7. data/lib/asciidoctor/standoc/blocks_notes.rb +2 -100
  8. data/lib/asciidoctor/standoc/cleanup.rb +2 -208
  9. data/lib/asciidoctor/standoc/cleanup_amend.rb +2 -53
  10. data/lib/asciidoctor/standoc/cleanup_block.rb +2 -172
  11. data/lib/asciidoctor/standoc/cleanup_boilerplate.rb +2 -212
  12. data/lib/asciidoctor/standoc/cleanup_footnotes.rb +2 -108
  13. data/lib/asciidoctor/standoc/cleanup_image.rb +2 -69
  14. data/lib/asciidoctor/standoc/cleanup_inline.rb +2 -189
  15. data/lib/asciidoctor/standoc/cleanup_maths.rb +2 -221
  16. data/lib/asciidoctor/standoc/cleanup_ref.rb +2 -169
  17. data/lib/asciidoctor/standoc/cleanup_ref_dl.rb +2 -103
  18. data/lib/asciidoctor/standoc/cleanup_reqt.rb +2 -110
  19. data/lib/asciidoctor/standoc/cleanup_section.rb +2 -184
  20. data/lib/asciidoctor/standoc/cleanup_section_names.rb +2 -91
  21. data/lib/asciidoctor/standoc/cleanup_symbols.rb +2 -47
  22. data/lib/asciidoctor/standoc/cleanup_table.rb +2 -67
  23. data/lib/asciidoctor/standoc/cleanup_terms.rb +2 -139
  24. data/lib/asciidoctor/standoc/cleanup_terms_designations.rb +2 -192
  25. data/lib/asciidoctor/standoc/cleanup_text.rb +2 -95
  26. data/lib/asciidoctor/standoc/cleanup_toc.rb +3 -0
  27. data/lib/asciidoctor/standoc/cleanup_xref.rb +2 -106
  28. data/lib/asciidoctor/standoc/converter.rb +2 -123
  29. data/lib/asciidoctor/standoc/datamodel/attributes_table_preprocessor.rb +2 -56
  30. data/lib/asciidoctor/standoc/datamodel/diagram_preprocessor.rb +2 -102
  31. data/lib/asciidoctor/standoc/datamodel/plantuml_renderer.rb +3 -404
  32. data/lib/asciidoctor/standoc/deprecated.rb +5 -0
  33. data/lib/asciidoctor/standoc/front.rb +2 -219
  34. data/lib/asciidoctor/standoc/front_contributor.rb +2 -191
  35. data/lib/asciidoctor/standoc/inline.rb +2 -231
  36. data/lib/asciidoctor/standoc/lists.rb +2 -119
  37. data/lib/asciidoctor/standoc/macros.rb +2 -203
  38. data/lib/asciidoctor/standoc/macros_form.rb +2 -62
  39. data/lib/asciidoctor/standoc/macros_note.rb +2 -44
  40. data/lib/asciidoctor/standoc/macros_plantuml.rb +2 -112
  41. data/lib/asciidoctor/standoc/macros_terms.rb +2 -180
  42. data/lib/asciidoctor/standoc/ref.rb +2 -251
  43. data/lib/asciidoctor/standoc/ref_sect.rb +2 -153
  44. data/lib/asciidoctor/standoc/ref_utility.rb +2 -0
  45. data/lib/asciidoctor/standoc/render.rb +2 -116
  46. data/lib/asciidoctor/standoc/reqt.rb +2 -89
  47. data/lib/asciidoctor/standoc/section.rb +2 -194
  48. data/lib/asciidoctor/standoc/table.rb +2 -84
  49. data/lib/asciidoctor/standoc/term_lookup_cleanup.rb +2 -178
  50. data/lib/asciidoctor/standoc/terms.rb +2 -153
  51. data/lib/asciidoctor/standoc/utils.rb +2 -100
  52. data/lib/asciidoctor/standoc/validate.rb +2 -157
  53. data/lib/asciidoctor/standoc/validate_section.rb +2 -54
  54. data/lib/isodoc/html/htmlstyle.css +44 -29
  55. data/lib/isodoc/html/htmlstyle.scss +17 -12
  56. data/lib/metanorma/standoc/base.rb +163 -0
  57. data/lib/{asciidoctor → metanorma}/standoc/basicdoc.rng +0 -0
  58. data/lib/{asciidoctor → metanorma}/standoc/biblio.rng +2 -2
  59. data/lib/metanorma/standoc/blocks.rb +239 -0
  60. data/lib/metanorma/standoc/blocks_notes.rb +101 -0
  61. data/lib/metanorma/standoc/cleanup.rb +157 -0
  62. data/lib/metanorma/standoc/cleanup_amend.rb +54 -0
  63. data/lib/metanorma/standoc/cleanup_block.rb +173 -0
  64. data/lib/metanorma/standoc/cleanup_boilerplate.rb +213 -0
  65. data/lib/metanorma/standoc/cleanup_footnotes.rb +109 -0
  66. data/lib/metanorma/standoc/cleanup_image.rb +70 -0
  67. data/lib/metanorma/standoc/cleanup_inline.rb +190 -0
  68. data/lib/metanorma/standoc/cleanup_maths.rb +222 -0
  69. data/lib/metanorma/standoc/cleanup_ref.rb +170 -0
  70. data/lib/metanorma/standoc/cleanup_ref_dl.rb +104 -0
  71. data/lib/metanorma/standoc/cleanup_reqt.rb +111 -0
  72. data/lib/metanorma/standoc/cleanup_section.rb +212 -0
  73. data/lib/metanorma/standoc/cleanup_section_names.rb +92 -0
  74. data/lib/metanorma/standoc/cleanup_symbols.rb +48 -0
  75. data/lib/metanorma/standoc/cleanup_table.rb +68 -0
  76. data/lib/metanorma/standoc/cleanup_terms.rb +140 -0
  77. data/lib/metanorma/standoc/cleanup_terms_designations.rb +199 -0
  78. data/lib/metanorma/standoc/cleanup_text.rb +74 -0
  79. data/lib/metanorma/standoc/cleanup_toc.rb +98 -0
  80. data/lib/metanorma/standoc/cleanup_xref.rb +107 -0
  81. data/lib/metanorma/standoc/converter.rb +126 -0
  82. data/lib/metanorma/standoc/datamodel/attributes_table_preprocessor.rb +57 -0
  83. data/lib/metanorma/standoc/datamodel/diagram_preprocessor.rb +103 -0
  84. data/lib/metanorma/standoc/datamodel/plantuml_renderer.rb +409 -0
  85. data/lib/metanorma/standoc/front.rb +224 -0
  86. data/lib/metanorma/standoc/front_contributor.rb +192 -0
  87. data/lib/metanorma/standoc/inline.rb +232 -0
  88. data/lib/{asciidoctor → metanorma}/standoc/isodoc.rng +104 -3
  89. data/lib/metanorma/standoc/lists.rb +120 -0
  90. data/lib/metanorma/standoc/macros.rb +205 -0
  91. data/lib/metanorma/standoc/macros_embed.rb +72 -0
  92. data/lib/metanorma/standoc/macros_form.rb +63 -0
  93. data/lib/metanorma/standoc/macros_note.rb +45 -0
  94. data/lib/metanorma/standoc/macros_plantuml.rb +113 -0
  95. data/lib/metanorma/standoc/macros_terms.rb +194 -0
  96. data/lib/metanorma/standoc/ref.rb +243 -0
  97. data/lib/metanorma/standoc/ref_sect.rb +153 -0
  98. data/lib/{asciidoctor/standoc/ref_date_id.rb → metanorma/standoc/ref_utility.rb} +43 -5
  99. data/lib/metanorma/standoc/render.rb +115 -0
  100. data/lib/metanorma/standoc/reqt.rb +90 -0
  101. data/lib/{asciidoctor → metanorma}/standoc/reqt.rng +0 -0
  102. data/lib/metanorma/standoc/section.rb +209 -0
  103. data/lib/metanorma/standoc/table.rb +85 -0
  104. data/lib/metanorma/standoc/term_lookup_cleanup.rb +179 -0
  105. data/lib/metanorma/standoc/terms.rb +160 -0
  106. data/lib/metanorma/standoc/utils.rb +101 -0
  107. data/lib/metanorma/standoc/validate.rb +158 -0
  108. data/lib/metanorma/standoc/validate_section.rb +55 -0
  109. data/lib/metanorma/standoc/version.rb +1 -1
  110. data/lib/{asciidoctor → metanorma}/standoc/views/datamodel/model_representation.adoc.erb +0 -0
  111. data/lib/{asciidoctor → metanorma}/standoc/views/datamodel/plantuml_representation.adoc.erb +0 -0
  112. data/lib/metanorma-standoc.rb +1 -1
  113. data/metanorma-standoc.gemspec +1 -1
  114. data/spec/assets/a1.adoc +8 -0
  115. data/spec/assets/a2.adoc +8 -0
  116. data/spec/assets/a3.adoc +9 -0
  117. data/spec/assets/a4.adoc +4 -0
  118. data/spec/{asciidoctor → metanorma}/base_spec.rb +499 -407
  119. data/spec/{asciidoctor → metanorma}/blank_spec.rb +1 -1
  120. data/spec/{asciidoctor → metanorma}/blocks_spec.rb +1 -1
  121. data/spec/{asciidoctor → metanorma}/cleanup_blocks_spec.rb +1 -1
  122. data/spec/{asciidoctor → metanorma}/cleanup_sections_spec.rb +1 -1
  123. data/spec/{asciidoctor → metanorma}/cleanup_spec.rb +5 -5
  124. data/spec/{asciidoctor → metanorma}/cleanup_terms_spec.rb +227 -119
  125. data/spec/{asciidoctor → metanorma}/datamodel/attributes_table_preprocessor_spec.rb +1 -1
  126. data/spec/{asciidoctor → metanorma}/datamodel/diagram_preprocessor_spec.rb +1 -1
  127. data/spec/{asciidoctor → metanorma}/inline_spec.rb +170 -1
  128. data/spec/{asciidoctor → metanorma}/isobib_cache_spec.rb +1 -1
  129. data/spec/{asciidoctor → metanorma}/lists_spec.rb +1 -1
  130. data/spec/{asciidoctor → metanorma}/macros_json2text_spec.rb +0 -0
  131. data/spec/{asciidoctor → metanorma}/macros_plantuml_spec.rb +3 -3
  132. data/spec/{asciidoctor → metanorma}/macros_spec.rb +97 -6
  133. data/spec/{asciidoctor → metanorma}/macros_yaml2text_spec.rb +0 -0
  134. data/spec/metanorma/refs_dl_spec.rb +863 -0
  135. data/spec/{asciidoctor → metanorma}/refs_spec.rb +522 -15
  136. data/spec/{asciidoctor → metanorma}/section_spec.rb +59 -1
  137. data/spec/{asciidoctor → metanorma}/table_spec.rb +1 -1
  138. data/spec/{asciidoctor → metanorma}/validate_spec.rb +2 -2
  139. data/spec/vcr_cassettes/dated_iso_ref_joint_iso_iec.yml +46 -46
  140. data/spec/vcr_cassettes/dated_iso_ref_joint_iso_iec1.yml +12 -12
  141. data/spec/vcr_cassettes/hide_refs.yml +599 -0
  142. data/spec/vcr_cassettes/isobib_get_123.yml +12 -12
  143. data/spec/vcr_cassettes/isobib_get_123_1.yml +24 -24
  144. data/spec/vcr_cassettes/isobib_get_123_1_fr.yml +35 -35
  145. data/spec/vcr_cassettes/isobib_get_123_2001.yml +13 -13
  146. data/spec/vcr_cassettes/isobib_get_124.yml +10 -10
  147. data/spec/vcr_cassettes/rfcbib_get_rfc8341.yml +18 -18
  148. data/spec/vcr_cassettes/separates_iev_citations_by_top_level_clause.yml +46 -46
  149. metadata +88 -32
  150. data/spec/asciidoctor/refs_dl_spec.rb +0 -864
@@ -0,0 +1,107 @@
1
+ module Metanorma
2
+ module Standoc
3
+ module Cleanup
4
+ # extending localities to cover ISO referencing
5
+ LOCALITY_REGEX_STR = <<~REGEXP.freeze
6
+ ^((?<locality>section|clause|part|paragraph|chapter|page|
7
+ table|annex|figure|example|note|formula|list|time|anchor|
8
+ locality:[^ \\t\\n\\r:,;=]+)(\\s+|=)
9
+ (?<ref>[^"][^ \\t\\n,:-]*|"[^"]+")
10
+ (-(?<to>[^"][^ \\t\\n,:-]*|"[^"]"))?|
11
+ (?<locality2>whole|locality:[^ \\t\\n\\r:,;=]+))(?<punct>[,:;]?)\\s*
12
+ (?<text>.*)$
13
+ REGEXP
14
+ LOCALITY_RE = Regexp.new(LOCALITY_REGEX_STR.gsub(/\s/, ""),
15
+ Regexp::IGNORECASE | Regexp::MULTILINE)
16
+
17
+ def tq(text)
18
+ text.sub(/^"/, "").sub(/"$/, "")
19
+ end
20
+
21
+ def extract_localities(elem)
22
+ f = elem&.children&.first or return
23
+ f.text? or return
24
+ head = f.remove.text
25
+ tail = elem&.children&.remove
26
+ extract_localities1(elem, head)
27
+ tail and elem << tail
28
+ end
29
+
30
+ def extract_localities1(elem, text)
31
+ b = elem.add_child("<localityStack/>").first if LOCALITY_RE.match text
32
+ while (m = LOCALITY_RE.match text)
33
+ ref = m[:ref] ? "<referenceFrom>#{tq m[:ref]}</referenceFrom>" : ""
34
+ refto = m[:to] ? "<referenceTo>#{tq m[:to]}</referenceTo>" : ""
35
+ b.add_child("<locality type='#{locality_label(m)}'>#{ref}#{refto}"\
36
+ "</locality>")
37
+ text = m[:text]
38
+ b = elem.add_child("<localityStack/>").first if m[:punct] == ";"
39
+ end
40
+ elem.add_child(text) if text
41
+ end
42
+
43
+ def locality_label(match)
44
+ loc = match[:locality] || match[:locality2]
45
+ /^locality:/.match?(loc) ? loc : loc&.downcase
46
+ end
47
+
48
+ def xref_to_eref(elem)
49
+ elem["bibitemid"] = elem["target"]
50
+ unless elem["citeas"] = @anchors&.dig(elem["target"], :xref)
51
+ @internal_eref_namespaces.include?(elem["type"]) or
52
+ @log.add("Crossreferences", elem,
53
+ "#{elem['target']} does not have a corresponding "\
54
+ "anchor ID in the bibliography!")
55
+ end
56
+ elem.delete("target")
57
+ extract_localities(elem) unless elem.children.empty?
58
+ end
59
+
60
+ def xref_cleanup(xmldoc)
61
+ xmldoc.xpath("//xref").each do |x|
62
+ /:/.match(x["target"]) and xref_to_internal_eref(x)
63
+ next unless x.name == "xref"
64
+
65
+ if refid? x["target"]
66
+ x.name = "eref"
67
+ xref_to_eref(x)
68
+ else x.delete("type")
69
+ end
70
+ end
71
+ end
72
+
73
+ def xref_to_internal_eref(elem)
74
+ a = elem["target"].split(":", 3)
75
+ unless a.size < 2 || a[0].empty? || a[1].empty?
76
+ elem["target"] = "#{a[0]}_#{a[1]}"
77
+ a.size > 2 and
78
+ elem.children = %{anchor="#{a[2..-1].join}",#{elem&.children&.text}}
79
+ elem["type"] = a[0]
80
+ @internal_eref_namespaces << a[0]
81
+ elem.name = "eref"
82
+ xref_to_eref(elem)
83
+ end
84
+ end
85
+
86
+ def quotesource_cleanup(xmldoc)
87
+ xmldoc.xpath("//quote/source | //terms/source").each do |x|
88
+ xref_to_eref(x)
89
+ end
90
+ end
91
+
92
+ def origin_cleanup(xmldoc)
93
+ xmldoc.xpath("//origin/concept[termref]").each do |x|
94
+ t = x.at("./termref")
95
+ x.replace(t)
96
+ end
97
+ xmldoc.xpath("//origin").each do |x|
98
+ x["citeas"] = @anchors&.dig(x["bibitemid"], :xref) or
99
+ @log.add("Crossreferences", x,
100
+ "#{x['bibitemid']} does not have a corresponding anchor "\
101
+ "ID in the bibliography!")
102
+ extract_localities(x) unless x.children.empty?
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,126 @@
1
+ require "asciidoctor"
2
+ require "metanorma/util"
3
+ require "metanorma/standoc/version"
4
+ require "metanorma/standoc/base"
5
+ require "metanorma/standoc/front"
6
+ require "metanorma/standoc/lists"
7
+ require "metanorma/standoc/ref"
8
+ require "metanorma/standoc/inline"
9
+ require "metanorma/standoc/blocks"
10
+ require "metanorma/standoc/section"
11
+ require "metanorma/standoc/table"
12
+ require "metanorma/standoc/validate"
13
+ require "metanorma/standoc/utils"
14
+ require "metanorma/standoc/cleanup"
15
+ require "metanorma/standoc/reqt"
16
+ require_relative "./macros"
17
+
18
+ module Metanorma
19
+ module Standoc
20
+ # A {Converter} implementation that generates Standoc output, and a document
21
+ # schema encapsulation of the document for validation
22
+ class Converter
23
+ Asciidoctor::Extensions.register do
24
+ preprocessor Metanorma::Standoc::Datamodel::AttributesTablePreprocessor
25
+ preprocessor Metanorma::Standoc::Datamodel::DiagramPreprocessor
26
+ preprocessor Metanorma::Plugin::Datastruct::Json2TextPreprocessor
27
+ preprocessor Metanorma::Plugin::Datastruct::Yaml2TextPreprocessor
28
+ preprocessor Metanorma::Plugin::Lutaml::LutamlPreprocessor
29
+ preprocessor Metanorma::Plugin::Lutaml::LutamlUmlAttributesTablePreprocessor
30
+ preprocessor Metanorma::Plugin::Lutaml::LutamlUmlDatamodelDescriptionPreprocessor
31
+ inline_macro Metanorma::Standoc::PreferredTermInlineMacro
32
+ inline_macro Metanorma::Standoc::AltTermInlineMacro
33
+ inline_macro Metanorma::Standoc::AdmittedTermInlineMacro
34
+ inline_macro Metanorma::Standoc::DeprecatedTermInlineMacro
35
+ inline_macro Metanorma::Standoc::RelatedTermInlineMacro
36
+ inline_macro Metanorma::Standoc::DomainTermInlineMacro
37
+ inline_macro Metanorma::Standoc::InheritInlineMacro
38
+ inline_macro Metanorma::Standoc::HTML5RubyMacro
39
+ inline_macro Metanorma::Standoc::ConceptInlineMacro
40
+ inline_macro Metanorma::Standoc::AutonumberInlineMacro
41
+ inline_macro Metanorma::Standoc::VariantInlineMacro
42
+ inline_macro Metanorma::Standoc::FootnoteBlockInlineMacro
43
+ inline_macro Metanorma::Standoc::TermRefInlineMacro
44
+ inline_macro Metanorma::Standoc::SymbolRefInlineMacro
45
+ inline_macro Metanorma::Standoc::IndexXrefInlineMacro
46
+ inline_macro Metanorma::Standoc::IndexRangeInlineMacro
47
+ inline_macro Metanorma::Standoc::AddMacro
48
+ inline_macro Metanorma::Standoc::DelMacro
49
+ inline_macro Metanorma::Standoc::FormInputMacro
50
+ inline_macro Metanorma::Standoc::FormLabelMacro
51
+ inline_macro Metanorma::Standoc::FormTextareaMacro
52
+ inline_macro Metanorma::Standoc::FormSelectMacro
53
+ inline_macro Metanorma::Standoc::FormOptionMacro
54
+ inline_macro Metanorma::Standoc::ToCInlineMacro
55
+ inline_macro Metanorma::Standoc::PassInlineMacro
56
+ inline_macro Metanorma::Plugin::Lutaml::LutamlFigureInlineMacro
57
+ inline_macro Metanorma::Plugin::Lutaml::LutamlTableInlineMacro
58
+ block_macro Metanorma::Plugin::Lutaml::LutamlDiagramBlockMacro
59
+ block Metanorma::Standoc::ToDoAdmonitionBlock
60
+ treeprocessor Metanorma::Standoc::ToDoInlineAdmonitionBlock
61
+ block Metanorma::Standoc::PlantUMLBlockMacro
62
+ block Metanorma::Plugin::Lutaml::LutamlDiagramBlock
63
+ block Metanorma::Standoc::PseudocodeBlockMacro
64
+ preprocessor Metanorma::Standoc::EmbedIncludeProcessor
65
+ end
66
+
67
+ include ::Asciidoctor::Converter
68
+ include ::Asciidoctor::Writer
69
+
70
+ include ::Metanorma::Standoc::Base
71
+ include ::Metanorma::Standoc::Front
72
+ include ::Metanorma::Standoc::Lists
73
+ include ::Metanorma::Standoc::Refs
74
+ include ::Metanorma::Standoc::Inline
75
+ include ::Metanorma::Standoc::Blocks
76
+ include ::Metanorma::Standoc::Section
77
+ include ::Metanorma::Standoc::Table
78
+ include ::Metanorma::Standoc::Utils
79
+ include ::Metanorma::Standoc::Cleanup
80
+ include ::Metanorma::Standoc::Validate
81
+
82
+ register_for "standoc"
83
+
84
+ $xreftext = {}
85
+
86
+ def initialize(backend, opts)
87
+ super
88
+ basebackend "html"
89
+ outfilesuffix ".xml"
90
+ @libdir = File.dirname(self.class::_file || __FILE__)
91
+ end
92
+
93
+ class << self
94
+ attr_accessor :_file
95
+ end
96
+
97
+ def self.inherited(konv) # rubocop:disable Lint/MissingSuper
98
+ konv._file = caller_locations(1..1).first.absolute_path
99
+ end
100
+
101
+ # path to isodoc assets in child gems
102
+ def html_doc_path(file)
103
+ File.join(@libdir, "../../isodoc/html", file)
104
+ end
105
+
106
+ def content(node)
107
+ node.content
108
+ end
109
+
110
+ def skip(node, name = nil)
111
+ name = name || node.node_name
112
+ w = "converter missing for #{name} node in Metanorma backend"
113
+ @log.add("AsciiDoc Input", node, w)
114
+ nil
115
+ end
116
+
117
+ alias_method :embedded, :content
118
+ alias_method :verse, :quote
119
+ alias_method :audio, :skip
120
+ alias_method :video, :skip
121
+ alias_method :inline_button, :skip
122
+ alias_method :inline_kbd, :skip
123
+ alias_method :inline_menu, :skip
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,57 @@
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.readlines.to_enum
18
+ Asciidoctor::Reader.new(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
@@ -0,0 +1,103 @@
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.readlines.to_enum
19
+ Asciidoctor::Reader.new(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