Adding in the ability to link to posts internally. Syntax: {% post_url 2010-07-21-name-of-post %}; useful for: [Some Link]({% post_url 2010-07-21-name-of-post %})

This commit is contained in:
Andrew Stone
2011-07-21 19:49:09 -04:00
parent 72b7b1f056
commit 5cffe5ecb5
3 changed files with 76 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
module Jekyll
class PostComparer
MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)$/
attr_accessor :date, :slug
def initialize(name)
who, cares, date, slug = *name.match(MATCHER)
@slug = slug
@date = Time.parse(date)
end
end
class PostUrl < Liquid::Tag
def initialize(tag_name, post, tokens)
super
@orig_post = post.strip
@post = PostComparer.new(@orig_post)
end
def render(context)
site = context.registers[:site]
site.posts.each do |p|
if p == @post
return p.url
end
end
puts "ERROR: post_url: \"#{@orig_post}\" could not be found"
return "#"
end
end
end
Liquid::Template.register_tag('post_url', Jekyll::PostUrl)