Module: Metanorma::Plugin::Lutaml::Utils
- Included in:
- BaseStructuredTextPreprocessor, LutamlEaXmiBase, SourceExtractor
- Defined in:
- lib/metanorma/plugin/lutaml/utils.rb
Overview
Helpers for lutaml macros
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.freeze
Class Method Summary collapse
- .create_liquid_environment ⇒ Object
- .load_express_from_folder(folder) ⇒ Object
-
.load_express_from_index(_document, path) ⇒ Object
TODO: Refactor this using Suma::SchemaConfig.
- .load_express_repo_from_cache(path) ⇒ Object
- .load_express_repo_from_path(document, path) ⇒ Object
-
.load_express_repositories(path:, cache_path:, document:, force_read: false) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity.
- .notify_render_errors(_document, errors) ⇒ Object
-
.parse_document_express_indexes(document, input_lines) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity.
- .processed_lines(document, input_lines) ⇒ Object
- .relative_file_path(document, file_path) ⇒ Object
-
.render_liquid_string(template_string:, contexts:, document:, include_path: nil, template_path: nil) ⇒ Object
rubocop:disable Metrics/MethodLength,Metrics/AbcSize.
- .save_express_repo_to_cache(path, repository, document) ⇒ Object
Class Method Details
.create_liquid_environment ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 67 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 |
.load_express_from_folder(folder) ⇒ Object
159 160 161 162 163 164 |
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 159 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
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 167 def load_express_from_index(_document, path) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength 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
140 141 142 143 |
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 140 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
153 154 155 156 157 |
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 153 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
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 97 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 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.}", :error, ) raise e # nil end |
.notify_render_errors(_document, errors) ⇒ Object
87 88 89 90 91 92 93 94 95 |
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 87 def notify_render_errors(_document, errors) errors.each do |error_obj| ::Metanorma::Util.log( "[metanorma-plugin-lutaml] Liquid render error: " \ "#{error_obj.}", :error, ) end end |
.parse_document_express_indexes(document, input_lines) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 190 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
79 80 81 82 83 84 85 |
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 79 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
31 32 33 34 35 36 37 38 |
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 31 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:, contexts:, document:, include_path: nil, template_path: nil) ⇒ Object
rubocop:disable Metrics/MethodLength,Metrics/AbcSize
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 40 def render_liquid_string( # rubocop:disable Metrics/MethodLength,Metrics/AbcSize template_string:, contexts:, document:, include_path: nil, template_path: nil ) # 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
145 146 147 148 149 150 151 |
# File 'lib/metanorma/plugin/lutaml/utils.rb', line 145 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 |