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



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

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



76
77
78
79
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 76

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

.express_from_folder(folder) ⇒ Object



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

def 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

.express_from_index(document, path) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 113

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)|
    schema_values["path"] || File.join(root_path.to_s,
                                       "#{schema_name}.exp")
  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



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

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



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

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



43
44
45
46
47
48
49
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 43

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



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

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

    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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 51

def process_express_index(path, cache_path, document,
force_read = false)
  if cache_path
    cache_full_path = Utils.relative_file_path(document,
                                               cache_path)
  end
  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



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
# 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