blib/lib/App/sdview/Output/HTML.pm

filter: Statementstmt 24.2% 33 / 136 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 28.5% 6 / 21 Podpod 0.0% 0 / 11 totaltotal 20.8% 39 / 187 CC 22 SCAR 30.2
CC
22
Cov
22%
output_table
72.0
_output_list
30.0
output_item
12.0
CRAP 20.4
↑ Summary
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2023 -- leonerd@leonerd.org.uk
5  
6 1 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Object::Pad 0.800;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 package App::sdview::Output::HTML 0.03;
12 class App::sdview::Output::HTML
13 :does(App::sdview::Output 0.13)
14 1 ▶ :strict(params);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use constant format => "HTML";
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use String::Tagged::HTML;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =head1 NAME
21  
22 C<App::sdview::Output::HTML> - generate HTML output from L<App::sdview>
23  
24 =head1 SYNOPSIS
25  
26 $ sdview Some/File.pod -o HTML > index.html
27  
28 =head1 DESCRIPTION
29  
30 This output module adds to L<App::sdview> the ability to output HTML; or at
31 least, a page fragment that might be used to construct a full HTML page.
32  
33 Currently, no header or CSS is generated, only the main body content by
34 relatively simple conversion - headers to C<< <h1> >>, C<< <h2> >>, etc.. and
35 inline formatting within paragraphs.
36  
37 =cut
38  
39 0 ▶method output_head1 ( $para ) { $self->_output_para( "h1", $para ); }
Subroutine output_head1: not called CC=1 SCAR=4.6
Pod undocumented
40 0 ▶method output_head2 ( $para ) { $self->_output_para( "h2", $para ); }
Subroutine output_head2: not called CC=1 SCAR=4.6
Pod undocumented
41 0 ▶method output_head3 ( $para ) { $self->_output_para( "h3", $para ); }
Subroutine output_head3: not called CC=1 SCAR=4.6
Pod undocumented
42 0 ▶method output_head4 ( $para ) { $self->_output_para( "h4", $para ); }
Subroutine output_head4: not called CC=1 SCAR=4.6
Pod undocumented
43  
44 0 ▶method output_plain ( $para, $prefix = "" ) { $self->_output_para( "p", $para, $prefix ); }
Subroutine output_plain: not called CC=2 SCAR=15.1
Pod undocumented
45  
46 0 ▶method _output_para ( $tag, $para, $prefix = "" )
Subroutine _output_para: not called CC=2 SCAR=17.9
47 {
48 0 $self->say( $prefix, $self->_convert_str( $para->text, $tag ) );
49 1 }
50  
51 0 ▶method output_verbatim ( $para, $prefix = "" )
Subroutine output_verbatim: not called CC=2 SCAR=17.9
Pod undocumented
52 {
53 0 $self->say( "$prefix<pre>\n" . $self->_convert_str( $para->text ), "</pre>" );
54 1 }
55  
56 0 ▶method output_item ( $para, $prefix = "", $tag = "li" )
Subroutine output_item: not called CC=3 SCAR=24.8
Pod undocumented
57 {
58 0 $self->_output_para( $tag, $para, $prefix );
59 1 }
60  
61 0 ▶method output_list_bullet ( $para, $prefix = "" ) { $self->_output_list( "ul", $para, $prefix ); }
Subroutine output_list_bullet: not called CC=2 SCAR=15.1
Pod undocumented
62 0 ▶method output_list_number ( $para, $prefix = "" ) { $self->_output_list( "ol", $para, $prefix ); }
Subroutine output_list_number: not called CC=2 SCAR=15.1
Pod undocumented
63 0 ▶method output_list_text ( $para, $prefix = "" ) { $self->_output_list( "dl", $para, $prefix ); }
Subroutine output_list_text: not called CC=2 SCAR=15.1
Pod undocumented
64  
65 0 ▶method _output_list ( $tag, $para, $prefix )
Subroutine _output_list: not called CC=5 SCAR=34.0
66 {
67 0 $self->say( "$prefix<$tag>" );
68  
69 0 foreach my $item ( $para->items ) {
70 0 ▶ if( $item->type eq "item" and $para->listtype eq "text" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->type eq 'item' and $para->listtype eq 'text') { } 0 0 0
Condition0%0/3
$item->type eq "item" and $para->listtype eq "text"
!ll&&!rl&&r
000
MC/DC0%0/2
$item->type eq "item" and $para->listtype eq "text"
$item->type eq "item" $para->listtype eq "text"
Truth table0%0/3
A and B
A= $item->type eq "item"B= $para->listtype eq "text"
ABresult
0X0
100
111
71 0 $self->say( "$prefix ", $self->_convert_str( $item->term, "dt" ) );
72 0 $self->output_item( $item, "$prefix ", "dd" );
73 }
74 else {
75 0 ▶ my $code = $self->can( "output_" . ( $item->type =~ s/-/_/gr ) ) or
Branch: 1 branch
BranchTrueFalseTotal
my $code = $self->can('output_' . $item->type =~ s/-/_/gru) or die 'TODO: Unhandled item type ' . $item->type 0 0 0
76 die "TODO: Unhandled item type " . $item->type;
77 0 $self->$code( $item, "$prefix " );
78 }
79 }
80  
81 0 $self->say( "$prefix</$tag>" );
82 1 }
83  
84 0 ▶method output_table ( $para, $prefix = "" )
Subroutine output_table: not called CC=8 SCAR=42.8
Pod undocumented
85 {
86 0 my @rows = $para->rows;
87  
88 0 $self->say( "$prefix<table>" );
89  
90 0 my $first = 1;
91 0 foreach my $row ( @rows ) {
92 0 ▶ my $celltag = $first ? "th" : "td";
Branch: 1 branch
BranchTrueFalseTotal
$first ? : 0 0 0
93 0 undef $first;
94  
95 0 $self->say( "$prefix <tr>" );
96  
97 0 foreach my $cell ( @$row ) {
98 0 ▶ my $align =
Branch: 2 branches
BranchTrueFalseTotal
$cell->align eq 'centre' ? : 0 0 0
$cell->align eq 'right' ? : 0 0 0
99 $cell->align eq "centre" ? "center" :
100 $cell->align eq "right" ? "right" :
101 undef;
102  
103 0 ▶ my $openelem = defined $align ? "<$celltag style=\"text-align: $align;\">" :
Branch: 1 branch
BranchTrueFalseTotal
defined $align ? : 0 0 0
104 "<$celltag>";
105 0 $self->say( "$prefix $openelem", $self->_convert_str( $cell->text ), "</$celltag>" );
106 }
107 0 $self->say( "$prefix </tr>" );
108 }
109  
110 0 $self->say( "$prefix</table>" );
111 1 }
112  
113 0 ▶method _convert_str ( $s, $tag = undef )
Subroutine _convert_str: not called CC=2 SCAR=17.9
114 {
115 return String::Tagged::HTML->clone(
116 $s,
117 only_tags => [qw( bold italic monospace underline strikethrough link )],
118 convert_tags => {
119 bold => "strong",
120 italic => "em",
121 monospace => "tt",
122 underline => "u",
123 strikethrough => "s",
124 # TODO: F
125 0 ▶ link => sub ($t, $v) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
126 0 ▶ ( defined $v->{uri} ) ? ( a => { href => $v->{uri} } ) : ()
Branch: 1 branch
BranchTrueFalseTotal
defined $v->{'uri'} ? : 0 0 0
127 },
128 },
129 0 )->as_html( $tag );
130 1 }
131  
132 =head1 TODO
133  
134 =over 4
135  
136 =item *
137  
138 Customisable page header, CSS, general page template?
139  
140 =back
141  
142 =cut
143  
144 =head1 AUTHOR
145  
146 Paul Evans <leonerd@leonerd.org.uk>
147  
148 =cut
149  
150 1 0x55AA;
↑ Summary