-
-
Notifications
You must be signed in to change notification settings - Fork 9.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support a new relative_include
tag
#2870
Changes from 7 commits
aa97f10
787bb58
9f558d1
0e45490
934c37b
e7c8bbf
4da07bb
afd30b0
3668437
a569799
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,8 +18,6 @@ class IncludeTag < Liquid::Tag | |
VALID_SYNTAX = /([\w-]+)\s*=\s*(?:"([^"\\]*(?:\\.[^"\\]*)*)"|'([^'\\]*(?:\\.[^'\\]*)*)'|([\w\.-]+))/ | ||
VARIABLE_SYNTAX = /(?<variable>[^{]*\{\{\s*(?<name>[\w\-\.]+)\s*(\|.*)?\}\}[^\s}]*)(?<params>.*)/ | ||
|
||
INCLUDES_DIR = '_includes' | ||
|
||
def initialize(tag_name, markup, tokens) | ||
super | ||
matched = markup.strip.match(VARIABLE_SYNTAX) | ||
|
@@ -96,8 +94,12 @@ def render_variable(context) | |
end | ||
end | ||
|
||
def includes_dir | ||
'_includes' | ||
end | ||
|
||
def render(context) | ||
dir = File.join(File.realpath(context.registers[:site].source), INCLUDES_DIR) | ||
dir = dir_to_include(context) | ||
|
||
file = render_variable(context) || @file | ||
validate_file_name(file) | ||
|
@@ -113,10 +115,14 @@ def render(context) | |
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 | ||
|
||
def dir_to_include(context) | ||
File.join(File.realpath(context.registers[:site].source), includes_dir) | ||
end | ||
|
||
def validate_path(path, dir, safe) | ||
if safe && !realpath_prefixed_with?(path, dir) | ||
raise IOError.new "The included file '#{path}' should exist and should not be a symlink" | ||
|
@@ -126,7 +132,7 @@ def validate_path(path, dir, safe) | |
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 +144,19 @@ def source(file, context) | |
File.read(file, file_read_opts(context)) | ||
end | ||
end | ||
|
||
class IncludeRelativeTag < IncludeTag | ||
def includes_dir | ||
'.' | ||
end | ||
|
||
def dir_to_include(context) | ||
page_path = context.registers[:page].nil? ? includes_dir : File.dirname(context.registers[:page]["path"]) | ||
File.join(File.realpath(context.registers[:site].source), page_path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should ensure this path is sanitized properly. Would you please change this line to Jekyll.sanitized_path(context.registers[:site].source, page_path) ? The |
||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why use case statements instead of just overwrite certain methods based on the tag class? If you have a separate class, you may as well separate the class-specific code from each other. 😉 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. true.dat There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you want me to do that, I am happy to submit a PR to your PR. Or something? #howtogithub There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, I was planing on just killing this and relying more on |
||
end | ||
end | ||
|
||
Liquid::Template.register_tag('include', Jekyll::Tags::IncludeTag) | ||
Liquid::Template.register_tag('include_relative', Jekyll::Tags::IncludeRelativeTag) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
--- | ||
title: Post | ||
layout: post | ||
include1: rel_include.html | ||
include2: include_relative/rel_include | ||
include3: rel_INCLUDE | ||
include4: params | ||
include5: clude | ||
--- | ||
|
||
Liquid tests | ||
- 1 {% include_relative include_relative/{{ page.include1 }} %} | ||
- 2 {% include_relative {{ page.include2 | append: '.html' }} %} | ||
- 3 {% include_relative include_relative/{{ page.include3 | downcase | append: '.html' }} %} | ||
|
||
Whitespace tests | ||
- 4 {% include_relative include_relative/{{page.include1}} %} | ||
- 5 {% include_relative include_relative/{{ page.include1}} %} | ||
- 6 {% include_relative include_relative/{{ page.include3 | downcase | append: '.html'}} %} | ||
|
||
Parameters test | ||
- 7 {% include_relative include_relative/{{ page.include4 | append: '.html' }} var1='foo' var2='bar' %} | ||
|
||
Partial variable test | ||
- 8 {% include_relative include_relative/rel_in{{ page.include5 }}.html %} | ||
|
||
Relative to self test: | ||
|
||
- 9 {% include_relative 2014-03-03-yaml-with-dots.md %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
relative_included |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dir_to_include
? Are we including the whole dir? Or is itresolved_includes_dir
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The second one. 😊