| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # WikiText parser modules, Copyright (C) 2006-7 Enno Cramer, Mikhael Goikhman | 
| 2 |  |  |  |  |  |  | # | 
| 3 |  |  |  |  |  |  | # This program is free software; you can redistribute it and/or modify | 
| 4 |  |  |  |  |  |  | # it under the terms of the Perl Artistic License or the GNU General | 
| 5 |  |  |  |  |  |  | # Public License as published by the Free Software Foundation; either | 
| 6 |  |  |  |  |  |  | # version 2 of the License, or (at your option) any later version. | 
| 7 |  |  |  |  |  |  | # | 
| 8 |  |  |  |  |  |  | # This program is distributed in the hope that it will be useful, | 
| 9 |  |  |  |  |  |  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 10 |  |  |  |  |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
| 11 |  |  |  |  |  |  | # GNU General Public License for more details. | 
| 12 |  |  |  |  |  |  | # | 
| 13 |  |  |  |  |  |  | # You should have received a copy of the GNU General Public License | 
| 14 |  |  |  |  |  |  | # along with this program; if not, write to the Free Software | 
| 15 |  |  |  |  |  |  | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | package Text::WikiText::InputFilter; | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 4 |  |  | 4 |  | 19 | use strict; | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 130 |  | 
| 20 | 4 |  |  | 4 |  | 18 | use warnings; | 
|  | 4 |  |  |  |  | 7 |  | 
|  | 4 |  |  |  |  | 91 |  | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 4 |  |  | 4 |  | 3670 | use IO::Handle; | 
|  | 4 |  |  |  |  | 29556 |  | 
|  | 4 |  |  |  |  | 3702 |  | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | sub new { | 
| 25 | 3 |  |  | 3 | 1 | 9 | my $class = shift; | 
| 26 | 3 |  |  |  |  | 5 | my $string_or_handle = shift; | 
| 27 | 3 |  |  |  |  | 6 | my $is_handle = ref($string_or_handle); | 
| 28 |  |  |  |  |  |  |  | 
| 29 | 3 |  | 33 |  |  | 48 | my $self = { | 
|  |  |  | 33 |  |  |  |  | 
| 30 |  |  |  |  |  |  | handle =>  $is_handle && $string_or_handle, | 
| 31 |  |  |  |  |  |  | string => !$is_handle && $string_or_handle, | 
| 32 |  |  |  |  |  |  | line_n => 0, | 
| 33 |  |  |  |  |  |  | eof    => 0, | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | lookahead => undef, | 
| 36 |  |  |  |  |  |  | filter    => [], | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | buffer    => undef, | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | last_prefix => undef, | 
| 41 |  |  |  |  |  |  | last_match  => undef, | 
| 42 |  |  |  |  |  |  | }; | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 3 |  |  |  |  | 12 | return bless $self, $class; | 
| 45 |  |  |  |  |  |  | } | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | sub line_n { | 
| 48 | 17 |  |  | 17 | 1 | 19 | my $self = shift; | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 17 |  |  |  |  | 37 | return $self->{line_n}; | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | sub last_prefix { | 
| 54 | 0 |  |  | 0 | 1 | 0 | my $self = shift; | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 0 |  |  |  |  | 0 | return $self->{last_prefix}; | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | sub last_match { | 
| 60 | 27 |  |  | 27 | 1 | 36 | my $self = shift; | 
| 61 |  |  |  |  |  |  |  | 
| 62 | 27 |  |  |  |  | 71 | return $self->{last_match}; | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | sub peek { | 
| 66 | 307 |  |  | 307 | 1 | 348 | my $self = shift; | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 307 | 100 |  |  |  | 600 | if (! defined $self->{buffer}) { | 
| 69 | 61 |  |  |  |  | 99 | my $line = $self->readline; | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 61 | 100 |  |  |  | 123 | if (defined $line) { | 
| 72 | 49 |  |  |  |  | 49 | foreach my $filter (@{$self->{filter}}) { | 
|  | 49 |  |  |  |  | 106 |  | 
| 73 | 46 | 100 |  |  |  | 303 | if ($line !~ s/^$filter//) { | 
| 74 | 3 |  |  |  |  | 5 | $line = undef; | 
| 75 | 3 |  |  |  |  | 6 | last; | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 61 |  |  |  |  | 116 | $self->{buffer} = $line; | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 307 |  |  |  |  | 775 | return $self->{buffer}; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | sub readline { | 
| 87 | 85 |  |  | 85 | 1 | 93 | my $self = shift; | 
| 88 |  |  |  |  |  |  |  | 
| 89 | 85 | 100 | 100 |  |  | 437 | return $self->{lookahead} | 
| 90 |  |  |  |  |  |  | if defined $self->{lookahead} || $self->{eof}; | 
| 91 |  |  |  |  |  |  |  | 
| 92 | 44 | 100 |  |  |  | 281 | my $line = $self->{handle} | 
|  |  | 50 |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | ? $self->{handle}->getline | 
| 94 |  |  |  |  |  |  | : $self->{string} =~ s/\A(.+\z|.*(?:\r*\n|\r))// ? $1 : undef; | 
| 95 |  |  |  |  |  |  |  | 
| 96 | 44 |  |  |  |  | 76 | $self->{eof} = !defined $line; | 
| 97 | 44 | 100 |  |  |  | 287 | $line =~ s/(?:\r*\n|\r)/\n/ if defined $line; | 
| 98 |  |  |  |  |  |  |  | 
| 99 | 44 |  |  |  |  | 59 | ++$self->{line_n}; | 
| 100 |  |  |  |  |  |  |  | 
| 101 | 44 |  |  |  |  | 184 | return $self->{lookahead} = $line; | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | sub try { | 
| 105 | 0 |  |  | 0 | 1 | 0 | my ($self, $arg) = @_; | 
| 106 |  |  |  |  |  |  |  | 
| 107 | 0 |  |  |  |  | 0 | $self->peek; | 
| 108 | 0 |  | 0 |  |  | 0 | my $ret = defined $self->{buffer} && $self->{buffer} =~ /^(\s*)($arg)/; | 
| 109 |  |  |  |  |  |  |  | 
| 110 | 0 |  |  |  |  | 0 | $self->{last_prefix} = $1; | 
| 111 | 0 |  |  |  |  | 0 | $self->{last_match} = $2; | 
| 112 |  |  |  |  |  |  |  | 
| 113 | 0 |  |  |  |  | 0 | return $ret; | 
| 114 |  |  |  |  |  |  | } | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | sub match { | 
| 117 | 194 |  |  | 194 | 1 | 238 | my ($self, $arg) = @_; | 
| 118 |  |  |  |  |  |  |  | 
| 119 | 194 |  |  |  |  | 302 | $self->peek; | 
| 120 | 194 |  | 100 |  |  | 4424 | my $ret = defined $self->{buffer} && $self->{buffer} =~ s/^(\s*)($arg)//; | 
| 121 |  |  |  |  |  |  |  | 
| 122 | 194 |  |  |  |  | 455 | $self->{last_prefix} = $1; | 
| 123 | 194 |  |  |  |  | 283 | $self->{last_match} = $2; | 
| 124 |  |  |  |  |  |  |  | 
| 125 | 194 |  |  |  |  | 708 | return $ret; | 
| 126 |  |  |  |  |  |  | } | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | sub commit { | 
| 129 | 41 |  |  | 41 | 1 | 51 | my $self = shift; | 
| 130 |  |  |  |  |  |  |  | 
| 131 | 41 |  |  |  |  | 59 | $self->{buffer} = undef; | 
| 132 | 41 |  |  |  |  | 94 | $self->{lookahead} = undef; | 
| 133 |  |  |  |  |  |  | } | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | sub flush_empty { | 
| 136 | 21 |  |  | 21 | 1 | 32 | my $self = shift; | 
| 137 |  |  |  |  |  |  |  | 
| 138 | 21 |  |  |  |  | 20 | local $_; | 
| 139 |  |  |  |  |  |  |  | 
| 140 | 21 |  | 100 |  |  | 44 | while ( | 
|  |  |  | 66 |  |  |  |  | 
|  |  |  | 66 |  |  |  |  | 
| 141 |  |  |  |  |  |  | (defined ($_ = $self->readline) && /^\s*$/) | 
| 142 |  |  |  |  |  |  | || (defined ($_ = $self->peek) && /^\s*$/) | 
| 143 |  |  |  |  |  |  | ) { | 
| 144 | 3 |  |  |  |  | 11 | $self->commit; | 
| 145 |  |  |  |  |  |  | } | 
| 146 |  |  |  |  |  |  | } | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | sub push_filter { | 
| 149 | 21 |  |  | 21 | 1 | 29 | my ($self, $filter) = @_; | 
| 150 |  |  |  |  |  |  |  | 
| 151 | 21 | 50 |  |  |  | 22 | push @{$self->{filter}}, defined $self->{last_prefix} | 
|  | 21 |  |  |  |  | 171 |  | 
| 152 |  |  |  |  |  |  | ? qr/\Q$self->{last_prefix}\E$filter/ | 
| 153 |  |  |  |  |  |  | : $filter; | 
| 154 |  |  |  |  |  |  | } | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | sub pop_filter { | 
| 157 | 21 |  |  | 21 | 1 | 29 | my $self = shift; | 
| 158 |  |  |  |  |  |  |  | 
| 159 | 21 |  |  |  |  | 23 | pop @{$self->{filter}}; | 
|  | 21 |  |  |  |  | 36 |  | 
| 160 | 21 |  |  |  |  | 60 | $self->{buffer} = undef; | 
| 161 |  |  |  |  |  |  | } | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | 1; | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | __END__ |