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