Sha256: 4b86bddac43fd46aed643227c661e6c7150479b7e745189fdf4d588e30fd7773

Contents?: true

Size: 1.66 KB

Versions: 20

Compression:

Stored size: 1.66 KB

Contents

module Metanorma
  module Plugin
    module Lutaml
      module Liquid
        class LocalFileSystem
          attr_accessor :roots, :patterns

          def initialize(roots, patterns = ["_%s.liquid"])
            @roots    = roots
            @patterns = patterns
          end

          def read_template_file(template_path)
            full_path = full_path(template_path)
            raise FileSystemError, "No such template '#{template_path}'" unless File.exist?(full_path)

            File.read(full_path)
          end

          def full_path(template_path)
            raise ::Liquid::FileSystemError, "Illegal template name '#{template_path}'" unless %r{\A[^./][a-zA-Z0-9_/]+\z}.match?(template_path)

            result_path = if template_path.include?('/')
              roots
                .map do |root|
                  patterns.map do |pattern|
                    File.join(root, File.dirname(template_path), pattern % File.basename(template_path))
                  end
                end
                .flatten
                .find { |path| File.file?(path) }
            else
              roots
                .map do |root|
                  patterns.map do |pattern|
                    File.join(root, pattern % template_path)
                  end
                end
                .flatten
                .find { |path| File.file?(path) }
            end

            unless roots.any? { |root| File.expand_path(result_path).start_with?(File.expand_path(root)) }
              raise ::Liquid::FileSystemError, "Illegal template path '#{File.expand_path(result_path)}'"
            end

            result_path
          end
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
metanorma-plugin-lutaml-0.6.0 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.5.0 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.4.20 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.4.19 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.4.18.2 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.4.18.1 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.4.18 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.4.17 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.4.15 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.4.14 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.4.13 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.4.12 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.4.11 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.4.10 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.4.9 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.4.8 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.4.7 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.4.6 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.4.5 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.4.4 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb