CheckPad/TodoParser.pm untested

filter: Statementstmt 0.0% 0 / 60 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 45 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 168 CC 12 SCAR 29.6
CC
12
Cov
0%
start
42.0
text
30.0
end
6.0
CRAP 19.2
1 package WWW::CheckPad::TodoParser;
2  
3 use strict;
4 use warnings;
5 use WWW::CheckPad::Parser;
6 use HTML::Parser;
7 use base qw(WWW::CheckPad::Parser);
8  
9  
10 ############################################################
11 # Usage: Called by WWW::CheckPad::Parser->convert_to_item().
12 #
13 # Will return array of following hash-structure.
14 # {
15 # id=>?, title=>?, is_finished=>?, finished_time=>?
16 # }
17 ############################################################
18 sub _parse {
19 my ($self, $content) = @_;
20  
21 $self->param('todos', []);
22 $self->parse($content);
23 $self->eof;
24  
25 my $todos = $self->param('todos');
26 return $todos;
27 }
28  
29  
30 sub start {
31 my ($self, $tagname, $attr, $attr_array,$source) = @_;
32  
33 # Take out the unfinished todo item from input tag.
34 if ($tagname eq 'input' and
35 defined $attr->{type} and $attr->{type} eq 'text' and
36 defined $attr->{id} and
37 defined $attr->{value} and
38 $attr->{id} =~ /^ms_([0-9]*)_edit/) {
39 $self->_add_todo($1, $attr->{value}, 0);
40 }
41 # Mark that we are in fished todo item area.
42 elsif ($tagname eq 'div' and
43 defined $attr->{id} and $attr->{id} =~ /ms_done_([0-9]+)/) {
44 $self->param('current_finished_id', $1);
45 }
46
47 # Finally we are in unfinished todo item.
48 elsif ($tagname eq 'div' and
49 defined $attr->{id} and
50 defined $self->param('current_finished_id') and
51 $attr->{id} eq $self->param('current_finished_id')) {
52 $self->param('in_finished_div', 1);
53 $self->_add_todo($self->param('current_finished_id'), "", 1);
54 }
55 # We are in the finished-date area of finished checkitem.
56 elsif ($self->param('in_finished_div') and
57 $tagname eq 'span' and
58 $attr->{class} eq 's10') {
59 #print "in the s10 area\n";
60 $self->param('in_finished_date_span', 1);
61 }
62
63 if ($self->param('in_finished_div')) {
64 #printf "in finished area: [%s] class=%s\n", $tagname, 'dummy';
65 }
66 }
67  
68  
69 sub text {
70 my ($self, $text) = @_;
71 my %time_calc_map = (
72 WWW::CheckPad->_jconvert('“ú‘O', 'euc-jp') => (60*60*24),
73 WWW::CheckPad->_jconvert('ŽžŠÔ‘O', 'euc-jp') => (60*60),
74 WWW::CheckPad->_jconvert('•ª‘O', 'euc-jp') => (60),
75 );
76  
77 if (defined $self->param('in_finished_div') and
78 $self->param('current_finished_id') > 0) {
79 ## Remove space and line-breaks.
80 $text =~ s/^[ \n]+//;
81 $text =~ s/[ \n]+$//;
82  
83 return if ($text eq '');
84 my $todo = $self->_get_last_todo();
85
86 # We are in the in_finished_date_span.
87 if ($self->param('in_finished_date_span') and
88 $text =~ /\(([0-9]+)(.*)\)/) {
89  
90 my $time_diff = $1 * $time_calc_map{$2};
91  
92 $todo->{finished_time} = time - $time_diff;
93 return;
94 }
95 # This is delete button ([x]).
96 elsif ($text =~ /\[x\]/) {
97 return;
98 }
99  
100 $todo->{title} .= $text;
101
102 }
103  
104 }
105  
106  
107 sub end {
108 my ($self, $tagname) = @_;
109  
110 if ($tagname eq 'span') {
111 $self->param('in_finished_date_span', 0);
112 }
113 }
114  
115  
116 sub comment {
117 my ($self, $token) = @_;
118 #printf "COMMENT: %s\n", $token;
119 my $id = $self->param('current_finished_id');
120 if (defined $id and
121 $token =~ /end of ms_($id)/) {
122 #printf "Found End Mark\n";
123 $self->param('current_finished_id', 0);
124 $self->param('in_finished_div', 0);
125 }
126 }
127  
128  
129 sub _add_todo {
130 my ($self, $id, $name, $finished) = @_;
131 my $todos = $self->param('todos');
132 my $todo = {
133 id => $id,
134 title => $name,
135 is_finished => $finished,
136 };
137 push @{$todos}, $todo;
138 return $todo;
139 }
140  
141  
142 sub _get_last_todo {
143 my ($self) = @_;
144 my $todos = $self->param('todos');
145 return $todos->[$#{$todos}];
146 }
147  
148  
149  
150  
151 1;