Module: FlexibleInclude::FlexibleClassMethods

Included in:
FlexibleInclude
Defined in:
lib/flexible_include_class.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.escape_html(string) ⇒ Object



9
10
11
12
13
14
# File 'lib/flexible_include_class.rb', line 9

def self.escape_html(string)
  string.gsub("&", "&")
        .gsub("{", "{")
        .gsub("}", "}")
        .gsub("<", "&lt;")
end

Instance Method Details

#access_allowed(path) ⇒ Object



3
4
5
6
7
# File 'lib/flexible_include_class.rb', line 3

def access_allowed(path)
  return true unless @read_regexes

  @read_regexes.find { |regex| regex.match(normalize_path(path)) }
end

#normalize_path(path) ⇒ Object



16
17
18
19
# File 'lib/flexible_include_class.rb', line 16

def normalize_path(path)
  ::JekyllSupport::JekyllPluginHelper.expand_env(path, die_if_undefined: true)
                                     .gsub('~', Dir.home)
end

#number_content(content) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/flexible_include_class.rb', line 21

def number_content(content)
  lines = content.split("\n")
  digits = lines.length.to_s.length
  i = 0
  numbered_content = lines.map do |line|
    i += 1
    number = i.to_s.rjust(digits, ' ')
    "<span class='unselectable numbered_line'> #{number}: </span>#{line}"
  end
  result = numbered_content.join "\n"
  result += "\n" unless result.end_with? "\n"
  result
end

#security_checkObject

If FLEXIBLE_INCLUDE_PATHS=‘~/lib/.:.:$WORK/.*’ Then @read_regexes will be set to regexes of [‘/home/my_user_id/lib/.*’, ‘/pwd/.*’, ‘/work/envar/path/.*’]



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/flexible_include_class.rb', line 37

def security_check
  @execution_denied = ENV.fetch('DISABLE_FLEXIBLE_INCLUDE', nil)

  return if @read_regexes

  flexible_include_paths = ENV.fetch('FLEXIBLE_INCLUDE_PATHS', nil)
  read_paths = normalize_path(flexible_include_paths) if flexible_include_paths
  return unless read_paths

  @read_regexes = read_paths.split(':').map do |path|
    abs_path = path.start_with?('/') ? path : (Pathname.new(Dir.pwd) + path).to_s
    Regexp.new(abs_path)
  end
end