# tlink.rb
#
# title 属性付 anchor plugin
#
# 使い方
# <%= tlink( "URL", "文字列", "title 属性の中身(省略可)" ) %>
#
# 第 3 パラメータを省略した時、URL が tDiary のツッコミアンカーならば、
# そのツッコミの内容が最初の行だけ表示されます。
#
# 例. <%= tlink( "http://tdiary.tdiary.net/20020131.html#c01", "このツッコミ" ) %>
# 出力結果:
# このツッコミ
#
# Copyright(C) 2002 NT
# Distributed under the GPL.
require 'net/http'
require 'cgi'
def getcomment( url )
result = ""
array = url.scan( %r[http://(.*?)/(.*)] )
host, path = array[0][0], array[0][1]
comnum = path.scan( %r[#c\d\d] )[0]
hata = 0
Net::HTTP.start( host, 80 ) { |http|
response , = http.get( "/#{path}" )
response.body.each { |line|
if line =~ %r[#{comnum}">]
hata = 1
elsif hata == 1 && line =~ %r[(.*?)(
|
)]
result = CGI::escapeHTML( $1 )
hata = 0
end
}
}
result
end
def tlink( url, str, title = nil )
unless title
title = getcomment( url )
end
%Q[#{str}]
end