=================================================================== RCS file: /cvs/todotxt/Text-Todo/lib/Text/Todo/Entry.pm,v retrieving revision 1.12 retrieving revision 1.17 diff -u -r1.12 -r1.17 --- todotxt/Text-Todo/lib/Text/Todo/Entry.pm 2010/01/10 00:13:14 1.12 +++ todotxt/Text-Todo/lib/Text/Todo/Entry.pm 2010/01/11 01:08:35 1.17 @@ -1,6 +1,6 @@ package Text::Todo::Entry; -# $RedRiver: Entry.pm,v 1.11 2010/01/09 07:08:45 andrew Exp $ +# $RedRiver: Entry.pm,v 1.16 2010/01/10 22:49:53 andrew Exp $ use warnings; use strict; @@ -17,49 +17,66 @@ my %tags_of; my %priority_of; my %completion_status_of; + my %known_tags_of; - my %tags = ( - context => q{@}, - project => q{+}, - ); - # XXX Should the completion (x) be case sensitive? - my $priority_completion_regex = qr/ + my $priority_completion_regex = qr{ ^ \s* - (?i: (x) \s+)? - (?i:\( ([A-Z]) \) \s+)? - /xms; + (?i:(x \s* [\d-]* ) \s*)? + (?i:\( ([A-Z]) \) \s*)? + }xms; - for my $tag ( keys %tags ) { - ## no critic strict - no strict 'refs'; # Violates use strict, but allows code generation - ## use critic + sub new { + my ( $class, $options ) = @_; - *{ $tag . 's' } = sub { - my ($self) = @_; - return $self->_tags($tag); - }; + my $self = bless anon_scalar(), $class; + my $ident = ident($self); - *{ 'in_' . $tag } = sub { - my ( $self, $item ) = @_; - return $self->_is_in( $tag . 's', $item ); + if ( !ref $options ) { + $options = { text => $options }; + } + elsif ( ref $options ne 'HASH' ) { + croak 'Invalid parameter passed!'; + } + + $known_tags_of{$ident} = { + context => q{@}, + project => q{+}, }; - } - sub replace { _update_entry(@_) } + if ( exists $options->{tags} && ref $options->{tags} eq 'HASH' ) { + foreach my $k ( keys %{ $options->{tags} } ) { + $known_tags_of{$ident}{$k} = $options->{tags}->{$k}; + } + } - sub new { - my ( $class, $text ) = @_; + for my $tag ( keys %{ $known_tags_of{$ident} } ) { + ## no critic strict + no strict + 'refs'; # Violates use strict, but allows code generation + ## use critic - my $self = bless anon_scalar(), $class; - my $ident = ident($self); + if ( !$self->can( $tag . 's' ) ) { + *{ $tag . 's' } = sub { + my ($self) = @_; + return $self->_tags($tag); + }; + } - $self->_update_entry($text); + if ( !$self->can( 'in_' . $tag ) ) { + *{ 'in_' . $tag } = sub { + my ( $self, $item ) = @_; + return $self->_is_in( $tag . 's', $item ); + }; + } + } + $self->replace( $options->{text} ); + return $self; } - sub _update_entry { + sub replace { my ( $self, $text ) = @_; my $ident = ident($self); @@ -67,17 +84,43 @@ $text_of{$ident} = $text; - foreach my $tag ( keys %tags ) { - my $symbol = quotemeta $tags{$tag}; + foreach my $tag ( keys %{ $known_tags_of{$ident} } ) { + my $symbol = quotemeta $known_tags_of{$ident}{$tag}; $tags_of{$ident}{$tag} = { map { $_ => q{} } - $text =~ / (?:^|\s) $symbol (\S+)/gxms }; + $text =~ / (?:^|\s) $symbol (\S*)/gxms }; } - ( $completion_status_of{$ident}, $priority_of{$ident} ) + my ( $completed, $priority ) = $text =~ / $priority_completion_regex /xms; + $completion_status_of{$ident} = _clean_completed($completed); + $priority_of{$ident} = $priority; + return 1; } + sub _clean_completed { + my ($completed) = @_; + + $completed ||= q{}; + $completed =~ s/^\s+|\s+$//gxms; + + if (!$completed) { + return; + } + + if ($completed =~ s/(x)\s*//ixms) { + my $status = $1; + if ($completed) { + return $completed; + } + else { + return $status; + } + } + + return; + } + sub _tags { my ( $self, $tag ) = @_; my $ident = ident($self); @@ -98,7 +141,7 @@ return $text_of{$ident}; } - sub depri { pri( $_[0], '' ) } + sub depri { my ($self) = @_; return $self->pri(q{}) } sub pri { my ( $self, $new_pri ) = @_; @@ -140,15 +183,17 @@ push @new, $addition; } - return $self->_update_entry( join q{ }, @new, $new ); + return $self->replace( join q{ }, @new, $new ); } sub append { my ( $self, $addition ) = @_; - return $self->_update_entry( join q{ }, $self->text, $addition ); + return $self->replace( join q{ }, $self->text, $addition ); } - sub do { + ## no critic 'homonym' + sub do { # This is what it is called in todo.sh + ## use critic my ($self) = @_; my $ident = ident($self); @@ -182,7 +227,7 @@ Since the $VERSION can't be automatically included, here is the RCS Id instead, you'll have to look up $VERSION. - $Id: Entry.pm,v 1.12 2010/01/10 00:13:14 andrew Exp $ + $Id: Entry.pm,v 1.17 2010/01/11 01:08:35 andrew Exp $ =head1 SYNOPSIS @@ -212,10 +257,16 @@ Creates an entry that can be manipulated. - my $entry = Text::Todo::Entry->new(['text of entry']); + my $entry = Text::Todo::Entry->new([ + 'text of entry' | { + [ text => 'text of entry' ,] + [ tags => { additional_arg => 'identfier' }, ] + } ]); If you don't pass any text, creates a blank entry. +See tags below for a description of additional tags. + =head2 text Returns the text of the entry. @@ -249,6 +300,24 @@ Each tag type generates two accessor functions {tag}s and in_{tag}. Current tags are context (@) and project (+). + +When creating a new object you can pass in new tags to recognize. + + my $entry = Text::Todo::Entry->new({ + text => 'do something DUE:2011-01-01', + tags => { due_date => 'DUE:' } + }); + + my @due_dates = $entry->due_dates; + +then @due_dates eq ( '2011-01-01' ); + +and you could also: + + if ($entry->in_due_date('2011-01-01')) { + # do something + } + =over