[BACK]Return to dudelicious.pl CVS log [TXT][DIR] Up to [local] / todotxt / Text-Todo / bin

Annotation of todotxt/Text-Todo/bin/dudelicious.pl, Revision 1.7

1.1       andrew      1: #!/usr/bin/env perl
                      2:
1.2       andrew      3: package Dudelicious;
                      4:
1.6       andrew      5: use Data::Dumper;
1.4       andrew      6: use version; our $VERSION = qv('0.1.0');
                      7:
1.6       andrew      8: BEGIN {
                      9:     use FindBin;
                     10:     use lib "$FindBin::Bin/../lib";
                     11:     use lib "$FindBin::Bin/mojo/lib";
                     12: }
1.2       andrew     13:
1.5       andrew     14: use Carp qw/ carp croak /;
1.6       andrew     15: use Digest::MD5 qw/ md5_hex /;
                     16: use Text::Todo;
1.5       andrew     17:
1.1       andrew     18: use Mojolicious::Lite;
1.6       andrew     19: use Mojo::JSON;
1.5       andrew     20:
                     21: app->home->parse( $ENV{DUDELICIOUS_HOME} ) if $ENV{DUDELICIOUS_HOME};
1.1       andrew     22:
1.6       andrew     23: plugin 'json_config' => {
                     24:     file    => 'dudelicious.conf',
                     25:     default => { todo_dir => $ENV{DUDELICIOUS_HOME} || '.', },
                     26: };
1.1       andrew     27:
1.5       andrew     28: get '/' => sub {
1.1       andrew     29:     my $self = shift;
1.5       andrew     30:
1.6       andrew     31:     my $dir = _todo($self)->file('todo_dir');
1.5       andrew     32:     opendir my $dh, $dir or croak "Unable to opendir $dir: $!";
                     33:     my @files = grep {/\.te?xt$/ixms} readdir $dh;
                     34:     closedir $dh;
                     35:
                     36:     $self->render( files => \@files, layout => 'todotxt' );
                     37: } => 'index';
                     38:
                     39: get '/l/:file' => sub {
1.6       andrew     40:     my $self   = shift;
                     41:     my $file   = $self->stash('file') . '.txt';
                     42:     my $format = $self->stash('format') || 'html';
1.7     ! andrew     43:     my $list   = _get_list( $self, $file );
1.5       andrew     44:
1.6       andrew     45:     if ( $format eq 'json' ) {
1.7     ! andrew     46:         $self->render_json($list);
1.6       andrew     47:     }
                     48:     else {
1.7     ! andrew     49:         $self->render( list => $list, layout => 'todotxt' );
1.6       andrew     50:     }
1.5       andrew     51: } => 'list';
                     52:
1.6       andrew     53: get '/l/:file/e/:line' => sub {
                     54:     my $self   = shift;
                     55:     my $file   = $self->stash('file') . '.txt';
                     56:     my $format = $self->stash('format') || 'html';
1.7     ! andrew     57:     my $entry  = _get_list( $self, $file )->[ $self->stash('line') - 1 ];
1.6       andrew     58:
                     59:     if ( $format eq 'json' ) {
1.7     ! andrew     60:         $self->render_json($entry);
1.6       andrew     61:     }
                     62:     else {
1.7     ! andrew     63:         $self->render( entry => $entry, layout => 'todotxt' );
1.6       andrew     64:     }
1.5       andrew     65: } => 'entry';
1.1       andrew     66:
1.3       andrew     67: app->start unless caller();
1.5       andrew     68:
1.6       andrew     69: sub _todo {
                     70:     my ($c) = @_;
1.5       andrew     71:
1.6       andrew     72:     if ( !$c->stash('todo') ) {
                     73:         my $todo = Text::Todo->new( $c->stash('config') );
                     74:         $c->stash( 'todo' => $todo );
                     75:     }
1.5       andrew     76:
1.6       andrew     77:     return $c->stash('todo');
                     78: }
1.5       andrew     79:
1.6       andrew     80: sub _get_list {
                     81:     my ( $c, $file ) = @_;
1.5       andrew     82:
1.6       andrew     83:     my $line = 1;
                     84:     return [
                     85:         map ( { line => $line++,
                     86:                 md5  => md5_hex( $_->text ),
                     87:                 text => $_->text,
                     88:                 done => $_->done,
                     89:             },
                     90:             _todo($c)->listfile($file),
                     91:         )
                     92:     ];
1.5       andrew     93: }
                     94:
