From 5cffe5ecb5194ef0e26e66ced1e2ef5285a934e7 Mon Sep 17 00:00:00 2001 From: Andrew Stone Date: Thu, 21 Jul 2011 19:49:09 -0400 Subject: [PATCH] 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 %}) --- features/create_sites.feature | 12 +++++++++++ lib/jekyll/tags/post_url.rb | 38 +++++++++++++++++++++++++++++++++++ test/test_tags.rb | 26 ++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 lib/jekyll/tags/post_url.rb diff --git a/features/create_sites.feature b/features/create_sites.feature index a7b5d9b1..8496a4e2 100644 --- a/features/create_sites.feature +++ b/features/create_sites.feature @@ -92,3 +92,15 @@ Feature: Create sites When I debug jekyll Then the _site directory should exist And I should see "Basic Site with include tag: Generated by Jekyll" in "_site/index.html" + + Scenario: Basic site with internal post linking + Given I have an "index.html" page that contains "URL: {% post_url 2020-01-31-entry2 %}" + And I have a configuration file with "permalink" set to "pretty" + And I have a _posts directory + And I have the following posts: + | title | date | layout | content | + | entry1 | 12/31/2007 | post | content for entry1. | + | entry2 | 01/31/2020 | post | content for entry2. | + When I run jekyll + Then the _site directory should exist + And I should see "URL: /2020/01/31/entry2/" in "_site/index.html" diff --git a/lib/jekyll/tags/post_url.rb b/lib/jekyll/tags/post_url.rb new file mode 100644 index 00000000..21d3a0b4 --- /dev/null +++ b/lib/jekyll/tags/post_url.rb @@ -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) diff --git a/test/test_tags.rb b/test/test_tags.rb index 50750757..cdd395b0 100644 --- a/test/test_tags.rb +++ b/test/test_tags.rb @@ -9,6 +9,11 @@ class TestTags < Test::Unit::TestCase Jekyll::DEFAULTS.merge({'pygments' => true}).merge(override) end site = Site.new(Jekyll.configuration) + + if override['read_posts'] + site.read_posts('') + end + info = { :filters => [Jekyll::Filters], :registers => { :site => site } } @converter = site.converters.find { |c| c.class == converter_class } payload = { "pygments_prefix" => @converter.pygments_prefix, @@ -137,4 +142,25 @@ CONTENT end end end + + context "simple page with post linking" do + setup do + content = < 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true}) + end + + should "not cause an error" do + assert_no_match /markdown\-html\-error/, @result + end + + should "have the url to the \"complex\" post from 2008-11-21" do + assert_match %r{/2008/11/21/complex/}, @result + end + end end