Class: Jekyll::Tagslist::TagslistTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll-tagslist/tagslist_tag.rb

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, text, tokens) ⇒ TagslistTag

Returns a new instance of TagslistTag.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/jekyll-tagslist/tagslist_tag.rb', line 12

def initialize(tag_name, text, tokens)
  super

  @text = text

  # set default values
  @threshold = 1

  # Default: asc
  # Value: asc, desc
  @order_by = 'asc'

  # Default: name
  # Value: name, time, count
  @sort_by = 'count'

  # Default: 0, no limit
  # Value: integers
  @limit = 0

  @show_count = 1

  parse_params
end

Instance Method Details

#render(context) ⇒ Object

end of initialize



37
38
39
40
41
42
43
44
45
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
77
78
79
# File 'lib/jekyll-tagslist/tagslist_tag.rb', line 37

def render(context)
  tags = context.registers[:site].tags.map do |tag, posts|
    [tag, posts.count] if posts.count >= @threshold
  end

  tags.compact!

  if @sort_by.eql? 'time'
    if @order_by.eql? 'desc'
      tags.reverse
    end
  else
    tags.sort! do |tag1, tag2|
      sort_seq = 0
      if @sort_by.eql? 'name'
        sort_seq = 0
      elsif @sort_by.eql? 'count'
        sort_seq = 1
      end

      if @order_by.eql? 'asc'
        tag1[sort_seq] <=> tag2[sort_seq]
      else
        tag2[sort_seq] <=> tag1[sort_seq]
      end
    end
  end

  html = ""

  tag_count = 0

  tags.each do |tag, count|
    if tag_count > @limit && @limit != 0
      break
    end
    count_html = @show_count == 0 ? "" : "<div class=\"tag-item-count\">#{count}</div>"
    html << "<div class=\"tag-item\"><div class=\"tag-item-name\">#{tag}</div>#{count_html}</div>\n"
    tag_count = tag_count + 1
  end

  html
end