Sha256: 781f630ce61c1b6358d8af3a6dc817bb74f850b2a3ee4e8bf812bc01416a376b

Contents?: true

Size: 1.99 KB

Versions: 18

Compression:

Stored size: 1.99 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)

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

            File.read(full_path)
          end

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

            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

            if result_path.nil?
              raise ::Liquid::FileSystemError,
                    "No documents in template path '#{File.expand_path(template_path)}'"
            end

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

            result_path
          end
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
metanorma-plugin-lutaml-0.7.16 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.7.15 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.7.14 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.7.13 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.7.12 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.7.11 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.7.10 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.7.9 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.7.8 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.7.7 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.7.6 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.7.5 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.7.4 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.7.3 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.7.2 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.7.1 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.7.0 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb
metanorma-plugin-lutaml-0.6.6 lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb