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

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