=================================================================== RCS file: /cvs/todotxt/Text-Todo/bin/dudelicious.pl,v retrieving revision 1.6 retrieving revision 1.11 diff -u -r1.6 -r1.11 --- todotxt/Text-Todo/bin/dudelicious.pl 2010/04/30 07:18:33 1.6 +++ todotxt/Text-Todo/bin/dudelicious.pl 2010/05/01 21:26:17 1.11 @@ -1,14 +1,15 @@ -#!/usr/bin/env perl +#!/usr/bin/perl package Dudelicious; +use 5.010; use Data::Dumper; use version; our $VERSION = qv('0.1.0'); BEGIN { use FindBin; use lib "$FindBin::Bin/../lib"; - use lib "$FindBin::Bin/mojo/lib"; + use lib "$FindBin::Bin/../mojo/lib"; } use Carp qw/ carp croak /; @@ -25,10 +26,36 @@ default => { todo_dir => $ENV{DUDELICIOUS_HOME} || '.', }, }; +app->renderer->add_helper( + todo => sub { + state $todo = Text::Todo->new( shift->stash('config') ); + return $todo; + } +); + +app->renderer->add_helper( + get_list => sub { + my ( $self, $file ) = @_; + + $self->helper('todo')->load($file) if $file; + + 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,67 +63,39 @@ $self->render( files => \@files, layout => 'todotxt' ); } => 'index'; +get '/todotxt' => 'todotxt'; + get '/l/:file' => sub { my $self = shift; my $file = $self->stash('file') . '.txt'; my $format = $self->stash('format') || 'html'; + my $list = $self->helper( 'get_list' => $file ); if ( $format eq 'json' ) { - $self->render_json( _get_list( $self, $file ) ); + $self->render_json($list); } else { - $self->render( - list => _get_list( $self, $file ), - layout => 'todotxt' - ); + $self->render( list => $list, layout => 'todotxt' ); } } => 'list'; get '/l/:file/e/:line' => sub { my $self = shift; my $file = $self->stash('file') . '.txt'; - my $entry = $self->stash('line') - 1; my $format = $self->stash('format') || 'html'; + my $entry + = $self->helper( 'get_list' => $file )->[ $self->stash('line') - 1 ]; if ( $format eq 'json' ) { - $self->render_json( _get_list( $self, $file )->[$entry] ); + $self->render_json($entry); } else { - $self->render( - entry => _get_list( $self, $file )->[$entry], - layout => 'todotxt' - ); + $self->render( entry => $entry, layout => 'todotxt' ); } } => 'entry'; -app->start unless caller(); +app->start if !caller(); -sub _todo { - my ($c) = @_; - - if ( !$c->stash('todo') ) { - my $todo = Text::Todo->new( $c->stash('config') ); - $c->stash( 'todo' => $todo ); - } - - return $c->stash('todo'); -} - -sub _get_list { - my ( $c, $file ) = @_; - - my $line = 1; - return [ - map ( { line => $line++, - md5 => md5_hex( $_->text ), - text => $_->text, - done => $_->done, - }, - _todo($c)->listfile($file), - ) - ]; -} - __DATA__ @@ list.txt.ep @@ -130,10 +129,24 @@ @@ layouts/todotxt.html.ep -