mirror of
https://github.com/jekyll/jekyll.git
synced 2026-04-28 03:01:03 -04:00
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:
38
lib/jekyll/tags/post_url.rb
Normal file
38
lib/jekyll/tags/post_url.rb
Normal 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)
|
||||
Reference in New Issue
Block a user