Class: Octopress::Tags::Include::Tag

Inherits:
Jekyll::Tags::IncludeTag
  • Object
show all
Defined in:
lib/octopress-include-tag.rb

Constant Summary collapse

PLUGIN_SYNTAX =
/(?<plugin>.+?):(?<path>\S+)\s?(?<other>.*)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ Tag

Returns a new instance of Tag.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/octopress-include-tag.rb', line 15

def initialize(tag_name, markup, tokens)
  @tag_markup = markup
  @tag_name = tag_name

  if matched = markup.strip.match(PLUGIN_SYNTAX)
    @plugin = matched['plugin'].strip
    @path = matched['path'].strip
  end

  # Trigger Jekyll's Include tag with compatible markup
  #
  super(tag_name, safe_markup(markup).join(' '), tokens)
end

Instance Attribute Details

#filtersObject

Returns the value of attribute filters.



13
14
15
# File 'lib/octopress-include-tag.rb', line 13

def filters
  @filters
end

#pathObject

Returns the value of attribute path.



10
11
12
# File 'lib/octopress-include-tag.rb', line 10

def path
  @path
end

#tag_markupObject (readonly)

Returns the value of attribute tag_markup.



11
12
13
# File 'lib/octopress-include-tag.rb', line 11

def tag_markup
  @tag_markup
end

#tag_nameObject (readonly)

Returns the value of attribute tag_name.



12
13
14
# File 'lib/octopress-include-tag.rb', line 12

def tag_name
  @tag_name
end

Instance Method Details

#parse_markup(context) ⇒ Object

Parses special markup, handling vars, conditions, and filters Returns:

- include path or nil if markup conditionals evaluate false


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/octopress-include-tag.rb', line 82

def parse_markup(context)
  # If conditional statements are present, only continue if they are true
  #
  return unless markup = TagHelpers::Conditional.parse(tag_markup, context)

  # If there are filters, store them for use later and strip them out of markup
  #
  if matched = markup.match(TagHelpers::Var::HAS_FILTERS)
    markup = matched['markup']
    @filters = matched['filters']
  end

  # If there is a ternary expression, replace it with the true result
  #
  markup = TagHelpers::Var.evaluate_ternary(markup, context)

  # Paths may be variables, check context to retrieve proper path
  #
  markup = TagHelpers::Path.parse(markup, context)

  markup
end

#render(context) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/octopress-include-tag.rb', line 46

def render(context)

  # Parse special markup until markup is simplified
  return unless markup = parse_markup(context)

  # If markup references a plugin e.g. plugin-name:include-file.html
  #
  if matched = markup.strip.match(PLUGIN_SYNTAX)

    # Call Octopress Ink to render the plugin's include file
    #
    content = render_ink_include(matched['plugin'], matched['path'], context)
    
  else

    # use Jekyll's default include tag
    #
    # Why safe_markup again? In initialize we didn't know what the path would be becuase 
    # we needed the context to parse vars and conditions. Now that we know them, we'll 
    # reset @file and @params as intended in order to render with Jekyll's include tag.
    # 
    @file, @params = safe_markup(markup)
    content = super(context).strip
  end

  unless content.nil? || filters.nil?
    content = TagHelpers::Var.render_filters(content, filters, context)
  end

  content
end

#render_ink_include(plugin, file, context) ⇒ Object

Call Octopress Ink to render the plugin’s include file



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/octopress-include-tag.rb', line 107

def render_ink_include(plugin, file, context)
  begin
    content = Octopress::Ink::Plugins.include(plugin, path).read
  rescue => error
    msg = "Include failed: {% #{tag_name} #{tag_markup}%}.\n"
    if !defined?(Octopress::Ink)
      msg += "To include plugin partials, first install Octopress Ink."
    else
      msg += "The plugin '#{plugin}' does not have an include named '#{path}'."
    end
    raise IOError.new(msg)
  end

  partial = Liquid::Template.parse(content)

  context.stack {
    context['include'] = parse_params(context)
    context['plugin'] = Octopress::Ink::Plugins.plugin(plugin).config(context['lang'])
    partial.render!(context)
  }.strip
end

#safe_markup(markup) ⇒ Object

Strip specials out of markup so that it is suitable for Jekyll include tag



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/octopress-include-tag.rb', line 31

def safe_markup(markup)
  file = markup.strip.match(/\S+/)[0]
  params = []

  markup.scan(Regexp.compile('('+VALID_SYNTAX.to_s+')+')).each do |param|
    params << param.first
  end

  if matched = tag_markup.match(VARIABLE_SYNTAX)
    file = matched['variable']
  end

  [file, params.join(' ')]
end