Tools/CLI/ToHTML.pm untested

filter: Statementstmt 0.0% 0 / 85 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 154 CC 13 SCAR 27.6
CC
13
Cov
0%
tree_to_text
42.0
doctype
20.0
output
12.0
CRAP 15.8
1 #!/usr/bin/perl
2  
3 package Mail::Summary::Tools::CLI::ToHTML;
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::HTML;
14  
15 #'END_USE
16  
17 use constant options => (
18 [ 'verbose|v!' => "Output verbose information" ],
19 [ 'input|i=s' => 'The summary YAML file to emit' ],
20 [ 'output|o=s' => 'A file to output to (defaults to STDOUT)' ],
21 [ 'archive|a=s' => 'On-line archive to use', { default => "google" } ],
22 [ 'compact|c!' => 'Emit compact HTML (no <div> tags, etc)' ],
23 [ 'body_only|b!' => 'Emit body fragment only (as opposed to a full, valid document)' ],
24 [ 'xml|x!' => "use HTML::Element's as_XML method instead of the indented as_HTML" ],
25 [ 'xhtml!' => "hackishly use startag_XML in as_HTML mode to emit valid xhtml (defaults to true)", { default => 1 }],
26 [ 'h1=s@' => 'Tags to use instead of h1 (e.g. --h1 p,b emits <p><b>heading</b></p>)', { default => ["h1"] } ],
27 [ 'h2=s@' => 'see h1', { default => ["h2"] } ],
28 [ 'h3=s@' => 'see h1', { default => ["h3"] } ],
29 [ 'lang=s' => 'sets the lang or xml:lang attribute of the <html> element', { default => "en" } ],
30 );
31  
32 sub output {
33 my $self = shift;
34 my $opt = $self->{opt};
35  
36 if ( !$opt->{output} or $opt->{output} eq '-' ) {
37 binmode STDOUT, ":utf8";
38 return \*STDOUT;
39 } elsif ( my $file = $opt->{output} ) {
40 open my $fh, ">:utf8", $file or die "Couldn't open output (open($file): $!)\n";
41 return $fh;
42 }
43 }
44  
45 sub validate {
46 my ( $self, $opt, $args ) = @_;
47 @$args and $opt->{$_} ||= shift @$args for qw/input output/;
48  
49 unless ( $opt->{input} ) {
50 $self->usage_error("Please specify an input summary YAML file.");
51 }
52
53 if ( @$args ) {
54 $self->usage_error("Unknown arguments: @$args.");
55 }
56  
57 foreach my $tag ( qw/h1 h2 h3/ ) {
58 @{ $opt->{$tag} } = map { split ',' } @{ $opt->{$tag} };
59 }
60  
61 $self->{opt} = $opt;
62 }
63  
64 sub run {
65 my ( $self, $opt, $args ) = @_;
66  
67 my $summary = Mail::Summary::Tools::Summary->load(
68 $opt->{input},
69 thread => {
70 default_archive => $opt->{archive} || "google",
71 archive_link_params => { cache => $self->app->context->cache },
72 },
73 );
74  
75 my $o = Mail::Summary::Tools::Output::HTML->new(
76 summary => $summary,
77 body_only => $opt->{body_only},
78 strip_divs => $opt->{compact},
79 lang => $opt->{lang},
80 map { $_ . "_tag" => $opt->{$_} } qw/h1 h2 h3/,
81 );
82  
83 my @tree = $o->process;
84  
85 $self->print_tree($self->doctype, @tree);
86 }
87  
88 sub pi_xhtml {
89 HTML::Element->new('~literal' => text => '<?xml version="1.0" encoding="UTF-8"?>');
90 }
91  
92 sub pi_xml {
93 HTML::Element->new('~pi' => text => 'xml version="1.0" encoding="UTF-8"');
94 }
95  
96 sub doctype_xhtml_11 {
97 HTML::Element->new('~declaration' => text =>
98 'DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"' . "\n" .
99 ' "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"'
100 );
101 }
102  
103 sub doctype_html_401 {
104 HTML::Element->new('~declaration' => text =>
105 'DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"' . "\n" .
106 ' "http://www.w3.org/TR/html4/loose.dtd"'
107 );
108 }
109  
110 sub doctype {
111 my $self = shift;
112 return if $self->{opt}{body_only};
113  
114 if ( $self->{opt}{xml} ) {
115 return ( $self->pi_xml, $self->doctype_xhtml_11 );
116 } elsif ( $self->{opt}{xhtml} ) {
117 return ( $self->pi_xhtml, $self->doctype_xhtml_11 );
118 } else {
119 return ( $self->doctype_html_401 );
120 }
121 }
122  
123 sub print_tree {
124 my ( $self, @tree ) = @_;
125 my $out = $self->output;
126 print $out $self->tree_to_text(@tree);
127 }
128  
129 sub tree_to_text {
130 my ( $self, @tree ) = @_;
131  
132 if ( $self->{opt}{xml} ) {
133 return join("", map { $_->as_XML } @tree);
134 } else {
135 # emit valid XHTML
136 no warnings 'redefine';
137 local *HTML::Element::starttag = sub {
138 my ( $elem, $p ) = @_;
139 my $empty = $elem->_empty_element_map->{$elem->tag};
140 $elem->starttag_XML($p, $empty ? 1 : () );
141 } if $self->{opt}{xhtml};
142  
143 # FIXME should this be moved down to Output::HTML?
144 unless ( $self->{opt}{xhtml} ) {
145 foreach my $subtree ( @tree ) {
146 if ( $subtree->tag eq "html" ) {
147 $subtree->attr( xmlns => undef );
148 $subtree->attr( lang => $subtree->attr( 'xml:lang' => undef ) );
149 }
150 }
151 }
152  
153 # no optional end tags, two space indent, default entity escaping
154 return join("", map { $_->as_HTML(undef, ' ', {}) } @tree);
155 }
156 }
157  
158 __PACKAGE__;
159  
160 __END__