| 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 | { | ||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | use parent qw( Markdown::Parser::Element ); | ||
| 19 | use vars qw( $VERSION ); | ||
| 20 | our $VERSION = 'v0.3.0'; | ||
| 21 | }; | ||
| 22 | |||
| 23 | use strict; | ||
| 24 | use warnings; | ||
| 25 | |||
| 26 | sub init | ||
| 27 | { | ||
| 28 | my $self = shift( @_ ); | ||
| 29 | $self->{id} = []; | ||
| 30 | $self->{level} = 0; | ||
| 31 | $self->{tag_name} = 'header'; | ||
| 32 | $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 | if( !$self->level && $self->raw->length ) | ||
| 35 | { | ||
| 36 | # # is level 1, ## is level 2, etc... | ||
| 37 | my $raw = $self->raw; | ||
| 38 | $self->level( length( ( $raw =~ /^(\#+)/ )[0] ) ); | ||
| 39 | } | ||
| 40 | return( $self ); | ||
| 41 | } | ||
| 42 | |||
| 43 | sub append { return( shift->_append_text( @_ ) ); } | ||
| 44 | |||
| 45 | sub as_markdown | ||
| 46 | { | ||
| 47 | my $self = shift( @_ ); | ||
| 48 | my $str = $self->children->map(sub{ $_->as_markdown })->join( '' ); | ||
| 49 | my $level = $self->level; | ||
| 50 | my $marker = '#' x $level; | ||
| 51 | return( "${marker} ${str}" ); | ||
| 52 | } | ||
| 53 | |||
| 54 | sub as_pod | ||
| 55 | { | ||
| 56 | my $self = shift( @_ ); | ||
| 57 | my $str = $self->children->map(sub{ $_->as_pod })->join( '' ); | ||
| 58 | my $level = $self->level; | ||
| 59 | my $marker = '=head' . $level; | ||
| 60 | return( "${marker} ${str}\n" ); | ||
| 61 | } | ||
| 62 | |||
| 63 | sub as_string | ||
| 64 | { | ||
| 65 | my $self = shift( @_ ); | ||
| 66 | my $arr = $self->new_array; | ||
| 67 | my $tag = 'h' . $self->level; | ||
| 68 | my $tag_open = $tag; | ||
| 69 | if( $self->id->length ) | ||
| 70 | { | ||
| 71 | $tag_open .= ' ' . $self->id->map(sub{ sprintf( 'id="%s"', $_ ) })->join( ' ' )->scalar; | ||
| 72 | } | ||
| 73 | if( $self->class->length ) | ||
| 74 | { | ||
| 75 | $tag_open .= ' class="' . $self->class->join( ' ' )->scalar . '"'; | ||
| 76 | } | ||
| 77 | $arr->push( "<$tag_open" ); | ||
| 78 | my $attr = $self->new_array; | ||
| 79 | $attr->push( $self->format_id ) if( $self->id->length ); | ||
| 80 | $attr->push( $self->format_class ) if( $self->class->length ); | ||
| 81 | my $attributes = $self->format_attributes; | ||
| 82 | $attr->push( $attributes->join( ' ' )->scalar ) if( $attributes->length ); | ||
| 83 | $arr->push( ' ' . $attr->join( ' ' )->scalar ) if( $attr->length ); | ||
| 84 | $arr->push( '>' ); | ||
| 85 | $arr->push( $self->children->map(sub | ||
| 86 | { | ||
| 87 | $_->as_string; | ||
| 88 | })->list ); | ||
| 89 | $arr->push( "</$tag>" ); | ||
| 90 | return( $arr->join( '' )->scalar ); | ||
| 91 | } | ||
| 92 | |||
| 93 | sub id { return( shift->_set_get_array_as_object( 'id', @_ ) ); } | ||
| 94 | |||
| 95 | sub level { return( shift->_set_get_number_as_object( 'level', @_ ) ); } | ||
| 96 | |||
| 97 | 1; | ||
| 98 | # NOTE: POD | ||
| 99 | __END__ |