Class: Jekyll::FilePath

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll-filepath.rb

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ FilePath

Returns a new instance of FilePath.



5
6
7
8
# File 'lib/jekyll-filepath.rb', line 5

def initialize(tag_name, markup, tokens)
  @markup = markup.strip
  super
end

Instance Method Details

#render(context) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jekyll-filepath.rb', line 10

def render(context)
  if @markup.empty?
    return "Expected syntax {% filepath [filename] %}"
  end
  
  rawFilename = Liquid::Template.parse(@markup).render context
  filename = rawFilename.gsub(/^("|')|("|')$/, '')
  path = ""
  page = context.environments.first["page"]
  
  if page["id"]
    context.registers[:site].posts.docs.each do |post|
      if post.id == page["id"]
        path = "#{post.relative_path}".gsub(/_posts/, '').gsub(/.md/, '')
      end
    end
  else
    path = page["url"]
  end
  
  path = File.dirname(path) if path =~ /\.\w+$/
  "/files/#{path}/#{filename}".gsub(/\/{2,}/, '/')
end