| 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
| |||||||||||||||
| 45 | { | ||||||||||||||
| 46 | 0 | ▶ | if( $order_list ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 79 | { | ||||||||||||||
| 80 | 0 | ▶ | if( $order_list ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 103 | 0 | ▶ | $attr->push( $self->format_class ) if( $self->class->length ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 104 | 0 | my $attributes = $self->format_attributes; | |||||||||||||
| 105 | 0 | ▶ | $attr->push( $attributes->join( ' ' )->scalar ) if( $attributes->length ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 106 | 0 | ▶ | $tmp->push( ' ' . $attr->join( ' ' )->scalar ) if( $attr->length ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 132 | 0 | $self->_set_get_scalar_as_object( 'type', $char ); | |||||||||||||
| 133 | 0 | my $type; | |||||||||||||
| 134 | 0 | ▶ | if( $char =~ /^\+|\-|\*$/ ) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 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__ | ||||||||||||||