webbyを試してみる

http://webby.rubyforge.org/

テキストファイルから適当にフィルタをかけてHTMLを生成するという
ちょうど欲しかったツールが見つかったので実験中。


ruby1.9で使うのに2箇所手を入れた。

マルチバイト文字に関するエラー解消

/usr/local/lib/ruby/gems/1.9.1/gems/webby-0.9.4/lib/webby/core_ext/string.rb
先頭に

# -*- encoding:utf-8 -*-

を入れる。
http://webby.lighthouseapp.com/projects/17680/tickets/19-multibyte-error-under-ruby-191#ticket-19-1

undef_method object_idのwarning解消

/usr/local/lib/ruby/gems/1.9.1/gems/webby-0.9.4/lib/webby/resources/resource.rb

class Resource

  instance_methods.each do |m|
      undef_method(m) unless m =~ %r/\A__|\?$/ ||
                             m.to_s == 'class' || # to_sと行末の||を追加
                             m.to_s == 'object_id' # 追加
  end

instance_methods.each do |m|が文字列で無いのに気づくのに少し手間取ってしまった。
1.9から変わったところなのかは知らない。

helperの中(code_rayとか)でマルチバイト使うとエラー

場当たり的に
/usr/local/lib/ruby/gems/1.9.1/gems/webby-0.9.4/lib/webby/helpers/capture_helper.rb:120

  def concat_erb( string, the_binding )
    _erb_buffer(the_binding) << string.force_encoding('utf-8') # force_encodingを追加
  end

templateの絶対パス相対パス

試す前にtemplateを調整しておく。

layouts/default.txt

<link rel="stylesheet" href="/css/blueprint/screen.css" type="text/css" media="screen, projection">

何箇所かあるが
href="/css/...をhref="css/...と相対パスにしておく。
変えないとローカルファイルをブラウザで開いたときサイドバーがサイドに来ない
白いページになる。


これはこれで不十分でcontent内にサブディレクトリを掘るとアウトになった。
テンプレートが適用されたページからの相対パスで指定する方法が必要だ。


まさにこの問題についての記事を発見。
http://www.fraction.jp/log/archives/2008/09/19/Webby__url_for_


記事のとおりにlib内にスクリプトを配置。
次に上記の
href="/css/..."をhref="css/..."
としたところを
href="<%url_for('/css/...')%>
と書き換えてwebbyする。
これで深い階層からも相対パスcssを参照できた。

文法

デフォルトの文法はtextile。
h1, ulタグなどの文章構造方面をサポートする。
http://redmine.jp/tech_note/textile/

文法を変えるには、templates/page.erb(新規ページに対して)かcontent/*.txt(既存ページに対して)のfilterを変える。


リンクを記述する時は

<%= link_to('Google', 'http://www.google.com/', :attrs => {:name => 'google'}) %>

とできるが素直に

<a href="http://google.com">Google</a>

と書いた方が早い。


むしろTextile文法で

"Google":http://google.com

とするのがよろしい。

シンタックスハイライトとかtexとか

http://webby.rubyforge.org/reference/
申し分なし。

tex2imgの動作まで

まずtex2imgヘルパーが動くようにする。

/usr/local/lib/ruby/gems/1.9.1/gems/webby-0.9.4/lib/webby/helpers/tex_img_helper.rb:最後の方

if  cmd_available?(%w[pdflatex --version]) \
and cmd_available?(%w[convert --help])
  register(TexImgHelper)
end

という部分があるのでpdflatexコマンドとconvertコマンドが要るのがわかる。
gentoo的には、

# emerge texlive imagemagick

さらに上記の
convert --help

convert --version
と書き換える。
convert --help
は終了コードが1なのでいつまでたってもTexImgHelperは登録されない。

仕上げに出力された画像に対するimgタグを相対パスで参照するように改造する。
/usr/local/lib/ruby/gems/1.9.1/gems/webby-0.9.4/lib/webby/helpers/tex_img_helper.rb:130

    relative_path=File.dirname(@page.path).split(%r|/|).map{'..'}
    relative_path.shift
    out = "<img src=\"#{::File.join(relative_path.join('/'), image_fn)}\""

webbyコマンド

動作を理解すべくwebbyコマンドのソースに目を通そうとするが、
rubygemsが何やっているか不明で難航。
とりえあずエラー時のバックトレースから
/usr/local/bin/webby
から
/usr/local/lib/ruby/gems/1.9.1/gems/webby-0.9.4/bin/webby
に処理が流れているらしいことが判明。
最終的に
/usr/local/lib/ruby/gems/1.9.1/gems/webby-0.9.4/lib/webby/apps/main.rb

::Webby::Apps::Main.run(ARGV)

が実行される。
なんかrakeに皮を被せたもののようだがrakeを知らないのでよくわからん。