File Coverage

blib/lib/App/sdview/Parser/Markdown.pm
Criterion Covered Total %
statement 113 125 90.4
branch 36 40 90.0
condition 20 27 74.0
subroutine 13 15 86.6
pod 0 4 0.0
total 182 211 86.2


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-2022 -- leonerd@leonerd.org.uk
5              
6 4     4   496458 use v5.26;
  4         28  
7 4     4   23 use warnings;
  4         8  
  4         134  
8              
9 4     4   1360 use Object::Pad 0.800;
  4         19323  
  4         261  
10              
11             package App::sdview::Parser::Markdown 0.11;
12             class App::sdview::Parser::Markdown
13             :does(App::sdview::Parser)
14 2     2   1243 :strict(params);
  2         9  
  2         104  
15              
16 4     4   3114 use File::Slurper 'read_text';
  4         13294  
  4         281  
17              
18 4     4   2046 use String::Tagged::Markdown;
  4         11628  
  4         192  
19              
20 4     4   30 use constant format => "Markdown";
  4         9  
  4         294  
21 4     4   34 use constant sort_order => 20;
  4         8  
  4         12524  
22              
23 0     0 0 0 sub find_file ( $class, $name ) { return undef }
  0         0  
  0         0  
  0         0  
  0         0  
24              
25 1         3 sub can_parse_file ( $class, $file )
26 1     1 0 7091 {
  1         2  
  1         2  
27 1         11 return $file =~ m/\.(?:md|markdown)$/;
28             }
29              
30 0         0 method parse_file ( $fh )
  0         0  
  0         0  
31 0     0 0 0 {
32 0         0 return $self->parse_string( read_text $fh );
33             }
34              
35             field @_paragraphs;
36              
37             sub _split_table_row ( $str )
38 16     16   128 {
  16         28  
  16         24  
39 16 50       70 $str =~ m/^\s*\|/ or return undef;
40 16 50       67 $str =~ m/\|\s*$/ or return undef;
41              
42 16         80 my @cols = split m/\|/, $str, -1;
43 16         29 shift @cols; pop @cols;
  16         28  
44              
45 16         130 s/^\s+//, s/\s+$// for @cols;
46              
47 16         83 return \@cols;
48             }
49              
50 15         25 method parse_string ( $str )
  15         27  
  15         21  
51 15     15 0 40 {
52 15         32 my $in_verb;
53              
54             my @lines;
55              
56 15         70 foreach ( split( m/\n/, $str ), "" ) {
57 97         155 my $line = $_; # So we have a copy, because foreach my ... will alias the readonly ""
58              
59 97 100       187 if( $in_verb ) {
60 4         21 my $para = $_paragraphs[-1];
61              
62 4 100       15 if( $line =~ m/^\`\`\`/ ) {
63 1         3 undef $in_verb;
64             next
65 1         3 }
66              
67 3 100       11 length $para->text and
68             $para->text->append( "\n" );
69              
70 3         43 $para->text->append( $line );
71 3         53 next;
72             }
73              
74 93 100       200 if( $line =~ s/^\`\`\`// ) {
75             # Ignore the type specifier for now
76 1         6 push @_paragraphs, App::sdview::Para::Verbatim->new(
77             text => String::Tagged->new,
78             );
79 1         12 $in_verb++;
80 1         2 next;
81             }
82              
83 92 100       187 if( length $line ) {
84 56         85 push @lines, $line;
85 56         100 next;
86             }
87              
88 36         81 while( @lines ) {
89 42 50 100     478 if( $lines[0] =~ m/^