api.rails2u.com をローカルのRails 2.0で動かす

http://api.rails2u.com/ はすごく便利なんだけど、Rails 2.0じゃないのが残念なんか勘違いしてたっぽい。手元と同じだ。とほほ。

とにかく、最新のがさくさく検索したかったので、手元で稼働させてみた。ってことにしてエントリは残す。

api.rails2u.com のソースを入手

これを展開する。

tar zxvf source.tar.gz

api.rails2u.com フォルダができる。

Rails 2.0のフォルダを作成

railsでフォルダを作って、app, lib, publicのファイルを丸ごとコピーする。

rails api.local
cp -r api.rails2u.com/{app,lib,public} api.local

ソースの修正

下記の内容で修正する。*1

Index: app/controllers/application.rb
===================================================================
--- app/controllers/application.rb	(revision 31)
+++ app/controllers/application.rb	(working copy)
@@ -4,17 +4,17 @@
 
   protected
   def set_charset
-    @headers['Content-Type'] = 'text/html; charset=UTF-8'
+    headers['Content-Type'] = 'text/html; charset=UTF-8'
   end
   
   def default_setup
-    @rp = RdocParser.new(Dir.glob(RAILS_ROOT + '/public/docs/*'))
+    @rp = RdocParser.new(Dir.glob(RAILS_ROOT + '/public/docs/*/rdoc'))
   end
 
   def fix_unicode_for_safari
-    user_agent = @request.env['HTTP_USER_AGENT']
+    user_agent = request.env['HTTP_USER_AGENT']
     if (user_agent.include? 'AppleWebKit' or user_agent.include? 'Konqueror')
-      @response.body.gsub!(/([^\x00-\xa0])/u) { |s| '&#x%x;' % $1.unpack('U')[0] }
+      response.body.gsub!(/([^\x00-\xa0])/u) { |s| '&#x%x;' % $1.unpack('U')[0] }
     end
   end
 
Index: app/controllers/doc_controller.rb
===================================================================
--- app/controllers/doc_controller.rb	(revision 31)
+++ app/controllers/doc_controller.rb	(working copy)
@@ -7,7 +7,7 @@
 
   def ajax_search
     default_setup
-    @search_word = @params['keyword']
+    @search_word = params['keyword']
     @search_word = '' if @search_word.nil?
     if not @search_word.length > 0
       #検索語がなかったらすぐrenderする
Index: lib/rdoc_parser.rb
===================================================================
--- lib/rdoc_parser.rb	(revision 31)
+++ lib/rdoc_parser.rb	(working copy)
@@ -75,7 +75,7 @@
   def classfiles_parse
     @classes = [] 
     @docs_path.each do|f|
-      name = File.basename f
+      name = f.split('/')[-2] #File.basename f
       body = File.open(f + '/' + CLASS_INDEX_FILE,'r').read
       entries = %r{<div id="index-entries">(.+)</div>}m.match(body)[1]
       while entries.sub!(%r!<a href="([^"]+?)">([^<]+?)</a>!,'')
@@ -90,7 +90,7 @@
   def methodfiles_parse
     @methods = [] 
     @docs_path.each do|f|
-      name = File.basename f
+      name = f.split('/')[-2] #File.basename f
       body = File.open(f + '/' + METHOD_INDEX_FILE,'r').read
       entries = %r{<div id="index-entries">(.+)</div>}m.match(body)[1]
       while entries.sub!(%r!<a href="([^"]+?)">(.+?)\s*[((](.*?)[))]</a>!,'')
@@ -104,7 +104,7 @@
   end
 
   def gen_docpath(name,filename)
-    DOC_PATH + '/' + name + '/' + filename
+    DOC_PATH + '/' + name + '/rdoc/' + filename
   end
 
 end

Gemのrdocにシンボリックリンクを張る

cd public/docs
ln -s /Library/Ruby/Gems/1.8/doc/* .
cd ../..

手元の環境はLeopardでGemを全部入れ直したからこうなってる。デフォルトのやつも同様に追加できるはず。Ubuntuでもできたし。

サーバ起動

script/server

http://localhost:3000/ でしあわせな気分に。Rails以外のrdocも当然検索できる。

今後の展開

HyperEstraierの検索とマージしたらもっといい感じかも。

*1:本当はpatch -p0でいいはずだけど、Safariで書いてるから文字化けするかもしれないので非推奨