1.1       andrew     95: __DATA__
                     96:
1.5       andrew     97: @@ list.txt.ep
1.6       andrew     98: % foreach my $entry (@{ $list }) {
                     99: %==  include 'entry', entry => $entry;
1.5       andrew    100: % }
                    101:
                    102: @@ entry.txt.ep
1.6       andrew    103: <%= $entry->{text} %>
1.5       andrew    104:
                    105: @@ layouts/todotxt.txt.ep
                    106: %= content
                    107:
1.1       andrew    108: @@ index.html.ep
1.5       andrew    109: % foreach my $file (@{ $files }) {
                    110: <%== $file %> <br />
                    111: % }
                    112:
                    113: @@ list.html.ep
                    114: <h1><%= $file %></h1>
                    115: <ol>
1.6       andrew    116: % foreach my $entry (@{ $list }) {
1.5       andrew    117:     <li>
1.6       andrew    118: %=  include 'entry', entry => $entry;
1.5       andrew    119:     </li>
                    120: % }
                    121: </ol>
1.1       andrew    122:
1.5       andrew    123: @@ entry.html.ep
1.6       andrew    124: <%= $entry->{text} %>
1.5       andrew    125:
                    126: @@ layouts/todotxt.html.ep
1.1       andrew    127: <!doctype html><html>
                    128:     <head><title>Funky!</title></head>
                    129:     <body><%== content %></body>
                    130: </html>
1.4       andrew    131:
                    132: __END__
                    133:
                    134: =head1 NAME
                    135:
                    136: dudelicious - A Mojolicous interface to your todotxt files
                    137:
                    138: =head1 VERSION
                    139:
                    140: Since the $VERSION can't be automatically included,
                    141: here is the RCS Id instead, you'll have to look up $VERSION.
                    142:
1.7     ! andrew    143:     $Id: dudelicious.pl,v 1.6 2010/04/30 06:18:33 andrew Exp $
1.4       andrew    144:
                    145: =head1 SYNOPSIS
                    146:
                    147:     dudelicious daemon
                    148:
                    149: Then browse to http://localhost:3000/
                    150:
                    151: =head1 DESCRIPTION
                    152:
                    153: A Mojolicous web app for access to your todotxt files
                    154:
                    155: The modules are there to give more access to my todo.txt file from more
                    156: places.  My goal is a web API for a web interface and then a WebOS version for
                    157: my Palm Pre.
                    158:
                    159: For more information see L<http://todotxt.com>
                    160:
                    161: =head1 USAGE
                    162:
                    163: See todo.pl -h
                    164:
                    165: =head1 OPTIONS
                    166:
                    167: See todo.pl -h
                    168:
                    169: =head1 REQUIRED ARGUMENTS
                    170:
                    171: See todo.pl -h
                    172:
                    173: =head1 CONFIGURATION AND ENVIRONMENT
                    174:
                    175: =head1 DIAGNOSTICS
                    176:
                    177: =head1 DEPENDENCIES
                    178:
                    179: Perl Modules:
                    180:
                    181: =over
                    182:
                    183: =item Text::Todo
                    184:
                    185: =item Mojolicous::Lite
                    186:
                    187: =item version
                    188:
                    189: =back
                    190:
                    191:
                    192: =head1 BUGS AND LIMITATIONS
                    193:
                    194: No bugs have been reported.
                    195:
                    196: =head1 AUTHOR
                    197:
                    198: Andrew Fresh  C<< <andrew@cpan.org> >>
                    199:
                    200:
                    201: =head1 LICENSE AND COPYRIGHT
                    202:
                    203: Copyright (c) 2010, Andrew Fresh C<< <andrew@cpan.org> >>. All rights reserved.
                    204:
                    205: This module is free software; you can redistribute it and/or
                    206: modify it under the same terms as Perl itself. See L<perlartistic>.
                    207:
                    208:
                    209: =head1 DISCLAIMER OF WARRANTY
                    210:
                    211: BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
                    212: FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
                    213: OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
                    214: PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
                    215: EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
                    216: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
                    217: ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
                    218: YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
                    219: NECESSARY SERVICING, REPAIR, OR CORRECTION.
                    220:
                    221: IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
                    222: WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
                    223: REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
                    224: LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
                    225: OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
                    226: THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
                    227: RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
                    228: FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
                    229: SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
                    230: SUCH DAMAGES.

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>