root/platform/tdiary/plugin/category_to_tagcloud.rb

Revision 21386, 4.0 kB (checked in by sho, 7 weeks ago)

platform/tdiary/contrib/plugin/category_to_tagcroud.rb: converted charset EUC-JP to UTF-8.

Line 
1#
2# category_to_tagcloud.rb
3#
4# Usage:
5# <% tag_list n %>
6# n: 表示最大数(default: nil)
7#
8# options configurable through settings:
9#   @options['tagcloud.hidecss'] : cssの出力 default: false
10#
11# This plugin modifes and includes tagcloud-ruby.
12# http://yatsu.info/articles/2005/08/05/ruby%E3%81%A7tagcloud-tagcloud-ruby
13
14require 'pstore'
15
16def category_enable?
17        !@plugin_files.grep(/\/category\.rb$/).empty?
18end
19
20def add tag, url, count, elapsed_time
21        @counts[tag] = count
22        @urls[tag] = url
23        @elapsed_times[tag] = elapsed_time
24end
25
26def print_html
27        tags = @counts.sort_by {|a, b| b }.reverse.map {|a, b| a }
28        return '' if tags.empty?
29        tags = tags[0..@limit-1] if @limit
30
31        if tags.size == 1
32                tag = tags[0]
33                url = @urls[tag]
34                elapsed_time = @elapsed_times[tag]
35                return %{<ul class="tagcloud"><li class="tagcloud24#{elapsed_time}"><a title="#{tag}" href="#{url}">#{tag}</a></li></ul>\n}
36        end
37
38        min = Math.sqrt(@counts[tags.last])
39        max = Math.sqrt(@counts[tags.first])
40        factor = 0
41
42        # special case all tags having the same count
43        if max - min == 0
44                min = min - 24
45                factor = 1
46        else
47          factor = 24 / (max - min)
48        end
49
50        html = %{<ul class="tagcloud">}
51        tags.sort{|a,b| a.downcase <=> b.downcase}.each do |tag|
52                count = @counts[tag]
53                level = ((Math.sqrt(count) - min) * factor).to_i
54                html << %{<li class="tagcloud#{level}#{@elapsed_times[tag]}"><a title="#{tag} - #{count}" href="#{@urls[tag]}">#{tag}</a></li>\n}
55        end
56        html << "</ul>"
57        html
58end
59
60def init_category_to_tagcloud
61        @counts = Hash.new
62        @urls = Hash.new
63        @elapsed_times = Hash.new
64        @conf['category_to_tagcloud.cache'] ||= "#{@cache_path}/category2tagcloud.cache"
65        @limit = nil
66end
67
68def tag_list limit = nil
69        return '' if bot?
70        return '' unless category_enable?
71
72        init_category_to_tagcloud
73        cache = @conf['category_to_tagcloud.cache']
74        @limit = limit
75
76        PStore.new(cache).transaction(read_only=true) do |db|
77                break unless db.root?('tagcloud') or db.root?('last_update')
78                break if Time.now.strftime('%Y%m%d').to_i > db['last_update']
79                @counts = db['tagcloud'][0]
80                @urls = db['tagcloud'][1]
81                @elapsed_times = db['tagcloud'][2]
82        end
83
84        gen_tag_list if @urls.empty?
85        print_html
86end
87
88def styleclass diff
89        c = ' old'
90        if diff > 30
91                c = " oldest"
92        elsif diff > 14
93                c = " older"
94        elsif diff < 7
95                c = " hot"
96        end
97        c
98end
99
100def gen_tag_list
101        init_category_to_tagcloud if @mode == 'append' or @mode == 'replace'
102        cache = @conf['category_to_tagcloud.cache']
103        info = Category::Tagcloud_Info.new(@cgi, @years, @conf)
104        categorized = @category_cache.categorize(info.category, info.years)
105
106        categorized.keys.each do |key|
107                count = categorized[key].size
108                ymd = categorized[key].keys.sort.reverse
109                diff = (Time.now - Time.local(ymd[0][0,4], ymd[0][4,2], ymd[0][6,2])) / 86400
110                url = "#{@conf.index}?category=#{CGI.escape(key)}"
111                add(key, url, count, "#{styleclass(diff.to_i)}")
112        end
113
114        PStore.new(cache).transaction do |db|
115                db['last_update'] = Time.now.strftime('%Y%m%d').to_i
116                db['tagcloud'] = [@counts, @urls, @elapsed_times]
117        end
118end
119
120def tagcloud_css
121        r = ''
122        r = "\t<style type=\"text/css\">\n"
123        for level in 0..24
124                font = 12 + level
125                r << "\t.tagcloud li.tagcloud#{level} {font-size: #{font}px;}\n"
126        end
127
128        r << "\t.tagcloud {line-height:1}\n"
129        r << "\t.tagcloud ul {list-style-type:none;}\n"
130        r << "\t.tagcloud li {display:inline;}\n"
131        r << "\t.tagcloud li a {text-decoration:none;}\n"
132        r << "\t</style>\n"
133        r
134end
135
136add_update_proc do
137        gen_tag_list if @mode == 'append' or @mode == 'replace'
138end
139
140add_header_proc do
141        tagcloud_css unless @conf['tagcloud.hidecss']
142end
143
144
145if category_enable?
146        module Category
147                class Tagcloud_Info < Info
148                        def years
149                                now_year = Time.now.year
150                                now_month = Time.now.month
151                                r = Hash.new
152       
153                                months = [
154                                        ['01'],['01','02'],['01','02','03'],['02','03','04'],['03','04','05'],
155                                        ['04','05','06'],['05','06','07'],['06','07','08'],['07','08','09'],
156                                        ['08','09','10'],['09','10','11'],['10','11','12']
157                                ][now_month - 1]
158       
159                                r[now_year.to_s] = months
160                                case now_month
161                                when 1
162                                        r["#{now_year - 1}"] = ['11','12']
163                                when 2
164                                        r["#{now_year - 1}"] = ['12']
165                                end
166                                r
167                        end
168                end
169        end
170end
171
172## vim: ts=3
Note: See TracBrowser for help on using the browser.