| 1 | # PurpleWiki::Parser::MoinMoin.pm | ||
| 2 | # vi:ai:sm:et:sw=4:ts=4 | ||
| 3 | # | ||
| 4 | # $Id: MoinMoin.pm 381 2004-05-31 19:21:37Z eekim $ | ||
| 5 | # | ||
| 6 | # Copyright (c) Blue Oxen Associates 2002-2004. All rights reserved. | ||
| 7 | # | ||
| 8 | # This file is part of PurpleWiki. PurpleWiki is derived from: | ||
| 9 | # | ||
| 10 | # UseModWiki v0.92 (c) Clifford A. Adams 2000-2001 | ||
| 11 | # AtisWiki v0.3 (c) Markus Denker 1998 | ||
| 12 | # CVWiki CVS-patches (c) Peter Merel 1997 | ||
| 13 | # The Original WikiWikiWeb (c) Ward Cunningham | ||
| 14 | # | ||
| 15 | # PurpleWiki is free software; you can redistribute it and/or modify | ||
| 16 | # it under the terms of the GNU General Public License as published by | ||
| 17 | # the Free Software Foundation; either version 2 of the License, or | ||
| 18 | # (at your option) any later version. | ||
| 19 | # | ||
| 20 | # This program is distributed in the hope that it will be useful, | ||
| 21 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 22 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 23 | # GNU General Public License for more details. | ||
| 24 | # | ||
| 25 | # You should have received a copy of the GNU General Public License | ||
| 26 | # along with this program; if not, write to the | ||
| 27 | # Free Software Foundation, Inc. | ||
| 28 | # 59 Temple Place, Suite 330 | ||
| 29 | # Boston, MA 02111-1307 USA | ||
| 30 | |||
| 31 | package PurpleWiki::Parser::MoinMoin; | ||
| 32 | |||
| 33 | use 5.005; | ||
| 34 | use strict; | ||
| 35 | vuse PurpleWiki::Config; | ||
| 36 | use PurpleWiki::InlineNode; | ||
| 37 | use PurpleWiki::StructuralNode; | ||
| 38 | use PurpleWiki::Tree; | ||
| 39 | use PurpleWiki::Sequence; | ||
| 40 | use PurpleWiki::Page; | ||
| 41 | |||
| 42 | our $VERSION; | ||
| 43 | $VERSION = sprintf("%d", q$Id: MoinMoin.pm 381 2004-05-31 19:21:37Z eekim $ =~ /\s(\d+)\s/); | ||
| 44 | |||
| 45 | my $sequence; | ||
| 46 | my $url; | ||
| 47 | |||
| 48 | ### markup regular expressions | ||
| 49 | my $rxNowiki = '<nowiki>.*?<\/nowiki>'; | ||
| 50 | my $rxTt = '<tt>.*?<\/tt>|\{\{\{.*?\}\}\}'; | ||
| 51 | my $rxFippleQuotes = "'''''.*?'''''"; | ||
| 52 | my $rxB = '<b>.*?<\/b>'; | ||
| 53 | my $rxTripleQuotes = "'''.*?'''"; | ||
| 54 | my $rxI = '<i>.*?<\/i>'; | ||
| 55 | my $rxDoubleQuotes = "''.*?''"; | ||
| 56 | |||
| 57 | ### link regular expressions | ||
| 58 | my $rxAddress = '[^]\s]*[\w/]'; | ||
| 59 | my $rxProtocols = '(?i:http|https|ftp|afs|news|mid|cid|nntp|mailto|wais):'; | ||
| 60 | my $rxWikiWord = '[A-Z]+[a-z]+[A-Z]\w*'; | ||
| 61 | my $rxSubpage = '[A-Z]+[a-z]+\w*'; | ||
| 62 | my $rxQuoteDelim = '(?:"")?'; | ||
| 63 | my $rxFreeLink = '\["[\w\/][\w\/\s]+"\]'; | ||
| 64 | my $rxTransclusion = '\[t [A-Z0-9]+\]'; | ||
| 65 | |||
| 66 | ### constructor | ||
| 67 | |||
| 68 | sub new { | ||
| 69 | my $this = shift; | ||
| 70 | my $self = {}; | ||
| 71 | |||
| 72 | bless($self, $this); | ||
| 73 | return $self; | ||
| 74 | } | ||
| 75 | |||
| 76 | ### methods | ||
| 77 | |||
| 78 | sub parse { | ||
| 79 | my $this = shift; | ||
| 80 | my $wikiContent = shift; | ||
| 81 | my %params = @_; | ||
| 82 | |||
| 83 | $params{config} = PurpleWiki::Config->instance(); | ||
| 84 | |||
| 85 | $url = $params{url}; | ||
| 86 | $sequence = new PurpleWiki::Sequence($params{config}->LocalSequenceDir, | ||
| 87 | $params{config}->RemoteSequenceURL); | ||
| 88 | |||
| 89 | # set default parameters | ||
| 90 | $params{wikiword} = $params{config}->WikiLinks | ||
| 91 | if (!defined $params{wikiword}); | ||
| 92 | $params{freelink} = $params{config}->FreeLinks | ||
| 93 | if (!defined $params{freelink}); | ||
| 94 | |||
| 95 | my $tree = PurpleWiki::Tree->new; | ||
| 96 | my ($currentNode, @sectionState, $nodeContent); | ||
| 97 | my ($listLength, $sectionLength, $indentLength); | ||
| 98 | my ($line, $listType, $currentNid); | ||
| 99 | my (@authors); | ||
| 100 | |||
| 101 | # @whiteSpace is used to keep track of indentations before lists | ||
| 102 | # and indented text. | ||
| 103 | my @whiteSpace; | ||
| 104 | |||
| 105 | my %listMap = ('ul' => '(\*)\s*(.*)', | ||
| 106 | 'ol' => '([1ia]\.)\s*(.*)', | ||
| 107 | 'dl' => '([^\:]+)\:\:\s+(.*)', | ||
| 108 | ); | ||
| 109 | |||
| 110 | my $aggregateListRegExp = join('|', values(%listMap)); | ||
| 111 | |||
| 112 | # The parsing strategy is as follows. First, process the text | ||
| 113 | # line-by-line, updating state variables as we go. At certain | ||
| 114 | # termination points, we update the data structure based on the | ||
| 115 | # state variables. | ||
| 116 | # | ||
| 117 | # The top of the text is parsed differently from the rest of the | ||
| 118 | # text. If there is metadata (delimited by braces), then the | ||
| 119 | # metadata is parsed; otherwise, the parser begins parsing for | ||
| 120 | # WikiText. Once it reaches this latter stage, it will treat | ||
| 121 | # metadata as WikiText according to the basic parsing rules. | ||
| 122 | # | ||
| 123 | # STATE VARIABLES | ||
| 124 | # | ||
| 125 | # The state variables we care about and why are: | ||
| 126 | # | ||
| 127 | # $isStart -- Determines whether we're at the top of the text | ||
| 128 | # (and should be parsing metadata) or not. | ||
| 129 | # | ||
| 130 | # @authors -- List of authors (defined in the metadata). This | ||
| 131 | # list is added to the tree structure's metadata once parsing | ||
| 132 | # is complete. | ||
| 133 | # | ||
| 134 | # $listDepth -- The depth of the current list (unordered, | ||
| 135 | # ordered, or definition) being parsed. Starts at 0. | ||
| 136 | # | ||
| 137 | # $indentDepth -- The depth of the current indented text (begins | ||
| 138 | # with a colon) being parsed. Starts at 0. | ||
| 139 | # | ||
| 140 | # $sectionDepth -- Depth of the current section. We start with | ||
| 141 | # a depth of 1, because we assume that the body text begins | ||
| 142 | # with a section. (See below for more detailed explanation.) | ||
| 143 | # | ||
| 144 | # $nodeContent -- Textual content of current node. | ||
| 145 | # | ||
| 146 | # $currentNode -- The current node. The initial node when we | ||
| 147 | # start parsing is the first section node. | ||
| 148 | # | ||
| 149 | # SECTIONS | ||
| 150 | # | ||
| 151 | # A document consists of sections. Each section may consist of | ||
| 152 | # subsections, ad infinitum. | ||
| 153 | # | ||
| 154 | # New sections are delimited by header markup (equal signs) and | ||
| 155 | # hard rules (four dashes). The depth of the header markup | ||
| 156 | # determines whether or not the section is a new section, a | ||
| 157 | # subsection, or in some cases, a supersection (e.g. a two equal | ||
| 158 | # sign header following a three equal sign header). | ||
| 159 | # | ||
| 160 | # A hard rule indicates a new section. Ideally, I'd like to make | ||
| 161 | # it so that the hard rule is ignored if it is directly followed | ||
| 162 | # by a header. If users want to have hard rules separating | ||
| 163 | # sections (i.e. separating headers), they should define that in | ||
| 164 | # the stylesheet instead of using hard rules. Expecting this | ||
| 165 | # behavior from users is probably a bit too draconian, however, | ||
| 166 | # and there may in fact be times when this is indeed the desired | ||
| 167 | # behavior (for example, as a placeholder for future content). | ||
| 168 | # The resulting data structure from a series of hard rules | ||
| 169 | # followed by headers won't be "clean," but it shouldn't be | ||
| 170 | # harmful either. | ||
| 171 | |||
| 172 | my $isStart = 1; | ||
| 173 | my $isBracePre = 0; | ||
| 174 | my $prevIndentType; | ||
| 175 | my $isPrevBlank = 0; | ||
| 176 | my @authors = (); | ||
| 177 | my $listDepth = 0; | ||
| 178 | my $indentDepth = 0; | ||
| 179 | my $sectionDepth = 1; | ||
| 180 | |||
| 181 | $currentNode = $tree->root->insertChild('type' => 'section'); | ||
| 182 | |||
| 183 | foreach $line (split(/\n/, $wikiContent)) { # Process lines one-at-a-time | ||
| 184 | chomp $line; | ||
| 185 | if ($isStart && $line =~ /^\{title (.+)\}$/) { | ||
| 186 | # The metadata below is not (currently) used by the | ||
| 187 | # Wiki. It's here to so that this parser can be used | ||
| 188 | # as a general documentation formatting system. | ||
| 189 | $tree->title($1); | ||
| 190 | } | ||
| 191 | elsif ($isStart && $line =~ /^\{subtitle (.+)\}$/) { | ||
| 192 | # See above. | ||
| 193 | $tree->subtitle($1); | ||
| 194 | } | ||
| 195 | elsif ($isStart && $line =~ /^\{docid (.+)\}$/) { | ||
| 196 | # See above. | ||
| 197 | $tree->id($1); | ||
| 198 | } | ||
| 199 | elsif ($isStart && $line =~ /^\{date (.+)\}$/) { | ||
| 200 | # See above. | ||
| 201 | $tree->date($1); | ||
| 202 | } | ||
| 203 | elsif ($isStart && $line =~ /^\{version (.+)\}$/) { | ||
| 204 | # See above. | ||
| 205 | $tree->version($1); | ||
| 206 | } | ||
| 207 | elsif ($isStart && $line =~ /^\{author (.+)\}$/) { | ||
| 208 | # See above. | ||
| 209 | my $authorString = $1; | ||
| 210 | $authorString =~ s/\s+(\S+\@\S+)$//; | ||
| 211 | my $authorEmail = $1 if ($1 ne $authorString); | ||
| 212 | if ($authorEmail) { | ||
| 213 | push @authors, [$authorString, $authorEmail]; | ||
| 214 | } | ||
| 215 | else { | ||
| 216 | push @authors, [$authorString]; | ||
| 217 | } | ||
| 218 | } | ||
| 219 | elsif ($line =~ /^\{sketch\}$/) { # WikiWhiteboard | ||
| 220 | $currentNode->insertChild(type=>'sketch'); | ||
| 221 | } | ||
| 222 | elsif ($line =~ /^----*$/) { # new section | ||
| 223 | $currentNode = &_terminateNode($currentNode, \$nodeContent, | ||
| 224 | %params); | ||
| 225 | $currentNode = &_resetList($currentNode, \$listDepth, undef); | ||
| 226 | @whiteSpace = (); | ||
| 227 | $prevIndentType = undef; | ||
| 228 | $currentNode = $currentNode->parent; | ||
| 229 | $currentNode = $currentNode->insertChild(type=>'section'); | ||
| 230 | $isPrevBlank = 0; | ||
| 231 | } | ||
| 232 | elsif ($line =~ /^(\=+)\s+(.+)\s+\=+/) { # header/section | ||
| 233 | $currentNode = &_terminateNode($currentNode, \$nodeContent, | ||
| 234 | %params); | ||
| 235 | $currentNode = &_resetList($currentNode, \$listDepth, \$indentDepth); | ||
| 236 | @whiteSpace = (); | ||
| 237 | $prevIndentType = undef; | ||
| 238 | $sectionLength = length $1; | ||
| 239 | $nodeContent = $2; | ||
| 240 | if ($sectionLength > $sectionDepth) { | ||
| 241 | while ($sectionLength > $sectionDepth) { | ||
| 242 | $currentNode = $currentNode->insertChild(type=>'section'); | ||
| 243 | $sectionDepth++; | ||
| 244 | } | ||
| 245 | } | ||
| 246 | else { | ||
| 247 | while ($sectionLength < $sectionDepth) { | ||
| 248 | $currentNode = $currentNode->parent; | ||
| 249 | $sectionDepth--; | ||
| 250 | } | ||
| 251 | if ( !$isStart && ($sectionLength == $sectionDepth) ) { | ||
| 252 | $currentNode = $currentNode->parent; | ||
| 253 | $currentNode = $currentNode->insertChild(type=>'section'); | ||
| 254 | } | ||
| 255 | } | ||
| 256 | $nodeContent =~ s/\s+\{nid ([A-Z0-9]+)\}$//s; | ||
| 257 | $currentNid = $1; | ||
| 258 | $currentNode = $currentNode->insertChild('type'=>'h', | ||
| 259 | 'content'=>&_parseInlineNode($nodeContent, %params)); | ||
| 260 | if (defined $currentNid && ($currentNid =~ /^[A-Z0-9]+$/)) { | ||
| 261 | $currentNode->id($currentNid); | ||
| 262 | } | ||
| 263 | $currentNode = $currentNode->parent; | ||
| 264 | undef $nodeContent; | ||
| 265 | $isPrevBlank = 0; | ||
| 266 | $isStart = 0 if ($isStart); | ||
| 267 | } | ||
| 268 | elsif ($line =~ /^(\s+)(\S.*)$/) { # list or indentation | ||
| 269 | my $wsLen = length $1; | ||
| 270 | my $string = $2; | ||
| 271 | |||
| 272 | if ($isBracePre) { | ||
| 273 | $nodeContent .= "$line\n"; | ||
| 274 | } | ||
| 275 | elsif ( ($string =~ /^[\*]/) || ($string =~ /^[1ia]\./) || | ||
| 276 | ($string =~ /^[^\:]+\:\:\s+/) ) { # new list item | ||
| 277 | foreach $listType (keys(%listMap)) { | ||
| 278 | if ($string =~ /^$listMap{$listType}$/x) { | ||
| 279 | $currentNode = &_terminateNode($currentNode, | ||
| 280 | \$nodeContent, | ||
| 281 | %params); | ||
| 282 | if ($isPrevBlank && $prevIndentType ne $listType) { | ||
| 283 | $currentNode = &_resetList($currentNode, \$listDepth, undef); | ||
| 284 | @whiteSpace = (); | ||
| 285 | $prevIndentType = $listType; | ||
| 286 | } | ||
| 287 | while ($indentDepth > 0) { | ||
| 288 | $currentNode = $currentNode->parent; | ||
| 289 | $indentDepth--; | ||
| 290 | } | ||
| 291 | my @listContents; | ||
| 292 | if ($listType eq 'dl') { | ||
| 293 | @listContents = ($1, $2); | ||
| 294 | } | ||
| 295 | else { | ||
| 296 | @listContents = ($2); | ||
| 297 | } | ||
| 298 | if ( (scalar @whiteSpace && | ||
| 299 | $wsLen > $whiteSpace[$#whiteSpace]) || | ||
| 300 | !(scalar @whiteSpace) ) { | ||
| 301 | push @whiteSpace, $wsLen; | ||
| 302 | } | ||
| 303 | else { | ||
| 304 | while (scalar @whiteSpace && | ||
| 305 | $wsLen < $whiteSpace[$#whiteSpace]) { | ||
| 306 | pop @whiteSpace; | ||
| 307 | } | ||
| 308 | push @whiteSpace, $wsLen | ||
| 309 | if (!scalar @whiteSpace); | ||
| 310 | } | ||
| 311 | $listLength = scalar @whiteSpace; | ||
| 312 | $currentNode = &_parseList($listType, $listLength, | ||
| 313 | \$listDepth, | ||
| 314 | $currentNode, | ||
| 315 | \%params, \$nodeContent, | ||
| 316 | @listContents); | ||
| 317 | } | ||
| 318 | } | ||
| 319 | } | ||
| 320 | else { # indented paragraph or li/p continuation | ||
| 321 | if ($wsLen == $whiteSpace[$#whiteSpace] && !$isPrevBlank) { # li/p continuation | ||
| 322 | $nodeContent .= "$string\n"; | ||
| 323 | } | ||
| 324 | else { | ||
| 325 | $currentNode = &_terminateNode($currentNode, \$nodeContent, | ||
| 326 | %params); | ||
| 327 | if ($prevIndentType ne 'indent') { | ||
| 328 | $currentNode = &_resetList($currentNode, \$listDepth, undef); | ||
| 329 | @whiteSpace = (); | ||
| 330 | $prevIndentType = 'indent'; | ||
| 331 | } | ||
| 332 | if ( (scalar @whiteSpace && | ||
| 333 | $wsLen > $whiteSpace[$#whiteSpace]) || | ||
| 334 | !(scalar @whiteSpace) ) { | ||
| 335 | push @whiteSpace, $wsLen; | ||
| 336 | } | ||
| 337 | else { | ||
| 338 | while (scalar @whiteSpace && | ||
| 339 | $wsLen < $whiteSpace[$#whiteSpace]) { | ||
| 340 | pop @whiteSpace; | ||
| 341 | } | ||
| 342 | push @whiteSpace, $wsLen | ||
| 343 | if (!scalar @whiteSpace); | ||
| 344 | } | ||
| 345 | $listLength = scalar @whiteSpace; | ||
| 346 | while ($listLength > $indentDepth) { | ||
| 347 | $currentNode = $currentNode->insertChild('type'=>'indent'); | ||
| 348 | $indentDepth++; | ||
| 349 | } | ||
| 350 | while ($listLength < $indentDepth) { | ||
| 351 | $currentNode = $currentNode->parent; | ||
| 352 | $indentDepth--; | ||
| 353 | } | ||
| 354 | $currentNode = $currentNode->insertChild('type'=>'p'); | ||
| 355 | if ($string =~ /\s*\{\{\{\s*$/) { | ||
| 356 | $string =~ s/\s*\{\{\{\s*$//; | ||
| 357 | $nodeContent .= "$string\n"; | ||
| 358 | $currentNode = &_terminateNode($currentNode, | ||
| 359 | \$nodeContent, | ||
| 360 | %params); | ||
| 361 | $currentNode = &_resetList($currentNode, \$listDepth, \$indentDepth); | ||
| 362 | @whiteSpace = (); | ||
| 363 | $prevIndentType = undef; | ||
| 364 | $isBracePre = 1; | ||
| 365 | $currentNode = $currentNode->insertChild(type => 'pre'); | ||
| 366 | } | ||
| 367 | else { | ||
| 368 | $nodeContent .= "$string\n"; | ||
| 369 | } | ||
| 370 | } | ||
| 371 | } | ||
| 372 | $isPrevBlank = 0; | ||
| 373 | $isStart = 0 if ($isStart); | ||
| 374 | } | ||
| 375 | elsif ($line =~ /^\{\{\{\s*$/) { # MoinMoin-style pre | ||
| 376 | |||
| 377 | # If there's already a pre, this has the effect of closing | ||
| 378 | # the previous one and starting a new one. For example: | ||
| 379 | # | ||
| 380 | # = Header = | ||
| 381 | # | ||
| 382 | # indented (hence preformatted) | ||
| 383 | # {{{ | ||
| 384 | # hello world! | ||
| 385 | # }}} | ||
| 386 | # | ||
| 387 | # will result in: | ||
| 388 | # | ||
| 389 | # section: | ||
| 390 | # h:Header | ||
| 391 | # pre: indented (hence preformatted) | ||
| 392 | # pre:hello world! | ||
| 393 | # | ||
| 394 | # This also creates an unusual side-effect: | ||
| 395 | # | ||
| 396 | # {{{ | ||
| 397 | # hello world! | ||
| 398 | # {{{ | ||
| 399 | # hello again. | ||
| 400 | # }}} | ||
| 401 | # | ||
| 402 | # which results in: | ||
| 403 | # | ||
| 404 | # section: | ||
| 405 | # h:Header | ||
| 406 | # pre:hello world! | ||
| 407 | # pre:hello again. | ||
| 408 | # | ||
| 409 | # In other words, the first {{{ did not need closing. | ||
| 410 | |||
| 411 | $currentNode = &_resetList($currentNode, \$listDepth, \$indentDepth); | ||
| 412 | @whiteSpace = (); | ||
| 413 | $prevIndentType = undef; | ||
| 414 | $currentNode = &_terminateNode($currentNode, | ||
| 415 | \$nodeContent, | ||
| 416 | %params); | ||
| 417 | $currentNode = $currentNode->insertChild('type'=>'pre'); | ||
| 418 | $isStart = 0 if ($isStart); | ||
| 419 | $isPrevBlank = 0; | ||
| 420 | $isBracePre = 1; | ||
| 421 | } | ||
| 422 | elsif ($line =~ /^\s*\}\}\}\s*$/) { # close MoinMoin pre | ||
| 423 | if ($currentNode->type eq 'pre') { | ||
| 424 | $currentNode = &_terminateNode($currentNode, | ||
| 425 | \$nodeContent, | ||
| 426 | %params); | ||
| 427 | $nodeContent = $1; | ||
| 428 | $isBracePre = 0; | ||
| 429 | } | ||
| 430 | else { | ||
| 431 | # just make it part of the text | ||
| 432 | $nodeContent .= $line; | ||
| 433 | } | ||
| 434 | $isPrevBlank = 0; | ||
| 435 | $isStart = 0 if ($isStart); | ||
| 436 | } | ||
| 437 | elsif ($line =~ /^\s*$/) { # blank line | ||
| 438 | if ($isBracePre) { | ||
| 439 | $nodeContent .= "\n"; | ||
| 440 | } | ||
| 441 | else { | ||
| 442 | $currentNode = &_terminateNode($currentNode, \$nodeContent, | ||
| 443 | %params); | ||
| 444 | $isPrevBlank = 1; | ||
| 445 | } | ||
| 446 | } | ||
| 447 | else { | ||
| 448 | if (!($currentNode->type eq 'p' && $indentDepth == 0) && | ||
| 449 | (!$isBracePre)) { | ||
| 450 | $currentNode = &_terminateNode($currentNode, | ||
| 451 | \$nodeContent, | ||
| 452 | %params); | ||
| 453 | $currentNode = &_resetList($currentNode, \$listDepth, \$indentDepth); | ||
| 454 | @whiteSpace = (); | ||
| 455 | $prevIndentType = undef; | ||
| 456 | $currentNode = $currentNode->insertChild('type'=>'p'); | ||
| 457 | } | ||
| 458 | if ($line =~ /\s*\{\{\{\s*$/) { | ||
| 459 | $line =~ s/\s*\{\{\{\s*$//; | ||
| 460 | $nodeContent .= "$line\n"; | ||
| 461 | $currentNode = &_terminateNode($currentNode, | ||
| 462 | \$nodeContent, | ||
| 463 | %params); | ||
| 464 | $currentNode = &_resetList($currentNode, \$listDepth, \$indentDepth); | ||
| 465 | @whiteSpace = (); | ||
| 466 | $prevIndentType = undef; | ||
| 467 | $isBracePre = 1; | ||
| 468 | $currentNode = $currentNode->insertChild(type => 'pre'); | ||
| 469 | } | ||
| 470 | else { | ||
| 471 | $nodeContent .= "$line\n"; | ||
| 472 | } | ||
| 473 | $isPrevBlank = 0; | ||
| 474 | $isStart = 0 if ($isStart); | ||
| 475 | } | ||
| 476 | } | ||
| 477 | $currentNode = &_terminateNode($currentNode, \$nodeContent, | ||
| 478 | %params); | ||
| 479 | if (scalar @authors > 0) { | ||
| 480 | $tree->authors(\@authors); | ||
| 481 | } | ||
| 482 | |||
| 483 | if ($params{'add_node_ids'}) { | ||
| 484 | &_addNodeIds($tree->root); | ||
| 485 | } | ||
| 486 | return $tree; | ||
| 487 | } | ||
| 488 | |||
| 489 | ### private | ||
| 490 | |||
| 491 | sub _resetList { | ||
| 492 | # "Resets" lists. When a new node is about to be created, this | ||
| 493 | # routine makes sure that any previous lists or indentations are | ||
| 494 | # reset to the correct nesting depth. | ||
| 495 | |||
| 496 | my ($currentNode, $listDepthRef, $indentDepthRef) = @_; | ||
| 497 | |||
| 498 | if ($listDepthRef && ${$listDepthRef}) { | ||
| 499 | while (${$listDepthRef} > 1) { | ||
| 500 | $currentNode = $currentNode->parent->parent; | ||
| 501 | ${$listDepthRef}--; | ||
| 502 | } | ||
| 503 | $currentNode = $currentNode->parent; | ||
| 504 | ${$listDepthRef} = 0; | ||
| 505 | } | ||
| 506 | if ($indentDepthRef) { | ||
| 507 | while (${$indentDepthRef} > 0) { | ||
| 508 | $currentNode = $currentNode->parent; | ||
| 509 | ${$indentDepthRef}--; | ||
| 510 | } | ||
| 511 | } | ||
| 512 | return $currentNode; | ||
| 513 | } | ||
| 514 | |||
| 515 | sub _terminateNode { | ||
| 516 | # "Closes" nodes. When the parser knows that it is ready to add | ||
| 517 | # content to the node (i.e. when it sees a blank line), this | ||
| 518 | # routine does the adding. | ||
| 519 | |||
| 520 | my ($currentNode, $nodeContentRef, %params) = @_; | ||
| 521 | my ($currentNid); | ||
| 522 | |||
| 523 | if (($currentNode->type eq 'p') || ($currentNode->type eq 'pre') || | ||
| 524 | ($currentNode->type eq 'li') || ($currentNode->type eq 'dd')) { | ||
| 525 | chomp ${$nodeContentRef}; | ||
| 526 | ${$nodeContentRef} =~ s/\s+\{nid ([A-Z0-9]+)\}$//s; | ||
| 527 | $currentNid = $1; | ||
| 528 | if (defined $currentNid && ($currentNid =~ /^[A-Z0-9]+$/)) { | ||
| 529 | $currentNode->id($currentNid); | ||
| 530 | } | ||
| 531 | if ($currentNode->type eq 'pre') { | ||
| 532 | ${$nodeContentRef} = '<nowiki>' . ${$nodeContentRef} . '</nowiki>'; | ||
| 533 | } | ||
| 534 | $currentNode->content(&_parseInlineNode(${$nodeContentRef}, %params)); | ||
| 535 | undef ${$nodeContentRef}; | ||
| 536 | return $currentNode->parent; | ||
| 537 | } | ||
| 538 | return $currentNode; | ||
| 539 | } | ||
| 540 | |||
| 541 | sub _parseList { | ||
| 542 | # List parsing is mostly handled the same, which is why it gets | ||
| 543 | # its own subroutine. The main thing this routine does is handle | ||
| 544 | # the nesting properly. | ||
| 545 | |||
| 546 | my ($listType, $listLength, $listDepthRef, | ||
| 547 | $currentNode, $paramRef, $nodeContentRef, | ||
| 548 | @nodeContents) = @_; | ||
| 549 | |||
| 550 | while ($listLength > ${$listDepthRef}) { | ||
| 551 | # Nested lists are children of list items, not of other lists. | ||
| 552 | # We need to find the last list item (if it exists) and | ||
| 553 | # create a sublist there; otherwise, we need to create a list | ||
| 554 | # item and give it a sublist. | ||
| 555 | if ($currentNode->type eq 'ul' || $currentNode->type eq 'ol' || | ||
| 556 | $currentNode->type eq 'dl') { | ||
| 557 | my $kidsRef = $currentNode->children; | ||
| 558 | if ($kidsRef) { | ||
| 559 | $currentNode = $kidsRef->[scalar @{$kidsRef} - 1]; | ||
| 560 | } | ||
| 561 | else { | ||
| 562 | if ($listType eq 'dl') { | ||
| 563 | $currentNode = $currentNode->insertChild(type=>'dd'); | ||
| 564 | } | ||
| 565 | else { | ||
| 566 | $currentNode = $currentNode->insertChild(type=>'li'); | ||
| 567 | } | ||
| 568 | } | ||
| 569 | } | ||
| 570 | $currentNode = $currentNode->insertChild(type=>$listType); | ||
| 571 | ${$listDepthRef}++; | ||
| 572 | } | ||
| 573 | while ($listLength < ${$listDepthRef}) { # assert($listLength != 0) | ||
| 574 | $currentNode = $currentNode->parent->parent; | ||
| 575 | ${$listDepthRef}--; | ||
| 576 | } | ||
| 577 | if ($listType eq 'dl') { | ||
| 578 | $nodeContents[0] =~ s/\s+\{nid ([A-Z0-9]+)\}$//s; | ||
| 579 | my $currentNid = $1; | ||
| 580 | $currentNode = $currentNode->insertChild(type=>'dt', | ||
| 581 | content=>&_parseInlineNode($nodeContents[0], %{$paramRef})); | ||
| 582 | if (defined $currentNid && ($currentNid =~ /^[A-Z0-9]+$/)) { | ||
| 583 | $currentNode->id($currentNid); | ||
| 584 | } | ||
| 585 | $currentNode = $currentNode->parent; | ||
| 586 | ${$nodeContentRef} = $nodeContents[1] . "\n"; | ||
| 587 | $currentNode = $currentNode->insertChild('type'=>'dd'); | ||
| 588 | } | ||
| 589 | else { | ||
| 590 | ${$nodeContentRef} = $nodeContents[0] . "\n"; | ||
| 591 | $currentNode = $currentNode->insertChild('type'=>'li'); | ||
| 592 | } | ||
| 593 | return $currentNode; | ||
| 594 | } | ||
| 595 | |||
| 596 | sub _parseInlineNode { | ||
| 597 | my ($text, %params) = @_; | ||
| 598 | my (@inlineNodes); | ||
| 599 | |||
| 600 | # This used to be an extended regular expression, but it wasn't | ||
| 601 | # working in some cases. | ||
| 602 | my $rx = qq{$rxNowiki|$rxTransclusion|$rxTt|$rxFippleQuotes|$rxB|}; | ||
| 603 | $rx .= qq{$rxTripleQuotes|$rxI|$rxDoubleQuotes|}; | ||
| 604 | $rx .= qq{\\\[$rxProtocols$rxAddress\\s+.+?\\\]|$rxProtocols$rxAddress}; | ||
| 605 | if ($params{wikiword}) { | ||
| 606 | $rx .= qq{|(?:$rxWikiWord)?\\\/$rxSubpage(?:\\\#[A-Z0-9]+)?}; | ||
| 607 | $rx .= qq{$rxQuoteDelim|[A-Z]\\w+:[^\\\]\\\#\\s"<>\:]+}; | ||
| 608 | $rx .= qq{(?:\\\#[A-Z0-9]+)?$rxQuoteDelim|$rxWikiWord}; | ||
| 609 | $rx .= qq{(?:\\\#[A-Z0-9]+)?$rxQuoteDelim}; | ||
| 610 | } | ||
| 611 | if ($params{freelink}) { | ||
| 612 | $rx .= qq{|$rxFreeLink}; | ||
| 613 | } | ||
| 614 | my @nodes = split(/($rx)/s, $text); | ||
| 615 | foreach my $node (@nodes) { | ||
| 616 | if ($node =~ /^$rxNowiki$/s) { | ||
| 617 | $node =~ s/^<nowiki>//; | ||
| 618 | $node =~ s/<\/nowiki>$//; | ||
| 619 | push @inlineNodes, PurpleWiki::InlineNode->new('type'=>'nowiki', | ||
| 620 | 'content'=>$node); | ||
| 621 | } | ||
| 622 | elsif ($node =~ /^$rxTransclusion$/s) { | ||
| 623 | # transclusion | ||
| 624 | my ($content) = ($node =~ /([A-Z0-9]+)/); | ||
| 625 | push @inlineNodes, PurpleWiki::InlineNode->new( | ||
| 626 | 'type' => 'transclusion', | ||
| 627 | 'content' => $content); | ||
| 628 | } | ||
| 629 | elsif ($node =~ /^$rxTt$/s) { | ||
| 630 | $node =~ s/^<tt>//; | ||
| 631 | $node =~ s/<\/tt>$//; | ||
| 632 | $node =~ s/^\{\{\{//; | ||
| 633 | $node =~ s/\}\}\}$//; | ||
| 634 | $node = "<nowiki>$node</nowiki>"; | ||
| 635 | push @inlineNodes, PurpleWiki::InlineNode->new('type'=>'tt', | ||
| 636 | 'children'=>&_parseInlineNode($node, %params)); | ||
| 637 | } | ||
| 638 | elsif ($node =~ /^$rxFippleQuotes$/s) { | ||
| 639 | $node =~ s/^'''//; | ||
| 640 | $node =~ s/'''$//; | ||
| 641 | push @inlineNodes, PurpleWiki::InlineNode->new('type'=>'b', | ||
| 642 | 'children'=>&_parseInlineNode($node, %params)); | ||
| 643 | } | ||
| 644 | elsif ($node =~ /^$rxB$/s) { | ||
| 645 | $node =~ s/^<b>//; | ||
| 646 | $node =~ s/<\/b>$//; | ||
| 647 | push @inlineNodes, PurpleWiki::InlineNode->new('type'=>'b', | ||
| 648 | 'children'=>&_parseInlineNode($node, %params)); | ||
| 649 | } | ||
| 650 | elsif ($node =~ /^$rxTripleQuotes$/s) { | ||
| 651 | $node =~ s/^'''//; | ||
| 652 | $node =~ s/'''$//; | ||
| 653 | push @inlineNodes, PurpleWiki::InlineNode->new('type'=>'b', | ||
| 654 | 'children'=>&_parseInlineNode($node, %params)); | ||
| 655 | } | ||
| 656 | elsif ($node =~ /^$rxI$/s) { | ||
| 657 | $node =~ s/^<i>//; | ||
| 658 | $node =~ s/<\/i>$//; | ||
| 659 | push @inlineNodes, PurpleWiki::InlineNode->new('type'=>'i', | ||
| 660 | 'children'=>&_parseInlineNode($node, %params)); | ||
| 661 | } | ||
| 662 | elsif ($node =~ /^$rxDoubleQuotes$/s) { | ||
| 663 | $node =~ s/^''//; | ||
| 664 | $node =~ s/''$//; | ||
| 665 | push @inlineNodes, PurpleWiki::InlineNode->new('type'=>'i', | ||
| 666 | 'children'=>&_parseInlineNode($node, %params)); | ||
| 667 | } | ||
| 668 | elsif ($node =~ /^\[($rxProtocols$rxAddress)\s*(.*?)\]$/s) { | ||
| 669 | # bracketed link | ||
| 670 | push @inlineNodes, PurpleWiki::InlineNode->new('type'=>'link', | ||
| 671 | 'href'=>$1, | ||
| 672 | 'content'=>$2); | ||
| 673 | } | ||
| 674 | elsif ($node =~ /^$rxProtocols$rxAddress$/s) { | ||
| 675 | # URL | ||
| 676 | if ($node =~ /\.(?:jpg|gif|png|bmp|jpeg)$/i) { | ||
| 677 | push @inlineNodes, | ||
| 678 | PurpleWiki::InlineNode->new('type'=>'image', | ||
| 679 | 'href'=>$node, | ||
| 680 | 'content'=>$node); | ||
| 681 | } | ||
| 682 | else { | ||
| 683 | push @inlineNodes, | ||
| 684 | PurpleWiki::InlineNode->new('type'=>'url', | ||
| 685 | 'href'=>$node, | ||
| 686 | 'content'=>$node); | ||
| 687 | } | ||
| 688 | } | ||
| 689 | elsif ($params{freelink} && ($node =~ /$rxFreeLink/s)) { | ||
| 690 | $node =~ s/^\[\"//; | ||
| 691 | $node =~ s/\"\]$//; | ||
| 692 | push @inlineNodes, PurpleWiki::InlineNode->new('type'=>'freelink', | ||
| 693 | 'content'=>$node); | ||
| 694 | } | ||
| 695 | elsif ($params{wikiword} && | ||
| 696 | ($node =~ /^(?:$rxWikiWord)?\/$rxSubpage(?:\#[A-Z0-9]+)?$rxQuoteDelim$/s)) { | ||
| 697 | $node =~ s/""$//; | ||
| 698 | push @inlineNodes, PurpleWiki::InlineNode->new('type'=>'wikiword', | ||
| 699 | 'content'=>$node); | ||
| 700 | } | ||
| 701 | elsif ($params{wikiword} && | ||
| 702 | ($node =~ /^([A-Z]\w+):([^\]\#\:\s"<>]+(?:\#[A-Z0-9]+)?)$rxQuoteDelim$/s)) { | ||
| 703 | my $site = $1; | ||
| 704 | my $page = $2; | ||
| 705 | if (&PurpleWiki::Page::siteExists($site)) { | ||
| 706 | $node =~ s/""$//; | ||
| 707 | push @inlineNodes, | ||
| 708 | PurpleWiki::InlineNode->new('type'=>'wikiword', | ||
| 709 | 'content'=>$node); | ||
| 710 | } | ||
| 711 | else { | ||
| 712 | if ($site =~ /^$rxWikiWord$/) { | ||
| 713 | push @inlineNodes, | ||
| 714 | PurpleWiki::InlineNode->new('type'=>'wikiword', | ||
| 715 | 'content'=>$site); | ||
| 716 | if ( ($page =~ /^$rxWikiWord(?:\#[A-Z0-9]+)?$/) || | ||
| 717 | ($page =~ /^$rxWikiWord\/$rxSubpage(?:\#[A-Z0-9]+)?$/) ) { | ||
| 718 | push @inlineNodes, | ||
| 719 | PurpleWiki::InlineNode->new('type'=>'text', | ||
| 720 | 'content'=>':'); | ||
| 721 | push @inlineNodes, | ||
| 722 | PurpleWiki::InlineNode->new('type'=>'wikiword', | ||
| 723 | 'content'=>$page); | ||
| 724 | } | ||
| 725 | elsif ($page =~ /(.*)(\/$rxSubpage(?:\#[A-Z0-9]+)?)$/) { | ||
| 726 | push @inlineNodes, | ||
| 727 | PurpleWiki::InlineNode->new('type'=>'text', | ||
| 728 | 'content'=>":$1"); | ||
| 729 | push @inlineNodes, | ||
| 730 | PurpleWiki::InlineNode->new('type'=>'wikiword', | ||
| 731 | 'content'=>$2); | ||
| 732 | } | ||
| 733 | else { | ||
| 734 | push @inlineNodes, | ||
| 735 | PurpleWiki::InlineNode->new('type'=>'text', | ||
| 736 | 'content'=>":$page"); | ||
| 737 | } | ||
| 738 | } | ||
| 739 | else { | ||
| 740 | if ( ($page =~ /^$rxWikiWord(?:\#[A-Z0-9]+)?$/) || | ||
| 741 | ($page =~ /^$rxWikiWord\/$rxSubpage(?:\#[A-Z0-9]+)?$/) ) { | ||
| 742 | push @inlineNodes, | ||
| 743 | PurpleWiki::InlineNode->new('type'=>'text', | ||
| 744 | 'content'=>"$site:"); | ||
| 745 | push @inlineNodes, | ||
| 746 | PurpleWiki::InlineNode->new('type'=>'wikiword', | ||
| 747 | 'content'=>$page); | ||
| 748 | } | ||
| 749 | elsif ($page =~ /(.*)(\/$rxSubpage(?:\#[A-Z0-9]+)?)$/) { | ||
| 750 | push @inlineNodes, | ||
| 751 | PurpleWiki::InlineNode->new('type'=>'text', | ||
| 752 | 'content'=>"$site:$1"); | ||
| 753 | push @inlineNodes, | ||
| 754 | PurpleWiki::InlineNode->new('type'=>'wikiword', | ||
| 755 | 'content'=>$2); | ||
| 756 | } | ||
| 757 | else { | ||
| 758 | push @inlineNodes, | ||
| 759 | PurpleWiki::InlineNode->new('type'=>'text', | ||
| 760 | 'content'=>"$site:$page"); | ||
| 761 | } | ||
| 762 | } | ||
| 763 | } | ||
| 764 | } | ||
| 765 | elsif ($params{wikiword} && | ||
| 766 | ($node =~ /$rxWikiWord(?:\#[A-Z0-9]+)?$rxQuoteDelim/s)) { | ||
| 767 | $node =~ s/""$//; | ||
| 768 | push @inlineNodes, PurpleWiki::InlineNode->new('type'=>'wikiword', | ||
| 769 | 'content'=>$node); | ||
| 770 | } | ||
| 771 | elsif ($node ne '') { | ||
| 772 | # FIXME: For now, substitute MoinMoin macros with something else | ||
| 773 | $node =~ s/\[\[/\[macro\[/g; | ||
| 774 | push @inlineNodes, PurpleWiki::InlineNode->new('type'=>'text', | ||
| 775 | 'content'=>$node); | ||
| 776 | } | ||
| 777 | } | ||
| 778 | return \@inlineNodes; | ||
| 779 | } | ||
| 780 | |||
| 781 | sub _addNodeIds { | ||
| 782 | my ($rootNode) = @_; | ||
| 783 | |||
| 784 | &_traverseAndAddNids($rootNode->children) | ||
| 785 | if ($rootNode->children); | ||
| 786 | } | ||
| 787 | |||
| 788 | sub _traverseAndAddNids { | ||
| 789 | my ($nodeListRef) = @_; | ||
| 790 | |||
| 791 | foreach my $node (@{$nodeListRef}) { | ||
| 792 | if (($node->type eq 'h' || $node->type eq 'p' || | ||
| 793 | $node->type eq 'li' || $node->type eq 'pre' || | ||
| 794 | $node->type eq 'dt' || $node->type eq 'dd') && | ||
| 795 | !$node->id) { | ||
| 796 | $node->id($sequence->getNext($url)); | ||
| 797 | } | ||
| 798 | my $childrenRef = $node->children; | ||
| 799 | &_traverseAndAddNids($childrenRef) | ||
| 800 | if ($childrenRef); | ||
| 801 | } | ||
| 802 | } | ||
| 803 | |||
| 804 | 1; | ||
| 805 | __END__ |