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

.load_express_from_folder(folder) ⇒ Object



120
121
122
123
124
125
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 120

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

.load_express_from_index(_document, path) ⇒ Object

TODO: Refactor this using Suma::SchemaConfig



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 128

def load_express_from_index(_document, path)
  yaml_content = YAML.safe_load(File.read(path))
  schema_yaml_base_path = Pathname.new(File.dirname(path))

  # If there is a global root path set, all subsequent paths are
  # relative to it.
  if yaml_content["path"]
    root_schema_path = Pathname.new(yaml_content["path"])
    schema_yaml_base_path = schema_yaml_base_path + root_schema_path
  end

  files_to_load = yaml_content["schemas"].map do |key, value|
    # If there is no path: set for a schema, we assume it uses the
    # schema name as the #{filename}.exp.
    schema_path = Pathname.new(value["path"] || "#{key}.exp")

    real_schema_path = schema_yaml_base_path + schema_path
    File.new(real_schema_path.cleanpath.to_s, encoding: "UTF-8")
  end

  ::Lutaml::Parser.parse(files_to_load)
end

.load_express_repo_from_cache(path) ⇒ Object



101
102
103
104
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 101

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

.load_express_repo_from_path(document, path) ⇒ Object



114
115
116
117
118
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 114

def load_express_repo_from_path(document, path)
  return load_express_from_folder(path) if File.directory?(path)

  load_express_from_index(document, path)
end

.load_express_repositories(path:, cache_path:, document:, force_read: false) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 59

def load_express_repositories(path:, cache_path:, document:, force_read: false)

  cache_full_path = cache_path &&
    Utils.relative_file_path(document, cache_path)

  # If there is cache and "force read" not set.
  if !force_read && cache_full_path && File.file?(cache_full_path)
    return load_express_repo_from_cache(cache_full_path)
  end

  # If there is no cache or "force read" is set.
  full_path = Utils.relative_file_path(document, path)
  lutaml_wrapper = load_express_repo_from_path(document, full_path)

  if cache_full_path && !File.file?(cache_full_path)
    save_express_repo_to_cache(
      cache_full_path,
      lutaml_wrapper.original_document,
      document,
    )
  end

  lutaml_wrapper
rescue Expressir::Error
  FileUtils.rm_rf(cache_full_path)

  load_express_repositories(
    path: path,
    cache_path: cache_path,
    document: document,
    force_read: true,
  )
rescue StandardError => e
  ::Metanorma::Util.log(
    "[metanorma-plugin-lutaml] Failed to load " \
    "#{full_path}: #{e.message}",
    :error,
  )
  raise e
  # nil
end

.notify_render_errors(document, errors) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 49

def notify_render_errors(document, errors)
  errors.each do |error_obj|
    ::Metanorma::Util.log(
      "[metanorma-plugin-lutaml] Liquid render error: " \
      "#{error_obj.message}",
      :error,
    )
  end
end

.parse_document_express_indexes(document, input_lines) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 151

def parse_document_express_indexes(document, input_lines)
  express_indexes = {}
  loop do
    line = input_lines.next

    # Finished parsing document attributes
    break if line.empty?

    match = line.match(LUTAML_EXP_IDX_TAG)
    next unless match

    name = match[:index_name]&.strip
    path = match[:index_path]&.strip
    cache = match[:cache_path]&.strip

    unless name && path
      raise StandardError.new("No name and path set in `:lutaml-express-index:` attribute.")
    end

    lutaml_expressir_wrapper = load_express_repositories(
      path: path,
      cache_path: cache,
      document: document,
    )

    if lutaml_expressir_wrapper
      express_indexes[name] = {
        wrapper: lutaml_expressir_wrapper,
        serialized_hash: nil,
      }
    end
  end

  express_indexes
rescue StopIteration
  express_indexes
ensure
  input_lines.rewind
end

.relative_file_path(document, file_path) ⇒ Object



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

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 27

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

.save_express_repo_to_cache(path, repository, document) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 106

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