Module: Octopress::TagHelpers::Path

Defined in:
lib/octopress-tag-helpers/path.rb

Constant Summary collapse

FILE =
/(?<path>\S+)(\s?)(?<other>.*)/

Class Method Summary collapse

Class Method Details

.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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/octopress-tag-helpers/path.rb', line 23

def self.expand(file, context)
  root = context.registers[:site].source

  # If relative path, e.g. ./somefile, ../somefile
  if file =~ /^\.+\//
    page = context['page']
    local_dir = File.dirname(page['path'])
    File.expand_path(File.join(root, local_dir, file))

  # If absolute or relative to a user directory, e.g. /Users/Bob/somefile or ~/somefile
  elsif file =~ /^[\/~]/
    File.expand_path(file)

  # Otherwise, assume relative to site root
  else
    File.join(root, file)
  end
end

.parse(markup, context) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/octopress-tag-helpers/path.rb', line 5

def self.parse(markup, context)
  matched = markup.strip.match(FILE)
  if matched
    (context[matched['path']].nil? ? matched['path'] : context[matched['path']]) + ' ' + (matched['other'] || '')
  else
    markup
  end
end