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

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