Class: Jekyll::Tags::IncludeTag
- Inherits:
-
Liquid::Tag
- Object
- Liquid::Tag
- Jekyll::Tags::IncludeTag
- Defined in:
- lib/jekyll/tags/include.rb
Constant Summary collapse
- SYNTAX_EXAMPLE =
"{% include file.ext param='value' param2='value' %}"
- VALID_SYNTAX =
/([\w-]+)\s*=\s*(?:"([^"\\]*(?:\\.[^"\\]*)*)"|'([^'\\]*(?:\\.[^'\\]*)*)'|([\w\.-]+))/
- INCLUDES_DIR =
'_includes'
Instance Method Summary collapse
- #blank? ⇒ Boolean
-
#file_read_opts(context) ⇒ Object
Grab file read opts in the context.
-
#initialize(tag_name, markup, tokens) ⇒ IncludeTag
constructor
A new instance of IncludeTag.
- #parse_params(context) ⇒ Object
- #render(context) ⇒ Object
- #retrieve_variable(context) ⇒ Object
-
#source(file, context) ⇒ Object
This method allows to modify the file content by inheriting from the class.
- #validate_dir(dir, safe) ⇒ Object
- #validate_file(file, safe) ⇒ Object
- #validate_file_name(file) ⇒ Object
- #validate_params ⇒ Object
Constructor Details
#initialize(tag_name, markup, tokens) ⇒ IncludeTag
Returns a new instance of IncludeTag.
20 21 22 23 24 |
# File 'lib/jekyll/tags/include.rb', line 20 def initialize(tag_name, markup, tokens) super @file, @params = markup.strip.split(' ', 2); validate_params if @params end |
Instance Method Details
#blank? ⇒ Boolean
125 126 127 |
# File 'lib/jekyll/tags/include.rb', line 125 def blank? false end |
#file_read_opts(context) ⇒ Object
Grab file read opts in the context
78 79 80 |
# File 'lib/jekyll/tags/include.rb', line 78 def file_read_opts(context) context.registers[:site].file_read_opts end |
#parse_params(context) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/jekyll/tags/include.rb', line 26 def parse_params(context) params = {} markup = @params while match = VALID_SYNTAX.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
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/jekyll/tags/include.rb', line 89 def render(context) dir = File.join(context.registers[:site].source, INCLUDES_DIR) validate_dir(dir, context.registers[:site].safe) file = retrieve_variable(context) || @file validate_file_name(file) path = File.join(dir, file) validate_file(path, context.registers[:site].safe) begin partial = Liquid::Template.parse(source(path, context)) context.stack do context['include'] = parse_params(context) if @params partial.render!(context) end rescue => e raise IncludeTagError.new e., File.join(INCLUDES_DIR, @file) end end |
#retrieve_variable(context) ⇒ Object
82 83 84 85 86 87 |
# File 'lib/jekyll/tags/include.rb', line 82 def retrieve_variable(context) if /\{\{([\w\-\.]+)\}\}/ =~ @file raise ArgumentError.new("No variable #{$1} was found in include tag") if context[$1].nil? context[$1] end end |
#source(file, context) ⇒ Object
This method allows to modify the file content by inheriting from the class.
130 131 132 |
# File 'lib/jekyll/tags/include.rb', line 130 def source(file, context) File.(file, file_read_opts(context)) end |
#validate_dir(dir, safe) ⇒ Object
111 112 113 114 115 |
# File 'lib/jekyll/tags/include.rb', line 111 def validate_dir(dir, safe) if File.symlink?(dir) && safe raise IOError.new "Includes directory '#{dir}' cannot be a symlink" end end |
#validate_file(file, safe) ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/jekyll/tags/include.rb', line 117 def validate_file(file, safe) if !File.exists?(file) raise IOError.new "Included file '#{@file}' not found in '#{INCLUDES_DIR}' directory" elsif File.symlink?(file) && safe raise IOError.new "The included file '#{INCLUDES_DIR}/#{@file}' should not be a symlink" end end |
#validate_file_name(file) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/jekyll/tags/include.rb', line 46 def validate_file_name(file) if file !~ /^[a-zA-Z0-9_\/\.-]+$/ || file =~ /\.\// || file =~ /\/\./ raise ArgumentError.new <<-eos Invalid syntax for include tag. File contains invalid characters or sequences: #{@file} Valid syntax: #{SYNTAX_EXAMPLE} eos end end |
#validate_params ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/jekyll/tags/include.rb', line 61 def validate_params full_valid_syntax = Regexp.compile('\A\s*(?:' + VALID_SYNTAX.to_s + '(?=\s|\z)\s*)*\z') unless @params =~ full_valid_syntax raise ArgumentError.new <<-eos Invalid syntax for include tag: #{@params} Valid syntax: #{SYNTAX_EXAMPLE} eos end end |