Class: Octopress::Ink::Tags::CategoryTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/octopress-ink/tags/category_tag.rb

Instance Method Summary collapse

Constructor Details

#initialize(tag, input, tokens) ⇒ CategoryTag



5
6
7
8
9
10
11
# File 'lib/octopress-ink/tags/category_tag.rb', line 5

def initialize(tag, input, tokens)
  super
  @tag    = tag
  if !input.nil?
    @input  = input.strip
  end
end

Instance Method Details



65
66
67
68
69
70
# File 'lib/octopress-ink/tags/category_tag.rb', line 65

def item_link(page, item)
  if dir = Bootstrap.send(item_name, item, page['lang'])
    path = File.join(@context['site']['baseurl'] || '', dir)
    %Q{<a class="#{item_name}-link" href="#{path}">#{item.capitalize}</a>}
  end
end

#item_list?Boolean



53
54
55
# File 'lib/octopress-ink/tags/category_tag.rb', line 53

def item_list?
  @tag.end_with? '_list'
end

#item_nameObject



57
58
59
# File 'lib/octopress-ink/tags/category_tag.rb', line 57

def item_name
  @tag.match('tag') ? 'tag' : 'category'
end

#item_name_pluralObject



61
62
63
# File 'lib/octopress-ink/tags/category_tag.rb', line 61

def item_name_plural
  @tag.match('tag') ? 'tags' : 'categories'
end

#item_tags(page) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/octopress-ink/tags/category_tag.rb', line 31

def item_tags(page)
  items = page[item_name_plural]

  return nil if items.nil? || items.empty?

  items = items.sort.map { |i| item_link(page, i) }.compact.map do |link|
    if item_list?
      link = %Q{<li class="#{item_name}-list-item">#{link}</li>}
    end

    link
  end

  return nil if items.empty?

  if item_list?
    %Q{<ul class="#{item_name}-list">#{items.join}</ul>}
  else
    %Q{<span class="#{item_name}-links">#{items.join(', ')}</span>}
  end
end

#render(context) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/octopress-ink/tags/category_tag.rb', line 13

def render(context)
  @context = context

  # Check to see if post loop is active, otherwise default to current page
  page = context['post'] || context['page']

  tags = Bootstrap.send(@tag)[page['url']]

  if tags.nil?
    if tags = item_tags(page)
      # Cache tags to speed up multiple references for the same post
      Bootstrap.send(@tag)[page['url']] = tags
    end
  end

  tags
end