Class: Metanorma::Plugin::Lutaml::Liquid::LocalFileSystem

Inherits:
Object
  • Object
show all
Defined in:
lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(roots, patterns = ["_%s.liquid"]) ⇒ LocalFileSystem

Returns a new instance of LocalFileSystem.



8
9
10
11
# File 'lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb', line 8

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

Instance Attribute Details

#patternsObject

Returns the value of attribute patterns.



6
7
8
# File 'lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb', line 6

def patterns
  @patterns
end

#rootsObject

Returns the value of attribute roots.



6
7
8
# File 'lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb', line 6

def roots
  @roots
end

Instance Method Details

#full_path(template_path) ⇒ Object

Raises:

  • (::Liquid::FileSystemError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb', line 20

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

#read_template_file(template_path) ⇒ Object

Raises:

  • (FileSystemError)


13
14
15
16
17
18
# File 'lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb', line 13

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