Tools/CLI/ToText.pm untested

filter: Statementstmt 0.0% 0 / 136 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 41 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 279 CC 19 SCAR 23.3
CC
19
Cov
0%
validate
30.0
expand_uri
20.0
really_shorten
20.0
CRAP 10.2
1 #!/usr/bin/perl
2  
3 package Mail::Summary::Tools::CLI::ToText;
4 use base qw/Mail::Summary::Tools::CLI::Command/;
5  
6 use strict;
7 use warnings;
8  
9 use Class::Autouse (<<'#\'END_USE' =~ m!(\w+::[\w:]+)!g);
10 #\
11  
12 use Mail::Summary::Tools::Summary;
13 use Mail::Summary::Tools::Output::TT;
14  
15 #'END_USE
16  
17 use Text::Wrap ();
18  
19 use constant options => (
20 [ 'verbose|v!' => "Output progress information" ],
21 [ 'input|i=s' => 'Summary file to read' ],
22 [ 'output|o=s' => 'The file to output (defaults to STDOUT)' ],
23 [ 'shortening|s!' => "Enable URI shortening (defaults to true)", { default => 1 } ],
24 [ 'shorten=s' => 'shortening service (defaults to "Metamark" -- http://xrl.us/)' ], #{ implies => "shortening", default => "Metamark" } ],
25 [ 'archive|a=s' => 'The on-line archive to use (defaults to "google")', { default => "google" }],
26 [ 'columns|c=i' => 'The column width to wrap to (defaults to 75)', { default => 75 } ],
27 [ 'force_wrap|w!' => 'Force wrapping of overflowed text (like long URIs)' ], # whether or not to force wrapping of overflowing text
28 [ 'template=s' => "Override the template toolkit file used to format the text" ],
29 );
30  
31 sub usage_desc {
32 "%c totext %o [summary.yaml]"
33 }
34  
35 sub wrap {
36 my ( $self, $text, $columns, $first_indent, $rest_indent ) = @_;
37  
38 $columns ||= $self->{opt}{columns} || 80;
39 $first_indent ||= ' ';
40 $rest_indent ||= ' ';
41  
42 no warnings 'once';
43 local $Text::Wrap::huge = $self->_wrap_huge;
44 local $Text::Wrap::columns = $columns;
45  
46 $text =~ s/\\(\S)/$1/g; # unquotemeta
47  
48 Text::Wrap::fill( $first_indent, $rest_indent, $self->process_body($text) );
49 }
50  
51 sub process_body {
52 my ( $self, $text ) = @_;
53  
54 $text =~ s/<(\w+:\S+?)>/$self->expand_uri($1)/ge;
55 $text =~ s/\[(.*?)\]\((\w+:\S+?)\)/$self->expand_uri($2, $1)/sge;
56  
57 return $text;
58 }
59  
60 sub bullet {
61 my ( $self, $text, $columns ) = @_;
62 $self->wrap( $text, $columns, ' * ', ' ' );
63 }
64  
65 sub subject {
66 my ( $self, $text, $columns ) = @_;
67 $self->wrap( $text, $columns, ' ', ' ' );
68 }
69  
70 sub heading {
71 my ( $self, $text, $columns ) = @_;
72 $self->wrap( $text, $columns, ' ', ' ' );
73 }
74  
75 sub _wrap_huge {
76 my $self = shift;
77 return $self->{opt}{force_wrap} ? 'wrap' : 'overflow'; # default to not breaking URIs
78 }
79  
80 sub shorten {
81 my ( $self, $uri ) = @_;
82  
83 if ( $self->should_shorten($uri) ) {
84 $self->really_shorten( $uri );
85 } else {
86 return $uri;
87 }
88 }
89  
90 sub rt_uri {
91 my ( $self, $rt, $id ) = @_;
92  
93 if ( $rt eq "perl" ) {
94 return $self->shorten("http://rt.perl.org/rt3/Ticket/Display.html?id=$id");
95 } else {
96 die "unknown rt installation: $rt";
97 }
98 }
99  
100 sub link_to_message {
101 my ( $self, $message_id, $text ) = @_;
102  
103 my $thread = $self->{__summary}->get_thread_by_id( $message_id )
104 || die "The link to <$message_id> could not be resolved, because no thread with that message ID is in the summary data";
105  
106 my $uri = $self->shorten($thread->archive_link->thread_uri);
107
108 $text ||= $thread->subject;
109  
110 "$text <$uri>";
111 }
112  
113 sub expand_uri {
114 my ( $self, $uri_string, $text ) = @_;
115  
116 my $uri = URI->new($uri_string);
117  
118 if ( $uri->scheme eq 'rt' ) {
119 my ( $rt, $id ) = ( $uri->authority, substr($uri->path, 1) );
120 my $rt_uri = $self->rt_uri($rt, $id);
121 $text ||= "[$rt #$id]";
122 return "$text <$rt_uri>";
123 } elsif ( $uri->scheme eq 'msgid' ) {
124 return $self->link_to_message( join("", grep { defined } $uri->authority, $uri->path), $text );
125 } else {
126 my $short_uri = $self->shorten($uri) || $uri;
127 return $text ? "$text <$short_uri>" : "<$short_uri>";
128 }
129 }
130  
131 sub really_shorten {
132 my ( $self, $uri ) = @_;
133 my $service = $self->{opt}{shorten};
134  
135 my $cache = $self->app->context->cache;
136  
137 my $cache_key = join(":", "shorten", $service, $uri);
138  
139 if ( my $short = $cache->get($cache_key) ) {
140 return $short;
141 } else {
142 $self->diag( "Shortening URI (cache miss): $uri" );
143 my $mod = "WWW::Shorten::$service";
144 unless ( $mod->can("makeashorterlink") ) {
145 my $file = join("/", split("::", $mod ) ) . ".pm";
146 require $file;
147 }
148  
149 no strict 'refs';
150 my $short = &{"${mod}::makeashorterlink"}( $uri );
151 $cache->set( $cache_key, $short ) if $short;
152 return $short || "$uri";
153 }
154 }
155  
156 sub shortening_enabled {
157 my ( $self, $uri ) = @_;
158 if ( $self->{opt}{shorten} ) {
159 return 1;
160 } else {
161 return;
162 }
163 }
164  
165 sub should_shorten {
166 my ( $self, $uri ) = @_;
167 return unless $self->shortening_enabled;
168  
169 length($uri) > 40 || $uri =~ /gmane|groups\.google/
170 }
171  
172 sub template_input {
173 my $self = shift;
174  
175 if ( my $file = $self->{opt}{template} ) {
176 open my $fh, "<", $file or die "Couldn't open template (open($file): $!)\n";
177 binmode $fh, ":utf8";
178 return $fh;
179 } else {
180 binmode DATA, ":utf8";
181 return \*DATA;
182 }
183 }
184  
185 sub template_output {
186 my $self = shift;
187 my $opt = $self->{opt};
188
189 if ( !$opt->{output} or $opt->{output} eq '-' ) {
190 binmode STDOUT, ":utf8";
191 return \*STDOUT;
192 } elsif ( my $file = $opt->{output} ) {
193 open my $fh, ">", $file or die "Couldn't open output (open($file): $!)\n";
194 binmode $fh, ":utf8";
195 return $fh;
196 }
197 }
198  
199 sub validate {
200 my ( $self, $opt, $args ) = @_;
201 @$args and $opt->{$_} ||= shift @$args for qw/input output/;
202  
203 unless ( $opt->{input} ) {
204 $self->usage_error("Please specify an input summary YAML file.");
205 }
206
207 if ( @$args ) {
208 $self->usage_error("Unknown arguments: @$args.");
209 }
210  
211 # FIXME implies is broken
212 $opt->{shortening} = 1 if exists $opt->{shorten};
213 $opt->{shorten} ||= 'Metamark' if $opt->{shortening};
214  
215 $self->{opt} = $opt;
216 }
217  
218 sub run {
219 my ( $self, $opt, $args ) = @_;
220  
221 my $summary = Mail::Summary::Tools::Summary->load(
222 $opt->{input},
223 thread => {
224 default_archive => $opt->{archive} || "google",
225 archive_link_params => { cache => $self->app->context->cache },
226 },
227 );
228  
229 my $o = Mail::Summary::Tools::Output::TT->new(
230 template_input => $self->template_input,
231 template_output => $self->template_output,
232 );
233  
234 $o->process(
235 $self->{__summary} = $summary, # FIXME Output::Plain
236 {
237 shorten => sub { $self->shorten(shift) },
238 wrap => sub { $self->wrap(shift) },
239 bullet => sub { $self->bullet(shift) },
240 heading => sub { $self->heading(shift) },
241 subject => sub { $self->subject(shift) },
242 },
243 );
244 }
245  
246 __PACKAGE__;
247  
248 =pod
249  
250 =head1 NAME
251  
252 Mail::Summary::Tools::CLI::ToText - Emit a formatted plain text summary
253  
254 =head1 SYNOPSIS
255  
256 # see command line usage
257  
258 =head1 DESCRIPTION
259  
260 =cut
261  
262 __DATA__