Permalink
Browse files

Merge branch 'post_url' of https://github.com/thatguystone/jekyll int…

…o thatguystone-post_url
  • Loading branch information...
mojombo committed Jan 23, 2012
2 parents c04a954 + 5cffe5e commit 16a4855200c7860fc741e152f77b3883c0e60f05
Showing with 76 additions and 0 deletions.
  1. +12 −0 features/create_sites.feature
  2. +38 −0 lib/jekyll/tags/post_url.rb
  3. +26 −0 test/test_tags.rb
@@ -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"
@@ -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)
View
@@ -9,6 +9,11 @@ def create_post(content, override = {}, converter_class = Jekyll::MarkdownConver
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,
@@ -172,4 +177,25 @@ def fill_post(code, override = {})
end
end
end
+
+ context "simple page with post linking" do
+ setup do
+ content = <<CONTENT
+---
+title: Post linking
+---
+
+{% post_url 2008-11-21-complex %}
+CONTENT
+ create_post(content, {'permalink' => '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

0 comments on commit 16a4855

Please sign in to comment.