Class: Jekyll::Tags::LocalizeInclude

Inherits:
IncludeTag
  • Object
show all
Defined in:
lib/jekyll/multiple/languages/plugin.rb

Instance Method Summary collapse

Instance Method Details

#render(context) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/jekyll/multiple/languages/plugin.rb', line 106

def render(context)
  if "#{context[@file]}" != "" #Check for page variable
    file = "#{context[@file]}"
  else
    file = @file
  end

  includes_dir = File.join(context.registers[:site].source, '_i18n/' + context.registers[:site].config['lang'])

  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
        contents = partial.render(context)
        site = context.registers[:site]
        ext = File.extname(file)

        converter = site.converters.find { |c| c.matches(ext) }
        contents = converter.convert(contents) unless converter.nil?

        contents
      end
    else
      "Included file '#{file}' not found in #{includes_dir} directory"
    end
  end
end