Module: Metanorma::Plugin::Lutaml::XmiRenderer

Included in:
LutamlEaXmiBase
Defined in:
lib/metanorma/plugin/lutaml/xmi_renderer.rb

Constant Summary collapse

LIQUID_INCLUDE_PATH =
File.join(
  Gem.loaded_specs["metanorma-plugin-lutaml"].full_gem_path,
  "lib", "metanorma", "plugin", "lutaml", "liquid_templates"
)
DEFAULT_RENDER_INCLUDE =
"packages"
RENDER_STYLES_INCLUDES =
{
  "default" => "packages",
  "entity_list" => "packages_entity_list",
  "entity_list_class" => "packages_entity_list_class",
  "data_dictionary" => "packages_data_dictionary",
}.freeze
RENDER_STYLE_ATTRIBUTE =
"render_style"

Instance Method Summary collapse

Instance Method Details

#get_name_path(attrs) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/metanorma/plugin/lutaml/xmi_renderer.rb', line 90

def get_name_path(attrs)
  return attrs["path"] if attrs["path"]

  return "#{attrs['package']}::#{attrs['name']}" if attrs["package"]

  attrs["name"]
end

#get_template(document, attrs) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/metanorma/plugin/lutaml/xmi_renderer.rb', line 79

def get_template(document, attrs)
  tmpl = get_default_template
  tmpl = attrs["template"] if attrs["template"]

  rel_tmpl_path = Utils.relative_file_path(
    document, tmpl
  )

  ::Liquid::Template.parse(File.read(rel_tmpl_path))
end

#model_representation(lutaml_doc, document, add_context, options) ⇒ Object

rubocop:disable Metrics/MethodLength



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/metanorma/plugin/lutaml/xmi_renderer.rb', line 22

def model_representation(lutaml_doc, document, add_context, options) # rubocop:disable Metrics/MethodLength
  fill_in_entities_refs_attributes(document, lutaml_doc, options)
  render_result, errors = Utils.render_liquid_string(
    template_string: template(options.section_depth || 2,
                              options.render_style,
                              options.include_root),
    contexts: create_context_object(lutaml_doc,
                                    add_context,
                                    options,
                                    "context"),
    document: document,
    include_path: template_path(document, options.template_path),
  )
  Utils.notify_render_errors(document, errors)
  render_result.split("\n")
end

#render_table(context, context_name, parent, attrs) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/metanorma/plugin/lutaml/xmi_renderer.rb', line 59

def render_table(context, context_name, parent, attrs) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  table_tmpl = get_template(parent.document, attrs)
  table_tmpl.assigns[context_name] = context

  if attrs["external_data"]
    data_array = attrs["external_data"].split(";")
    data_array.each do |data_item|
      context_name, external_data_path = data_item.split(":")
      external_data = content_from_file(
        parent.document, external_data_path.strip
      )
      table_tmpl.assigns[context_name.strip] = external_data
    end
  end

  rendered_table = table_tmpl.render
  block = create_open_block(parent, "", attrs)
  parse_content(block, rendered_table, attrs)
end

#template(section_depth, render_style, include_root) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/metanorma/plugin/lutaml/xmi_renderer.rb', line 45

def template(section_depth, render_style, include_root)
  include_name = RENDER_STYLES_INCLUDES.fetch(render_style,
                                              DEFAULT_RENDER_INCLUDE)
  result = ""
  if include_root
    result += <<~LIQUID
      {% 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 %}
    LIQUID
  end
  result + <<~LIQUID
    {% 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 %}
  LIQUID
end

#template_path(document, tmpl_path) ⇒ Object



39
40
41
42
43
# File 'lib/metanorma/plugin/lutaml/xmi_renderer.rb', line 39

def template_path(document, tmpl_path)
  return LIQUID_INCLUDE_PATH if tmpl_path.nil?

  Utils.relative_file_path(document, tmpl_path)
end