| 1 | # section_footer2.rb $Revision 1.0 $ |
|---|
| 2 | # |
|---|
| 3 | # Copyright (c) 2008 SHIBATA Hiroshi <h-sbt@nifty.com> |
|---|
| 4 | # You can redistribute it and/or modify it under GPL2. |
|---|
| 5 | # |
|---|
| 6 | |
|---|
| 7 | require 'digest/md5' |
|---|
| 8 | require 'open-uri' |
|---|
| 9 | require 'timeout' |
|---|
| 10 | require 'yaml' |
|---|
| 11 | require "pathname" |
|---|
| 12 | |
|---|
| 13 | def permalink( date, index, escape = true ) |
|---|
| 14 | ymd = date.strftime( "%Y%m%d" ) |
|---|
| 15 | uri = @conf.index.dup |
|---|
| 16 | uri[0, 0] = @conf.base_url unless %r|^https?://|i =~ uri |
|---|
| 17 | uri.gsub!( %r|/\./|, '/' ) |
|---|
| 18 | if escape then |
|---|
| 19 | uri + CGI::escape(anchor( "#{ymd}p%02d" % index )) |
|---|
| 20 | else |
|---|
| 21 | uri + anchor( "#{ymd}p%02d" % index ) |
|---|
| 22 | end |
|---|
| 23 | end |
|---|
| 24 | |
|---|
| 25 | add_section_enter_proc do |date, index| |
|---|
| 26 | @category_to_tag_list = {} |
|---|
| 27 | end |
|---|
| 28 | |
|---|
| 29 | alias subtitle_link_original subtitle_link |
|---|
| 30 | def subtitle_link( date, index, subtitle ) |
|---|
| 31 | s = '' |
|---|
| 32 | if subtitle then |
|---|
| 33 | s = subtitle.sub( /^(\[([^\[]+?)\])+/ ) do |
|---|
| 34 | $&.scan( /\[(.*?)\]/ ) do |tag| |
|---|
| 35 | @category_to_tag_list[tag] = false # false when diary |
|---|
| 36 | end |
|---|
| 37 | '' |
|---|
| 38 | end |
|---|
| 39 | end |
|---|
| 40 | subtitle_link_original( date, index, s.strip ) |
|---|
| 41 | end |
|---|
| 42 | |
|---|
| 43 | add_section_leave_proc do |date, index| |
|---|
| 44 | r = '<div class="tags">' |
|---|
| 45 | |
|---|
| 46 | unless @conf.mobile_agent? then |
|---|
| 47 | # カテゴリタグの追加 |
|---|
| 48 | if @category_to_tag_list and not @category_to_tag_list.empty? then |
|---|
| 49 | r << "Tags: " |
|---|
| 50 | @category_to_tag_list.each do |tag, blog| |
|---|
| 51 | if blog |
|---|
| 52 | r << %Q|<a href="#{@index}?blogcategory=#{h tag}">#{tag}</a> | |
|---|
| 53 | else |
|---|
| 54 | r << category_anchor( "#{tag}" ).sub( /^\[/, '' ).sub( /\]$/, '' ) << ' ' |
|---|
| 55 | end |
|---|
| 56 | end |
|---|
| 57 | end |
|---|
| 58 | |
|---|
| 59 | # 「このエントリを含む del.icio.us(json API)」 |
|---|
| 60 | r << add_delicious_json(date, index) |
|---|
| 61 | |
|---|
| 62 | # SBM アイコンの追加 |
|---|
| 63 | yaml_dir = "#{@cache_path}/yaml/" |
|---|
| 64 | Dir.glob( yaml_dir + "*.yaml" ) do |file| |
|---|
| 65 | r << parse_sbm_yaml(file, date, index) |
|---|
| 66 | end |
|---|
| 67 | |
|---|
| 68 | # Permalinkの追加 |
|---|
| 69 | r << add_permalink(date, index) |
|---|
| 70 | end |
|---|
| 71 | |
|---|
| 72 | r << "</div>\n" |
|---|
| 73 | end |
|---|
| 74 | |
|---|
| 75 | def add_delicious_json(date, index) |
|---|
| 76 | require 'json' |
|---|
| 77 | |
|---|
| 78 | url_md5 = Digest::MD5.hexdigest(permalink(date, index, false)) |
|---|
| 79 | cache_dir = "#{@cache_path}/delicious/#{date.strftime( "%Y%m" )}/" |
|---|
| 80 | file_name = "#{cache_dir}/#{url_md5}.json" |
|---|
| 81 | cache_time = 8 * 60 * 60 # 8 hour |
|---|
| 82 | update = false |
|---|
| 83 | count = 0 |
|---|
| 84 | |
|---|
| 85 | r = " | " |
|---|
| 86 | r << %Q|<a href="http://del.icio.us/url/#{url_md5}"><img src="http://images.del.icio.us/static/img/delicious.small.gif" width="10" height="10" style="border: none;vertical-align: middle;" alt="このエントリを含む del.icio.us" title="このエントリを含む del.icio.us">| |
|---|
| 87 | |
|---|
| 88 | begin |
|---|
| 89 | Dir::mkdir( cache_dir ) unless File::directory?( cache_dir ) |
|---|
| 90 | cached_time = nil |
|---|
| 91 | cached_time = File::mtime( file_name ) if File::exist?( file_name ) |
|---|
| 92 | |
|---|
| 93 | unless cached_time.nil? |
|---|
| 94 | if Time.now > cached_time + cache_time |
|---|
| 95 | update = true |
|---|
| 96 | end |
|---|
| 97 | end |
|---|
| 98 | |
|---|
| 99 | if cached_time.nil? or update |
|---|
| 100 | begin |
|---|
| 101 | timeout(10) do |
|---|
| 102 | open( 'http://badges.del.icio.us/feeds/json/url/data?hash=' + url_md5 ) do |file| |
|---|
| 103 | File::open( file_name, 'wb' ) do |f| |
|---|
| 104 | f.write( file.read ) |
|---|
| 105 | end |
|---|
| 106 | end |
|---|
| 107 | end |
|---|
| 108 | rescue TimeoutError |
|---|
| 109 | rescue |
|---|
| 110 | end |
|---|
| 111 | end |
|---|
| 112 | rescue |
|---|
| 113 | end |
|---|
| 114 | |
|---|
| 115 | begin |
|---|
| 116 | File::open( file_name ) do |f| |
|---|
| 117 | data = JSON.parse(@conf.to_native( f.read, 'utf-8' )) |
|---|
| 118 | unless data[0].nil? |
|---|
| 119 | count = data[0]["total_posts"].to_i |
|---|
| 120 | end |
|---|
| 121 | end |
|---|
| 122 | rescue |
|---|
| 123 | end |
|---|
| 124 | |
|---|
| 125 | if count > 0 |
|---|
| 126 | r << %Q| #{count} users| |
|---|
| 127 | end |
|---|
| 128 | |
|---|
| 129 | r << %Q|</a>| |
|---|
| 130 | |
|---|
| 131 | return r |
|---|
| 132 | end |
|---|
| 133 | |
|---|
| 134 | def parse_sbm_yaml(file, date, index) |
|---|
| 135 | config = YAML.load(Pathname.new(file).expand_path.read) |
|---|
| 136 | |
|---|
| 137 | r = " | " |
|---|
| 138 | unless config.nil? then |
|---|
| 139 | r << %Q|<a href="#{config["url"]}#{permalink(date, index)}")>| |
|---|
| 140 | r << %Q|<img src="#{config["src"]}" style="border: none;vertical-align: middle;" | |
|---|
| 141 | r << %Q|title="#{config["title"]}" | |
|---|
| 142 | r << %Q|alt="#{config["title"]}" />| |
|---|
| 143 | unless config["counter"].nil? then |
|---|
| 144 | r << %Q| <img src="#{config["counter"]}#{permalink(date, index)}" style="border: none;vertical-align: middle;" />| |
|---|
| 145 | end |
|---|
| 146 | r << %Q|</a>| |
|---|
| 147 | end |
|---|
| 148 | |
|---|
| 149 | return r |
|---|
| 150 | end |
|---|
| 151 | |
|---|
| 152 | def add_permalink(date, index) |
|---|
| 153 | r = " | " |
|---|
| 154 | r << %Q|<a href="#{permalink(date, index, false)}">Permalink</a> | |
|---|
| 155 | return r |
|---|
| 156 | end |
|---|