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



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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/metanorma/plugin/lutaml/liquid/multiply_local_file_system.rb', line 23

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

#read_template_file(template_path) ⇒ Object



13
14
15
16
17
18
19
20
21
# 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)

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

  File.read(full_path)
end