version 1.5, 2010/04/29 05:50:33 |
version 1.14, 2010/05/01 22:47:51 |
|
|
#!/usr/bin/env perl |
#!/usr/bin/perl |
|
|
package Dudelicious; |
package Dudelicious; |
|
|
|
use 5.010; |
|
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; |
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}; |
app->renderer->add_helper( |
_read_config_from_file( app->home->rel_file('dudelicious.conf') ); |
todo => sub { |
|
my ($self) = @_; |
|
state $todo = Text::Todo->new( $self->stash('config') ); |
|
|
plugin 'default_helpers'; |
my $file = $self->stash('file'); |
|
if ($file) { |
|
$file =~ s/(?:\.txt)?$/\.txt/ixms; |
|
$todo->load($file); |
|
} |
|
|
my $todo = Text::Todo->new( \%config ); |
return $todo; |
|
} |
|
); |
|
|
|
app->renderer->add_helper( |
|
get_list => sub { |
|
my ($self) = @_; |
|
|
|
my $line = 1; |
|
return [ |
|
map { |
|
line => $line++, |
|
md5 => md5_hex( $_->text ), |
|
text => $_->text, |
|
done => $_->done, |
|
}, |
|
$self->helper('todo')->list |
|
]; |
|
} |
|
); |
|
|
get '/' => sub { |
get '/' => sub { |
my $self = shift; |
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: $!"; |
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; |
|
|
$self->render( files => \@files, layout => 'todotxt' ); |
$self->render( files => \@files, layout => 'todotxt' ); |
} => 'index'; |
} => 'index'; |
|
|
|
get '/todotxt' => 'todotxt'; |
|
|
get '/l/:file' => sub { |
get '/l/:file' => sub { |
my $self = shift; |
my $self = shift; |
my $file = $self->stash('file') . '.txt'; |
|
|
|
$self->render( list => [ $todo->listfile($file) ], layout => 'todotxt' ); |
my $format = $self->stash('format') || 'html'; |
|
|
|
if ( $format eq 'json' ) { |
|
$self->render_json( $self->helper('get_list') ); |
|
} |
|
else { |
|
$self->render( |
|
list => $self->helper('get_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 $entry = $self->stash('entry') - 1; |
|
|
|
$self->render( |
my $format = $self->stash('format') || 'html'; |
entry => $todo->listfile($file)->[$entry], |
my $entry = $self->helper('get_list')->[ $self->stash('line') - 1 ]; |
layout => 'todotxt' |
|
); |
if ( $format eq 'json' ) { |
|
$self->render_json($entry); |
|
} |
|
else { |
|
$self->render( entry => $entry, layout => 'todotxt' ); |
|
} |
} => 'entry'; |
} => 'entry'; |
|
|
app->start unless caller(); |
get '/l/:file/t' => sub { |
|
my $self = shift; |
|
|
sub _read_config_from_file { |
my $format = $self->stash('format') || 'html'; |
my ($conf_file) = @_; |
|
|
|
app->log->debug("Reading configuration from $conf_file."); |
if ( $format eq 'json' ) { |
|
$self->render_json( $self->helper('todo')->known_tags ); |
|
} |
|
else { |
|
$self->render( |
|
tags => $self->helper('todo')->known_tags, |
|
layout => 'todotxt' |
|
); |
|
} |
|
} => 'tags'; |
|
|
if ( -e $conf_file ) { |
get '/l/:file/t/:tag' => sub { |
if ( open FILE, "<", $conf_file ) { |
my $self = shift; |
my @lines = <FILE>; |
|
close FILE; |
|
|
|
my $line = ''; |
my $format = $self->stash('format') || 'html'; |
foreach my $l (@lines) { |
my $items = $self->helper('todo')->listtag( $self->stash('tag') ); |
next if $l =~ m/^\s*#/; |
|
$line .= $l; |
|
} |
|
|
|
my $json = Mojo::JSON->new; |
if ( $format eq 'json' ) { |
my $json_config = $json->decode($line) || {}; |
$self->render_json($items); |
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 { |
else { |
app->log->debug("Configuration [$conf_file] is not available."); |
$self->render( items => $items, layout => 'todotxt' ); |
} |
} |
|
} => 'tag'; |
|
|
$ENV{SCRIPT_NAME} = $config{base} if defined $config{base}; |
app->start if !caller(); |
|
|
# set proper templates base dir, if defined |
1; |
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__ |
__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} %> |
|
|
|
@@ tags.txt.ep |
|
% foreach my $tag (keys %{ $tags }) { |
|
<%= $tag %>, <%= $tags->{$tag} %> |
|
% } |
|
|
|
@@ tag.txt.ep |
|
# <%= $tag %> |
|
% foreach my $item (@{ $items}) { |
|
<%= $item %> |
|
% } |
|
|
@@ layouts/todotxt.txt.ep |
@@ layouts/todotxt.txt.ep |
%= content |
%= content |
|
|
@@ index.html.ep |
@@ index.html.ep |
% foreach my $file (@{ $files }) { |
% foreach my $file (@{ $files }) { |
<%== $file %> <br /> |
% my ($basename) = $file =~ /^(.*?)(?:\.[^\.]+)?$/xms; |
|
<a href="<%= url_for 'list' %>/<%= $basename %>"><%= $file %></a><br /> |
% } |
% } |
|
|
@@ 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} %> |
|
|
|
@@ tags.html.ep |
|
% foreach my $tag (keys%{ $tags }) { |
|
<a href="<%= url_for 'tag', format => '' %>/<%= $tag %>"><%= $tag %> == <%= $tags->{$tag} %></a><br /> |
|
% } |
|
|
|
@@ tag.html.ep |
|
<h2><%= $tag %></h2> |
|
% foreach my $item (@{ $items }) { |
|
<%= $item %><br /> |
|
% } |
|
|
@@ layouts/todotxt.html.ep |
@@ layouts/todotxt.html.ep |
<!doctype html><html> |
<!doctype html><html> |
<head><title>Funky!</title></head> |
<head> |
|
<title>Funky!</title> |
|
<link rel="stylesheet" href="<%= url_for 'todotxt', format => 'css' %>"> |
|
</head> |
<body><%== content %></body> |
<body><%== content %></body> |
</html> |
</html> |
|
|
|
@@ todotxt.css.ep |
|
body { |
|
background: LightGoldenRodYellow; |
|
color: DarkSlateBlue; |
|
} |
|
|
|
.inplaceeditor-saving { |
|
background: url(images/saving.gif) bottom right no-repeat; |
|
} |
|
|
|
|
__END__ |
__END__ |