ReVIEWドキュメント中に使われている要素をカウントする

ReVIEWドキュメントの中にどんな要素が、どれくらい含まれているのか数えます。

#!/usr/bin/ruby
# -*- coding: utf-8 -*-

#カウント用ハッシュの初期値0
my_count = Hash.new(0)

while line = ARGF.gets
  #多くのブロック要素
  if line =~ /^(\/\/.+?)[\[\{]/ then
  my_count[$1] += 1
  
  #見出し
  elsif line =~ /^(={1,5}) / then
    tmp_key = $1 + "h" + $1.length.to_s
    my_count[tmp_key] += 1
  
  #コラム
  elsif line =~ /^(===[column])/ then
    my_count[$1] += 1
  
  #箇条書き
  elsif line =~ /^ (\*+) / then
    tmp_key = $1 + " ul(" + $1.length.to_s + ")"
    my_count[tmp_key] += 1
  
  #箇条番号
  elsif line =~ /^[1-9][0-9]?\. / then
    my_count["ol"] += 1
  
  #用語リスト
  elsif line =~ /^: / then
    my_count[": dl"] += 1
  
  #コメントは出力する
  elsif line =~ /^(\#@\# |\#@warn).+/ then
    puts "comment(#{ARGF.filename}:#{ARGF.file.lineno}) : #{$&}"
  
  end
  
  #インライン要素
  line.scan(/@<.+?>/).each {|elem|
    my_count[elem] += 1
  }
end


#項目名でソートして出力
my_count.sort.each{|key,val|
  puts "#{key} : #{val}"
}

こんなかんじで出力されるはず。

comment(hoge.re:123) : #@# ------------------------------------------
* ul(1) : 16
//emlist : 76
//indepimage : 211
//table : 43
====h4 : 13
===h3 : 24
==h2 : 35
=h1 : 9
@<b> : 344
@<code> : 7
@<icon> : 2
@<kw> : 154