Class: Jekyll::Tags::IncludeTag
- Inherits:
-
Liquid::Tag
- Object
- Liquid::Tag
- Jekyll::Tags::IncludeTag
- Defined in:
- lib/jekyll/tags/include.rb
Constant Summary collapse
- MATCHER =
/([\w-]+)\s*=\s*(?:"([^"\\]*(?:\\.[^"\\]*)*)"|'([^'\\]*(?:\\.[^'\\]*)*)'|([\w\.-]+))/
Instance Method Summary collapse
-
#initialize(tag_name, markup, tokens) ⇒ IncludeTag
constructor
A new instance of IncludeTag.
- #parse_params(context) ⇒ Object
- #render(context) ⇒ Object
-
#validate_syntax ⇒ Object
ensure the entire markup string from start to end is valid syntax, and params are separated by spaces.
Constructor Details
#initialize(tag_name, markup, tokens) ⇒ IncludeTag
Returns a new instance of IncludeTag.
7 8 9 10 |
# File 'lib/jekyll/tags/include.rb', line 7 def initialize(tag_name, markup, tokens) super @file, @params = markup.strip.split(' ', 2); end |
Instance Method Details
#parse_params(context) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/jekyll/tags/include.rb', line 12 def parse_params(context) validate_syntax params = {} markup = @params while match = MATCHER.match(markup) do markup = markup[match.end(0)..-1] value = if match[2] match[2].gsub(/\\"/, '"') elsif match[3] match[3].gsub(/\\'/, "'") elsif match[4] context[match[4]] end params[match[1]] = value end params end |
#render(context) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/jekyll/tags/include.rb', line 51 def render(context) includes_dir = File.join(context.registers[:site].source, '_includes') if File.symlink?(includes_dir) return "Includes directory '#{includes_dir}' cannot be a symlink" end if @file !~ /^[a-zA-Z0-9_\/\.-]+$/ || @file =~ /\.\// || @file =~ /\/\./ return "Include file '#{@file}' contains invalid characters or sequences" end Dir.chdir(includes_dir) do choices = Dir['**/*'].reject { |x| File.symlink?(x) } if choices.include?(@file) source = File.read(@file) partial = Liquid::Template.parse(source) context.stack do context['include'] = parse_params(context) if @params partial.render(context) end else "Included file '#{@file}' not found in _includes directory" end end end |
#validate_syntax ⇒ Object
ensure the entire markup string from start to end is valid syntax, and params are separated by spaces
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/jekyll/tags/include.rb', line 35 def validate_syntax full_matcher = Regexp.compile('\A\s*(?:' + MATCHER.to_s + '(?=\s|\z)\s*)*\z') unless @params =~ full_matcher raise SyntaxError.new <<-eos Invalid syntax for include tag: #{@params} Valid syntax: {% include file.ext param='value' param2="value" %} eos end end |