1
+ # Tag Cloud for Octopress, modified by pf_miles, for use with utf-8 encoded blogs(all regexp added 'u' option).
2
+ # modified by alswl, tag_cloud -> category_cloud
3
+ # =======================
4
+ #
5
+ # Description:
6
+ # ------------
7
+ # Easy output tag cloud and category list.
8
+ #
9
+ # Syntax:
10
+ # -------
11
+ # {% tag_cloud [counter:true] %}
12
+ # {% category_list [counter:true] %}
13
+ #
14
+ # Example:
15
+ # --------
16
+ # In some template files, you can add the following markups.
17
+ #
18
+ # ### source/_includes/custom/asides/tag_cloud.html ###
19
+ #
20
+ # <section>
21
+ # <h1>Tag Cloud</h1>
22
+ # <span id="tag-cloud">{% tag_cloud %}</span>
23
+ # </section>
24
+ #
25
+ # ### source/_includes/custom/asides/category_list.html ###
26
+ #
27
+ # <section>
28
+ # <h1>Categories</h1>
29
+ # <ul id="category-list">{% category_list counter:true %}</ul>
30
+ # </section>
31
+ #
32
+ # Notes:
33
+ # ------
34
+ # Be sure to insert above template files into `default_asides` array in `_config.yml`.
35
+ # And also you can define styles for 'tag-cloud' or 'category-list' in a `.scss` file.
36
+ # (ex: `sass/custom/_styles.scss`)
37
+ #
38
+ # Licence:
39
+ # --------
40
+ # Distributed under the [MIT License][MIT].
41
+ #
42
+ # [MIT]: http://www.opensource.org/licenses/mit-license.php
43
+ #
44
+ module Jekyll
45
+
46
+ class CategoryCloud < Liquid ::Tag
47
+
48
+ def initialize ( tag_name , markup , tokens )
49
+ @opts = { }
50
+ if markup . strip =~ /\s *counter:(\w +)/iu
51
+ @opts [ 'counter' ] = ( $1 == 'true' )
52
+ markup = markup . strip . sub ( /counter:\w +/iu , '' )
53
+ end
54
+ super
55
+ end
56
+
57
+ def render ( context )
58
+ lists = { }
59
+ max , min = 1 , 1
60
+ config = context . registers [ :site ] . config
61
+ category_dir = config [ 'root' ] + config [ 'category_dir' ] + '/'
62
+ categories = context . registers [ :site ] . categories
63
+ categories . keys . sort_by { |str | str . downcase } . each do |category |
64
+ count = categories [ category ] . count
65
+ lists [ category ] = count
66
+ max = count if count > max
67
+ end
68
+
69
+ html = ''
70
+ lists . each do | category , counter |
71
+ url = category_dir + category . gsub ( /_|\P {Word}/u , '-' ) . gsub ( /-{2,}/u , '-' ) . downcase
72
+ style = "font-size: #{ 100 + ( 60 * Float ( counter ) /max ) } %"
73
+ html << "<a href='#{ url } ' style='#{ style } '>#{ category } "
74
+ if @opts [ 'counter' ]
75
+ html << "(#{ categories [ category ] . count } )"
76
+ end
77
+ html << "</a> "
78
+ end
79
+ html
80
+ end
81
+ end
82
+
83
+ class CategoryList < Liquid ::Tag
84
+
85
+ def initialize ( tag_name , markup , tokens )
86
+ @opts = { }
87
+ if markup . strip =~ /\s *counter:(\w +)/iu
88
+ @opts [ 'counter' ] = ( $1 == 'true' )
89
+ markup = markup . strip . sub ( /counter:\w +/iu , '' )
90
+ end
91
+ super
92
+ end
93
+
94
+ def render ( context )
95
+ html = ""
96
+ config = context . registers [ :site ] . config
97
+ category_dir = config [ 'root' ] + config [ 'category_dir' ] + '/'
98
+ categories = context . registers [ :site ] . categories
99
+ categories . keys . sort_by { |str | str . downcase } . each do |category |
100
+ url = category_dir + category . gsub ( /_|\P {Word}/u , '-' ) . gsub ( /-{2,}/u , '-' ) . downcase
101
+ html << "<li><a href='#{ url } '>#{ category } "
102
+ if @opts [ 'counter' ]
103
+ html << " (#{ categories [ category ] . count } )"
104
+ end
105
+ html << "</a></li>"
106
+ end
107
+ html
108
+ end
109
+ end
110
+
111
+ end
112
+
113
+ Liquid ::Template . register_tag ( 'category_cloud' , Jekyll ::CategoryCloud )
114
+ Liquid ::Template . register_tag ( 'category_list' , Jekyll ::CategoryList )
0 commit comments