root/platform/tdiary/plugin/section_footer.rb

Revision 37332, 6.5 kB (checked in by drry, 2 years ago)
  • fixed regexes.
  • fixed HTML.
  • et cetera.
Line 
1# -*- coding: utf-8 -*-
2# section_footer.rb
3#
4# Copyright (c) 2005 SHIBATA Hiroshi <h-sbt@nifty.com>
5# You can redistribute it and/or modify it under GPL2.
6#
7
8require 'digest/md5'
9require 'open-uri'
10require 'timeout'
11
12def permalink( date, index, escape = true )
13        ymd = date.strftime( "%Y%m%d" )
14        uri = @conf.index.dup
15        uri.sub!( %r|\A(?!https?://)|i, @conf.base_url )
16        uri.gsub!( %r|/\.(?=/)|, "" ) # /././ -> /
17        link = uri + anchor( "#{ymd}p%02d" % index )
18        link.sub!( "#", "%23" ) if escape
19        link
20end
21
22add_header_proc do
23        <<-SCRIPT
24        <link rel="stylesheet" href="theme/hatena_bookmark.css" type="text/css" media="all">
25        <script type="text/javascript" src="http://b.hatena.ne.jp/js/BookmarkCommentViewerAllInOne.1.2.js" charset="utf-8"></script>
26        <script type="text/javascript">
27        initCreateRelAfterIcon();
28        BookmarkCommentViewer.options['dateFormat'] = '%y-%m-%d';
29        // BookmarkCommentViewer.options['blankCommentHide'] = true;
30        BookmarkCommentViewer.options['tags'] = false;
31        </script>
32        SCRIPT
33end
34
35add_section_enter_proc do |date, index|
36        @category_to_tag_list = {}
37end
38
39alias subtitle_link_original subtitle_link
40def subtitle_link( date, index, subtitle )
41        s = ''
42        if subtitle then
43                s = subtitle.sub( /^(\[([^\[]+?)\])+/ ) do
44                        $&.scan( /\[(.*?)\]/ ) do |tag|
45                                @category_to_tag_list[tag] = false # false when diary
46                        end
47                        ''
48                end
49        end
50        subtitle_link_original( date, index, s.strip )
51end
52
53add_section_leave_proc do |date, index|
54        r = '<div class="tags">'
55
56        unless @conf.mobile_agent? then
57                # カテゴリタグの追加
58                if @category_to_tag_list and not @category_to_tag_list.empty? then
59                        r << "Tags: "
60                        @category_to_tag_list.each do |tag, blog|
61                                if blog
62                                        r << %Q|<a href="#{@index}?blogcategory=#{h tag}">#{tag}</a> |
63                                else
64                                        r << category_anchor( "#{tag}" ).sub( /^\[/, '' ).sub( /\]$/, '' ) << ' '
65                                end
66                        end
67                end
68
69                # 「このエントリの Delicious history (JSON)」
70                r << add_delicious_json(date, index)
71
72                # 「このエントリを含む Delicious (画像API)」
73                # r << add_delicious(date, index)
74
75                # 「このエントリを含むはてなブックーク」
76                r << add_hatenabm(date, index)
77
78                # 「このエントリを含む livedoor クリップ」
79                r << add_ldclip(date, index)
80
81                # 「このエントリを含む Buzzurl」
82                r << add_buzzurl(date, index)
83
84                # 「このエントリを含む Yahoo!ブックマーク」
85                r << add_yahoobm(date, index)
86
87                # Permalinkの追加
88                r << add_permalink(date, index)
89        end
90
91        r << "</div>\n"
92end
93
94def add_permalink(date, index)
95        r = " | "
96        r << %Q|<a href="#{permalink(date, index, false)}">Permalink</a> |
97        return r
98end
99
100def add_hatenabm(date, index)
101        r = " | "
102        r << %Q|<a href="http://b.hatena.ne.jp/entry/#{permalink(date, index)}"><img src="http://d.hatena.ne.jp/images/b_entry.gif" style="border: 0 none;vertical-align: middle;" title="#{@section_footer_hatenabm_label}" alt="#{@section_footer_hatenabm_label}" width="16" height="12"> <img src="http://b.hatena.ne.jp/entry/image/normal/#{permalink(date, index)}" style="border: 0 none;vertical-align: middle;"></a> <img src="http://r.hatena.ne.jp/images/popup.gif" style="border: 0 none;vertical-align: middle;" onclick="iconImageClickHandler(this, '#{permalink(date, index, false)}', event);" alt="">|
103        return r
104end
105
106def add_ldclip(date, index)
107        r = " | "
108        r << %Q|<a href="http://clip.livedoor.com/page/#{permalink(date, index)}"><img src="http://parts.blog.livedoor.jp/img/cmn/clip_16_16_w.gif" width="16" height="16" style="border: 0 none;vertical-align: middle;" alt="#{@section_footer_ldclip_label}" title="#{@section_footer_ldclip_label}"> <img src="http://image.clip.livedoor.com/counter/#{permalink(date, index)}" style="border: 0 none;vertical-align: middle;"></a>|
109        return r
110end
111
112def add_buzzurl(date, index)
113        r = " | "
114        r << %Q|<a href="http://buzzurl.jp/entry/#{permalink(date, index)}"><img src="http://buzzurl.jp/static/image/api/icon/add_icon_mini_10.gif" style="border: 0 none;vertical-align: middle;" title="#{@section_footer_buzzurl_label}" alt="#{@section_footer_buzzurl_label}" width="16" height="12" class="icon"> <img src="http://buzzurl.jp/api/counter/#{permalink(date, index)}" style="border: 0 none;vertical-align: middle;"></a>|
115        return r
116end
117
118def add_delicious(date, index)
119        url_md5 = Digest::MD5.hexdigest(permalink(date, index, false))
120
121        r = " | "
122        r << %Q|<a href="http://delicious.com/url/#{url_md5}"><img src="http://static.delicious.com/img/delicious.small.gif" width="10" height="10" style="border: 0 none;vertical-align: middle;" alt="#{@section_footer_delicious_label}" title="#{@section_footer_delicious_label}"> <img src="http://del.icio.us/feeds/img/savedcount/#{url_md5}?aggregate" style="border: 0 none;margin: 0;"></a>|
123        return r
124end
125
126def add_delicious_json(date, index)
127        require 'fileutils'
128        begin
129                require 'json'
130        rescue
131                retry if require 'rubygems'
132        end
133
134        url_md5 = Digest::MD5.hexdigest(permalink(date, index, false))
135        cache_dir = "#{@cache_path}/delicious/#{date.strftime( "%Y%m" )}/"
136        file_name = "#{cache_dir}/#{url_md5}.json"
137        cache_time = 8 * 60 * 60  # 8 hour
138        update = false
139        count = 0
140
141        r = " | "
142        r << %Q|<a href="http://delicious.com/url/#{url_md5}"><img src="http://static.delicious.com/img/delicious.small.gif" width="10" height="10" style="border: 0 none;vertical-align: middle;" alt="#{@section_footer_delicious_label}" title="#{@section_footer_delicious_label}">|
143
144        begin
145                FileUtils.mkdir_p( cache_dir ) unless File::directory?( cache_dir )
146                cached_time = nil
147                cached_time = File::mtime( file_name ) if File::exist?( file_name )
148
149                unless cached_time.nil?
150                        if Time.now > cached_time + cache_time
151                                update = true
152                        end
153                end
154
155                if cached_time.nil? or update
156                        begin
157                                timeout(10) do
158                                        open( "http://feeds.delicious.com/v2/json/urlinfo/#{url_md5}") do |file|
159                                                File::open( file_name, 'wb' ) do |f|
160                                                        f.write( file.read )
161                                                end
162                                        end
163                                end
164                        rescue => e
165                                @logger.debug( e )
166                        end
167                end
168        rescue
169        end
170
171        begin
172                File::open( file_name ) do |f|
173                                data = JSON.parse(@conf.to_native( f.read, 'utf-8' ))
174                        unless data[0].nil?
175                                count = data[0]["total_posts"].to_i
176                        end
177                end
178        rescue
179        end
180
181        if count > 0
182                r << %Q| #{count} users</a>|
183        else
184                r << %Q|</a>|
185        end
186
187        return r
188end
189
190def add_yahoobm(date, index)
191        r = " | "
192        r << %Q|<a href="http://bookmarks.yahoo.co.jp/url?url=#{permalink(date, index)}&amp;opener=bm&amp;ei=UTF-8"><img src="http://i.yimg.jp/images/sicons/ybm16.gif" style="border: 0 none;vertical-align: middle;" title="#{@section_footer_yahoobm_label}" alt="#{@section_footer_yahoobm_label}" width="16" height="16" class="icon"></a>|
193        return r
194end
195
Note: See TracBrowser for help on using the browser.