File Coverage

blib/lib/App/sdview/Output/Markdown.pm
Criterion Covered Total %
statement 98 108 90.7
branch 9 10 90.0
condition n/a
subroutine 17 19 89.4
pod 0 9 0.0
total 124 146 84.9


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, 2021-2023 -- leonerd@leonerd.org.uk
5              
6 2     2   1043 use v5.26;
  2         7  
7 2     2   11 use warnings;
  2         6  
  2         70  
8              
9 2     2   11 use Object::Pad 0.800;
  2         25  
  2         90  
10              
11             package App::sdview::Output::Markdown 0.11;
12             class App::sdview::Output::Markdown
13             :does(App::sdview::Output)
14 1     1   616 :strict(params);
  1         3  
  1         48  
15              
16 2     2   550 use String::Tagged::Markdown 0.02;
  2         49  
  2         70  
17              
18 2     2   10 use constant format => "Markdown";
  2         6  
  2         4596  
19              
20             =head1 NAME
21              
22             C - generate Markdown output from L
23              
24             =head1 SYNOPSIS
25              
26             $ sdview README.pod -o Markdown > README.md
27              
28             =head1 DESCRIPTION
29              
30             This output module adds to L the ability to output text in
31             Markdown formatting. Given a Markdown file as input, the output should be
32             relatively similar, up to minor details like whitespacing. Given input in some
33             other format, it will do a reasonable job attempting to represent most of the
34             structure and formatting.
35              
36             =cut
37              
38 2     2 0 6 method output_head1 ( $para ) { $self->_output_head( "#", $para ); }
  2         4  
  2         3  
  2         3  
  2         8  
39 1     1 0 8 method output_head2 ( $para ) { $self->_output_head( "##", $para ); }
  1         3  
  1         2  
  1         1  
  1         4  
40 0     0 0 0 method output_head3 ( $para ) { $self->_output_head( "###", $para ); }
  0         0  
  0         0  
  0         0  
  0         0  
41 0     0 0 0 method output_head4 ( $para ) { $self->_output_head( "####", $para ); }
  0         0  
  0         0  
  0         0  
  0         0  
42              
43 3         5 method _output_head ( $leader, $para )
  3         5  
  3         5  
  3         3  
44 3     3   6 {
45 3         10 $self->maybe_blank;
46              
47 3         15 $self->say( $leader, " ", $self->_convert_str( $para->text ) );
48             }
49              
50 5         9 method output_plain ( $para )
  5         8  
  5         7  
51 5     5 0 10 {
52 5         15 $self->maybe_blank;
53              
54 5         17 $self->say( $self->_convert_str( $para->text ) );
55             }
56              
57 1         2 method output_verbatim ( $para )
  1         2  
  1         2  
58 1     1 0 6 {
59 1         4 $self->maybe_blank;
60              
61             # TODO: Offer a choice of ``` vs indented
62              
63 1         10 $self->say( "```" );
64 1         4 $self->say( $para->text );
65 1         8 $self->say( "```" );
66             }
67              
68 1     1 0 3 method output_list_bullet ( $para ) { $self->_output_list( $para ); }
  1         3  
  1         2  
  1         2  
  1         4  
69 1     1 0 5 method output_list_number ( $para ) { $self->_output_list( $para ); }
  1         2  
  1         3  
  1         2  
  1         5  
70              
71 2         3 method _output_list ( $para )
  2         4  
  2         4  
72 2     2   4 {
73 2         7 $self->maybe_blank;
74              
75 2         7 my $n = $para->initial;
76 2         21 foreach my $item ( $para->items ) {
77 6         10 my $leader;
78              
79 6 100       16 if( $para->listtype eq "bullet" ) {
    50          
80 3         7 $leader = "*";
81             }
82             elsif( $para->listtype eq "number" ) {
83 3         18 $leader = sprintf "%d.", $n++;
84             }
85              
86 6         22 $self->say( $leader, " ", $self->_convert_str( $item->text ) );
87             }
88             }
89              
90 2         3 method output_table ( $para )
  2         5  
  2         3  
91 2     2 0 6 {
92 2         9 $self->maybe_blank;
93              
94 2         9 my @rows = $para->rows;
95              
96 2         5 my $first = 1;
97 2         4 foreach my $row ( @rows ) {
98 3         7 my @cells = @$row;
99 3         8 $self->say( join "|", "", ( map { " " . $self->_convert_str( $_->text ) . " " } @cells ), "" );
  7         759  
100              
101 3 100       13 next unless $first;
102              
103             my @aligns = map {
104 2         8 my $n = length $_->text;
  5         13  
105 5 100       35 $_->align eq "centre" ? ":".("-"x($n-2)).":" :
    100          
106             $_->align eq "right" ? ("-"x($n-1)).":" :
107             ("-"x $n );
108             } @cells;
109 2         5 $self->say( join "|", "", ( map { " $_ " } @aligns ), "" );
  5         17  
110 2         8 undef $first;
111             }
112             }
113              
114 21         32 method _convert_str ( $s )
  21         34  
  21         25  
115 21     21   45 {
116 1         2 return String::Tagged::Markdown->clone( $s,
117             only_tags => [qw( C B I F L )],
118             convert_tags => {
119             C => "fixed",
120             B => "bold",
121             I => "italic",
122             F => "italic", # There isn't a "filename" format in Markdown
123 1     1   2 L => sub ($t, $v) { return link => $v->{target} },
  1         5  
  1         74  
  1         2  
124             }
125 21         192 )->build_markdown;
126             }
127              
128             =head1 AUTHOR
129              
130             Paul Evans
131              
132             =cut
133              
134             0x55AA;