# $Id: subdomain3.rb,v 1.3 2003-09-02 23:24:44+09 waka Exp waka $
# http://www.double-red.net/
=begin
ndiary 用フィルタ
ハイパーリンクの直後にリンク先のサブドメインを表示する
@@注意--------------------------------------------------
inlinedecorate、をゐなりフォーマッタと併用時、
ndiary.conf にてそれらの記述よりも後に指定して利用
@@その他の設定------------------------------------------
ndiary.conf に MY_DIARY_SITE を指定をすると(必須ではない)
指定した個所の日記内へのリンク時、サブドメインではなく日付とトピック番号をつけるようになる。
('|' で URL をつなげると、複数指定も可能)
例)
* ndiary.conf:
MY_DIARY_SITE = 'www.double-red.net/ndiary'
↓
* 日記内リンク表記(inlinedecorate 使用時):
サブドメインのかわりに日付とトピック番号がつきます(http://www.double-red.net/ndiary/200308.html#27_t1)
↓
* 生成 HTML:
サブドメインのかわりに日付とトピック番号がつきます[2003/08/30 @topic1])
=end
class Subdomain
def initialize(str)
@str = str
@url = url
@title = ""
@site_name = nil
@protocol = nil
@domain = nil
@subdomain = nil
@day = nil
@topic = nil
@html = nil
end
def html
@html = @url.split("/").last
end
def url
if @str =~ // then
@title = $1
end
if @title.empty? then
@title = @url.gsub(protocol,"")
end
return @title
end
def site_name
if @str =~ /(.+?)<\/a>/ then
@site_name = $1
end
end
def protocol
@protocol = @url.split(":").first + "://"
end
def domain
if @url =~ /\/\/(.+?)(?:\/|$)/ then
@domain = $1
end
end
def subdomain
subdomain = domain.split(":").first
unless subdomain =~ /(\d+?\.){3}\d/ then # IP アドレスっぽいのはそのままに
@subdomain = subdomain.split(".")
if @subdomain.nitems > 2 then
@subdomain.delete_at(0)
return @subdomain.join(".")
else
return @subdomain.join(".")
end
else
@subdomain = subdomain
end
end
def date
date = Array.new
if @html =~ /html?/i then
date << @html.split(".").at(0).gsub(/(^[0-9]{4})\d\d\w?/,'\1')
date << @html.split(".").at(0).gsub(/\d{4}([0-9]{2})\w?/,'\1')
if @html =~ /#\d+_t\d+/ then
date << @html.split("#").last.gsub(/_.+$/,"")
end
return date.join("/")
end
end
def topic
if @html =~ /html/i then
@html.split("_").last.delete("t")
end
end
end
class Filter
def subdomain3(str, type)
mydiarysite = ''
if @diary.respond_to?('config') then
unless @diary.config['MY_DIARY_SITE'].to_s.empty?
mydiarysite = @diary.config['MY_DIARY_SITE'].to_s
end
end
case type
when :P, :UL, :DL
begin
str.gsub!(/(.+?<\/a>(?:\[.+?\])?)/){
url = $1
begin
sd = Subdomain.new(url)
if ! mydiarysite.empty? and url =~ Regexp.new(mydiarysite) then
if sd.html =~ /(\d{6}\.html#\d+)(_t\d+)?/ then
if $2 then
subdomain = sd.date + " @topic" + sd.topic
else
subdomain = sd.date
end
elsif sd.html =~ /\d{6}\.html$/ then
subdomain = sd.date
else
subdomain = sd.subdomain
end
else
subdomain = sd.subdomain
end
anchor = ""
anchor << sd.site_name
anchor << ""
anchor << "[" + subdomain + "]"
rescue
puts "#{$!}"
puts url
puts "error at filter subdomain3.(this is subdomain3 filter\'s message.)"
end
}
rescue
puts "#{$!}"
puts "error at filter subdomain3.(this is subdomain3 filter\'s message.)"
end
end
end
end
=begin
Local Variables:
mode: ruby
coding: sjis-dos
End:
=end