=================================================================== RCS file: /cvs/todotxt/Text-Todo/bin/dudelicious.pl,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- todotxt/Text-Todo/bin/dudelicious.pl 2010/04/28 01:50:47 1.4 +++ todotxt/Text-Todo/bin/dudelicious.pl 2010/04/29 05:50:33 1.5 @@ -6,28 +6,136 @@ BEGIN { use FindBin; use lib "$FindBin::Bin/mojo/lib" } +use Carp qw/ carp croak /; + use Mojolicious::Lite; +use Text::Todo; -get '/' => 'index'; +use Data::Dumper; -get '/:groovy' => sub { +my %config = ( todo_dir => $ENV{DUDELICIOUS_HOME} || '.', ); + +app->home->parse( $ENV{DUDELICIOUS_HOME} ) if $ENV{DUDELICIOUS_HOME}; +_read_config_from_file( app->home->rel_file('dudelicious.conf') ); + +plugin 'default_helpers'; + +my $todo = Text::Todo->new( \%config ); + +get '/' => sub { my $self = shift; - $self->render_text($self->param('groovy'), layout => 'funky'); -}; + my $dir = $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 '/l/:file' => sub { + my $self = shift; + my $file = $self->stash('file') . '.txt'; + + $self->render( list => [ $todo->listfile($file) ], layout => 'todotxt' ); +} => 'list'; + +get '/l/:file/e/:entry' => sub { + my $self = shift; + my $file = $self->stash('file') . '.txt'; + my $entry = $self->stash('entry') - 1; + + $self->render( + entry => $todo->listfile($file)->[$entry], + layout => 'todotxt' + ); +} => 'entry'; + app->start unless caller(); + +sub _read_config_from_file { + my ($conf_file) = @_; + + app->log->debug("Reading configuration from $conf_file."); + + if ( -e $conf_file ) { + if ( open FILE, "<", $conf_file ) { + my @lines = ; + close FILE; + + my $line = ''; + foreach my $l (@lines) { + next if $l =~ m/^\s*#/; + $line .= $l; + } + + my $json = Mojo::JSON->new; + my $json_config = $json->decode($line) || {}; + die $json->error if !$json_config && $json->error; + + %config = ( %config, %$json_config ); + + unshift @INC, $_ + for ( + ref $config{perl5lib} eq 'ARRAY' + ? @{ $config{perl5lib} } + : $config{perl5lib} + ); + } + } + else { + app->log->debug("Configuration [$conf_file] is not available."); + } + + $ENV{SCRIPT_NAME} = $config{base} if defined $config{base}; + + # set proper templates base dir, if defined + app->renderer->root( app->home->rel_dir( $config{templatesdir} ) ) + if defined $config{templatesdir}; + + # set proper public base dir, if defined + app->static->root( app->home->rel_dir( $config{publicdir} ) ) + if defined $config{publicdir}; +} + __DATA__ +@@ list.txt.ep +% foreach my $i (0..$#{ $list }) { +%== include 'entry', entry => $list->[$i], line => $i + 1; +% } + +@@ 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 $i (0..$#{ $list }) { +
  1. +%= include 'entry', entry => $list->[$i], line => $i + 1; +
  2. +% } +
+ +@@ entry.html.ep +<%= $entry->text %> + +@@ layouts/todotxt.html.ep Funky! <%== content %> + __END__ =head1 NAME @@ -39,7 +147,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.5 2010/04/29 04:50:33 andrew Exp $ =head1 SYNOPSIS