読み込み対象の絞り込み

gonzuiは,読み込みをはじく,フィルタとしての正規表現パターンを設定することが可能だが,
読み取りを許可するパターンを設定する方法はない.
そこで,gonzuiを改造してみた.
ただ,これ,やっぱり,デフォルトのフィルタと共存させるのはちょっとつらいなぁ.
なんか,いいアイデアないかなぁ.

んで,改造

gonzuircに,include_patternを追加する.以下抜粋.

:exclude_pattern          => /~$|.bak$|CVS|.svn/,
:include_pattern          => /.cpp$|.c$|.h$/,
:gonzui_log_file          => "/var/gonzui/gonzui.log",

そして,このconfigファイルを読み込むConfigクラス,config.rbを修正する.

module Gonzui
class Config
include Util
def initialize
# 中略
@include_pattern = nil

これでinclude_patternをConfigクラスのメンバ変数として使える.
次にfetcher.rbのAbstractFetcherを編集.

class AbstractFetcher
include Util
def initialize(config, source_uri, options = {})
@include_pattern = @config.include_pattern

次にexclude_patternに被せるため,かなりダサイ関数?を追加.

def include_only_extention(relative_path)
if( @include_pattern==nil)
return false
end
if(@include_pattern.match(relative_path)!=nil)
return false
end
return true
end

んでもって,collect関数?を修正.

def collect
directory = @base_uri.path
relative_paths = []
Dir.all_files(directory).map {|file_name|
next if exclude?(file_name)
next if include_only_extention(file_name)
relative_path = File.relative_path(file_name, directory)
relative_paths.push(relative_path)
}
return relative_paths
end

さらにHTTPFetcherクラスも修正.

def collect
relative_paths = []
@content.scan(/href=(["'])(.*?)1/i).each {|qmark, link|
u = URI.parse(link)
next if u.path.nil?
u.path.chomp!("/")
next unless u.relative?
next if /^./.match(u.path)
next if exclude?(u.path)
next if include_only_extention(u.path)
relative_paths.push(u.path)
}
return relative_paths
end

これでgonzuircのinclude_patternに書き込まれた正規表現をパスしたもののみがデータベースにインポートされるようになるはず.
誰か,綺麗なのを作ったら,ください.
なんか,不安だ.このソースコード.