| 1 | ## | ||
| 2 | # name: WikiText::Kwiki::Parser | ||
| 3 | # abstract: Kwiki WikiText Parser Module | ||
| 4 | # author: Ingy döt Net <ingy@cpan.org> | ||
| 5 | # license: perl | ||
| 6 | # copyright: 2008, 2010, 2011 | ||
| 7 | |||
| 8 | package WikiText::Kwiki::Parser; | ||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use base 'WikiText::Parser'; | ||
| 12 | |||
| 13 | # use XXX; | ||
| 14 | |||
| 15 | # Reusable regexp generators used by the grammar | ||
| 16 | my $ALPHANUM = '\p{Letter}\p{Number}\pM'; | ||
| 17 | |||
| 18 | # These are all stolen from URI.pm | ||
| 19 | my $reserved = q{;/?:@&=+$,[]#}; | ||
| 20 | my $mark = q{-_.!~*'()}; | ||
| 21 | my $unreserved = "A-Za-z0-9\Q$mark\E"; | ||
| 22 | my $uric = quotemeta($reserved) . $unreserved . "%"; | ||
| 23 | |||
| 24 | sub create_grammar { | ||
| 25 | my $all_blocks = [ | ||
| 26 | qw( | ||
| 27 | pre | ||
| 28 | hr | ||
| 29 | hx | ||
| 30 | ul | ||
| 31 | ol | ||
| 32 | table | ||
| 33 | p | ||
| 34 | empty | ||
| 35 | else | ||
| 36 | ) | ||
| 37 | ]; | ||
| 38 | my $all_phrases = [ | ||
| 39 | qw( | ||
| 40 | tt | ||
| 41 | b | ||
| 42 | i | ||
| 43 | nolink | ||
| 44 | camel | ||
| 45 | hyper | ||
| 46 | force | ||
| 47 | ) | ||
| 48 | ]; | ||
| 49 | |||
| 50 | return { | ||
| 51 | _all_blocks => $all_blocks, | ||
| 52 | _all_phrases => $all_phrases, | ||
| 53 | |||
| 54 | top => { | ||
| 55 | blocks => $all_blocks, | ||
| 56 | }, | ||
| 57 | |||
| 58 | pre => { | ||
| 59 | match => qr/^((?: +\S.*\n)(?:(?: +\S.*\n| *\n)*(?: +\S.*\n))?)/, | ||
| 60 | filter => sub { | ||
| 61 | my $node = shift; | ||
| 62 | while (not /^\S/m) { | ||
| 63 | s/^ //gm; | ||
| 64 | } | ||
| 65 | $node->{text} = $_; | ||
| 66 | }, | ||
| 67 | }, | ||
| 68 | |||
| 69 | hr => { | ||
| 70 | match => qr/^--+(?:\s*\n)?/, | ||
| 71 | }, | ||
| 72 | |||
| 73 | hx => { | ||
| 74 | match => qr/^(=+) *(.*?)(\s+=+)?\s*?\n+/, | ||
| 75 | phrases => $all_phrases, | ||
| 76 | filter => sub { | ||
| 77 | my $node = shift; | ||
| 78 | $node->{type} = 'h' . length($node->{1}); | ||
| 79 | $_ = $node->{text} = $node->{2}; | ||
| 80 | }, | ||
| 81 | }, | ||
| 82 | |||
| 83 | ul => { | ||
| 84 | match => re_list('\*'), | ||
| 85 | blocks => [qw(ul ol subl li)], | ||
| 86 | filter => sub { s/^[\*\0] *//mg }, | ||
| 87 | }, | ||
| 88 | |||
| 89 | ol => { | ||
| 90 | match => re_list('\0'), | ||
| 91 | blocks => [qw(ul ol subl li)], | ||
| 92 | filter => sub { s/^[\*\0] *//mg }, | ||
| 93 | }, | ||
| 94 | |||
| 95 | subl => { | ||
| 96 | type => 'li', | ||
| 97 | |||
| 98 | match => qr/^( # Block must start at beginning | ||
| 99 | # Capture everything in $1 | ||
| 100 | (.*)\n # Capture the whole first line | ||
| 101 | [\*\0]+\ .*\n # Line starting with one or more bullet | ||
| 102 | (?:[\*\0]+\ .*\n)* # Lines starting with '*' or '0' | ||
| 103 | )(?:\s*\n)?/x, # Eat trailing lines | ||
| 104 | blocks => [qw(ul ol li2)], | ||
| 105 | }, | ||
| 106 | |||
| 107 | li => { | ||
| 108 | match => qr/(.*\n)/, # Capture the whole line | ||
| 109 | phrases => $all_phrases, | ||
| 110 | }, | ||
| 111 | |||
| 112 | li2 => { | ||
| 113 | type => '', | ||
| 114 | match => qr/(.*\n)/, # Capture the whole line | ||
| 115 | phrases => $all_phrases, | ||
| 116 | }, | ||
| 117 | |||
| 118 | table => { | ||
| 119 | match => qr/^( | ||
| 120 | ( | ||
| 121 | (?m:^\|.*\|\ \n(?=\|)) | ||
| 122 | | | ||
| 123 | (?m:^\|.*\|\ \ +\n) | ||
| 124 | | | ||
| 125 | (?ms:^\|.*?\|\n) | ||
| 126 | )+ | ||
| 127 | )(?:\s*\n)?/x, | ||
| 128 | blocks => ['tr'], | ||
| 129 | }, | ||
| 130 | |||
| 131 | tr => { | ||
| 132 | match => qr/^((?m:^\|.*?\|(?:\n| \n(?=\|)| +\n)))/s, | ||
| 133 | blocks => ['td'], | ||
| 134 | filter => sub { s/\s+\z// }, | ||
| 135 | }, | ||
| 136 | |||
| 137 | td => { | ||
| 138 | match => qr/\|?\s*(.*?)\s*\|\n?/s, | ||
| 139 | phrases => $all_phrases, | ||
| 140 | }, | ||
| 141 | |||
| 142 | p => { | ||
| 143 | match => qr/^( | ||
| 144 | (?: | ||
| 145 | (?! | ||
| 146 | [\ \#\=\|] | | ||
| 147 | [\*\0]\ | | ||
| 148 | ----\n | ||
| 149 | ) | ||
| 150 | .*\S.*\n | ||
| 151 | )+ | ||
| 152 | ) | ||
| 153 | (\ *\n)* | ||
| 154 | /x, | ||
| 155 | phrases => $all_phrases, | ||
| 156 | filter => sub { s/ +$//gm }, | ||
| 157 | }, | ||
| 158 | |||
| 159 | empty => { | ||
| 160 | match => qr/^(?:#.*|\ *)\n/, | ||
| 161 | filter => sub { | ||
| 162 | my $node = shift; | ||
| 163 | $node->{type} = ''; | ||
| 164 | }, | ||
| 165 | }, | ||
| 166 | |||
| 167 | else => { | ||
| 168 | match => qr/^(.*)\n/, | ||
| 169 | phrases => [], | ||
| 170 | filter => sub { | ||
| 171 | my $node = shift; | ||
| 172 | $node->{type} = 'p'; | ||
| 173 | }, | ||
| 174 | }, | ||
| 175 | |||
| 176 | tt => { | ||
| 177 | match => re_huggy(q{\[\=}, q{\]}), | ||
| 178 | }, | ||
| 179 | |||
| 180 | b => { | ||
| 181 | match => re_huggy(q{\*}), | ||
| 182 | phrases => $all_phrases, | ||
| 183 | }, | ||
| 184 | |||
| 185 | i => { | ||
| 186 | match => re_huggy(q{\/}), | ||
| 187 | phrases => $all_phrases, | ||
| 188 | }, | ||
| 189 | |||
| 190 | nolink => { | ||
| 191 | type => 'text', | ||
| 192 | match => qr/!(\w+)/, | ||
| 193 | }, | ||
| 194 | |||
| 195 | camel => { | ||
| 196 | type => 'link', | ||
| 197 | match => qr/([A-Z][$ALPHANUM]*[a-z][A-Z][$ALPHANUM]*)/, | ||
| 198 | filter => sub { | ||
| 199 | my $node = shift; | ||
| 200 | $node->{attributes}{target} = $node->{1}; | ||
| 201 | }, | ||
| 202 | }, | ||
| 203 | |||
| 204 | hyper => { | ||
| 205 | type => 'link', | ||
| 206 | match => qr{ | ||
| 207 | ( | ||
| 208 | (?:http|https|ftp|irc|file): | ||
| 209 | (?://)? | ||
| 210 | [$uric]+ | ||
| 211 | [A-Za-z0-9/#] | ||
| 212 | ) | ||
| 213 | }x, | ||
| 214 | filter => sub { | ||
| 215 | my $node = shift; | ||
| 216 | $node->{attributes}{target} = $node->{1}; | ||
| 217 | }, | ||
| 218 | }, | ||
| 219 | |||
| 220 | force => { | ||
| 221 | type => 'link', | ||
| 222 | match => qr/\[([^\]]+)\]/, | ||
| 223 | filter => sub { | ||
| 224 | my $node = shift; | ||
| 225 | $node->{attributes}{target} = $node->{1}; | ||
| 226 | }, | ||
| 227 | }, | ||
| 228 | }; | ||
| 229 | } | ||
| 230 | |||
| 231 | sub re_huggy { | ||
| 232 | my $brace1 = shift; | ||
| 233 | my $brace2 = shift || $brace1; | ||
| 234 | |||
| 235 | qr/ | ||
| 236 | (?:^|(?<=[^{$ALPHANUM}$brace1]))$brace1(?=\S)(?!$brace2) | ||
| 237 | (.*?) | ||
| 238 | $brace2(?=[^{$ALPHANUM}$brace2]|\z) | ||
| 239 | /x; | ||
| 240 | } | ||
| 241 | |||
| 242 | sub re_list { | ||
| 243 | my $bullet = shift; | ||
| 244 | return qr/^( # Block must start at beginning | ||
| 245 | # Capture everything in $1 | ||
| 246 | ^$bullet+\ .*\n # Line starting with one or more bullet | ||
| 247 | (?:[\*\0]+\ .*\n)* # Lines starting with '*' or '0' | ||
| 248 | )(?:\s*\n)?/x, # Eat trailing lines | ||
| 249 | } | ||
| 250 | |||
| 251 | 1; |