=begin
# html2feed
HTMLからRSSを作成するためのプラグイン。pragger付属のcustom_feedに手を加えたもの。
## ダウンロード
http://bmky.net/product/pragger/#html2feed
## 解説
オリジナルとの違いは
* descriptionを指定することができる
(未指定の場合はsplitのfirstmatch)
* 全てのtitleの前につけるprefixを指定することができる
(複数ページからまとめて取得する際のタイトル識別に使用)
* urlを指定しなくてもlinkを取得することができる
* dateのparseに失敗した場合、firstmatchを取得する
(後でvalidな形式に変換する必要がある)
* limitを指定することで取得数を制限できる。
## 使い方
- module: myplugin::RSS::html2feed
config:
url: addr of data -- optional
capture: regex for content -- optional
split: regex for item
title: regex for title
prefix: regex for prefix title -- optional
date: regex for date
link: regex for link
description: regex for description -- optional
limit: 10 # 取得する最大数(任意)
## 更新履歴
2008/05/01
: limitで取得数を制限できるようにした
2007/11/30
: リリース
=end
require 'open-uri'
require 'kconv'
def html2feed( config, data )
if config['url']
data = []
open( config['url'] ) { |r|
data << r.read.toutf8
}
end
items = []
data.each do |input|
body = ""
if config['capture']
if input =~ Regexp.new( config['capture'], Regexp::MULTILINE )
body = $1
end
else
body = input
end
articles = []
if config['split']
body.gsub( Regexp.new( config['split'], Regexp::MULTILINE ) ) do
if !config['limit'] or articles.length < config['limit']
articles.push $1
end
end
else
articles.push body
end
prefix = ""
if config['prefix']
if input =~ Regexp.new( config['prefix'] )
prefix = $1
end
end
articles.each do |i|
if config['title']
title = Regexp.new( config['title'] )
if i =~ title
mytitle = prefix + $1
i.instance_eval do
@title = mytitle
def title
@title
end
end
end
end
if config['date']
date = Regexp.new( config['date'] )
if i =~ date
mydate = Time.parse( $1 ) rescue $1
i.instance_eval do
@date = mydate
def date
@date
end
end
end
end
if config['link'] or config['url']
link = Regexp.new( config['link'] )
if i =~ link
mylink = config['url'] ? ( config['url'] + $1 ) : $1
i.instance_eval do
@link = mylink
def link
@link
end
end
end
end
if config['description']
description = Regexp.new( config['description'] )
if i =~ description
mydescription = $1
i.instance_eval do
@description = mydescription
def description
@description
end
end
end
end
end
items += articles
end
return items
end