Class: LiquidPlus::IncludeTag

Inherits:
Jekyll::Tags::IncludeTag
  • Object
show all
Defined in:
lib/jekyll-liquid-plus/tags/include.rb

Constant Summary collapse

LOCALS =
/(.*?)(([\w-]+)\s*=\s*(?:"([^"\\]*(?:\\.[^"\\]*)*)"|'([^'\\]*(?:\\.[^'\\]*)*)'|([\w\.-]+)))(.*)?/

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ IncludeTag

Returns a new instance of IncludeTag.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/jekyll-liquid-plus/tags/include.rb', line 34

def initialize(tag_name, markup, tokens)
  @tag = tag_name.strip
  @markup = markup.strip
  # If raw, strip from the markup as not to confuse the Path syntax parsing
  if @markup =~ /^(\s*raw\s)?(.+?)(\sraw\s*)?$/
    @markup = $2.strip
    @raw = true unless $1.nil? and $3.nil?
  end

  split_params
end

Instance Method Details

#default_renderObject



31
# File 'lib/jekyll-liquid-plus/tags/include.rb', line 31

alias_method :default_render, :render

#get_path(context) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/jekyll-liquid-plus/tags/include.rb', line 106

def get_path(context)
  root = (@tag == 'include') ? INCLUDES_DIR : nil
  if file = Path.parse(@markup, context, root)
    file = Path.expand(file, context) if @tag =~ /render/
    @markup = file
    file
  end
end

#include_tag(context) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/jekyll-liquid-plus/tags/include.rb', line 58

def include_tag(context)
  @markup = @file
  @markup += @params if @params
  @page = context.registers[:page]['path']
  content = default_render(context)
  parse_convertible(content, context)
end

#not_found(context) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/jekyll-liquid-plus/tags/include.rb', line 84

def not_found(context)
  @file = File.join(INCLUDES_DIR, @file) if @tag == 'include'
  name = File.basename(@file)
  dir  = @file.to_s.sub(context.registers[:site].source + '/', '').sub(name, '')
  msg = "File '#{name}' not found"
  msg +=  " in '#{dir}'" if dir.length > 0 && name != dir
  raise IOError.new msg
end

#parse_convertible(content, context) ⇒ Object



93
94
95
96
97
# File 'lib/jekyll-liquid-plus/tags/include.rb', line 93

def parse_convertible(content, context)
  page = Jekyll::ConvertiblePage.new(context.registers[:site], @file, content)
  page.render({})
  content = page.output.strip
end

#read_rawObject



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/jekyll-liquid-plus/tags/include.rb', line 123

def read_raw
  path = Pathname.new @file
  Dir.chdir(File.dirname(path)) do
    content = path.read
    if content =~ /\A-{3}(.+[^\A])-{3}\n(.+)/m
      @local_vars = YAML.safe_load($1.strip)
      content = $2.strip
    end
    content
  end
end

#render(context) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/jekyll-liquid-plus/tags/include.rb', line 46

def render(context)
  validate_params if @params
  @file = get_path(context)
  if @tag == 'include' && @file && Cache.exists(Path.expand(File.join(INCLUDES_DIR, @file), context))
    include_tag(context)
  elsif @tag =~ /render/ && @file && Cache.exists(@file)
    render_tag(context)
  elsif @file
    not_found(context)
  end
end

#render_tag(context) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/jekyll-liquid-plus/tags/include.rb', line 66

def render_tag(context)
  @markup = @file
  @markup += @params if @params
  content = read_raw

  unless @raw
    partial = Liquid::Template.parse(content)

    content = context.stack do
      context['render'] = Jekyll::Tags::IncludeTag.new('include', @markup, []).parse_params(context) if @params
      context['page'] = context['page'].deep_merge(@local_vars) if @local_vars and @local_vars.keys.size > 0
      partial.render!(context)
    end
    content = parse_convertible(content, context)
  end
  content
end

#split_paramsObject



99
100
101
102
103
104
# File 'lib/jekyll-liquid-plus/tags/include.rb', line 99

def split_params
  if @markup =~ LOCALS
    @params = ' ' + $2
    @markup = $1 + $7
  end
end

#strip_yaml(content) ⇒ Object

Strip out yaml header from partials



116
117
118
119
120
121
# File 'lib/jekyll-liquid-plus/tags/include.rb', line 116

def strip_yaml(content)
  if content =~ /\A-{3}(.+[^\A])-{3}\n(.+)/m
    $2.strip
  end
  content
end