#!/usr/bin/env perl use strict; use warnings; use utf8; use IO::File; use Perl6::Say; use Text::MicroTemplate qw/render_mt/; use DateTime; use Path::Class; use Getopt::Long; binmode STDOUT, ':utf8'; &main;exit; sub main { my $schedule_limit = 7; # schedule を何日ぶん表示するか my $todo_limit = 20; # TODO を何件表示するか GetOptions( 'todo-limit=i' => \$todo_limit, 'schedule-limit=i' => \$schedule_limit, ); my @dirs = @ARGV; my $rows = _aggregate(@dirs); my $schedules = _find_schedules($rows, $schedule_limit); my $todoes = _find_todoes($rows, $todo_limit); print _render($schedules, $todoes); } sub _aggregate { my @dirs = @_; my @rows; for my $dir (@dirs) { die "$dir is not a directory" unless -d $dir; dir($dir)->recurse(callback => sub { my $file = shift; return unless -f $file; my $fh = IO::File->new($file, 'r'); $fh->binmode(':utf8'); while (my $line = <$fh>) { if ($line =~ /\[(\d{4})-(\d\d)-(\d\d)\](.)\s*(.+)/) { my ($y, $m, $d, $type, $body) = ($1, $2, $3, $4, $5); push @rows => { date => sprintf( '%04d-%02d-%02d', $y, $m, $d ), y => $y, m => $m, d => $d, type => $type, body => $body, }; } } $fh->close; }); } return \@rows; } sub _find_schedules { my ($rows, $schedule_limit) = @_; my $date_re = _date_re($schedule_limit); my @schedules = sort { $a->{date} cmp $b->{date} } grep { $_->{date} =~ $date_re } grep { $_->{type} =~ /[!\@]/ } @$rows; return \@schedules; } sub _find_todoes { my ($rows, $todo_limit) = @_; my @todoes = grep { $_->{type} =~ /^[!\+]$/ } @$rows; @todoes = sort { $a->{date} cmp $b->{date} } @todoes; @todoes = @todoes[0..$todo_limit]; return \@todoes; } sub _date_re { my $limit = shift; my @set; my $dt = DateTime->today(); for (0..$limit) { push @set, $dt->ymd; $dt->add(days => 1); # DT は破壊的 } my $src = join '|', @set; qr{$src}; } sub _render { my ($schedules, $todoes) = @_; my $tmpl = join '', ; render_mt($tmpl, $schedules, $todoes)->as_string; } sub _type_class { my $row = shift; +{ '@' => 'type_schedule', '+' => 'type_todo', '!' => 'type_deadline', }->{$row->{type}} } =head1 SYNOPSIS $ howmtop2html.pl --todo-limit=30 # 何件TODO表示するか --schedule-limit=7 # 何日ぶんスケジュール表示するか =head1 DESCRIPTION howm の予定やらをケータイでみやすいようにフォーマットしちゃいます。 howm そのものよりも機能をそぎおとして軽量な実装です(「このスクリプトの実装は手抜きですよ」という意味の大人的表現。) =head1 COPYRIGHT This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of the license can be found in the LICENSE file included with this module. =cut __END__ ? my ($schedules, $todoes) = @_; gp.ath.cx
howm.html
schedule(@!)
? for my $schedule (@$schedules) { {date} ?>{type} ?>{body} ?>
? }
todo(!+)
? for my $todo (@$todoes) { {date} ?>{type} ?>{body} ?>
? }