Skip to content

Commit 16a4855

Browse files
committed
Merge branch 'post_url' of https://github.com/thatguystone/jekyll into thatguystone-post_url
2 parents c04a954 + 5cffe5e commit 16a4855

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

features/create_sites.feature

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,15 @@ Feature: Create sites
9292
When I debug jekyll
9393
Then the _site directory should exist
9494
And I should see "Basic Site with include tag: Generated by Jekyll" in "_site/index.html"
95+
96+
Scenario: Basic site with internal post linking
97+
Given I have an "index.html" page that contains "URL: {% post_url 2020-01-31-entry2 %}"
98+
And I have a configuration file with "permalink" set to "pretty"
99+
And I have a _posts directory
100+
And I have the following posts:
101+
| title | date | layout | content |
102+
| entry1 | 12/31/2007 | post | content for entry1. |
103+
| entry2 | 01/31/2020 | post | content for entry2. |
104+
When I run jekyll
105+
Then the _site directory should exist
106+
And I should see "URL: /2020/01/31/entry2/" in "_site/index.html"

lib/jekyll/tags/post_url.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module Jekyll
2+
3+
class PostComparer
4+
MATCHER = /^(.+\/)*(\d+-\d+-\d+)-(.*)$/
5+
6+
attr_accessor :date, :slug
7+
8+
def initialize(name)
9+
who, cares, date, slug = *name.match(MATCHER)
10+
@slug = slug
11+
@date = Time.parse(date)
12+
end
13+
end
14+
15+
class PostUrl < Liquid::Tag
16+
def initialize(tag_name, post, tokens)
17+
super
18+
@orig_post = post.strip
19+
@post = PostComparer.new(@orig_post)
20+
end
21+
22+
def render(context)
23+
site = context.registers[:site]
24+
25+
site.posts.each do |p|
26+
if p == @post
27+
return p.url
28+
end
29+
end
30+
31+
puts "ERROR: post_url: \"#{@orig_post}\" could not be found"
32+
33+
return "#"
34+
end
35+
end
36+
end
37+
38+
Liquid::Template.register_tag('post_url', Jekyll::PostUrl)

test/test_tags.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ def create_post(content, override = {}, converter_class = Jekyll::MarkdownConver
99
Jekyll::DEFAULTS.merge({'pygments' => true}).merge(override)
1010
end
1111
site = Site.new(Jekyll.configuration)
12+
13+
if override['read_posts']
14+
site.read_posts('')
15+
end
16+
1217
info = { :filters => [Jekyll::Filters], :registers => { :site => site } }
1318
@converter = site.converters.find { |c| c.class == converter_class }
1419
payload = { "pygments_prefix" => @converter.pygments_prefix,
@@ -172,4 +177,25 @@ def fill_post(code, override = {})
172177
end
173178
end
174179
end
180+
181+
context "simple page with post linking" do
182+
setup do
183+
content = <<CONTENT
184+
---
185+
title: Post linking
186+
---
187+
188+
{% post_url 2008-11-21-complex %}
189+
CONTENT
190+
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
191+
end
192+
193+
should "not cause an error" do
194+
assert_no_match /markdown\-html\-error/, @result
195+
end
196+
197+
should "have the url to the \"complex\" post from 2008-11-21" do
198+
assert_match %r{/2008/11/21/complex/}, @result
199+
end
200+
end
175201
end

0 commit comments

Comments
 (0)