|
3 | 3 | module Jekyll
|
4 | 4 |
|
5 | 5 | class HamlPartialTag < Liquid::Tag
|
6 |
| - def initialize(tag_name, file, tokens) |
| 6 | + def initialize(tag_name, user_string, tokens) |
7 | 7 | super
|
8 |
| - @file = file.strip |
| 8 | + @user_string = user_string |
9 | 9 | end
|
10 | 10 |
|
11 | 11 | def render(context)
|
| 12 | + @user_string = Liquid::Template.parse(@user_string) |
| 13 | + .render(context) |
| 14 | + .gsub(%r{\"|\'},'') |
| 15 | + .strip |
| 16 | + |
| 17 | + file, arg = @user_string.split(' ') |
| 18 | + relative = arg.nil? ? false : !!(arg =~ /true/) |
| 19 | + |
12 | 20 | includes_dir = File.join(context.registers[:site].source, '_includes')
|
13 | 21 |
|
14 | 22 | if File.symlink?(includes_dir)
|
15 | 23 | return "Includes directory '#{includes_dir}' cannot be a symlink"
|
16 | 24 | end
|
17 | 25 |
|
18 |
| - if @file !~ /^[a-zA-Z0-9_\/\.-]+$/ || @file =~ /\.\// || @file =~ /\/\./ |
19 |
| - return "Include file '#{@file}' contains invalid characters or sequences" |
| 26 | + if file !~ /^[a-zA-Z0-9_\/\.-]+$/ || file =~ /\.\// || file =~ /\/\./ |
| 27 | + return "Include file '#{file}' contains invalid characters or sequences" |
20 | 28 | end
|
21 | 29 |
|
22 |
| - return "File must have \".haml\" extension" if @file !~ /\.haml$/ |
23 |
| - |
24 |
| - Dir.chdir(includes_dir) do |
25 |
| - choices = Dir['**/*'].reject { |x| File.symlink?(x) } |
26 |
| - if choices.include?(@file) |
27 |
| - source = File.read(@file) |
28 |
| - conversion = ::Haml::Engine.new(source).render.delete("\n") |
29 |
| - partial = Liquid::Template.parse(conversion) |
30 |
| - begin |
31 |
| - return partial.render!(context) |
32 |
| - rescue => e |
33 |
| - print "Liquid Exception: #{e.message}" |
34 |
| - print "in #{self.data["layout"]}" |
35 |
| - e.backtrace.each do |backtrace| |
36 |
| - puts backtrace |
37 |
| - end |
38 |
| - abort("Build Failed") |
39 |
| - end |
| 30 | + return "File must have \".haml\" extension" if file !~ /\.haml$/ |
40 | 31 |
|
41 |
| - context.stack do |
42 |
| - return partial.render(context) |
| 32 | + if relative |
| 33 | + include_file(file, context) |
| 34 | + else |
| 35 | + Dir.chdir(includes_dir) do |
| 36 | + choices = Dir['**/*'].reject { |x| File.symlink?(x) } |
| 37 | + if choices.include?(file) |
| 38 | + include_file(file, context) |
| 39 | + else |
| 40 | + "Included file '#{file}' not found in _includes directory" |
43 | 41 | end
|
44 |
| - else |
45 |
| - "Included file '#{@file}' not found in _includes directory" |
46 | 42 | end
|
47 | 43 | end
|
48 | 44 | end
|
49 |
| - end |
50 | 45 |
|
| 46 | + private |
| 47 | + |
| 48 | + def include_file(file, context) |
| 49 | + source = File.read(file) |
| 50 | + conversion = ::Haml::Engine.new(source).render.delete("\n") |
| 51 | + partial = Liquid::Template.parse(conversion) |
| 52 | + begin |
| 53 | + return partial.render!(context) |
| 54 | + rescue => e |
| 55 | + print "Liquid Exception: #{e.message}" |
| 56 | + print "in #{self.data["layout"]}" |
| 57 | + e.backtrace.each do |backtrace| |
| 58 | + puts backtrace |
| 59 | + end |
| 60 | + abort("Build Failed") |
| 61 | + end |
| 62 | + |
| 63 | + context.stack do |
| 64 | + return partial.render(context) |
| 65 | + end |
| 66 | + end |
| 67 | + end |
51 | 68 | end
|
52 | 69 |
|
53 | 70 | Liquid::Template.register_tag('haml', Jekyll::HamlPartialTag)
|
0 commit comments