[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.6

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

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