metanorma-plugin-lutaml 0.7.72 → 0.7.74

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 (28) hide show
  1. checksums.yaml +4 -4
  2. data/README.adoc +3 -0
  3. data/docs/usages/lutaml-jsonschema.adoc +77 -0
  4. data/docs/usages/lutaml-xsd.adoc +59 -0
  5. data/lib/metanorma/plugin/lutaml/base_preprocessor.rb +9 -2
  6. data/lib/metanorma/plugin/lutaml/cache_registry.rb +5 -0
  7. data/lib/metanorma/plugin/lutaml/express_remark_adapter.rb +37 -7
  8. data/lib/metanorma/plugin/lutaml/express_remark_figures.rb +194 -0
  9. data/lib/metanorma/plugin/lutaml/express_schema_listing.rb +65 -9
  10. data/lib/metanorma/plugin/lutaml/liquid_templates/xsd/_attribute.liquid +24 -0
  11. data/lib/metanorma/plugin/lutaml/liquid_templates/xsd/_attribute_group.liquid +60 -0
  12. data/lib/metanorma/plugin/lutaml/liquid_templates/xsd/_attribute_value_constraints.liquid +65 -0
  13. data/lib/metanorma/plugin/lutaml/liquid_templates/xsd/_child_elements.liquid +34 -0
  14. data/lib/metanorma/plugin/lutaml/liquid_templates/xsd/_complex_type.liquid +109 -0
  15. data/lib/metanorma/plugin/lutaml/liquid_templates/xsd/_diagram.liquid +25 -0
  16. data/lib/metanorma/plugin/lutaml/liquid_templates/xsd/_diagram_children.liquid +31 -0
  17. data/lib/metanorma/plugin/lutaml/liquid_templates/xsd/_element.liquid +110 -0
  18. data/lib/metanorma/plugin/lutaml/liquid_templates/xsd/_xml_example_attribute.liquid +2 -0
  19. data/lib/metanorma/plugin/lutaml/liquid_templates/xsd/_xml_example_child.liquid +26 -0
  20. data/lib/metanorma/plugin/lutaml/liquid_templates/xsd/_xsd_ref.liquid +19 -0
  21. data/lib/metanorma/plugin/lutaml/liquid_templates/xsd/_xsd_schema.liquid +32 -0
  22. data/lib/metanorma/plugin/lutaml/lutaml_jsonschema_preprocessor.rb +136 -0
  23. data/lib/metanorma/plugin/lutaml/lutaml_preprocessor.rb +1 -0
  24. data/lib/metanorma/plugin/lutaml/lutaml_xsd_preprocessor.rb +33 -6
  25. data/lib/metanorma/plugin/lutaml/version.rb +1 -1
  26. data/lib/metanorma-plugin-lutaml.rb +4 -0
  27. data/metanorma-plugin-lutaml.gemspec +1 -0
  28. metadata +37 -2
