Support a new `relative_include` tag

This commit is contained in:
Garen Torikian 2014-09-03 15:43:51 -07:00
parent 18eb8e6fb7
commit aa97f1025d
7 changed files with 90 additions and 8 deletions

View File

@ -18,9 +18,15 @@ module Jekyll
VALID_SYNTAX = /([\w-]+)\s*=\s*(?:"([^"\\]*(?:\\.[^"\\]*)*)"|'([^'\\]*(?:\\.[^'\\]*)*)'|([\w\.-]+))/
VARIABLE_SYNTAX = /(?<variable>[^{]*\{\{\s*(?<name>[\w\-\.]+)\s*(\|.*)?\}\}[^\s}]*)(?<params>.*)/
INCLUDES_DIR = '_includes'
def initialize(tag_name, markup, tokens)
@tag_name = tag_name
case tag_name
when 'include'
@includes_dir = '_includes'
when 'relative_include'
@includes_dir = ''
end
super
matched = markup.strip.match(VARIABLE_SYNTAX)
if matched
@ -97,8 +103,12 @@ eos
end
def render(context)
dir = File.join(File.realpath(context.registers[:site].source), INCLUDES_DIR)
case @tag_name
when 'include'
dir = File.join(File.realpath(context.registers[:site].source), @includes_dir)
when 'relative_include'
dir = File.join(File.realpath(context.registers[:site].source), File.dirname(context.registers[:page]["path"]))
end
file = render_variable(context) || @file
validate_file_name(file)
@ -113,7 +123,7 @@ eos
partial.render!(context)
end
rescue => e
raise IncludeTagError.new e.message, File.join(INCLUDES_DIR, @file)
raise IncludeTagError.new e.message, File.join(@includes_dir, @file)
end
end
@ -126,7 +136,7 @@ eos
end
def path_relative_to_source(dir, path)
File.join(INCLUDES_DIR, path.sub(Regexp.new("^#{dir}"), ""))
File.join(@includes_dir, path.sub(Regexp.new("^#{dir}"), ""))
end
def realpath_prefixed_with?(path, dir)
@ -138,7 +148,11 @@ eos
File.read(file, file_read_opts(context))
end
end
class RelativeIncludeTag < IncludeTag
end
end
end
Liquid::Template.register_tag('include', Jekyll::Tags::IncludeTag)
Liquid::Template.register_tag('relative_include', Jekyll::Tags::RelativeIncludeTag)

View File

@ -0,0 +1,25 @@
---
title: Post
layout: post
include1: rel_include.html
include2: relative_includes/rel_include
include3: rel_INCLUDE
include4: params
include5: clude
---
Liquid tests
- 1 {% relative_include relative_includes/{{ page.include1 }} %}
- 2 {% relative_include {{ page.include2 | append: '.html' }} %}
- 3 {% relative_include relative_includes/{{ page.include3 | downcase | append: '.html' }} %}
Whitespace tests
- 4 {% relative_include relative_includes/{{page.include1}} %}
- 5 {% relative_include relative_includes/{{ page.include1}} %}
- 6 {% relative_include relative_includes/{{ page.include3 | downcase | append: '.html'}} %}
Parameters test
- 7 {% relative_include relative_includes/{{ page.include4 | append: '.html' }} var1='foo' var2='bar' %}
Partial variable test
- 8 {% relative_include relative_includes/rel_in{{ page.include5 }}.html %}

View File

@ -0,0 +1,7 @@
<span id='include-param'>{{include.param}}</span>
<ul id='param-list'>
{% for param in include %}
<li>{{param[0]}} = {{param[1]}}</li>
{% endfor %}
</ul>

View File

@ -0,0 +1 @@
relative_included

View File

@ -14,7 +14,7 @@ class TestGeneratedSite < Test::Unit::TestCase
end
should "ensure post count is as expected" do
assert_equal 42, @site.posts.size
assert_equal 43, @site.posts.size
end
should "insert site.posts into the index" do

View File

@ -48,7 +48,7 @@ class TestSite < Test::Unit::TestCase
Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir})
end
@site = Site.new(Jekyll.configuration)
@num_invalid_posts = 2
@num_invalid_posts = 4
end
should "have an empty tag hash by default" do

View File

@ -509,5 +509,40 @@ CONTENT
assert_match %r{8 included}, @content
end
end
context "relative include tag with variable and liquid filters" do
setup do
stub(Jekyll).configuration do
site_configuration({'pygments' => true})
end
site = Site.new(Jekyll.configuration)
post = Post.new(site, source_dir, '', "2014-09-02-relative-includes.markdown")
layouts = { "default" => Layout.new(site, source_dir('_layouts'), "simple.html")}
post.render(layouts, {"site" => {"posts" => []}})
@content = post.content
end
should "include file as variable with liquid filters" do
assert_match %r{1 relative_include}, @content
assert_match %r{2 relative_include}, @content
assert_match %r{3 relative_include}, @content
end
should "include file as variable and liquid filters with arbitrary whitespace" do
assert_match %r{4 relative_include}, @content
assert_match %r{5 relative_include}, @content
assert_match %r{6 relative_include}, @content
end
should "include file as variable and filters with additional parameters" do
assert_match '<li>var1 = foo</li>', @content
assert_match '<li>var2 = bar</li>', @content
end
should "include file as partial variable" do
assert_match %r{8 relative_include}, @content
end
end
end
end