Class: LiquidPlus::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-liquid-plus/helpers/path.rb

Class Method Summary collapse

Class Method Details

.exists(file, context) ⇒ Object



59
60
61
62
# File 'lib/jekyll-liquid-plus/helpers/path.rb', line 59

def exists(file, context)
  path = expand(file, context)
  Cache.exists(path)
end

.expand(file, context) ⇒ Object

Allow paths to begin from the directory of the context page or have absolute paths

Input:

- file: "file.html"
- context: A Jekyll context object

Returns the full path to a file


24
25
26
27
28
29
30
31
32
33
34
# File 'lib/jekyll-liquid-plus/helpers/path.rb', line 24

def expand(file, context)
  root = context.registers[:site].source
  if file =~ /^\.\/(.+)/
    local_dir = File.dirname context.registers[:page]['path']
    File.join root, local_dir, $1
  elsif file =~ /^[\/~]/
    Pathname.new(file).expand_path
  else
    File.join root, file
  end
end

.get_paths(files, context) ⇒ Object

Read file paths from context variables if necessary

Input:

- file: file.html || some_var || path/to/file.md
- context: A Jekyll context object

Returns a list of file paths



72
73
74
75
76
77
# File 'lib/jekyll-liquid-plus/helpers/path.rb', line 72

def get_paths(files, context)
  files = files.split(/ (\|\||or) /).map do |file|
    file = file.strip
    context[file].nil? ? file : context[file]
  end
end

.parse(markup, context, path = nil) ⇒ Object



8
9
10
11
12
13
# File 'lib/jekyll-liquid-plus/helpers/path.rb', line 8

def parse(markup, context, path=nil)
  files = Conditional.parse(markup, context)
  if files
    select(files, context, path)
  end
end

.select(files, context, path) ⇒ Object

Find the first file which exists

Input:

- file: "file.html || some_var || path/to/file.md" - a list of files
- context: A Jekyll context object
- path: e.g. "_includes" - a directory to prepend to the path

Return the first file path which exists, or the last if none exist If the last path is ‘none’ this returns false



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/jekyll-liquid-plus/helpers/path.rb', line 46

def select(files, context, path)
  files = get_paths(files, context)
  files.each_with_index do |f, i|
    file = path ? File.join(path, f) : f
    if exists(file, context)
      return f
    # If "file.html || none" is passed, fail gracefully
    elsif i == files.size - 1
      return f.downcase == 'none' ? false : f
    end
  end
end