===================================================================
RCS file: /cvs/todotxt/Text-Todo/bin/dudelicious.pl,v
retrieving revision 1.8
retrieving revision 1.16
diff -u -r1.8 -r1.16
--- todotxt/Text-Todo/bin/dudelicious.pl 2010/04/30 18:17:40 1.8
+++ todotxt/Text-Todo/bin/dudelicious.pl 2010/05/05 03:01:08 1.16
@@ -2,6 +2,7 @@
package Dudelicious;
+use 5.010;
use Data::Dumper;
use version; our $VERSION = qv('0.1.0');
@@ -25,10 +26,42 @@
default => { todo_dir => $ENV{DUDELICIOUS_HOME} || '.', },
};
+app->renderer->add_helper(
+ todo => sub {
+ my ($self) = @_;
+ state $todo = Text::Todo->new( $self->stash('config') );
+
+ my $file = $self->stash('file');
+ if ($file) {
+ $file =~ s/(?:\.txt)?$/\.txt/ixms;
+ $todo->load($file);
+ }
+
+ return $todo;
+ }
+);
+
+app->renderer->add_helper(
+ get_list => sub {
+ my ($self) = @_;
+
+ my $line = 1;
+ return [
+ map {
+ line => $line++,
+ md5 => md5_hex( $_->text ),
+ text => $_->text,
+ done => $_->done,
+ },
+ $self->helper('todo')->list
+ ];
+ }
+);
+
get '/' => sub {
my $self = shift;
- my $dir = _todo($self)->file('todo_dir');
+ my $dir = $self->helper('todo')->file('todo_dir');
opendir my $dh, $dir or croak "Unable to opendir $dir: $!";
my @files = grep {/\.te?xt$/ixms} readdir $dh;
closedir $dh;
@@ -36,25 +69,30 @@
$self->render( files => \@files, layout => 'todotxt' );
} => 'index';
+get '/todotxt' => 'todotxt';
+
get '/l/:file' => sub {
- my $self = shift;
- my $file = $self->stash('file') . '.txt';
+ my $self = shift;
+
my $format = $self->stash('format') || 'html';
- my $list = _get_list( $self, $file );
if ( $format eq 'json' ) {
- $self->render_json($list);
+ $self->render_json( $self->helper('get_list') );
}
else {
- $self->render( list => $list, layout => 'todotxt' );
+ $self->render(
+ list => $self->helper('get_list'),
+ tags => $self->helper('todo')->known_tags,
+ layout => 'todotxt'
+ );
}
} => 'list';
get '/l/:file/e/:line' => sub {
- my $self = shift;
- my $file = $self->stash('file') . '.txt';
+ my $self = shift;
+
my $format = $self->stash('format') || 'html';
- my $entry = _get_list( $self, $file )->[ $self->stash('line') - 1 ];
+ my $entry = $self->helper('get_list')->[ $self->stash('line') - 1 ];
if ( $format eq 'json' ) {
$self->render_json($entry);
@@ -64,34 +102,39 @@
}
} => 'entry';
-app->start unless caller();
+get '/l/:file/t' => sub {
+ my $self = shift;
-sub _todo {
- my ($c) = @_;
+ my $format = $self->stash('format') || 'html';
- if ( !$c->stash('todo') ) {
- my $todo = Text::Todo->new( $c->stash('config') );
- $c->stash( 'todo' => $todo );
+ if ( $format eq 'json' ) {
+ $self->render_json( $self->helper('todo')->known_tags );
}
+ else {
+ $self->render(
+ tags => $self->helper('todo')->known_tags,
+ layout => 'todotxt'
+ );
+ }
+} => 'tags';
- return $c->stash('todo');
-}
+get '/l/:file/t/:tag' => sub {
+ my $self = shift;
-sub _get_list {
- my ( $c, $file ) = @_;
+ my $format = $self->stash('format') || 'html';
+ my $items = $self->helper('todo')->listtag( $self->stash('tag') );
- my $line = 1;
- return [
- map ( { line => $line++,
- md5 => md5_hex( $_->text ),
- text => $_->text,
- done => $_->done,
- },
- _todo($c)->listfile($file),
- )
- ];
-}
+ if ( $format eq 'json' ) {
+ $self->render_json($items);
+ }
+ else {
+ $self->render( items => $items, layout => 'todotxt' );
+ }
+} => 'tag';
+app->start if !caller();
+
+1;
__DATA__
@@ list.txt.ep
@@ -102,16 +145,34 @@
@@ entry.txt.ep
<%= $entry->{text} %>
+@@ tags.txt.ep
+% foreach my $tag (keys %{ $tags }) {
+<%= $tag %>: <%= $tags->{$tag} %>
+% }
+
+@@ tag.txt.ep
+# <%== $tag %>
+% foreach my $item (@{ $items}) {
+<%= $item %>
+% }
+
@@ layouts/todotxt.txt.ep
%= content
@@ index.html.ep
% foreach my $file (@{ $files }) {
-<%== $file %>
+% my ($basename) = $file =~ /^(.*?)(?:\.[^\.]+)?$/xms;
+<%= $file %>
% }
@@ list.html.ep