format_svn_log.rb(Subversionのログを見やすく整形)

結局Rubyでやりました。日付のところもまったく問題なし。
以下に公開しておきます。
見やすく整形といってもしょぼい出力しか出てきません。
デザインセンスが欲しい・・
ちなみにXMLパーサもテンプレートエンジンもRubyインタプリタに標準添付のものだけでできました。標準添付ライブラリが充実しているので必要な部品を後入れしなくていいっていうのはRubyの大きなアドバンテージだと思います。

#!/usr/bin/ruby
#format_svn_log.rb
require "rexml/document"
require "time.rb"
require 'erb'

class LogEntry
  attr_accessor :revision
  attr_accessor :author
  attr_accessor :date
  attr_accessor :msg
  attr_accessor :paths
end

class ModefiedPath
  attr_accessor :action
  attr_accessor :path
end

class LogEntries < Array
  def initialize(log)
    begin
      doc = REXML::Document.new(log)
      doc.elements.each("log/logentry") do |elem|
        ent = LogEntry.new
        ent.revision = elem.attributes["revision"]
        ent.author   = elem.elements["author"].text
        ent.date     = Time.xmlschema(elem.elements["date"].text)
        ent.msg      = elem.elements["msg"].text
        ent.paths    = Array.new
        elem.elements["paths"].elements.each("path") do |child|
           path = ModefiedPath.new
           path.action = child.attributes["action"]
           path.path   = child.text
           ent.paths.push(path)
        end
        self.push(ent)
      end
    rescue
      raise "Bad log file!\n#{$!}"
    end
  end
end

class Printer
  SCRIPT = <<EOS
<font size="2">
<% entries.each do |ent| %>
------------------------------------------------------------------------
<span style="background-color: rgb(192, 192, 192); font-weight: bold;">
r<%= ent.revision %> | <%= ent.author %> | <%= ent.date.getlocal %> |
</span>
変更のあったパス:
<% ent.paths.each do |path| %>&nbsp;&nbsp;<%= path.action %>&nbsp;<%= path.path %>
<% end %>
<%= ent.msg %>
<% end %>
</font>
EOS

  def initialize()
    @erb = ERB.new(SCRIPT)
  end
  def print(entries)
    @erb.result(binding)
  end
end

if ARGV[0]
  log = File.open(ARGV[0])
else
  log = STDIN
end
begin
  entries = LogEntries.new(log)
rescue
  warn "Bad log file!"
  exit 1
end
puts Printer.new().print(entries)

使いかた

# svn log -v --xml | ruby format_svn_log.rb

または

# ruby format_svn_log.rb logfile.xml