| 1 | package Text::MetaMarkup; | ||
| 2 | use 5.006; | ||
| 3 | use strict; | ||
| 4 | use Carp qw(croak); | ||
| 5 | |||
| 6 | our $VERSION = '0.01'; | ||
| 7 | |||
| 8 | my $link = qr/ \[ ([^\]]*) \] /x; | ||
| 9 | my $inlinetag = do { | ||
| 10 | our $acc = | ||
| 11 | qr/(?> \{ (?: (?> \\. | [^\\{}]+ | (??{ $acc }) ) )* \} )/xs; | ||
| 12 | qr/ \{ (\w+): ( (?: (?> \\. | [^\\{}]+ | (??{ $acc }) ) )* ) \} /xs; | ||
| 13 | }; | ||
| 14 | my $text = qr/(?: (?> \\. | [^{\[\\]+ ) )*/sx; | ||
| 15 | my $stuff = qr/(?>$text) | (?>$link) | (?>$inlinetag)/x; | ||
| 16 | my $paratag = qr/(\w+):\s*/; | ||
| 17 | my $n = qr/\n\s*\n/; | ||
| 18 | |||
| 19 | BEGIN { | ||
| 20 | # Undocumented feature: this module can export its regexes. | ||
| 21 | # Suggested use line: use Text::MetaMarkup _prefix => 'mm_', qw(:re); | ||
| 22 | if (eval { require Exporter::Tidy }) { | ||
| 23 | Exporter::Tidy->import( | ||
| 24 | re => [ qw/link inlinetag text stuff paratag n/ ], | ||
| 25 | _map => { | ||
| 26 | link => \$link, inlinetag => \$inlinetag, text => \$text, | ||
| 27 | stuff => \$stuff, paratag => \$paratag, n => \$n | ||
| 28 | } | ||
| 29 | ); | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | sub new { my $class = shift; bless { @_ }, $class } | ||
| 34 | |||
| 35 | sub parse { | ||
| 36 | my ($self, $type, $arg) = @_; | ||
| 37 | local $/; | ||
| 38 | if ($type eq 'file') { | ||
| 39 | open my $fh, '<', $arg or croak $!; | ||
| 40 | ($type, $arg) = ('fh', $fh); | ||
| 41 | } | ||
| 42 | if ($type eq 'fh') { | ||
| 43 | $arg = readline $arg; | ||
| 44 | } | ||
| 45 | return $self->parse_document($arg); | ||
| 46 | } | ||
| 47 | |||
| 48 | sub parse_document { | ||
| 49 | my ($self, $document) = @_; | ||
| 50 | return if not defined $document; | ||
| 51 | my $result; | ||
| 52 | |||
| 53 | if ($self->can('start_document')) { | ||
| 54 | my $r = $self->start_document(); | ||
| 55 | $result .= $r if defined $r; | ||
| 56 | } | ||
| 57 | |||
| 58 | my @paragraphs = split /$n/, $document; | ||
| 59 | for (@paragraphs) { | ||
| 60 | my $r = $self->parse_paragraph($_); | ||
| 61 | $result .= $r if defined $r; | ||
| 62 | } | ||
| 63 | |||
| 64 | if ($self->can('end_document')) { | ||
| 65 | my $r = $self->end_document(); | ||
| 66 | $result .= $r if defined $r; | ||
| 67 | } | ||
| 68 | |||
| 69 | return $result; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub parse_paragraph { | ||
| 73 | my ($self, $text) = @_; | ||
| 74 | /^#/ and return; | ||
| 75 | my $tag = $text =~ s/^$paratag// ? $1 : ''; | ||
| 76 | |||
| 77 | my $result; | ||
| 78 | |||
| 79 | if ($self->can('start_paragraph')) { | ||
| 80 | my $r = $self->start_paragraph($tag); | ||
| 81 | $result .= $r if defined $r; | ||
| 82 | } | ||
| 83 | |||
| 84 | my $method = "paragraph_$tag"; | ||
| 85 | $self->can($method) or $method = 'paragraph'; | ||
| 86 | my $r = $self->$method($tag, $text); | ||
| 87 | $result .= $r if defined $r; | ||
| 88 | |||
| 89 | if ($self->can('end_paragraph')) { | ||
| 90 | my $r = $self->end_paragraph($tag); | ||
| 91 | $result .= $r if defined $r; | ||
| 92 | } | ||
| 93 | return $result; | ||
| 94 | }; | ||
| 95 | |||
| 96 | sub parse_paragraph_text { | ||
| 97 | my ($self, $paragraph) = @_; | ||
| 98 | return if not defined $paragraph; | ||
| 99 | |||
| 100 | # Store and then use, to avoid some strange bug where Perl seems to forget | ||
| 101 | # the value of pos($paragraph) | ||
| 102 | |||
| 103 | my @chunks; | ||
| 104 | while ($paragraph =~ /\G($stuff)/g) { | ||
| 105 | push @chunks, [ $1, $2, $3, $4 ]; | ||
| 106 | } | ||
| 107 | |||
| 108 | my $result; | ||
| 109 | while (my $chunk = shift @chunks) { | ||
| 110 | my $char = substr $chunk->[0], 0, 1; | ||
| 111 | if ($char eq '[') { | ||
| 112 | my $r = $self->parse_link($chunk->[1]); | ||
| 113 | $result .= $r if defined $r; | ||
| 114 | } elsif ($char eq '{') { | ||
| 115 | my ($tag, $text) = ($chunk->[2], $chunk->[3]); | ||
| 116 | my $method = "inline_$tag"; | ||
| 117 | $self->can($method) or $method = 'inline'; | ||
| 118 | my $r = $self->$method($tag, $text); | ||
| 119 | $result .= $r if defined $r; | ||
| 120 | } else { | ||
| 121 | my $r = $self->text($chunk->[0]); | ||
| 122 | $result .= $r if defined $r; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | return $result; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub parse_link { | ||
| 129 | my ($self, $link) = @_; | ||
| 130 | my ($href, $text) = split /\|/, $link, 2; | ||
| 131 | my ($scheme, $rest) = split /:/, $href, 2; | ||
| 132 | if (not defined $rest) { | ||
| 133 | $rest = $scheme; | ||
| 134 | $scheme = ''; | ||
| 135 | } | ||
| 136 | |||
| 137 | my $method = "link_$scheme"; | ||
| 138 | $self->can($method) or $method = 'link'; | ||
| 139 | |||
| 140 | return $self->$method( | ||
| 141 | { href => $href, scheme => $scheme, rest => $rest}, | ||
| 142 | $text | ||
| 143 | ); | ||
| 144 | } | ||
| 145 | |||
| 146 | sub link_ { | ||
| 147 | my ($self, $href, $text) = @_; | ||
| 148 | return unless $href->{href} or $text; | ||
| 149 | $self->link($href, $text); | ||
| 150 | } | ||
| 151 | |||
| 152 | |||
| 153 | sub text { | ||
| 154 | my ($self, $text) = @_; | ||
| 155 | return if not defined $text; | ||
| 156 | $text =~ s/\\(.)/$1/gs; | ||
| 157 | return $self->escape($text); | ||
| 158 | } | ||
| 159 | |||
| 160 | 1; | ||
| 161 | |||
| 162 | __END__ |