@@ -0,0 +1,65 @@
1
+ {%- assign simple_type = attribute.simple_type -%}
2
+ {%- if simple_type == nil and attribute.referenced_object -%}
3
+ {%- assign simple_type = attribute.referenced_object.simple_type -%}
4
+ {%- endif -%}
5
+ {%- if simple_type -%}
6
+ {%- assign restriction = simple_type.restriction -%}
7
+ {%- assign union = simple_type.union -%}
8
+ {%- if restriction -%}
9
+ {%- assign enum_values = restriction.enumeration | map: "value" -%}
10
+ {%- assign pattern_values = restriction.pattern | map: "value" -%}
11
+ {%- capture constraint_body -%}
12
+ {% if restriction.base -%}
13
+ Base type: `{{ restriction.base }}`
14
+ {% endif -%}
15
+ {% if enum_values.size > 0 %}
16
+
17
+ Allowed values:
18
+
19
+ {% for enum_value in enum_values %}
20
+ * `{{ enum_value }}`
21
+ {% endfor %}
22
+ {% endif -%}
23
+ {% if pattern_values.size > 0 %}
24
+
25
+ Patterns:
26
+
27
+ {% for pattern_value in pattern_values %}
28
+ * `{{ pattern_value }}`
29
+ {% endfor %}
30
+ {% endif -%}
31
+ {%- endcapture -%}
32
+ {%- assign constraint_body = constraint_body | strip -%}
33
+ {%- if constraint_body != "" %}
34
+ +
35
+ --
36
+ {{ constraint_body }}
37
+ --
38
+ {% endif -%}
39
+ {%- elsif union -%}
40
+ {%- capture union_body -%}
41
+ {% if union.member_types -%}
42
+ Member types: `{{ union.member_types }}`
43
+ {% endif -%}
44
+ {% for union_simple_type in union.simple_type -%}
45
+ {% assign union_restriction = union_simple_type.restriction -%}
46
+ {% assign union_enum_values = union_restriction.enumeration | map: "value" -%}
47
+ {% if union_enum_values.size > 0 %}
48
+
49
+ Allowed `{{ union_restriction.base }}` values:
50
+
51
+ {% for union_enum_value in union_enum_values %}
52
+ * `{{ union_enum_value }}`
53
+ {% endfor %}
54
+ {% endif -%}
55
+ {% endfor -%}
56
+ {%- endcapture -%}
57
+ {%- assign union_body = union_body | strip -%}
58
+ {%- if union_body != "" %}
59
+ +
60
+ --
61
+ {{ union_body }}
62
+ --
63
+ {% endif -%}
64
+ {%- endif -%}
65
+ {%- endif -%}
@@ -0,0 +1,34 @@
1
+ {%- assign marker = marker | default: "*" -%}
2
+ {%- assign child_marker = marker | append: "*" -%}
3
+ {%- capture cardinality -%}[{{ child.min_occurrences | default: "1" }}..{{ child.max_occurrences | default: "1" }}]{%- endcapture -%}
4
+ {%- if child.annotation? or child.attribute? or child.attribute_group? -%}
5
+ {%- elsif child.sequence? %}
6
+ {{ marker }} Sequence {{ cardinality }}
7
+ {% for element in child.resolved_element_order -%}
8
+ {% render "child_elements", child: element, marker: child_marker %}
9
+ {% endfor -%}
10
+ {%- elsif child.choice? %}
11
+ {{ marker }} Choice {{ cardinality }}
12
+ {% for element in child.resolved_element_order -%}
13
+ {% render "child_elements", child: element, marker: child_marker %}
14
+ {% endfor -%}
15
+ {%- elsif child.all? %}
16
+ {{ marker }} All {{ cardinality }}
17
+ {% for element in child.resolved_element_order -%}
18
+ {% render "child_elements", child: element, marker: child_marker %}
19
+ {% endfor -%}
20
+ {%- elsif child.any? %}
21
+ {{ marker }} Any element from any namespace ({{ child.process_contents | default: "strict" }} processing) {{ cardinality }}
22
+ {%- elsif child.simple_content? %}
23
+ {{ marker }} Simple content based on `{{ child.base_type | default: child.base }}` {{ cardinality }}
24
+ {%- elsif child.element? -%}
25
+ {%- assign element_name = child.referenced_name | default: child.name | default: child.ref -%}
26
+ {%- assign element_type = child.referenced_type | default: child.type -%}
27
+ {{ marker }} `<{{ element_name }}>`{% if element_type %} of type `{{ element_type }}`{% endif %} {{ cardinality }}
28
+ {%- elsif child.child_elements.size > 0 -%}
29
+ {%- assign group_name = child.name | default: child.ref | default: "anonymous" -%}
30
+ {{ marker }} Group `{{ group_name }}` {{ cardinality }}
31
+ {% for element in child.child_elements -%}
32
+ {% render "child_elements", child: element, marker: child_marker %}
33
+ {% endfor -%}
34
+ {%- endif -%}
@@ -0,0 +1,109 @@
1
+ {%- assign item_attributes = item.attribute_elements -%}
2
+ {%- assign used_by_items = item.used_by -%}
3
+ {%- assign content_model_items = item.resolved_element_order -%}
4
+ {%- capture docs -%}
5
+ {% for paragraph in item.annotation.documentation -%}
6
+ {{ paragraph.content }}
7
+
8
+ {% endfor -%}
9
+ {%- endcapture -%}
10
+ {%- assign docs = docs | strip -%}
11
+ {%- capture used_by_list -%}
12
+ {% for used_by in used_by_items -%}
13
+ {%- capture used_by_kind -%}{% if used_by.element? %}element{% elsif used_by.attribute_group? %}attribute_group{% else %}type{% endif %}{%- endcapture %}
14
+ * {{ used_by.name }}{% render "xsd_ref", name: used_by.name, kind: used_by_kind, schema: schema %}
15
+ {% endfor -%}
16
+ {%- endcapture -%}
17
+ {%- assign used_by_list = used_by_list | strip -%}
18
+ {%- assign example_element = used_by_items | first -%}
19
+ {%- assign example_element_name = example_element.name -%}
20
+ {%- capture attribute_list -%}
21
+ {% for attribute in item_attributes -%}
22
+ {% render "attribute", attribute: attribute %}
23
+
24
+ {% endfor -%}
25
+ {%- endcapture -%}
26
+ {%- assign attribute_list = attribute_list | strip -%}
27
+ {%- capture content_model_list -%}
28
+ {% for child in content_model_items -%}
29
+ {% render "child_elements", child: child %}
30
+ {% endfor -%}
31
+ {%- endcapture -%}
32
+ {%- assign content_model_list = content_model_list | strip -%}
33
+ {%- capture example_attributes -%}
34
+ {% for attr in item_attributes -%}
35
+ {% render "xml_example_attribute", attr: attr -%}
36
+ {% endfor -%}
37
+ {%- endcapture -%}
38
+ {%- capture example_children -%}
39
+ {% if item.mixed -%}
40
+ <!-- mixed content -->
41
+ {% endif -%}
42
+ {% for child in content_model_items -%}
43
+ {% render "xml_example_child", child: child, indent: " " -%}
44
+ {% endfor -%}
45
+ {%- endcapture -%}
46
+
47
+ [[xsd_type_{{ item.name }}]]
48
+ === `{{ item.name }}`
49
+
50
+ ==== General
51
+
52
+ {% if docs != "" %}
53
+ {{ docs }}
54
+ {% else %}
55
+ No documentation supplied.
56
+ {% endif %}
57
+
58
+ {% if used_by_items.size > 0 %}
59
+ This complex type is used by:
60
+
61
+ {{ used_by_list }}
62
+ {% endif %}
63
+
64
+ {% unless diagrams == false %}
65
+ ==== Structure diagram
66
+
67
+ {% render "diagram", item: item, attributes: item_attributes, children: content_model_items, schema: schema %}
68
+ {% endunless %}
69
+
70
+ ==== Attributes
71
+
72
+ {% if item_attributes.size > 0 %}
73
+ {{ attribute_list }}
74
+ {% else %}
75
+ No attributes defined.
76
+ {% endif %}
77
+
78
+ ==== Content model
79
+
80
+ {% if item.mixed %}
81
+ This complex type allows mixed content.
82
+
83
+ {% endif %}
84
+ {% if content_model_list != "" %}
85
+ {{ content_model_list }}
86
+ {% else %}
87
+ No child elements defined.
88
+ {% endif %}
89
+
90
+ {% if example_element_name %}
91
+ [example]
92
+ .Illustrative element using `{{ item.name }}`
93
+ ====
94
+ [source,xml]
95
+ -----
96
+ <{{ example_element_name }}{{ example_attributes }}{% if example_children != "" %}>
97
+ {% if example_children != "" %}{{ example_children }}{% endif %}
98
+ </{{ example_element_name }}>
99
+ {% else %}/>
100
+ {% endif %}
101
+ -----
102
+ ====
103
+ {% endif %}
104
+
105
+ .XSD source of `{{ item.name }}`
106
+ [source,xml]
107
+ -----
108
+ {{ item.to_formatted_xml }}
109
+ -----
@@ -0,0 +1,25 @@
1
+ {%- assign root_id = "schema_" | append: item.name -%}
2
+ {%- if item.element? -%}{%- assign root_anchor = "xsd_" -%}{%- elsif item.attribute_group? -%}{%- assign root_anchor = "xsd_attribute_group_" -%}{%- else -%}{%- assign root_anchor = "xsd_type_" -%}{%- endif -%}
3
+ The diagram shows attributes and the content model in schema order.
4
+ Element edges show occurrence ranges; see the declarations below for all constraints.
5
+
6
+ .Structure of `{{ item.name }}`
7
+ [plantuml,format=svg]
8
+ ....
9
+ @startuml {{ root_anchor }}{{ item.name }}
10
+ !pragma layout smetana
11
+ left to right direction
12
+ hide empty members
13
+ hide circle
14
+ skinparam classAttributeIconSize 0
15
+ skinparam shadowing false
16
+ class "{{ item.name }}{% if item.element? and item.type %} : {{ item.type }}{% endif %}" as {{ root_id }} [[#{{ root_anchor }}{{ item.name }}]] {
17
+ {% for attribute in attributes %}
18
+ {%- assign attribute_name = attribute.referenced_name | default: attribute.name | default: attribute.ref -%}
19
+ {%- assign attribute_type = attribute.referenced_type | default: attribute.type -%}
20
+ @{{ attribute_name }}{% if attribute_type %} : {{ attribute_type }}{% endif %} [{{ attribute.cardinality | default: "0..1" }}]
21
+ {% endfor %}
22
+ }
23
+ {% render "diagram_children", children: children, parent_id: root_id, schema: schema %}
24
+ @enduml
25
+ ....
@@ -0,0 +1,31 @@
1
+ {%- assign position = 0 -%}
2
+ {% for child in children %}
3
+ {%- assign node_id = parent_id | append: "_" | append: forloop.index0 -%}
4
+ {%- assign minimum = child.min_occurs | default: child.min_occurrences | default: "1" -%}
5
+ {%- assign maximum = child.max_occurs | default: child.max_occurrences | default: "1" | replace: "unbounded", "*" -%}
6
+ {%- if child.annotation? or child.attribute? or child.attribute_group? -%}
7
+ {%- elsif child.element? -%}
8
+ {%- assign position = position | plus: 1 -%}
9
+ {%- assign element_name = child.referenced_name | default: child.name | default: child.ref -%}
10
+ {%- assign element_type = child.referenced_type | default: child.type -%}
11
+ {%- assign global_element = schema.elements_sorted_by_name | where: "name", element_name | first -%}
12
+ {%- assign element_type_name = element_type | split: ":" | last -%}
13
+ {%- assign global_type = schema.complex_types_sorted_by_name | where: "name", element_type_name | first -%}
14
+ class "{{ element_name }}{% if element_type %} : {{ element_type }}{% endif %}" as {{ node_id }}{% if global_element %} [[#xsd_{{ global_element.name }}]]{% elsif global_type %} [[#xsd_type_{{ global_type.name }}]]{% endif %}
15
+ {{ parent_id }} --> {{ node_id }} : {{ position }}. [{{ minimum }}..{{ maximum }}]
16
+ {%- elsif child.any? %}
17
+ {%- assign position = position | plus: 1 -%}
18
+ class "Any element\n{{ child.process_contents | default: "strict" }} processing" as {{ node_id }}
19
+ {{ parent_id }} --> {{ node_id }} : {{ position }}. [{{ minimum }}..{{ maximum }}]
20
+ {%- elsif child.simple_content? %}
21
+ {%- assign position = position | plus: 1 -%}
22
+ class "Simple content\n{{ child.base_type | default: child.base }}" as {{ node_id }}
23
+ {{ parent_id }} --> {{ node_id }}
24
+ {%- elsif child.resolved_element_order.size > 0 %}
25
+ {%- assign position = position | plus: 1 -%}
26
+ {%- capture label -%}{% if child.sequence? %}Sequence{% elsif child.choice? %}Choice{% elsif child.all? %}All{% elsif child.complex_content? %}Complex content{% else %}Group{% endif %}{%- endcapture %}
27
+ class "{{ label }}" as {{ node_id }}
28
+ {{ parent_id }} --> {{ node_id }} : {{ position }}.
29
+ {% render "diagram_children", children: child.resolved_element_order, parent_id: node_id, schema: schema %}
30
+ {% endif %}
31
+ {% endfor %}
@@ -0,0 +1,110 @@
1
+ {%- assign item_complex_type = item.referenced_complex_type -%}
2
+ {%- assign item_attributes = item.attributes -%}
3
+ {%- assign used_by_items = item.used_by -%}
4
+ {%- assign content_model_items = item_complex_type.resolved_element_order -%}
5
+ {%- capture docs -%}
6
+ {% for paragraph in item.annotation.documentation -%}
7
+ {{ paragraph.content }}
8
+
9
+ {% endfor -%}
10
+ {%- endcapture -%}
11
+ {%- assign docs = docs | strip -%}
12
+ {%- capture used_by_list -%}
13
+ {% for used_by in used_by_items -%}
14
+ {%- capture used_by_kind -%}{% if used_by.element? %}element{% elsif used_by.attribute_group? %}attribute_group{% else %}type{% endif %}{%- endcapture %}
15
+ * {{ used_by.name }}{% render "xsd_ref", name: used_by.name, kind: used_by_kind, schema: schema %}
16
+ {% endfor -%}
17
+ {%- endcapture -%}
18
+ {%- assign used_by_list = used_by_list | strip -%}
19
+ {%- capture attribute_list -%}
20
+ {% for attribute in item_attributes -%}
21
+ {% render "attribute", attribute: attribute %}
22
+
23
+ {% endfor -%}
24
+ {%- endcapture -%}
25
+ {%- assign attribute_list = attribute_list | strip -%}
26
+ {%- capture content_model_list -%}
27
+ {% for child in content_model_items -%}
28
+ {% render "child_elements", child: child %}
29
+ {% endfor -%}
30
+ {%- endcapture -%}
31
+ {%- assign content_model_list = content_model_list | strip -%}
32
+ {%- capture example_attributes -%}
33
+ {% for attr in item_attributes -%}
34
+ {% render "xml_example_attribute", attr: attr -%}
35
+ {% endfor -%}
36
+ {%- endcapture -%}
37
+ {%- capture example_children -%}
38
+ {% if item_complex_type.mixed -%}
39
+ <!-- mixed content -->
40
+ {% endif -%}
41
+ {% for child in content_model_items -%}
42
+ {% render "xml_example_child", child: child, indent: " " -%}
43
+ {% endfor -%}
44
+ {%- endcapture -%}
45
+
46
+ [[xsd_{{ item.name }}]]
47
+ === `<{{ item.name }}>`
48
+
49
+ ==== General
50
+
51
+ {% if docs != "" %}
52
+ {{ docs }}
53
+ {% else %}
54
+ No documentation supplied.
55
+ {% endif %}
56
+
57
+ {% if used_by_items.size > 0 %}
58
+ This element is used by:
59
+
60
+ {{ used_by_list }}
61
+ {% endif %}
62
+
63
+ {% unless diagrams == false %}
64
+ ==== Structure diagram
65
+
66
+ {% render "diagram", item: item, attributes: item_attributes, children: content_model_items, schema: schema %}
67
+ {% endunless %}
68
+
69
+ ==== Attributes
70
+
71
+ {% if item_attributes.size > 0 %}
72
+ {{ attribute_list }}
73
+ {% else %}
74
+ No attributes defined.
75
+ {% endif %}
76
+
77
+ ==== Content model
78
+
79
+ {% if item.type %}
80
+ This element uses the `{{ item.type }}` type{% render "xsd_ref", name: item.type, kind: "type", schema: schema %}.
81
+
82
+ {% endif %}
83
+ {% if item_complex_type.mixed %}
84
+ This element allows mixed content.
85
+
86
+ {% endif %}
87
+ {% if content_model_list != "" %}
88
+ {{ content_model_list }}
89
+ {% else %}
90
+ No child elements defined.
91
+ {% endif %}
92
+
93
+ [example]
94
+ .Illustrative usage of `<{{ item.name }}>`
95
+ ====
96
+ [source,xml]
97
+ -----
98
+ <{{ item.name }}{{ example_attributes }}{% if example_children != "" %}>
99
+ {% if example_children != "" %}{{ example_children }}{% endif %}
100
+ </{{ item.name }}>
101
+ {% else %}/>
102
+ {% endif %}
103
+ -----
104
+ ====
105
+
106
+ .XSD source of `<{{ item.name }}>`
107
+ [source,xml]
108
+ -----
109
+ {{ item.to_formatted_xml }}
110
+ -----
@@ -0,0 +1,2 @@
1
+ {%- assign attribute_name = attr.referenced_name | default: attr.name | default: attr.ref -%}
2
+ {%- assign attribute_value = attr.fixed | default: attr.default | default: attr.referenced_type | default: attr.type | default: "..." %} {{ attribute_name }}="{{ attribute_value }}"{%- comment -%}{%- endcomment -%}
@@ -0,0 +1,26 @@
1
+ {%- assign indent = indent | default: " " -%}
2
+ {%- assign child_indent = indent | append: " " -%}
3
+ {%- if child.annotation? or child.attribute? or child.attribute_group? -%}
4
+ {%- elsif child.sequence? -%}
5
+ {% for element in child.resolved_element_order -%}
6
+ {% render "xml_example_child", child: element, indent: indent %}
7
+ {% endfor -%}
8
+ {%- elsif child.choice? %}
9
+ {{ indent }}<!-- One of: -->
10
+ {% for element in child.resolved_element_order -%}
11
+ {% render "xml_example_child", child: element, indent: child_indent %}
12
+ {% endfor -%}
13
+ {%- elsif child.all? -%}
14
+ {% for element in child.resolved_element_order -%}
15
+ {% render "xml_example_child", child: element, indent: indent %}
16
+ {% endfor -%}
17
+ {%- elsif child.any? %}
18
+ {{ indent }}<!-- Any element from any namespace -->
19
+ {%- elsif child.element? -%}
20
+ {%- assign element_name = child.referenced_name | default: child.name | default: child.ref -%}
21
+ {{ indent }}<{{ element_name }}>...</{{ element_name }}>
22
+ {%- elsif child.child_elements.size > 0 -%}
23
+ {% for element in child.child_elements -%}
24
+ {% render "xml_example_child", child: element, indent: indent %}
25
+ {% endfor -%}
26
+ {%- endif -%}
@@ -0,0 +1,19 @@
1
+ {%- comment -%}
2
+ Cross-reference to a global schema component, rendered as " (<<anchor>>)".
3
+ Renders nothing when `name` is not a global component of `kind`
4
+ ("element", "type" or "attribute_group"). Namespace prefixes are ignored.
5
+ {%- endcomment -%}
6
+ {%- if name -%}
7
+ {%- assign ref_name = name | split: ":" | last -%}
8
+ {%- if kind == "type" -%}
9
+ {%- assign found = schema.complex_types_sorted_by_name | where: "name", ref_name | first -%}
10
+ {%- assign prefix = "xsd_type_" -%}
11
+ {%- elsif kind == "attribute_group" -%}
12
+ {%- assign found = schema.attribute_groups_sorted_by_name | where: "name", ref_name | first -%}
13
+ {%- assign prefix = "xsd_attribute_group_" -%}
14
+ {%- else -%}
15
+ {%- assign found = schema.elements_sorted_by_name | where: "name", ref_name | first -%}
16
+ {%- assign prefix = "xsd_" -%}
17
+ {%- endif -%}
18
+ {%- if found %} (<<{{ prefix }}{{ ref_name }}>>){% endif -%}
19
+ {%- endif -%}
@@ -0,0 +1,32 @@
1
+ {%- comment -%}
2
+ Default lutaml_xsd template: renders every global element, complex type and
3
+ attribute group of the schema. Pass `diagrams: false` to omit the PlantUML
4
+ structure diagrams.
5
+ {%- endcomment -%}
6
+ {%- assign elements = schema.elements_sorted_by_name -%}
7
+ {%- assign complex_types = schema.complex_types_sorted_by_name -%}
8
+ {%- assign attribute_groups = schema.attribute_groups_sorted_by_name -%}
9
+ {%- if elements.size > 0 %}
10
+ == Elements
11
+
12
+ {% for item in elements %}
13
+ {% render "element", item: item, schema: schema, diagrams: diagrams %}
14
+
15
+ {% endfor %}
16
+ {%- endif %}
17
+ {%- if complex_types.size > 0 %}
18
+ == Complex types
19
+
20
+ {% for item in complex_types %}
21
+ {% render "complex_type", item: item, schema: schema, diagrams: diagrams %}
22
+
23
+ {% endfor %}
24
+ {%- endif %}
25
+ {%- if attribute_groups.size > 0 %}
26
+ == Attribute groups
27
+
28
+ {% for item in attribute_groups %}
29
+ {% render "attribute_group", item: item, schema: schema, diagrams: diagrams %}
30
+
31
+ {% endfor %}
32
+ {%- endif %}
@@ -0,0 +1,136 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "lutaml/jsonschema"
4
+
5
+ module Metanorma
6
+ module Plugin
7
+ module Lutaml
8
+ # Preprocessor for JSON Schema files. Parses the schema through
9
+ # lutaml-jsonschema and renders documentation from its annotations
10
+ # (title, description, constraints).
11
+ #
12
+ # Three forms are supported:
13
+ # Default: lutaml_jsonschema::path[]
14
+ # renders the built-in reference (properties tables and
15
+ # definitions) as embeddable AsciiDoc
16
+ # Direct: lutaml_jsonschema::path[context, template, options]
17
+ # renders a Liquid template file with the schema as context
18
+ # Block: [lutaml_jsonschema, path, context, options] .... ----
19
+ # renders the inline Liquid template with the schema
20
+ class LutamlJsonschemaPreprocessor < BasePreprocessor
21
+ CACHE = CacheStore.new
22
+
23
+ JSONSCHEMA_PREPROCESSOR_REGEX = %r{
24
+ ^ # Start of line
25
+ \[ # Opening bracket
26
+ (?:\blutaml_jsonschema\b) # lutaml_jsonschema
27
+ , # Comma separator
28
+ (?<index_names>[^,]+)? # Schema path(s), ";"-separated
29
+ ,? # Optional comma
30
+ (?<context_name>[^,]+)? # Optional context name
31
+ (?<options>,.*)? # Optional options
32
+ \] # Closing bracket
33
+ }x
34
+
35
+ JSONSCHEMA_DIRECT_REGEX = %r{
36
+ ^\s* # Start of line
37
+ lutaml_jsonschema:: # Macro prefix
38
+ (?<file_path>[^\[]+?) # Schema file path
39
+ \[ # Opening bracket
40
+ (?<attributes>[^\]]*) # Optional context, template, options
41
+ \] # Closing bracket
42
+ \s*$ # End of line
43
+ }x
44
+
45
+ protected
46
+
47
+ def lutaml_liquid?(line)
48
+ line.match(JSONSCHEMA_PREPROCESSOR_REGEX)
49
+ end
50
+
51
+ def load_lutaml_file(document, file_path, _options)
52
+ load_schema_set(document, file_path).schemas.values.first
53
+ end
54
+
55
+ def index_type_name
56
+ "JSON Schema"
57
+ end
58
+
59
+ private
60
+
61
+ def process_text_blocks(document, input_lines, express_indexes)
62
+ match = input_lines.peek.match(JSONSCHEMA_DIRECT_REGEX)
63
+ return handle_direct_block(document, input_lines, match) if match
64
+
65
+ super
66
+ end
67
+
68
+ def handle_direct_block(document, input_lines, match)
69
+ input_lines.next # consume the matched line
70
+ file_path = match[:file_path].strip
71
+ render_direct(document, file_path, match[:attributes])
72
+ .split("\n", -1)
73
+ rescue StandardError => e
74
+ ::Metanorma::Util.log(
75
+ "[#{self.class.name}] Failed to render JSON Schema " \
76
+ "`#{file_path}`: #{e.message}", :error
77
+ )
78
+ raise e
79
+ end
80
+
81
+ def render_direct(document, file_path, attributes)
82
+ context_name, template_path, options =
83
+ parse_direct_attributes(attributes)
84
+ return render_default(document, file_path) unless context_name
85
+
86
+ render_template_file(document, file_path, context_name,
87
+ template_path, options)
88
+ end
89
+
90
+ def parse_direct_attributes(attributes)
91
+ named, positional = split_attributes(attributes)
92
+ if positional.size == 1
93
+ raise StandardError,
94
+ "lutaml_jsonschema:: needs both a context name and a " \
95
+ "template path, or neither (for the default reference)"
96
+ end
97
+
98
+ options = named.to_h { |attr| attr.split("=", 2).map(&:strip) }
99
+ [positional[0], positional[1], options]
100
+ end
101
+
102
+ def split_attributes(attributes)
103
+ attributes.to_s.split(",").map(&:strip).reject(&:empty?)
104
+ .partition { |attr| attr.include?("=") }
105
+ end
106
+
107
+ def render_default(document, file_path)
108
+ ::Lutaml::Jsonschema::Exports::Adoc
109
+ .new(load_schema_set(document, file_path), embeddable: true)
110
+ .render
111
+ end
112
+
113
+ def render_template_file(document, file_path, context_name,
114
+ template_path, options)
115
+ schema = load_lutaml_file(document, file_path, options)
116
+ template_file = Utils.relative_file_path(document, template_path)
117
+ tmpl = template([File.read(template_file, encoding: "UTF-8")])
118
+ tmpl.registers[:file_system] = build_file_system(document, options)
119
+ tmpl.assigns[context_name] = schema.to_liquid
120
+ tmpl.render
121
+ end
122
+
123
+ def load_schema_set(document, file_path)
124
+ full_path = Utils.relative_file_path(document, file_path)
125
+ unless File.file?(full_path)
126
+ raise StandardError, index_missing_message(file_path)
127
+ end
128
+
129
+ CACHE.fetch_or_store([full_path, File.mtime(full_path)]) do
130
+ ::Lutaml::Jsonschema::SchemaSet.load_from_files(full_path)
131
+ end
132
+ end
133
+ end
134
+ end
135
+ end
136
+ end
@@ -60,6 +60,7 @@ module Metanorma
60
60
  # repository: Liquid memoizes a drop class's invokable methods
61
61
  # on first render, so the listing method must exist by then.
62
62
  ExpressSchemaListing.apply
63
+ ExpressRemarkFigures.apply
63
64
  adapter.decorate_remarks(options)
64
65
  unwrapped
65
66
  end