不使用なCSSルールをチェックするツール(css-redundancy-checker)の使い方 & カスタマイズ

css-redundancy-checkerはcssファイルから使われていない不要な定義を外す(削除する)ためのチェックツール。

rubyで書かれています。

インストール

css-redundancy-checkerに必要なもの

  • Ruby本体 (1.8.4で動作確認済み)
  • Rubygems
  • Hpricot

macなら上2つは標準で入ってます。

windowsの場合のインストール方法はこっち参照。

Hpricotのインストールは上2つインストール後に

  • windows
    • DOS窓から「gem install hpricot」
  • mac
    • ターミナルから「gem install hpricot」

css-redundancy-checker.rbの入手

Google Codeに置いてある。

一応SVNで取ってくるようになっているけど

http://css-redundancy-checker.googlecode.com/svn/trunk/ からダイレクトに
css-redundancy-checker.rbをダウンロードしてもOK。

改造

ダウンロードしたてだと、ディレクトリ指定ではhtmlのみが対象だし、指定した階層のファイルしかチェックしに行かない。

今回は多階層のphpサイトでチェックしたかったのでcss-redundancy-checker.rbをちょっと改造。

13行目の

things = Dir["#{source}/*.html"]

things = Dir["#{source}/**/*.php"]

にする。

こうするとサブフォルダの中のphpファイルも調査対象にできる

実行

コマンドラインでやります。

css-redundancy-checker.rb [チェックするcss] [チェック対象のディレクトリ or  対象URLリストが記述されたtxtファイル]

結果はこんな感じ

$ ruby css-redundancy-checker.rb hachy/base_theme.css hachy
C:/Ruby/lib/ruby/gems/1.8/gems/hpricot-0.6.164-x86-mswin32/lib/hpricot/builder.rb:26: warning: `&' interpreted as argument prefix
Parsing all html files within hachy for selectors in hachy/base_theme.css...
-------------
Parsing hachy/archives.php
Parsing hachy/index.php
-------------
The following selectors are NOT used in of the html files in hachy
-------------
#content div.entry-body blockquote
#content div.entry-body h4
#content div.entry-body h5
#content div.entry-body h6
#content div.entry-body ol
#paging span.next
* html .clearfix
.comment_container
.comment_container p
.comment_meta
.comments
.comments h3

おお!すごいらくちーん。


ネタ元