| 1 | package Markdown::Compiler::Target::Tree; | ||
| 2 | use Moo; | ||
| 3 | use Storable qw( dclone ); | ||
| 4 | |||
| 5 | has tree => ( | ||
| 6 | is => 'ro', | ||
| 7 | ); | ||
| 8 | |||
| 9 | has tree => ( | ||
| 10 | is => 'ro', | ||
| 11 | lazy => 1, | ||
| 12 | builder => '_build_html', | ||
| 13 | ); | ||
| 14 | |||
| 15 | has functions => ( | ||
| 16 | is => 'ro', | ||
| 17 | lazy => 1, | ||
| 18 | default => sub { | ||
| 19 | return +{ | ||
| 20 | 'Markdown::Compiler::Parser::Node::Header' => 'header', | ||
| 21 | 'Markdown::Compiler::Parser::Node::HR' => 'hr', | ||
| 22 | 'Markdown::Compiler::Parser::Node::Paragraph' => 'paragraph', | ||
| 23 | 'Markdown::Compiler::Parser::Node::Paragraph::BoldItalic' => 'paragraph_bolditalic', | ||
| 24 | 'Markdown::Compiler::Parser::Node::Paragraph::Bold' => 'paragraph_bold', | ||
| 25 | 'Markdown::Compiler::Parser::Node::Paragraph::Italic' => 'paragraph_italic', | ||
| 26 | 'Markdown::Compiler::Parser::Node::Paragraph::String' => 'paragraph_string', | ||
| 27 | 'Markdown::Compiler::Parser::Node::Paragraph::Link' => 'paragraph_link', | ||
| 28 | 'Markdown::Compiler::Parser::Node::Paragraph::Image' => 'paragraph_image', | ||
| 29 | 'Markdown::Compiler::Parser::Node::Table' => 'table', | ||
| 30 | 'Markdown::Compiler::Parser::Node::Blockquote' => 'blockquote', | ||
| 31 | 'Markdown::Compiler::Parser::Node::Codeblock' => 'codeblock', | ||
| 32 | 'Markdown::Compiler::Parser::Node::List' => 'list', | ||
| 33 | } | ||
| 34 | } | ||
| 35 | ); | ||
| 36 | |||
| 37 | sub _build_tree { | ||
| 38 | my ( $self ) = @_; | ||
| 39 | |||
| 40 | return $self->_compile( dclone $self->tree ); | ||
| 41 | } | ||
| 42 | |||
| 43 | sub _compile { | ||
| 44 | my ( $self, $tree ) = @_; | ||
| 45 | |||
| 46 | my $str; | ||
| 47 | |||
| 48 | while ( defined ( my $node = shift @{ $tree } ) ) { | ||
| 49 | # Children should be compiled first. | ||
| 50 | if ( $node->children and @{$node->children} >= 1 ) { | ||
| 51 | |||
| 52 | # If this node can be compiled, then we will compile it, giving it the content | ||
| 53 | if ( my $code = $self->can($self->functions->{ref($node)}) ) { | ||
| 54 | $str .= $code->($self, $node, $self->_compile($node->children)); | ||
| 55 | next; | ||
| 56 | } | ||
| 57 | warn "This is an odd place to be.... children but the parent can't be compiled?"; | ||
| 58 | } | ||
| 59 | |||
| 60 | # This node has no children to compile. | ||
| 61 | else { | ||
| 62 | if ( my $code = $self->can($self->functions->{ref($node)}) ) { | ||
| 63 | $str .= $code->($self, $node); | ||
| 64 | next; | ||
| 65 | } | ||
| 66 | |||
| 67 | } | ||
| 68 | } | ||
| 69 | return $str; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub header { | ||
| 73 | my ( $self, $node, $content ) = @_; | ||
| 74 | |||
| 75 | return sprintf( "header( size => ) ) | ||
| 76 | |||
| 77 | "header() | ||
| 78 | |||
| 79 | my $header = "h" . $node->size; | ||
| 80 | |||
| 81 | return "<$header>$content</$header>\n\n"; | ||
| 82 | |||
| 83 | } | ||
| 84 | |||
| 85 | sub hr { | ||
| 86 | |||
| 87 | } | ||
| 88 | |||
| 89 | sub paragraph { | ||
| 90 | my ( $self, $node, $content ) = @_; | ||
| 91 | |||
| 92 | return "<p>$content</p>\n\n"; | ||
| 93 | |||
| 94 | } | ||
| 95 | |||
| 96 | sub paragraph_bolditalic { | ||
| 97 | my ( $self, $node, $content ) = @_; | ||
| 98 | |||
| 99 | return "<strong><em>$content</em></strong>"; | ||
| 100 | |||
| 101 | } | ||
| 102 | |||
| 103 | sub paragraph_bold { | ||
| 104 | my ( $self, $node, $content ) = @_; | ||
| 105 | |||
| 106 | return "<strong>$content</strong>"; | ||
| 107 | |||
| 108 | } | ||
| 109 | |||
| 110 | sub paragraph_italic { | ||
| 111 | my ( $self, $node, $content ) = @_; | ||
| 112 | |||
| 113 | return "<em>$content</em>"; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub paragraph_string { | ||
| 117 | my ( $self, $node ) = @_; | ||
| 118 | |||
| 119 | return $node->content; | ||
| 120 | |||
| 121 | } | ||
| 122 | |||
| 123 | sub paragraph_link { | ||
| 124 | my ( $self, $node ) = @_; | ||
| 125 | |||
| 126 | return sprintf( '<a href="%s" title="%s">%s</a>', | ||
| 127 | $node->href, | ||
| 128 | $node->text ? $node->text : "", | ||
| 129 | $node->title ? $node->title : "", | ||
| 130 | ); | ||
| 131 | } | ||
| 132 | |||
| 133 | sub paragraph_image { | ||
| 134 | my ( $self, $node ) = @_; | ||
| 135 | |||
| 136 | return sprintf( '<img src="%s" title="%s" alt="%s">', | ||
| 137 | $node->href, | ||
| 138 | $node->text ? $node->text : "", | ||
| 139 | $node->title ? $node->title : "", | ||
| 140 | ); | ||
| 141 | } | ||
| 142 | |||
| 143 | sub table { | ||
| 144 | |||
| 145 | } | ||
| 146 | |||
| 147 | sub blockquote { | ||
| 148 | |||
| 149 | } | ||
| 150 | |||
| 151 | sub codeblock { | ||
| 152 | |||
| 153 | } | ||
| 154 | |||
| 155 | sub list { | ||
| 156 | |||
| 157 | } | ||
| 158 | |||
| 159 | |||
| 160 | |||
| 161 | |||
| 162 | |||
| 163 | # package Markdown::Compiler::Parser::Node; | ||
| 164 | # package Markdown::Compiler::Parser::Node::Header; | ||
| 165 | # package Markdown::Compiler::Parser::Node::HR; | ||
| 166 | # package Markdown::Compiler::Parser::Node::Paragraph; | ||
| 167 | # package Markdown::Compiler::Parser::Node::Paragraph::BoldItalic; | ||
| 168 | # package Markdown::Compiler::Parser::Node::Paragraph::Bold; | ||
| 169 | # package Markdown::Compiler::Parser::Node::Paragraph::Italic; | ||
| 170 | # package Markdown::Compiler::Parser::Node::Paragraph::String; | ||
| 171 | # package Markdown::Compiler::Parser::Node::Paragraph::Link; | ||
| 172 | # package Markdown::Compiler::Parser::Node::Paragraph::Image; | ||
| 173 | # package Markdown::Compiler::Parser::Node::Table; | ||
| 174 | # package Markdown::Compiler::Parser::Node::Blockquote; | ||
| 175 | # package Markdown::Compiler::Parser::Node::Codeblock; | ||
| 176 | # package Markdown::Compiler::Parser::Node::List; | ||
| 177 | |||
| 178 | |||
| 179 | |||
| 180 | 1; | ||
| 181 |