=================================================================== RCS file: /cvs/todotxt/Text-Todo/bin/dudelicious.pl,v retrieving revision 1.5 retrieving revision 1.10 diff -u -r1.5 -r1.10 --- todotxt/Text-Todo/bin/dudelicious.pl 2010/04/29 05:50:33 1.5 +++ todotxt/Text-Todo/bin/dudelicious.pl 2010/05/01 21:23:19 1.10 @@ -1,31 +1,61 @@ -#!/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 Text::Todo; +use Mojo::JSON; -use Data::Dumper; +app->home->parse( $ENV{DUDELICIOUS_HOME} ) if $ENV{DUDELICIOUS_HOME}; -my %config = ( todo_dir => $ENV{DUDELICIOUS_HOME} || '.', ); +plugin 'json_config' => { + file => 'dudelicious.conf', + default => { 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') ); +app->renderer->add_helper( + todo => sub { + state $todo = Text::Todo->new( shift->stash('config') ); + return $todo; + } +); -plugin 'default_helpers'; +app->renderer->add_helper( + get_list => sub { + my ( $self, $file ) = @_; -my $todo = Text::Todo->new( \%config ); + $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->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; @@ -33,80 +63,48 @@ $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 $file = $self->stash('file') . '.txt'; + my $format = $self->stash('format') || 'html'; + my $list = $self->helper( 'get_list' => $file ); - $self->render( list => [ $todo->listfile($file) ], layout => 'todotxt' ); + if ( $format eq 'json' ) { + $self->render_json($list); + } + else { + $self->render( list => $list, layout => 'todotxt' ); + } } => 'list'; -get '/l/:file/e/:entry' => sub { - my $self = shift; - my $file = $self->stash('file') . '.txt'; - my $entry = $self->stash('entry') - 1; +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 ]; - $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} - ); - } + if ( $format eq 'json' ) { + $self->render_json($entry); } else { - app->log->debug("Configuration [$conf_file] is not available."); + $self->render( entry => $entry, layout => 'todotxt' ); } +} => 'entry'; - $ENV{SCRIPT_NAME} = $config{base} if defined $config{base}; +app->start unless caller(); - # 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; +% foreach my $entry (@{ $list }) { +%== include 'entry', entry => $entry; % } @@ entry.txt.ep -<%= $entry->text %> +<%= $entry->{text} %> @@ layouts/todotxt.txt.ep %= content @@ -119,23 +117,36 @@ @@ list.html.ep

<%= $file %>

    -% foreach my $i (0..$#{ $list }) { +% foreach my $entry (@{ $list }) {
  1. -%= include 'entry', entry => $list->[$i], line => $i + 1; +%= include 'entry', entry => $entry;
  2. % }
@@ entry.html.ep -<%= $entry->text %> +<%= $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 @@ -147,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.5 2010/04/29 04:50:33 andrew Exp $ + $Id: dudelicious.pl,v 1.10 2010/05/01 20:23:19 andrew Exp $ =head1 SYNOPSIS