Class: Metanorma::Plugin::Glossarist::Liquid::LocalFileSystem

Inherits:
Object
  • Object
show all
Defined in:
lib/metanorma/plugin/glossarist/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.



10
11
12
13
# File 'lib/metanorma/plugin/glossarist/liquid/multiply_local_file_system.rb', line 10

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

Instance Attribute Details

#patternsObject

Returns the value of attribute patterns.



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

def patterns
  @patterns
end

#rootsObject

Returns the value of attribute roots.



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

def roots
  @roots
end

Instance Method Details

#full_path(template_path) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity



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
63
64
65
66
67
68
69
70
71
72
# File 'lib/metanorma/plugin/glossarist/liquid/multiply_local_file_system.rb', line 26

def full_path(template_path) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  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



15
16
17
18
19
20
21
22
23
24
# File 'lib/metanorma/plugin/glossarist/liquid/multiply_local_file_system.rb', line 15

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