lib/Markdown/Parser/ListItem.pm

filter: Statementstmt 27.6% 26 / 94 Branchbran 0.0% 0 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 45.0% 9 / 20 Podpod 100% 7 / 7 totaltotal 27.1% 42 / 155 CC 18 SCAR 30.7
CC
18
Cov
20%
as_string
56.0
as_markdown
30.0
type
30.0
CRAP 21.5
« lib/Markdown/Parser/TableCell.pm ↑ Summary blib/lib/Markdown/Parser/Image.pm »
1 ##----------------------------------------------------------------------------
2 ## Markdown Parser Only - ~/lib/Markdown/Parser/ListItem.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2021 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/08/23
7 ## Modified 2022/09/19
8 ## All rights reserved
9 ##
10 ## This program is free software; you can redistribute it and/or modify it
11 ## under the same terms as Perl itself.
12 ##----------------------------------------------------------------------------
13 package Markdown::Parser::ListItem;
14 BEGIN
15 {
16 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶ use parent qw( Markdown::Parser::Element );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶ use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶ use Devel::Confess;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶ use constant TAB_SPACES => ' ';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶ our $VERSION = 'v0.2.0';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 };
24  
25 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 sub init
29 {
30 0 ▶ my $self = shift( @_ );
Subroutine init: not called CC=1 SCAR=6.9
Pod documented
31 0 $self->{indent} = '';
32 0 $self->{order} = '';
33 0 $self->{type} = '';
34 0 $self->{tag_name} = 'list_item';
35 0 return( $self->SUPER::init( @_ ) );
36 }
37  
38 sub as_markdown
39 {
40 0 ▶ my $self = shift( @_ );
Subroutine as_markdown: not called CC=5 SCAR=34.0
Pod documented
41 0 ▶ my $str = $self->children->map(sub{ $_->as_markdown })->join( '' );
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 0 my $order_list = $self->order;
43 # Set some default value since key information has not been set yet
44 0 ▶ unless( $self->type->length )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->type->length) 0 0 0
45 {
46 0 ▶ if( $order_list )
Branch: 1 branch
BranchTrueFalseTotal
if ($order_list) { } 0 0 0
47 {
48 0 $self->type( '1.' );
49 }
50 else
51 {
52 0 $self->type( '*' );
53 }
54 }
55 0 my $marker = $self->type;
56 0 ▶ $marker .= '.' if( $marker =~ /^\d+$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $marker =~ /^\d+$/ 0 0 0
57 0 my $indent = ' ' x $self->indent;
58 0 my $lines = $self->new_array( [split( /\n/, $str )] );
59 0 substr( $lines->[0], 0, 0 ) = "${marker} ";
60 # If the list has indentation, indent each lines of the string in this list item
61 0 ▶ if( $self->indent > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->indent > 0) 0 0 0
62 {
63 $lines->for(sub
64 {
65 0 ▶ my( $i, $val ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 substr( $lines->[$i], 0, 0 ) = $indent;
67 0 });
68 }
69 0 return( $lines->join( "\n" )->scalar );
70 }
71  
72 sub as_pod
73 {
74 0 ▶ my $self = shift( @_ );
Subroutine as_pod: not called CC=4 SCAR=30.0
Pod documented
75 0 ▶ my $str = $self->children->map(sub{ $_->as_pod })->join( '' );
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 0 my $order_list = $self->order;
77 # Set some default value since key information has not been set yet
78 0 ▶ unless( $self->type->length )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->type->length) 0 0 0
79 {
80 0 ▶ if( $order_list )
Branch: 1 branch
BranchTrueFalseTotal
if ($order_list) { } 0 0 0
81 {
82 0 $self->type( '1.' );
83 }
84 else
85 {
86 0 $self->type( '*' );
87 }
88 }
89 0 my $marker = $self->type;
90 0 ▶ $marker .= '.' if( $marker =~ /^\d+$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $marker =~ /^\d+$/ 0 0 0
91 0 return( "=item ${marker} ${str}" );
92 }
93  
94 sub as_string
95 {
96 0 ▶ my $self = shift( @_ );
Subroutine as_string: not called CC=7 SCAR=40.3
Pod documented
97 0 my $arr = $self->new_array;
98 0 my $tag = 'li';
99 0 my $tag_open = $tag;
100 0 my $tmp = $self->new_array( [ "<$tag_open" ] );
101 0 my $attr = $self->new_array;
102 0 ▶ $attr->push( $self->format_id ) if( $self->id->length );
Branch: 1 branch
BranchTrueFalseTotal
if $self->id->length 0 0 0
103 0 ▶ $attr->push( $self->format_class ) if( $self->class->length );
Branch: 1 branch
BranchTrueFalseTotal
if $self->class->length 0 0 0
104 0 my $attributes = $self->format_attributes;
105 0 ▶ $attr->push( $attributes->join( ' ' )->scalar ) if( $attributes->length );
Branch: 1 branch
BranchTrueFalseTotal
if $attributes->length 0 0 0
106 0 ▶ $tmp->push( ' ' . $attr->join( ' ' )->scalar ) if( $attr->length );
Branch: 1 branch
BranchTrueFalseTotal
if $attr->length 0 0 0
107 0 $tmp->push( '>' );
108 0 $arr->push( ( TAB_SPACES x $self->indent ) . $tmp->join( '' )->scalar );
109 0 ▶ my $text_array = $self->children->map(sub{ $_->as_string });
Subroutine __ANON__: not called CC=1 SCAR=6.9
110 0 ▶ $arr->push( $text_array->list ) if( $text_array->length );
Branch: 1 branch
BranchTrueFalseTotal
if $text_array->length 0 0 0
111 ## We provide indentation on the closing tag if it is not on the same line, typical of a one line enclosed text
112 0 ▶ $arr->push( TAB_SPACES x $self->indent ) if( $text_array->length > 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $text_array->length > 1 0 0 0
113 0 $arr->push( "</$tag>" );
114 ## If the enclosed text has new lines embedded, we position the enclosing <li></li> on a new line each
115 ## return( $arr->join( $text_array->length > 1 ? "\n" : '' )->scalar );
116 0 return( $arr->join( '' )->scalar );
117 }
118  
119 0 ▶sub indent { return( shift->_set_get_number_as_object( 'indent', @_ ) ); }
Subroutine indent: not called CC=1 SCAR=6.9
Pod documented
120  
121 0 ▶sub order { return( shift->_set_get_boolean( 'order', @_ ) ); }
Subroutine order: not called CC=1 SCAR=6.9
Pod documented
122  
123 # sub type { return( shift->_set_get_scalar_as_object( 'type', @_ ) ); }
124 sub type
125 {
126 0 ▶ my $self = shift( @_ );
Subroutine type: not called CC=5 SCAR=34.0
Pod documented
127 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
128 {
129 ## Ex: *, +, -, 1, 2, 3....
130 0 my $char = shift( @_ );
131 0 ▶ return( $self->error( "No character provided to set this list item type. Alternatively you can use the order() method to set it directly." ) ) if( !length( $char ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $char 0 0 0
132 0 $self->_set_get_scalar_as_object( 'type', $char );
133 0 my $type;
134 0 ▶ if( $char =~ /^\+|\-|\*$/ )
Branch: 2 branches
BranchTrueFalseTotal
if ($char =~ /^\+|\-|\*$/) { } 0 0 0
elsif ($char =~ /^\d+\.?$/) { } 0 0 0
135 {
136 0 $type = 0;
137 }
138 elsif( $char =~ /^\d+\.?$/ )
139 {
140 0 $type = 1;
141 }
142 else
143 {
144 0 return( $self->error( "Clueless about what type of list corresponds to this character \"$char\"." ) );
145 }
146 0 $self->order( $type );
147 }
148 0 return( $self->_set_get_scalar_as_object( 'type' ) );
149 }
150  
151 1 1;
152 # NOTE: POD
153 __END__
« lib/Markdown/Parser/TableCell.pm ↑ Summary blib/lib/Markdown/Parser/Image.pm »