howm->はてな(その3)

30分プログラム、その101。id:mzp:20070730:howmのバージョンアップ。
追加したのは、

使い方

$ ./howm-hatena ./howm
 2007-07-23:
*1185197435*howmの文字コードの設定
[2007-07-23 22:30] >>> ~/c/config/emacs
....

ソースコード

#! /usr/bin/perl
# -*- mode:perl; coding:utf-8 -*-
#
# howm-hws.pl - howm memo to hatena diary spliter
#
# Copyright(C) 2007 by mzp
# Author: MIZUNO Hiroki <hiroki1124@gmail.com>
# http://mzp.sakura.ne.jp/
#
# Timestamp: 2007/07/30 23:40:24
#
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#

use strict;
use warnings;
use Time::Local;

my $HOWM_DIR = shift @ARGV ||'~/howm';

sub array_equal{
    my ($a,$b) = @_;
    for(my $i = 0 ; $i < @$a ; $i++){
	return 0 unless($a->[$i] == $b->[$i]);
    }
    return 1;
}

my @files = glob "$HOWM_DIR/*/*/*.howm"; # already sorted

my @date;
while(@files){
    my $file = shift @files;

    my ($year,$mon,$day,$hour,$min,$sec) =
	($file =~ /(\d{4})-(\d{2})-(\d{2}) # YEAR-MONTH-DAY
                  -(\d{2})(\d{2})(\d{2}) # HOUR MIN SEC
                  \.howm\Z/xo)
	or die "invalid file name: $file";
    my $time = timelocal($sec,$min,$hour,$day,$mon-1,$year-1900);

    # date split
    unless(@date and array_equal(\@date,[$year,$mon,$day])){
	@date = ($year,$mon,$day);
	print join('-',@date),":\n";
    }

    # print file content
    # ここからが違う
    open(FILE,$file);
    my @entries = (split(/(^= .*$)/m,join('',<FILE>)))[1,-1];

    for(my $i = 0 ; $i < @entries ; $i+=2){
	my $title   = $entries[$i];
	my $content = $entries[$i+1];

	next if $title =~ /\[(?:p|private|password)\]/i;

	$title =~ s/^= /*$time*/;
	print $title;

	$content =~ s/\s+\Z//m;
	print $content,"\n";
    }
    close(FILE);
}