Class: Jekyll::Tags::HamlIncludeTag

Inherits:
IncludeTag
  • Object
show all
Defined in:
lib/jekyll-haml/tags/haml_partial.rb

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, file, tokens) ⇒ HamlIncludeTag

Returns a new instance of HamlIncludeTag.



6
7
8
9
# File 'lib/jekyll-haml/tags/haml_partial.rb', line 6

def initialize(tag_name, file, tokens)
  super
  @file = file.strip
end

Instance Method Details

#load_cached_partial(path, context) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jekyll-haml/tags/haml_partial.rb', line 11

def load_cached_partial(path, context)
  context.registers[:cached_partials] ||= {}
  cached_partial = context.registers[:cached_partials]

  if cached_partial.key?(path)
    cached_partial[path]
  else
    html = ::Haml::Engine.new(File.read(path)).render.delete("\n")          
    unparsed_file = context.registers[:site]
      .liquid_renderer
      .file(path)
    begin
      cached_partial[path] = unparsed_file.parse(html)
    rescue => e
      e.template_name = path
      e.markup_context = "haml " if e.markup_context.nil?
      raise e
    end
  end
end

#render(context) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/jekyll-haml/tags/haml_partial.rb', line 32

def render(context)
  if @file !~ /^[a-zA-Z0-9_\/\.-]+$/ || @file =~ /\.\// || @file =~ /\/\./
    return "Include file '#{@file}' contains invalid characters or sequences"
  end
  return "File must have \".haml\" extension" if @file !~ /\.haml$/
  
  site = context.registers[:site]
  path = locate_include_file(context, @file, site.safe)
  return unless path
  add_include_to_dependency(site, path, context)
  
  partial = load_cached_partial(path, context)

  context.stack do
    begin
      partial.render!(context)
    rescue Liquid::Error => e
      e.template_name = path
      e.markup_context = "included " if e.markup_context.nil?
      raise e
    end
  end
end