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

filter: Statementstmt 0.0% 0 / 84 Branchbran 0.0% 0 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 129 CC 13 SCAR 29.6
CC
13
Cov
0%
as_string
42.0
as_pod
30.0
as_markdown
20.0
CRAP 19.4
1 ##----------------------------------------------------------------------------
2 ## Markdown Parser Only - ~/lib/Markdown/Parser/List.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::List;
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->{tag_name} = 'list';
33 return( $self->SUPER::init( @_ ) );
34 }
35  
36 sub as_markdown
37 {
38 my $self = shift( @_ );
39 my $base_class = $self->base_class;
40 my $order_list = $self->order;
41 my $n = 0;
42 $self->children->foreach(sub
43 {
44 my $li = shift( @_ );
45 # Skip unless this child it a list item
46 return(1) unless( $li->isa( "${base_class}::ListItem" ) );
47 $n++;
48 if( $order_list )
49 {
50 $li->type( "${n}." );
51 }
52 elsif( !$li->type->length )
53 {
54 $li->type( '*' );
55 }
56 });
57 return( $self->children->map(sub{ $_->as_markdown })->join( "\n" )->scalar );
58 }
59  
60 # Basically same as markdown
61 sub as_pod
62 {
63 my $self = shift( @_ );
64 my $base_class = $self->base_class;
65 my $order_list = $self->order;
66 my $n = 0;
67 $self->children->foreach(sub
68 {
69 my $li = shift( @_ );
70 # Skip unless this child it a list item
71 return(1) unless( $li->isa( "${base_class}::ListItem" ) );
72 $n++;
73 if( $order_list )
74 {
75 $li->type( "${n}." );
76 }
77 elsif( !$li->type->length )
78 {
79 $li->type( '*' );
80 }
81 });
82 my $indent = $self->indent;
83 $indent = 4 unless( $indent > 0 );
84 return( "=over ${indent}\n\n" . $self->children->map(sub{ $_->as_pod })->join( "\n\n" )->scalar . "\n\n=back" );
85 }
86  
87 sub as_string
88 {
89 my $self = shift( @_ );
90 my $arr = $self->new_array;
91 my $tag = $self->order ? 'ol' : 'ul';
92 my $tag_open = $tag;
93 my $tmp = $self->new_array( [ "<$tag_open" ] );
94 my $attr = $self->new_array;
95 $attr->push( $self->format_id ) if( $self->id->length );
96 $attr->push( $self->format_class ) if( $self->class->length );
97 my $attributes = $self->format_attributes;
98 $attr->push( $attributes->join( ' ' )->scalar ) if( $attributes->length );
99 $tmp->push( ' ' . $attr->join( ' ' )->scalar ) if( $attr->length );
100 $tmp->push( '>' );
101
102 $arr->push( ( TAB_SPACES x $self->indent ) . $tmp->join( '' )->scalar );
103 # my $ct = $self->new_array;
104 $arr->push( $self->children->map(sub
105 {
106 $_->as_string;
107 })->list );
108 $arr->push( ( TAB_SPACES x $self->indent ) . "</$tag>" );
109 return( $arr->join( "\n" )->scalar );
110 }
111  
112 sub indent { return( shift->_set_get_number_as_object( 'indent', @_ ) ); }
113  
114 sub order { return( shift->_set_get_boolean( 'order', @_ ) ); }
115  
116 1;
117 # NOTE: POD
118 __END__