| 1 | package Web::PerlDistSite::MenuItem::MarkdownFile; | ||
| 2 | |||
| 3 | our $VERSION = '0.001008'; | ||
| 4 | |||
| 5 | use Moo; | ||
| 6 | use Web::PerlDistSite::Common -lexical, -all; | ||
| 7 | |||
| 8 | use Text::Markdown (); | ||
| 9 | |||
| 10 | extends 'Web::PerlDistSite::MenuItem::File'; | ||
| 11 | |||
| 12 | sub body_class { | ||
| 13 | return 'page from-markdown'; | ||
| 14 | } | ||
| 15 | |||
| 16 | sub extra_top ( $self ) { | ||
| 17 | HTML::HTML5::Parser->new->parse_balanced_chunk( | ||
| 18 | sprintf( | ||
| 19 | '<div class="heading container py-3"><h1 class="display-2">%s</h1></div>', | ||
| 20 | esc_html( $self->title ), | ||
| 21 | ) | ||
| 22 | ); | ||
| 23 | } | ||
| 24 | |||
| 25 | sub extra_bottom { return; } | ||
| 26 | |||
| 27 | sub compile_page ( $self ) { | ||
| 28 | state $m = Text::Markdown->new( empty_element_suffix => '>' ); | ||
| 29 | state $p = HTML::HTML5::Parser->new; | ||
| 30 | my $dom = $self->project->get_template_page( $self ); | ||
| 31 | |||
| 32 | $dom->getElementsByTagName( 'body' )->shift->setAttribute( class => $self->body_class ); | ||
| 33 | |||
| 34 | my $raw = $self->raw_content; | ||
| 35 | my @raw = split /\n-{4}\n/, $raw; | ||
| 36 | |||
| 37 | $dom->getElementsByTagName( 'main' )->shift->appendChild( $_ ) | ||
| 38 | for $self->extra_top; | ||
| 39 | |||
| 40 | if ( @raw == 1 ) { | ||
| 41 | my $html = $m->markdown( $raw ); | ||
| 42 | my $content = $p->parse_balanced_chunk( $html ); | ||
| 43 | my $article = $dom->createElement( 'article' ); | ||
| 44 | $article->setAttribute( class => 'container' ); | ||
| 45 | $article->appendChild( $content ); | ||
| 46 | $dom->getElementsByTagName( 'main' )->shift->appendChild( $article ); | ||
| 47 | } | ||
| 48 | else { | ||
| 49 | my $count = 0; | ||
| 50 | my $article = $dom->createElement( 'article' ); | ||
| 51 | $article->setAttribute( class => 'has-sections has-section-count-' . scalar @raw ); | ||
| 52 | for my $raw ( @raw ) { | ||
| 53 | my $html = $m->markdown( $raw ); | ||
| 54 | my $content = $p->parse_balanced_chunk( $html ); | ||
| 55 | my $section = $dom->createElement( 'section' ); | ||
| 56 | my $div = $dom->createElement( 'div' ); | ||
| 57 | $div->setAttribute( class => 'container' ); | ||
| 58 | $div->appendChild( $content ); | ||
| 59 | $section->appendChild( $div ); | ||
| 60 | $article->appendChild( $section ); | ||
| 61 | } | ||
| 62 | $dom->getElementsByTagName( 'main' )->shift->appendChild( $article ); | ||
| 63 | } | ||
| 64 | |||
| 65 | $dom->getElementsByTagName( 'main' )->shift->appendChild( $_ ) | ||
| 66 | for $self->extra_bottom; | ||
| 67 | |||
| 68 | |||
| 69 | return $self->_compile_dom( $dom ); | ||
| 70 | } | ||
| 71 | |||
| 72 | 1; |