=================================================================== RCS file: /cvs/todotxt/Text-Todo/bin/dudelicious.pl,v retrieving revision 1.4 retrieving revision 1.11 diff -u -r1.4 -r1.11 --- todotxt/Text-Todo/bin/dudelicious.pl 2010/04/28 01:50:47 1.4 +++ todotxt/Text-Todo/bin/dudelicious.pl 2010/05/01 21:26:17 1.11 @@ -1,33 +1,152 @@ -#!/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/mojo/lib" } +BEGIN { + use FindBin; + use lib "$FindBin::Bin/../lib"; + use lib "$FindBin::Bin/../mojo/lib"; +} +use Carp qw/ carp croak /; +use Digest::MD5 qw/ md5_hex /; +use Text::Todo; + use Mojolicious::Lite; +use Mojo::JSON; -get '/' => 'index'; +app->home->parse( $ENV{DUDELICIOUS_HOME} ) if $ENV{DUDELICIOUS_HOME}; -get '/:groovy' => sub { - my $self = shift; - $self->render_text($self->param('groovy'), layout => 'funky'); +plugin 'json_config' => { + file => 'dudelicious.conf', + default => { todo_dir => $ENV{DUDELICIOUS_HOME} || '.', }, }; -app->start unless caller(); +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 = $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; + + $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($list); + } + else { + $self->render( list => $list, layout => 'todotxt' ); + } +} => 'list'; + +get '/l/:file/e/:line' => sub { + my $self = shift; + my $file = $self->stash('file') . '.txt'; + my $format = $self->stash('format') || 'html'; + my $entry + = $self->helper( 'get_list' => $file )->[ $self->stash('line') - 1 ]; + + if ( $format eq 'json' ) { + $self->render_json($entry); + } + else { + $self->render( entry => $entry, layout => 'todotxt' ); + } +} => 'entry'; + +app->start if !caller(); + __DATA__ +@@ list.txt.ep +% foreach my $entry (@{ $list }) { +%== include 'entry', entry => $entry; +% } + +@@ entry.txt.ep +<%= $entry->{text} %> + +@@ layouts/todotxt.txt.ep +%= content + @@ index.html.ep -% layout 'funky'; -Yea baby! +% foreach my $file (@{ $files }) { +<%== $file %>
+% } -@@ layouts/funky.html.ep +@@ list.html.ep +

<%= $file %>

+
    +% foreach my $entry (@{ $list }) { +
  1. +%= include 'entry', entry => $entry; +
  2. +% } +
+ +@@ entry.html.ep +<%= $entry->{text} %> + +@@ layouts/todotxt.html.ep - Funky! + + Funky! + + <%== content %> +@@ todotxt.css.ep +body { + background: LightGoldenRodYellow; + color: DarkSlateBlue; +} + +.inplaceeditor-saving { + background: url(images/saving.gif) bottom right no-repeat; +} + + __END__ =head1 NAME @@ -39,7 +158,7 @@ Since the $VERSION can't be automatically included, here is the RCS Id instead, you'll have to look up $VERSION. - $Id: dudelicious.pl,v 1.4 2010/04/28 00:50:47 andrew Exp $ + $Id: dudelicious.pl,v 1.11 2010/05/01 20:26:17 andrew Exp $ =head1 SYNOPSIS