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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/jekyll/multiple/languages/plugin.rb', line 85

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