blib/lib/Markdown/Parser/ListItem.pm untested

filter: Statementstmt 0.0% 0 / 103 Branchbran 0.0% 0 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 161 CC 18 SCAR 31.8
CC
18
Cov
0%
as_string
56.0
as_markdown
30.0
type
30.0
CRAP 24.1
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 use strict;
17 use warnings;
18 use parent qw( Markdown::Parser::Element );
19 use vars qw( $VERSION );
20 use Devel::Confess;
21 use constant TAB_SPACES => ' ';
22 our $VERSION = 'v0.2.0';
23 };
24  
25 use strict;
26 use warnings;
27  
28 sub init
29 {
30 my $self = shift( @_ );
31 $self->{indent} = '';
32 $self->{order} = '';
33 $self->{type} = '';
34 $self->{tag_name} = 'list_item';
35 return( $self->SUPER::init( @_ ) );
36 }
37  
38 sub as_markdown
39 {
40 my $self = shift( @_ );
41 my $str = $self->children->map(sub{ $_->as_markdown })->join( '' );
42 my $order_list = $self->order;
43 # Set some default value since key information has not been set yet
44 unless( $self->type->length )
45 {
46 if( $order_list )
47 {
48 $self->type( '1.' );
49 }
50 else
51 {
52 $self->type( '*' );
53 }
54 }
55 my $marker = $self->type;
56 $marker .= '.' if( $marker =~ /^\d+$/ );
57 my $indent = ' ' x $self->indent;
58 my $lines = $self->new_array( [split( /\n/, $str )] );
59 substr( $lines->[0], 0, 0 ) = "${marker} ";
60 # If the list has indentation, indent each lines of the string in this list item
61 if( $self->indent > 0 )
62 {
63 $lines->for(sub
64 {
65 my( $i, $val ) = @_;
66 substr( $lines->[$i], 0, 0 ) = $indent;
67 });
68 }
69 return( $lines->join( "\n" )->scalar );
70 }
71  
72 sub as_pod
73 {
74 my $self = shift( @_ );
75 my $str = $self->children->map(sub{ $_->as_pod })->join( '' );
76 my $order_list = $self->order;
77 # Set some default value since key information has not been set yet
78 unless( $self->type->length )
79 {
80 if( $order_list )
81 {
82 $self->type( '1.' );
83 }
84 else
85 {
86 $self->type( '*' );
87 }
88 }
89 my $marker = $self->type;
90 $marker .= '.' if( $marker =~ /^\d+$/ );
91 return( "=item ${marker} ${str}" );
92 }
93  
94 sub as_string
95 {
96 my $self = shift( @_ );
97 my $arr = $self->new_array;
98 my $tag = 'li';
99 my $tag_open = $tag;
100 my $tmp = $self->new_array( [ "<$tag_open" ] );
101 my $attr = $self->new_array;
102 $attr->push( $self->format_id ) if( $self->id->length );
103 $attr->push( $self->format_class ) if( $self->class->length );
104 my $attributes = $self->format_attributes;
105 $attr->push( $attributes->join( ' ' )->scalar ) if( $attributes->length );
106 $tmp->push( ' ' . $attr->join( ' ' )->scalar ) if( $attr->length );
107 $tmp->push( '>' );
108 $arr->push( ( TAB_SPACES x $self->indent ) . $tmp->join( '' )->scalar );
109 my $text_array = $self->children->map(sub{ $_->as_string });
110 $arr->push( $text_array->list ) if( $text_array->length );
111 ## We provide indentation on the closing tag if it is not on the same line, typical of a one line enclosed text
112 $arr->push( TAB_SPACES x $self->indent ) if( $text_array->length > 1 );
113 $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 return( $arr->join( '' )->scalar );
117 }
118  
119 sub indent { return( shift->_set_get_number_as_object( 'indent', @_ ) ); }
120  
121 sub order { return( shift->_set_get_boolean( 'order', @_ ) ); }
122  
123 # sub type { return( shift->_set_get_scalar_as_object( 'type', @_ ) ); }
124 sub type
125 {
126 my $self = shift( @_ );
127 if( @_ )
128 {
129 ## Ex: *, +, -, 1, 2, 3....
130 my $char = shift( @_ );
131 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 ) );
132 $self->_set_get_scalar_as_object( 'type', $char );
133 my $type;
134 if( $char =~ /^\+|\-|\*$/ )
135 {
136 $type = 0;
137 }
138 elsif( $char =~ /^\d+\.?$/ )
139 {
140 $type = 1;
141 }
142 else
143 {
144 return( $self->error( "Clueless about what type of list corresponds to this character \"$char\"." ) );
145 }
146 $self->order( $type );
147 }
148 return( $self->_set_get_scalar_as_object( 'type' ) );
149 }
150  
151 1;
152 # NOTE: POD
153 __END__