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