File Coverage

blib/lib/App/sdview/Output/HTML.pm
Criterion Covered Total %
statement 114 124 91.9
branch 12 14 85.7
condition 3 3 100.0
subroutine 19 21 90.4
pod 0 11 0.0
total 148 173 85.5


line stmt bran cond sub pod time code
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 2     2   722766 use v5.26;
  2         6  
7 2     2   13 use warnings;
  2         2  
  2         107  
8              
9 2     2   603 use Object::Pad 0.800;
  2         7546  
  2         97  
10              
11             package App::sdview::Output::HTML 0.03;
12             class App::sdview::Output::HTML
13             :does(App::sdview::Output 0.13)
14 2     2   1161 :strict(params);
  2         2520  
  2         222  
15              
16 2     2   144 use constant format => "HTML";
  2         4  
  2         116  
17              
18 2     2   990 use String::Tagged::HTML;
  2         18237  
  2         4610  
19              
20             =head1 NAME
21              
22             C - generate HTML output from L
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 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<<

>>, C<<

>>, etc.. and

35             inline formatting within paragraphs.
36              
37             =cut
38              
39 2     2 0 124 method output_head1 ( $para ) { $self->_output_para( "h1", $para ); }
  2         6  
  2         4  
  2         4  
  2         20  
40 1     1 0 348 method output_head2 ( $para ) { $self->_output_para( "h2", $para ); }
  1         2  
  1         2  
  1         1  
  1         2  
41 0     0 0 0 method output_head3 ( $para ) { $self->_output_para( "h3", $para ); }
  0         0  
  0         0  
  0         0  
  0         0  
42 0     0 0 0 method output_head4 ( $para ) { $self->_output_para( "h4", $para ); }
  0         0  
  0         0  
  0         0  
  0         0  
43              
44 8     8 0 3205 method output_plain ( $para, $prefix = "" ) { $self->_output_para( "p", $para, $prefix ); }
  8         15  
  8         12  
  8         12  
  8         10  
  8         22  
45              
46 22     22   28 method _output_para ( $tag, $para, $prefix = "" )
  22         39  
  22         33  
  22         24  
  22         30  
  22         26  
47             {
48 22         70 $self->say( $prefix, $self->_convert_str( $para->text, $tag ) );
49             }
50              
51 1     1 0 308 method output_verbatim ( $para, $prefix = "" )
  1         2  
  1         3  
  1         2  
  1         1  
52             {
53 1         4 $self->say( "$prefix
\n" . $self->_convert_str( $para->text ), "
" );
54             }
55              
56 11     11 0 15 method output_item ( $para, $prefix = "", $tag = "li" )
  11         16  
  11         13  
  11         16  
  11         15  
  11         10  
57             {
58 11         20 $self->_output_para( $tag, $para, $prefix );
59             }
60              
61 3     3 0 62 method output_list_bullet ( $para, $prefix = "" ) { $self->_output_list( "ul", $para, $prefix ); }
  3         7  
  3         5  
  3         5  
  3         4  
  3         12  
62 1     1 0 29 method output_list_number ( $para, $prefix = "" ) { $self->_output_list( "ol", $para, $prefix ); }
  1         2  
  1         3  
  1         2  
  1         2  
  1         3  
63 1     1 0 28 method output_list_text ( $para, $prefix = "" ) { $self->_output_list( "dl", $para, $prefix ); }
  1         2  
  1         1  
  1         2  
  1         2  
  1         5  
64              
65 5     5   8 method _output_list ( $tag, $para, $prefix )
  5         10  
  5         9  
  5         24  
  5         9  
  5         6  
66             {
67 5         23 $self->say( "$prefix<$tag>" );
68              
69 5         56 foreach my $item ( $para->items ) {
70 13 100 100     2034 if( $item->type eq "item" and $para->listtype eq "text" ) {
71 3         42 $self->say( "$prefix ", $self->_convert_str( $item->term, "dt" ) );
72 3         759 $self->output_item( $item, "$prefix ", "dd" );
73             }
74             else {
75 10 50       106 my $code = $self->can( "output_" . ( $item->type =~ s/-/_/gr ) ) or
76             die "TODO: Unhandled item type " . $item->type;
77 10         87 $self->$code( $item, "$prefix " );
78             }
79             }
80              
81 5         1037 $self->say( "$prefix" );
82             }
83              
84 2     2 0 50 method output_table ( $para, $prefix = "" )
  2         5  
  2         3  
  2         3  
  2         2  
85             {
86 2         6 my @rows = $para->rows;
87              
88 2         13 $self->say( "$prefix" ); " ); " );
89              
90 2         13 my $first = 1;
91 2         5 foreach my $row ( @rows ) {
92 4 100       26 my $celltag = $first ? "th" : "td";
93 4         7 undef $first;
94              
95 4         12 $self->say( "$prefix
96              
97 4         26 foreach my $cell ( @$row ) {
98 10 100       1170 my $align =
    100          
99             $cell->align eq "centre" ? "center" :
100             $cell->align eq "right" ? "right" :
101             undef;
102              
103 10 100       86 my $openelem = defined $align ? "<$celltag style=\"text-align: $align;\">" :
104             "<$celltag>";
105 10         44 $self->say( "$prefix $openelem", $self->_convert_str( $cell->text ), "" );
106             }
107 4         721 $self->say( "$prefix
108             }
109              
110 2         67 $self->say( "$prefix
" );
111             }
112              
113 36     36   196 method _convert_str ( $s, $tag = undef )
  36         72  
  36         47  
  36         45  
  36         40  
114             {
115 2         3 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 2     2   166 link => sub ($t, $v) {
  2         3  
  2         4  
126 2 50       9 ( defined $v->{uri} ) ? ( a => { href => $v->{uri} } ) : ()
127             },
128             },
129 36         370 )->as_html( $tag );
130             }
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
147              
148             =cut
149              
150             0x55AA;