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