version 1.5, 2010/04/29 05:50:33 |
version 1.7, 2010/04/30 07:23:32 |
|
|
|
|
package Dudelicious; |
package Dudelicious; |
|
|
|
use Data::Dumper; |
use version; our $VERSION = qv('0.1.0'); |
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 Carp qw/ carp croak /; |
|
use Digest::MD5 qw/ md5_hex /; |
|
use Text::Todo; |
|
|
use Mojolicious::Lite; |
use Mojolicious::Lite; |
use Text::Todo; |
use Mojo::JSON; |
|
|
use Data::Dumper; |
|
|
|
my %config = ( todo_dir => $ENV{DUDELICIOUS_HOME} || '.', ); |
|
|
|
app->home->parse( $ENV{DUDELICIOUS_HOME} ) if $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'; |
plugin 'json_config' => { |
|
file => 'dudelicious.conf', |
|
default => { todo_dir => $ENV{DUDELICIOUS_HOME} || '.', }, |
|
}; |
|
|
my $todo = Text::Todo->new( \%config ); |
|
|
|
get '/' => sub { |
get '/' => sub { |
my $self = shift; |
my $self = shift; |
|
|
my $dir = $todo->file('todo_dir'); |
my $dir = _todo($self)->file('todo_dir'); |
opendir my $dh, $dir or croak "Unable to opendir $dir: $!"; |
opendir my $dh, $dir or croak "Unable to opendir $dir: $!"; |
my @files = grep {/\.te?xt$/ixms} readdir $dh; |
my @files = grep {/\.te?xt$/ixms} readdir $dh; |
closedir $dh; |
closedir $dh; |
|
|
} => 'index'; |
} => 'index'; |
|
|
get '/l/:file' => sub { |
get '/l/:file' => sub { |
my $self = shift; |
my $self = shift; |
my $file = $self->stash('file') . '.txt'; |
my $file = $self->stash('file') . '.txt'; |
|
my $format = $self->stash('format') || 'html'; |
|
my $list = _get_list( $self, $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'; |
} => 'list'; |
|
|
get '/l/:file/e/:entry' => sub { |
get '/l/:file/e/:line' => sub { |
my $self = shift; |
my $self = shift; |
my $file = $self->stash('file') . '.txt'; |
my $file = $self->stash('file') . '.txt'; |
my $entry = $self->stash('entry') - 1; |
my $format = $self->stash('format') || 'html'; |
|
my $entry = _get_list( $self, $file )->[ $self->stash('line') - 1 ]; |
|
|
$self->render( |
if ( $format eq 'json' ) { |
entry => $todo->listfile($file)->[$entry], |
$self->render_json($entry); |
layout => 'todotxt' |
} |
); |
else { |
|
$self->render( entry => $entry, layout => 'todotxt' ); |
|
} |
} => 'entry'; |
} => 'entry'; |
|
|
app->start unless caller(); |
app->start unless caller(); |
|
|
sub _read_config_from_file { |
sub _todo { |
my ($conf_file) = @_; |
my ($c) = @_; |
|
|
app->log->debug("Reading configuration from $conf_file."); |
if ( !$c->stash('todo') ) { |
|
my $todo = Text::Todo->new( $c->stash('config') ); |
if ( -e $conf_file ) { |
$c->stash( 'todo' => $todo ); |
if ( open FILE, "<", $conf_file ) { |
|
my @lines = <FILE>; |
|
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}; |
return $c->stash('todo'); |
|
} |
|
|
# set proper templates base dir, if defined |
sub _get_list { |
app->renderer->root( app->home->rel_dir( $config{templatesdir} ) ) |
my ( $c, $file ) = @_; |
if defined $config{templatesdir}; |
|
|
|
# set proper public base dir, if defined |
my $line = 1; |
app->static->root( app->home->rel_dir( $config{publicdir} ) ) |
return [ |
if defined $config{publicdir}; |
map ( { line => $line++, |
|
md5 => md5_hex( $_->text ), |
|
text => $_->text, |
|
done => $_->done, |
|
}, |
|
_todo($c)->listfile($file), |
|
) |
|
]; |
} |
} |
|
|
__DATA__ |
__DATA__ |
|
|
@@ list.txt.ep |
@@ list.txt.ep |
% foreach my $i (0..$#{ $list }) { |
% foreach my $entry (@{ $list }) { |
%== include 'entry', entry => $list->[$i], line => $i + 1; |
%== include 'entry', entry => $entry; |
% } |
% } |
|
|
@@ entry.txt.ep |
@@ entry.txt.ep |
<%= $entry->text %> |
<%= $entry->{text} %> |
|
|
@@ layouts/todotxt.txt.ep |
@@ layouts/todotxt.txt.ep |
%= content |
%= content |
|
|
@@ list.html.ep |
@@ list.html.ep |
<h1><%= $file %></h1> |
<h1><%= $file %></h1> |
<ol> |
<ol> |
% foreach my $i (0..$#{ $list }) { |
% foreach my $entry (@{ $list }) { |
<li> |
<li> |
%= include 'entry', entry => $list->[$i], line => $i + 1; |
%= include 'entry', entry => $entry; |
</li> |
</li> |
% } |
% } |
</ol> |
</ol> |
|
|
@@ entry.html.ep |
@@ entry.html.ep |
<%= $entry->text %> |
<%= $entry->{text} %> |
|
|
@@ layouts/todotxt.html.ep |
@@ layouts/todotxt.html.ep |
<!doctype html><html> |
<!doctype html><html> |
<head><title>Funky!</title></head> |
<head><title>Funky!</title></head> |
<body><%== content %></body> |
<body><%== content %></body> |
</html> |
</html> |
|
|
|
|
__END__ |
__END__ |
|
|