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

Included in:
BasePreprocessor, BaseStructuredTextPreprocessor, LutamlEaXmiBase, SourceExtractor
Defined in:
lib/metanorma/plugin/lutaml/utils.rb

Overview

Helpers for lutaml macros

Defined Under Namespace

Modules: LiquidErrorCapturer

Constant Summary collapse

LUTAML_EXP_IDX_TAG =
%r{
  ^:lutaml-express-index: # Start of the pattern
  (?<index_name>.+?)      # Capture index name
  ;                       # Separator
  (?<index_path>.+?)      # Capture index path
  ;?                      # Optional separator
  (?<cache_group>         # Optional cache group
    \s*cache=             # Cache prefix
    (?<cache_path>.+)     # Capture cache path
  )?                      # End of optional group
  $                       # End of the pattern
}x

Class Method Summary collapse

Class Method Details

.create_liquid_environmentObject



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 86

def create_liquid_environment
  ::Liquid::Environment.new.tap do |liquid_env|
    liquid_env.register_tag(
      "keyiterator",
      ::Metanorma::Plugin::Lutaml::Liquid::CustomBlocks::KeyIterator,
    )
    liquid_env.register_filter(
      ::Metanorma::Plugin::Lutaml::Liquid::CustomFilters,
    )
  end
end

.error_signature(err) ⇒ Object



132
133
134
135
136
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 132

def error_signature(err)
  orig = err.is_a?(LiquidErrorCapturer::WithOriginal) && err.original_error
  base = "#{err.class}: #{err.message}"
  orig ? "#{base} (#{orig.class}: #{orig.message})" : base
end

.load_express_from_folder(folder) ⇒ Object



199
200
201
202
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 199

def load_express_from_folder(folder)
  file_paths = Dir["#{folder}/*.exp"]
  ::Expressir::Express::Parser.from_files(file_paths)
end

.load_express_from_index(_document, path) ⇒ Object

TODO: Refactor this using Suma::SchemaConfig



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 205

def load_express_from_index(_document, path) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  yaml_content = YAML.safe_load_file(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

  file_paths = yaml_content["schemas"].map do |key, value|
    schema_path = Pathname.new(value["path"] || "#{key}.exp")
    (schema_yaml_base_path + schema_path).cleanpath.to_s
  end

  ::Expressir::Express::Parser.from_files(file_paths)
end

.load_express_repo_from_cache(path) ⇒ Object



181
182
183
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 181

def load_express_repo_from_cache(path)
  ::Lutaml::Express::Parsers::Exp.parse_cache(path)
end

.load_express_repo_from_path(document, path) ⇒ Object



193
194
195
196
197
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 193

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

rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity



138
139
140
141
142
143
144
145
146
147
148
149
150
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
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 138

def load_express_repositories( # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  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_doc = 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_doc,
      document,
    )
  end

  lutaml_doc
rescue Expressir::Error, Expressir::Express::Error::ExpressError
  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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 108

def notify_render_errors(_document, errors)
  return if errors.empty?

  grouped = errors.group_by { |e| error_signature(e) }
  grouped.each do |sig, errs|
    total = errs.size
    already_seen = @seen_liquid_errors.include?(sig)
    @seen_liquid_errors << sig
    next if already_seen

    err = errs.first
    backtrace = err.backtrace&.first(3)&.join("\n").to_s
    count_label = total > 1 ? " (#{total}x)" : ""
    parts = ["[metanorma-plugin-lutaml] Liquid render error#{count_label}:"]
    parts << "  #{err.class}: #{err.message}"
    if err.is_a?(LiquidErrorCapturer::WithOriginal) && err.original_error
      orig = err.original_error
      parts << "  Caused by: #{orig.class}: #{orig.message}"
    end
    parts << backtrace unless backtrace.empty?
    ::Metanorma::Util.log(parts.join("\n"), :error)
  end
end

.parse_document_express_indexes(document, input_lines) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 224

def parse_document_express_indexes(document, input_lines) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  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_model = load_express_repositories(
      path: path,
      cache_path: cache,
      document: document,
    )

    if lutaml_expressir_model
      express_indexes[name] = { model: lutaml_expressir_model }
    end
  end

  express_indexes
rescue StopIteration
  express_indexes
ensure
  input_lines.rewind
end

.processed_lines(document, input_lines) ⇒ Object



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

def processed_lines(document, input_lines)
  result = []
  loop do
    result.push(*process_text_blocks(document, input_lines))
  end
  result
end

.relative_file_path(document, file_path) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 46

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(contexts:, document:, template_string: nil, include_path: nil, template_path: nil) ⇒ Object

rubocop:disable Metrics/MethodLength,Metrics/AbcSize



55
56
57
58
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
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 55

def render_liquid_string( # rubocop:disable Metrics/MethodLength,Metrics/AbcSize
  contexts:, document:,
  template_string: nil, include_path: nil, template_path: nil
)
  unless ::Liquid::Context <= LiquidErrorCapturer
    ::Liquid::Context.prepend(LiquidErrorCapturer)
  end

  # Allow includes for the template
  include_paths = [
    Utils.relative_file_path(document, ""),
    include_path, template_path
  ].compact

  if template_path
    template_string = File.read(template_path)
  end

  liquid_template = ::Liquid::Template
    .parse(template_string, environment: create_liquid_environment)
  liquid_template.registers[:file_system] =
    ::Metanorma::Plugin::Lutaml::Liquid::LocalFileSystem
      .new(include_paths, ["%s.liquid", "_%s.liquid", "_%s.adoc"])
  rendered_string = liquid_template
    .render(contexts,
            strict_variables: false,
            error_mode: :warn)

  [rendered_string, liquid_template.errors]
end

.save_express_repo_to_cache(path, repository, document) ⇒ Object



185
186
187
188
189
190
191
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 185

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