# -*- coding: utf-8 -*-
# tlink.rb
#
# title 属性付 anchor plugin
#
# 使い方
# <%= tlink( "URL", "文字列", "title 属性の中身(省略可)" ) %>
#
# 第 3 パラメータを省略した時、URL の末尾が c#?? ならば、
# そのツッコミの内容が最初の行だけ表示されます。
# 末尾が p#?? ならば、サブタイトルがあればサブタイトルが、
# なければ最初のパラグラフが表示されます。
#
# 例. <%= tlink( "http://tdiary.tdiary.net/20020131.html#c01", "このツッコミ" ) %>
# 出力結果:
# このツッコミ
#
# Copyright(C) 2002 NT
# Distributed under the GPL2 or any later version.
#
# Modified: by abbey
#
=begin ChangeLog
2003-03-03 NT
* add the function to try regetting a subtitle.
2003-01-29 NT
* fix a critical bug.
2003-01-28 NT
* correspond to change in movile mode.
2002-11-19 NT
* add require 'pstore'.
2002-07-01 NT
* change some regular expressions.
2002-05-18 NT
* remove "," from %Q[#{str}].
2002-05-16 MUTOH Masao
* cache mechanism support.
* code cleanup(remove require 'cgi',
getcomment rename to tlink_getcomment).
2002-05-05 NT
* add URL to User-Agent
2002-04-21 abbey
* add error shori
2002-04-20 NT
* change User-Agent
* modify some regular expressions
2002-04-19 NT
* modify some regular expressions
* add User-Agent
2002-04-18 abbey
* adapt to port numbers except 80
* adapt to #pXX
2002-04-17 NT
* create
=end
require 'net/http'
require 'kconv'
require 'pstore'
def tlink_initialize
dir = @cache_path + "/tlink"
@tlink_path = dir + "/tlink.dat"
Dir.mkdir(dir, 0700) unless FileTest.exist?(dir)
PStore.new(@tlink_path).transaction do |db|
if (db.root?('tlinkdata')) then
@tlink_dic = db["tlinkdata"]
else
@tlink_dic = Hash.new
end
end
end
def tlink_finalize
db = PStore.new(@tlink_path)
db.transaction do
begin
db["tlinkdata"] = @tlink_dic
rescue PStore::Error
end
end
end
def tlink_getcomment( url )
result = ""
ref = base_url
agent = { "User-Agent" => "DoCoMo (compatible; tDiary plugin; tlink; #{ref})" }
host, path, frag = url.scan( %r[http://(.*?)/(.*)#((?:p|c)\d\d)] )[0]
if /p0/ =~ frag
frag = "(?:" + frag + "|" + frag.sub( /p/, "p#" ).sub( /#0/, "#" ) + ")"
end
port = 80
if /(.*):(\d+)/ =~ host
host = $1
port = $2
end
hata = 0
http = Net::HTTP.new( host, port )
begin
http.open_timeout = 3
response , = http.get( "/#{path}", agent )
response.body.each { |line|
if %r[(?:.*?) (.*?)(?:P|H3)>] =~ line.toutf8
result = $1
break
else
hata = 1
end
elsif hata == 1 && %r[^\t*(.*?)
] =~ line.toutf8
result = $1
hata = 0
break
end
}
rescue
result = ""
end
result = CGI::escapeHTML( result.gsub( %r[?[aA](.*?)>], "" ) ).gsub( / /, " " )
end
tlink_initialize
def tlink( url, str, title = nil )
unless title
if @tlink_dic[url] && %r[#(p|c)\d\d$] =~ url && @tlink_dic[url] != ''
title = @tlink_dic[url]
elsif @tlink_dic[url] && %r[#(p|c)\d\d$] !~ url
title = @tlink_dic[url]
else
if /#{url}/ =~ ENV["REDIRECT_URL"] && /#{url}/ =~ @date.strftime('%Y%m%d')
else
title = tlink_getcomment( url )
@tlink_dic[url] = title
tlink_finalize
end
end
end
%Q[#{str}]
end
# Local Variables:
# mode: ruby
# indent-tabs-mode: t
# tab-width: 3
# ruby-indent-level: 3
# End: