| 1 | package Silki::Markdent::Dialect::Silki::SpanParser; | ||
| 2 | { | ||
| 3 | $Silki::Markdent::Dialect::Silki::SpanParser::VERSION = '0.29'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use namespace::autoclean; | ||
| 9 | |||
| 10 | use List::AllUtils qw( insert_after_string ); | ||
| 11 | use Silki::Markdent::Event::FileLink; | ||
| 12 | use Silki::Markdent::Event::ImageLink; | ||
| 13 | use Silki::Markdent::Event::WikiLink; | ||
| 14 | |||
| 15 | use Moose; | ||
| 16 | use MooseX::SemiAffordanceAccessor; | ||
| 17 | use MooseX::StrictConstructor; | ||
| 18 | |||
| 19 | extends 'Markdent::Dialect::Theory::SpanParser'; | ||
| 20 | |||
| 21 | sub _possible_span_matches { | ||
| 22 | my $self = shift; | ||
| 23 | |||
| 24 | my @look_for = $self->SUPER::_possible_span_matches(@_); | ||
| 25 | |||
| 26 | # inside code span | ||
| 27 | return @look_for if @look_for == 1; | ||
| 28 | |||
| 29 | for my $val (qw( image_link file_link wiki_link )) { | ||
| 30 | insert_after_string 'code_start', $val, @look_for; | ||
| 31 | } | ||
| 32 | |||
| 33 | return @look_for; | ||
| 34 | } | ||
| 35 | |||
| 36 | # More or less stolen from Text::Markdown | ||
| 37 | my $nested_brackets; | ||
| 38 | $nested_brackets = qr{ | ||
| 39 | (?> # Atomic matching | ||
| 40 | [^\[\]]+ # Anything other than brackets | ||
| 41 | | | ||
| 42 | \[ | ||
| 43 | (??{ $nested_brackets }) # Recursive set of nested curlies | ||
| 44 | \] | ||
| 45 | )* | ||
| 46 | }x; | ||
| 47 | |||
| 48 | sub _match_wiki_link { | ||
| 49 | my $self = shift; | ||
| 50 | my $text = shift; | ||
| 51 | |||
| 52 | return unless ${$text} =~ / \G | ||
| 53 | (?: | ||
| 54 | \[ | ||
| 55 | ($nested_brackets) | ||
| 56 | \] | ||
| 57 | )? | ||
| 58 | \(\( | ||
| 59 | (.+?) | ||
| 60 | \)\) | ||
| 61 | /xmgc; | ||
| 62 | |||
| 63 | my %p = ( link_text => $2 ); | ||
| 64 | $p{display_text} = $1 | ||
| 65 | if defined $1; | ||
| 66 | |||
| 67 | my $event | ||
| 68 | = $self->_make_event( 'Silki::Markdent::Event::WikiLink' => %p ); | ||
| 69 | |||
| 70 | $self->_markup_event($event); | ||
| 71 | |||
| 72 | return 1; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub _match_file_link { | ||
| 76 | my $self = shift; | ||
| 77 | my $text = shift; | ||
| 78 | |||
| 79 | return unless ${$text} =~ / \G | ||
| 80 | (?: | ||
| 81 | \[ | ||
| 82 | ($nested_brackets) | ||
| 83 | \] | ||
| 84 | )? | ||
| 85 | {{ | ||
| 86 | \s* | ||
| 87 | file: | ||
| 88 | \s* | ||
| 89 | ([^}]+) | ||
| 90 | \s* | ||
| 91 | }} | ||
| 92 | /xmgc; | ||
| 93 | |||
| 94 | my %p = ( link_text => $2 ); | ||
| 95 | $p{display_text} = $1 if defined $1; | ||
| 96 | |||
| 97 | my $event | ||
| 98 | = $self->_make_event( 'Silki::Markdent::Event::FileLink' => %p ); | ||
| 99 | |||
| 100 | $self->_markup_event($event); | ||
| 101 | |||
| 102 | return 1; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub _match_image_link { | ||
| 106 | my $self = shift; | ||
| 107 | my $text = shift; | ||
| 108 | |||
| 109 | return unless ${$text} =~ / \G | ||
| 110 | {{ | ||
| 111 | \s* | ||
| 112 | image: | ||
| 113 | \s* | ||
| 114 | ([^}]+) | ||
| 115 | \s* | ||
| 116 | }} | ||
| 117 | /xmgc; | ||
| 118 | |||
| 119 | my %p = ( link_text => $1 ); | ||
| 120 | |||
| 121 | my $event | ||
| 122 | = $self->_make_event( 'Silki::Markdent::Event::ImageLink' => %p ); | ||
| 123 | |||
| 124 | $self->_markup_event($event); | ||
| 125 | |||
| 126 | return 1; | ||
| 127 | } | ||
| 128 | |||
| 129 | sub _match_plain_text { | ||
| 130 | my $self = shift; | ||
| 131 | my $text = shift; | ||
| 132 | |||
| 133 | my $escape_re = $self->_escape_re(); | ||
| 134 | |||
| 135 | # Note that we're careful not to consume any of the characters marking the | ||
| 136 | # (possible) end of the plain text. If those things turn out to _not_ be | ||
| 137 | # markup, we'll get them on the next pass, because we always match at | ||
| 138 | # least one character, so we should never get stuck in a loop. | ||
| 139 | return | ||
| 140 | unless ${$text} =~ /\G | ||
| 141 | ( .+? ) # at least one character followed by ... | ||
| 142 | (?= | ||
| 143 | $escape_re | ||
| 144 | | | ||
| 145 | \* # possible span markup | ||
| 146 | | | ||
| 147 | _ | ||
| 148 | | | ||
| 149 | \p{SpaceSeparator}* \` | ||
| 150 | | | ||
| 151 | \(\( # possible wiki link | ||
| 152 | | | ||
| 153 | \{\{ # possible wiki command | ||
| 154 | | | ||
| 155 | !?\[ # possible image or link | ||
| 156 | | | ||
| 157 | < [^>]+ > # an HTML tag | ||
| 158 | | | ||
| 159 | &\S+; # an HTML entity | ||
| 160 | | | ||
| 161 | \z # or the end of the string | ||
| 162 | ) | ||
| 163 | /xgcs; | ||
| 164 | |||
| 165 | $self->_print_debug("Interpreting as plain text\n\n[$1]\n") | ||
| 166 | if $self->debug(); | ||
| 167 | |||
| 168 | $self->_save_span_text($1); | ||
| 169 | |||
| 170 | return 1; | ||
| 171 | } | ||
| 172 | |||
| 173 | __PACKAGE__->meta()->make_immutable(); | ||
| 174 | |||
| 175 | 1; | ||
| 176 | |||
| 177 | # ABSTRACT: Parses span-level markup for the Silki Markdown dialect | ||
| 178 | |||
| 179 | __END__ |