| 1 | ##---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 2 | ## Markdown Parser Only - ~/lib/Markdown/Parser/Header.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::Header; | ||||||||||||||||||||||||||||
| 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 | our $VERSION = 'v0.3.0'; | |||||||||||||||||||||||||||
| 21 | }; | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | sub init | ||||||||||||||||||||||||||||
| 27 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
init: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 29 | 0 | $self->{id} = []; | |||||||||||||||||||||||||||
| 30 | 0 | $self->{level} = 0; | |||||||||||||||||||||||||||
| 31 | 0 | $self->{tag_name} = 'header'; | |||||||||||||||||||||||||||
| 32 | 0 | $self->SUPER::init( @_ ); | |||||||||||||||||||||||||||
| 33 | # If the header level is not set yet and we have some raw data, let's guess it from the raw data provided | ||||||||||||||||||||||||||||
| 34 | 0 | ▶ | if( !$self->level && $self->raw->length ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $self->level and $self->raw->length
MC/DC0%0/2
not $self->level and $self->raw->length
not $self->level
$self->raw->length
Truth table0%0/3
not(A) and B
A= not $self->levelB= $self->raw->length
| |||||||||||||||||||||||||||||
| 35 | { | ||||||||||||||||||||||||||||
| 36 | # # is level 1, ## is level 2, etc... | ||||||||||||||||||||||||||||
| 37 | 0 | my $raw = $self->raw; | |||||||||||||||||||||||||||
| 38 | 0 | $self->level( length( ( $raw =~ /^(\#+)/ )[0] ) ); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 0 | return( $self ); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | ▶ | sub append { return( shift->_append_text( @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
append: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | sub as_markdown | ||||||||||||||||||||||||||||
| 46 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
as_markdown: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 48 | 0 | ▶ | my $str = $self->children->map(sub{ $_->as_markdown })->join( '' ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 49 | 0 | my $level = $self->level; | |||||||||||||||||||||||||||
| 50 | 0 | my $marker = '#' x $level; | |||||||||||||||||||||||||||
| 51 | 0 | return( "${marker} ${str}" ); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | sub as_pod | ||||||||||||||||||||||||||||
| 55 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
as_pod: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 57 | 0 | ▶ | my $str = $self->children->map(sub{ $_->as_pod })->join( '' ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 58 | 0 | my $level = $self->level; | |||||||||||||||||||||||||||
| 59 | 0 | my $marker = '=head' . $level; | |||||||||||||||||||||||||||
| 60 | 0 | return( "${marker} ${str}\n" ); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | sub as_string | ||||||||||||||||||||||||||||
| 64 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
as_string: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 66 | 0 | my $arr = $self->new_array; | |||||||||||||||||||||||||||
| 67 | 0 | my $tag = 'h' . $self->level; | |||||||||||||||||||||||||||
| 68 | 0 | my $tag_open = $tag; | |||||||||||||||||||||||||||
| 69 | 0 | ▶ | if( $self->id->length ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | { | ||||||||||||||||||||||||||||
| 71 | 0 | ▶ | $tag_open .= ' ' . $self->id->map(sub{ sprintf( 'id="%s"', $_ ) })->join( ' ' )->scalar; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if( $self->class->length ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | { | ||||||||||||||||||||||||||||
| 75 | 0 | $tag_open .= ' class="' . $self->class->join( ' ' )->scalar . '"'; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | 0 | $arr->push( "<$tag_open" ); | |||||||||||||||||||||||||||
| 78 | 0 | my $attr = $self->new_array; | |||||||||||||||||||||||||||
| 79 | 0 | ▶ | $attr->push( $self->format_id ) if( $self->id->length ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | $attr->push( $self->format_class ) if( $self->class->length ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | my $attributes = $self->format_attributes; | |||||||||||||||||||||||||||
| 82 | 0 | ▶ | $attr->push( $attributes->join( ' ' )->scalar ) if( $attributes->length ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | $arr->push( ' ' . $attr->join( ' ' )->scalar ) if( $attr->length ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | $arr->push( '>' ); | |||||||||||||||||||||||||||
| 85 | $arr->push( $self->children->map(sub | ||||||||||||||||||||||||||||
| 86 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 87 | 0 | $_->as_string; | |||||||||||||||||||||||||||
| 88 | 0 | })->list ); | |||||||||||||||||||||||||||
| 89 | 0 | $arr->push( "</$tag>" ); | |||||||||||||||||||||||||||
| 90 | 0 | return( $arr->join( '' )->scalar ); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | sub id { return( shift->_set_get_array_as_object( 'id', @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
id: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | sub level { return( shift->_set_get_number_as_object( 'level', @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
level: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 1 | 1; | |||||||||||||||||||||||||||
| 98 | # NOTE: POD | ||||||||||||||||||||||||||||
| 99 | __END__ | ||||||||||||||||||||||||||||