Module: Metanorma::Plugin::Lutaml::Utils

Defined in:
lib/metanorma/plugin/lutaml/utils.rb

Overview

Helpers for lutaml macros

Constant Summary collapse

LUTAML_EXP_IDX_TAG =
/^:lutaml-express-index:(?<index_name>.+?);(?<index_path>.+?);?(\s*cache=(?<cache_path>.+))?$/.freeze

Class Method Summary collapse

Class Method Details

.express_decorate_wrapper(wrapper, document) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 92

def express_decorate_wrapper(wrapper, document)
  serialized = wrapper.to_liquid
  serialized["schemas"] = serialized["schemas"].map do |j|
    j.merge("relative_path_prefix" => Utils.relative_file_path(document, File.dirname(j["file"])))
  end
  serialized
end

.express_from_cache(path) ⇒ Object



66
67
68
69
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 66

def express_from_cache(path)
  ::Lutaml::Parser
    .parse(File.new(path), ::Lutaml::Parser::EXPRESS_CACHE_PARSE_TYPE)
end

.express_from_folder(folder) ⇒ Object



87
88
89
90
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 87

def express_from_folder(folder)
  files = Dir["#{folder}/*.exp"].map { |nested_path| File.new(nested_path, encoding: "UTF-8") }
  ::Lutaml::Parser.parse(files)
end

.express_from_index(document, path) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 100

def express_from_index(document, path)
  yaml_content = YAML.safe_load(File.read(path))
  root_path = yaml_content["path"]
  schemas_paths = yaml_content
    .fetch("schemas")
    .map do |(schema_name, schema_values)|
    if schema_values["path"]
      schema_values["path"]
    else
      File.join(root_path.to_s, "#{schema_name}.exp")
    end
  end
  files_to_load = schemas_paths.map do |path|
    File.new(Utils.relative_file_path(document, path), encoding: "UTF-8")
  end
  ::Lutaml::Parser.parse(files_to_load)
end

.express_from_path(document, path) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 79

def express_from_path(document, path)
  if File.directory?(path)
    return express_from_folder(path)
  end

  express_from_index(document, path)
end

.express_write_cache(path, repository, document) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 71

def express_write_cache(path, repository, document)
  root_path = Pathname.new(relative_file_path(document, ""))
  Expressir::Express::Cache
    .to_file(path,
             repository,
             root_path: root_path)
end

.notify_render_errors(document, errors) ⇒ Object



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

def notify_render_errors(document, errors)
  errors.each do |error_obj|
    document
      .logger
      .warn("Liquid render error: #{error_obj.message}")
  end
end

.parse_document_express_indexes(document, input_lines) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 118

def parse_document_express_indexes(document, input_lines)
  express_indexes = {}
  loop do
    line = input_lines.next
    break if line.length.zero?

    match = line.match(LUTAML_EXP_IDX_TAG)
    if match
      name = match[:index_name]
      path = match[:index_path]
      cache = match[:cache_path]
      repositories = process_express_index(path.strip, cache, document)
      if repositories
        express_indexes[name.strip] = express_decorate_wrapper(repositories, document)
      end
    end
  end
  express_indexes
rescue StopIteration
  express_indexes
ensure
  input_lines.rewind
end

.process_express_index(path, cache_path, document, force_read = false) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 46

def process_express_index(path, cache_path, document, force_read = false)
  cache_full_path = Utils.relative_file_path(document, cache_path) if cache_path
  if !force_read && cache_full_path && File.file?(cache_full_path)
    return express_from_cache(cache_full_path)
  end

  full_path = Utils.relative_file_path(document, path)
  wrapper = express_from_path(document, full_path)
  if cache_full_path && !File.file?(cache_full_path)
    express_write_cache(cache_full_path, wrapper.original_document, document)
  end
  wrapper
rescue Expressir::Error
  FileUtils.rm_rf(cache_full_path)
  process_express_index(path, cache_path, document, true)
rescue StandardError => e
  document.logger.warn("Failed to load #{full_path}: #{e.message}")
  nil
end

.relative_file_path(document, file_path) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 16

def relative_file_path(document, file_path)
  docfile_directory = File.dirname(
    document.attributes["docfile"] || "."
  )
  document
    .path_resolver
    .system_path(file_path, docfile_directory)
end

.render_liquid_string(template_string:, context_items:, context_name:, document:, include_path: nil) ⇒ Object



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

def render_liquid_string(template_string:, context_items:,
                         context_name:, document:, include_path: nil)
  liquid_template = ::Liquid::Template.parse(template_string)
  # Allow includes for the template
  include_paths = [Utils.relative_file_path(document, ""), include_path].compact
  liquid_template.registers[:file_system] = ::Metanorma::Plugin::Lutaml::Liquid::LocalFileSystem.new(include_paths, ["_%s.liquid", "_%s.adoc"])
  rendered_string = liquid_template
    .render(context_name => context_items,
            strict_variables: true,
            error_mode: :warn)
  [rendered_string, liquid_template.errors]
end