| 1 | # PurpleWiki::Parser::WikiText.pm | ||
| 2 | # vi:ai:sm:et:sw=4:ts=4 | ||
| 3 | # | ||
| 4 | # $Id: WikiText.pm 461 2004-08-08 23:20:49Z cdent $ | ||
| 5 | # | ||
| 6 | # Copyright (c) Blue Oxen Associates 2002-2003. 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::WikiText; | ||
| 32 | |||
| 33 | 16 | ▶ | use 5.005; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 34 | 16 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 35 | 16 | ▶ | use PurpleWiki::Config; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 36 | 16 | ▶ | use PurpleWiki::InlineNode; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 37 | 16 | ▶ | use PurpleWiki::StructuralNode; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 38 | 16 | ▶ | use PurpleWiki::Tree; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 39 | 0 | ▶ | use PurpleWiki::Sequence; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 40 | use PurpleWiki::Page; | ||
| 41 | |||
| 42 | our $VERSION; | ||
| 43 | $VERSION = sprintf("%d", q$Id: WikiText.pm 461 2004-08-08 23:20:49Z cdent $ =~ /\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 $rxDoubleBracketed = '\[\[[\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); | ||
| 99 | |||
| 100 | my %listMap = ('ul' => '(\*+)\s*(.*)', | ||
| 101 | 'ol' => '(\#+)\s*(.*)', | ||
| 102 | 'dl' => q{(\;+)([^:]+\:?)\:(.*)}, | ||
| 103 | ); | ||
| 104 | |||
| 105 | my $aggregateListRegExp = join('|', values(%listMap)); | ||
| 106 | |||
| 107 | # The parsing strategy is as follows. First, process the text | ||
| 108 | # line-by-line, updating state variables as we go. At certain | ||
| 109 | # termination points, we update the data structure based on the | ||
| 110 | # state variables. | ||
| 111 | # | ||
| 112 | # The top of the text is parsed differently from the rest of the | ||
| 113 | # text. If there is metadata (delimited by braces), then the | ||
| 114 | # metadata is parsed; otherwise, the parser begins parsing for | ||
| 115 | # WikiText. Once it reaches this latter stage, it will treat | ||
| 116 | # metadata as WikiText according to the basic parsing rules. | ||
| 117 | # | ||
| 118 | # STATE VARIABLES | ||
| 119 | # | ||
| 120 | # The state variables we care about and why are: | ||
| 121 | # | ||
| 122 | # $isStart -- Determines whether we're at the top of the text | ||
| 123 | # (and should be parsing metadata) or not. | ||
| 124 | # | ||
| 125 | # @authors -- List of authors (defined in the metadata). This | ||
| 126 | # list is added to the tree structure's metadata once parsing | ||
| 127 | # is complete. | ||
| 128 | # | ||
| 129 | # $listDepth -- The depth of the current list (unordered, | ||
| 130 | # ordered, or definition) being parsed. Starts at 0. | ||
| 131 | # | ||
| 132 | # $indentDepth -- The depth of the current indented text (begins | ||
| 133 | # with a colon) being parsed. Starts at 0. | ||
| 134 | # | ||
| 135 | # $sectionDepth -- Depth of the current section. We start with | ||
| 136 | # a depth of 1, because we assume that the body text begins | ||
| 137 | # with a section. (See below for more detailed explanation.) | ||
| 138 | # | ||
| 139 | # $nodeContent -- Textual content of current node. | ||
| 140 | # | ||
| 141 | # $currentNode -- The current node. The initial node when we | ||
| 142 | # start parsing is the first section node. | ||
| 143 | # | ||
| 144 | # SECTIONS | ||
| 145 | # | ||
| 146 | # A document consists of sections. Each section may consist of | ||
| 147 | # subsections, ad infinitum. | ||
| 148 | # | ||
| 149 | # New sections are delimited by header markup (equal signs) and | ||
| 150 | # hard rules (four dashes). The depth of the header markup | ||
| 151 | # determines whether or not the section is a new section, a | ||
| 152 | # subsection, or in some cases, a supersection (e.g. a two equal | ||
| 153 | # sign header following a three equal sign header). | ||
| 154 | # | ||
| 155 | # A hard rule indicates a new section. Ideally, I'd like to make | ||
| 156 | # it so that the hard rule is ignored if it is directly followed | ||
| 157 | # by a header. If users want to have hard rules separating | ||
| 158 | # sections (i.e. separating headers), they should define that in | ||
| 159 | # the stylesheet instead of using hard rules. Expecting this | ||
| 160 | # behavior from users is probably a bit too draconian, however, | ||
| 161 | # and there may in fact be times when this is indeed the desired | ||
| 162 | # behavior (for example, as a placeholder for future content). | ||
| 163 | # The resulting data structure from a series of hard rules | ||
| 164 | # followed by headers won't be "clean," but it shouldn't be | ||
| 165 | # harmful either. | ||
| 166 | |||
| 167 | my $isStart = 1; | ||
| 168 | my $isBracePre = 0; | ||
| 169 | my @authors = (); | ||
| 170 | my $listDepth = 0; | ||
| 171 | my $indentDepth = 0; | ||
| 172 | my $sectionDepth = 1; | ||
| 173 | |||
| 174 | $currentNode = $tree->root->insertChild('type' => 'section'); | ||
| 175 | |||
| 176 | foreach $line (split(/\n/, $wikiContent)) { # Process lines one-at-a-time | ||
| 177 | chomp $line; | ||
| 178 | if ($isStart && $line =~ /^\{title (.+)\}$/) { | ||
| 179 | # The metadata below is not (currently) used by the | ||
| 180 | # Wiki. It's here to so that this parser can be used | ||
| 181 | # as a general documentation formatting system. | ||
| 182 | $tree->title($1); | ||
| 183 | } | ||
| 184 | elsif ($isStart && $line =~ /^\{subtitle (.+)\}$/) { | ||
| 185 | # See above. | ||
| 186 | $tree->subtitle($1); | ||
| 187 | } | ||
| 188 | elsif ($isStart && $line =~ /^\{docid (.+)\}$/) { | ||
| 189 | # See above. | ||
| 190 | $tree->id($1); | ||
| 191 | } | ||
| 192 | elsif ($isStart && $line =~ /^\{date (.+)\}$/) { | ||
| 193 | # See above. | ||
| 194 | $tree->date($1); | ||
| 195 | } | ||
| 196 | elsif ($isStart && $line =~ /^\{version (.+)\}$/) { | ||
| 197 | # See above. | ||
| 198 | $tree->version($1); | ||
| 199 | } | ||
| 200 | elsif ($isStart && $line =~ /^\{author (.+)\}$/) { | ||
| 201 | # See above. | ||
| 202 | my $authorString = $1; | ||
| 203 | $authorString =~ s/\s+(\S+\@\S+)$//; | ||
| 204 | my $authorEmail = $1 if ($1 ne $authorString); | ||
| 205 | if ($authorEmail) { | ||
| 206 | push @authors, [$authorString, $authorEmail]; | ||
| 207 | } | ||
| 208 | else { | ||
| 209 | push @authors, [$authorString]; | ||
| 210 | } | ||
| 211 | } | ||
| 212 | elsif ($line =~ /^\{sketch\}$/) { # WikiWhiteboard | ||
| 213 | $currentNode->insertChild(type=>'sketch'); | ||
| 214 | } | ||
| 215 | elsif ($line =~ /^----*$/) { # new section | ||
| 216 | $currentNode = &_terminateNode($currentNode, \$nodeContent, | ||
| 217 | %params); | ||
| 218 | $currentNode = $currentNode->parent; | ||
| 219 | $currentNode = $currentNode->insertChild(type=>'section'); | ||
| 220 | } | ||
| 221 | elsif ($line =~ /^($aggregateListRegExp)$/) { # Process lists | ||
| 222 | |||
| 223 | # Lists, for the most part, get handled the same way, | ||
| 224 | # whether they are unordered, ordered, or definition. | ||
| 225 | # However, definition lists are a bit funky as commented | ||
| 226 | # below. | ||
| 227 | |||
| 228 | foreach $listType (keys(%listMap)) { # repeat for all | ||
| 229 | # three list types | ||
| 230 | if ($line =~ /^$listMap{$listType}$/x) { | ||
| 231 | $currentNode = &_terminateNode($currentNode, | ||
| 232 | \$nodeContent, | ||
| 233 | %params); | ||
| 234 | while ($indentDepth > 0) { | ||
| 235 | $currentNode = $currentNode->parent; | ||
| 236 | $indentDepth--; | ||
| 237 | } | ||
| 238 | my @listContents = ($2, $3); | ||
| 239 | if ($listType eq 'dl') { | ||
| 240 | |||
| 241 | # Definition lists have to be handled in a | ||
| 242 | # special manner in order to enable the | ||
| 243 | # desired behavior in certain situations: | ||
| 244 | # external links and InterWiki links in the | ||
| 245 | # definition title. For more detailed info, | ||
| 246 | # check out the test cases in t/parser07.t and | ||
| 247 | # t/tree_test11.txt. | ||
| 248 | # | ||
| 249 | # UseModWiki didn't have these problems | ||
| 250 | # because of the way the parser worked. | ||
| 251 | # UseMod did a regexp substitution of inline | ||
| 252 | # formatting and links before parsing for | ||
| 253 | # structure. We don't do things that way. | ||
| 254 | # Parsing and output is decoupled. We parse | ||
| 255 | # for structure first, then for inline | ||
| 256 | # content. | ||
| 257 | # | ||
| 258 | # Probably the "right" way to handle these | ||
| 259 | # situations is to write a more sophisticated | ||
| 260 | # parser, one that does partial inline | ||
| 261 | # processing first, then does structural | ||
| 262 | # parsing, then completes the inline | ||
| 263 | # processing. But I don't want to do that for | ||
| 264 | # this one exceptional situation. Instead, | ||
| 265 | # I'm going to do some UseMod-like analysis on | ||
| 266 | # inline content in order to determine how the | ||
| 267 | # structure of the list will break down. | ||
| 268 | # | ||
| 269 | # I'm also going to take one shortcut. I'm | ||
| 270 | # going to assume that the original regexp for | ||
| 271 | # determining whether or not a line is part of | ||
| 272 | # a definition list still holds. If I wanted | ||
| 273 | # to be super anal, then something like: | ||
| 274 | # | ||
| 275 | # ;http://www.blueoxen.org/ | ||
| 276 | # | ||
| 277 | # would not be a definition list; it would be | ||
| 278 | # part of a paragraph. By keeping the | ||
| 279 | # original regexp, the above will be | ||
| 280 | # considered a title without a definition, | ||
| 281 | # even though there is no concluding colon. | ||
| 282 | # It bugs the super anal side of me, but the | ||
| 283 | # reality is, it should be harmless. Plus, | ||
| 284 | # this documentation is longer than the code | ||
| 285 | # needed to make this work, so people who poke | ||
| 286 | # around shouldn't be surprised. | ||
| 287 | |||
| 288 | # rejoin @listContents, and parse it again | ||
| 289 | my $listContentString = join(':', @listContents); | ||
| 290 | if ($listContentString =~ | ||
| 291 | /^([^\:]*\[$rxProtocols$rxAddress\s*.*?\][^\:]*)\:(.*)$/) { | ||
| 292 | @listContents = ($1, $2); | ||
| 293 | } | ||
| 294 | elsif ($listContentString =~ | ||
| 295 | /^([^\:]*$rxProtocols$rxAddress[^\:]*)\:(.*)$/) { | ||
| 296 | @listContents = ($1, $2); | ||
| 297 | } | ||
| 298 | elsif ($listContentString =~ | ||
| 299 | /^([^\:]*)([A-Z]\w+)\:([^\]\#\s"<>]+(?:\#[A-Z0-9]+)?$rxQuoteDelim[^\:]*)\:(.*)$/) { | ||
| 300 | my $start = $1; | ||
| 301 | my $site = $2; | ||
| 302 | my $page = $3; | ||
| 303 | my $rest = $4; | ||
| 304 | if (&PurpleWiki::Page::siteExists($site)) { | ||
| 305 | @listContents = ("$start$site:$page", $rest); | ||
| 306 | } | ||
| 307 | } | ||
| 308 | } | ||
| 309 | $currentNode = &_parseList($listType, length $1, | ||
| 310 | \$listDepth, $currentNode, | ||
| 311 | \%params, \$nodeContent, | ||
| 312 | @listContents); | ||
| 313 | $isStart = 0 if ($isStart); | ||
| 314 | } | ||
| 315 | } | ||
| 316 | } | ||
| 317 | elsif ($line =~ /^(\:+)(.*)$/) { # indented paragraphs | ||
| 318 | $currentNode = &_terminateNode($currentNode, \$nodeContent, | ||
| 319 | %params); | ||
| 320 | $currentNode = &_resetList($currentNode, \$listDepth, undef); | ||
| 321 | $listLength = length $1; | ||
| 322 | $nodeContent = "$2\n"; | ||
| 323 | while ($listLength > $indentDepth) { | ||
| 324 | $currentNode = $currentNode->insertChild('type'=>'indent'); | ||
| 325 | $indentDepth++; | ||
| 326 | } | ||
| 327 | while ($listLength < $indentDepth) { | ||
| 328 | $currentNode = $currentNode->parent; | ||
| 329 | $indentDepth--; | ||
| 330 | } | ||
| 331 | $currentNode = $currentNode->insertChild('type'=>'p'); | ||
| 332 | $isStart = 0 if ($isStart); | ||
| 333 | } | ||
| 334 | elsif ($line =~ /^(\=+)\s+(.+)\s+\=+/) { # header/section | ||
| 335 | my $currentNid; | ||
| 336 | $currentNode = &_terminateNode($currentNode, \$nodeContent, | ||
| 337 | %params); | ||
| 338 | $currentNode = &_resetList($currentNode, \$listDepth, \$indentDepth); | ||
| 339 | $sectionLength = length $1; | ||
| 340 | $nodeContent = $2; | ||
| 341 | if ($sectionLength > $sectionDepth) { | ||
| 342 | while ($sectionLength > $sectionDepth) { | ||
| 343 | $currentNode = $currentNode->insertChild(type=>'section'); | ||
| 344 | $sectionDepth++; | ||
| 345 | } | ||
| 346 | } | ||
| 347 | else { | ||
| 348 | while ($sectionLength < $sectionDepth) { | ||
| 349 | $currentNode = $currentNode->parent; | ||
| 350 | $sectionDepth--; | ||
| 351 | } | ||
| 352 | if ( !$isStart && ($sectionLength == $sectionDepth) ) { | ||
| 353 | $currentNode = $currentNode->parent; | ||
| 354 | $currentNode = $currentNode->insertChild(type=>'section'); | ||
| 355 | } | ||
| 356 | } | ||
| 357 | if ($nodeContent =~ s/\s*\{nid ([A-Z0-9]+)\}$//s) { | ||
| 358 | $currentNid = $1; | ||
| 359 | } | ||
| 360 | $currentNode = $currentNode->insertChild('type'=>'h', | ||
| 361 | 'content'=>&_parseInlineNode($nodeContent, %params)); | ||
| 362 | if (defined $currentNid && ($currentNid =~ /^[A-Z0-9]+$/)) { | ||
| 363 | $currentNode->id($currentNid); | ||
| 364 | } | ||
| 365 | $currentNode = $currentNode->parent; | ||
| 366 | undef $nodeContent; | ||
| 367 | $isStart = 0 if ($isStart); | ||
| 368 | } | ||
| 369 | elsif ($line =~ /^(\s+\S.*)$/) { # preformatted or continued li | ||
| 370 | if ($currentNode->type ne 'pre' && $currentNode->type ne 'li' && | ||
| 371 | $currentNode->type ne 'dd' && | ||
| 372 | !($currentNode->type eq 'p' && $indentDepth > 0) ) { | ||
| 373 | $currentNode = &_resetList($currentNode, \$listDepth, \$indentDepth); | ||
| 374 | $currentNode = &_terminateNode($currentNode, | ||
| 375 | \$nodeContent, | ||
| 376 | %params); | ||
| 377 | $currentNode = $currentNode->insertChild('type'=>'pre'); | ||
| 378 | } | ||
| 379 | $nodeContent .= "$1\n"; | ||
| 380 | $isStart = 0 if ($isStart); | ||
| 381 | } | ||
| 382 | elsif ($line =~ /^(.*)\{\{\{\s*$/) { # MoinMoin-style pre | ||
| 383 | |||
| 384 | # If there's already a pre, this has the effect of closing | ||
| 385 | # the previous one and starting a new one. For example: | ||
| 386 | # | ||
| 387 | # = Header = | ||
| 388 | # | ||
| 389 | # indented (hence preformatted) | ||
| 390 | # {{{ | ||
| 391 | # hello world! | ||
| 392 | # }}} | ||
| 393 | # | ||
| 394 | # will result in: | ||
| 395 | # | ||
| 396 | # section: | ||
| 397 | # h:Header | ||
| 398 | # pre: indented (hence preformatted) | ||
| 399 | # pre:hello world! | ||
| 400 | # | ||
| 401 | # This also creates an unusual side-effect: | ||
| 402 | # | ||
| 403 | # {{{ | ||
| 404 | # hello world! | ||
| 405 | # {{{ | ||
| 406 | # hello again. | ||
| 407 | # }}} | ||
| 408 | # | ||
| 409 | # which results in: | ||
| 410 | # | ||
| 411 | # section: | ||
| 412 | # h:Header | ||
| 413 | # pre:hello world! | ||
| 414 | # pre:hello again. | ||
| 415 | # | ||
| 416 | # In other words, the first {{{ did not need closing. | ||
| 417 | |||
| 418 | $nodeContent .= "$1" if ($1); | ||
| 419 | $currentNode = &_resetList($currentNode, \$listDepth, \$indentDepth); | ||
| 420 | $currentNode = &_terminateNode($currentNode, | ||
| 421 | \$nodeContent, | ||
| 422 | %params); | ||
| 423 | $currentNode = $currentNode->insertChild('type'=>'pre'); | ||
| 424 | $isStart = 0 if ($isStart); | ||
| 425 | $isBracePre = 1; | ||
| 426 | } | ||
| 427 | elsif ($line =~ /^\s*\}\}\}\s*$/) { # close MoinMoin pre | ||
| 428 | if ($currentNode->type eq 'pre') { | ||
| 429 | $currentNode = &_terminateNode($currentNode, | ||
| 430 | \$nodeContent, | ||
| 431 | %params); | ||
| 432 | $isBracePre = 0; | ||
| 433 | } | ||
| 434 | else { | ||
| 435 | # just make it part of the text | ||
| 436 | $nodeContent .= $line; | ||
| 437 | } | ||
| 438 | } | ||
| 439 | elsif ($line =~ /^\s*$/) { # blank line | ||
| 440 | if ($isBracePre) { | ||
| 441 | $nodeContent .= "\n"; | ||
| 442 | } | ||
| 443 | else { | ||
| 444 | $currentNode = &_terminateNode($currentNode, \$nodeContent, | ||
| 445 | %params); | ||
| 446 | $currentNode = &_resetList($currentNode, \$listDepth, \$indentDepth); | ||
| 447 | } | ||
| 448 | } | ||
| 449 | else { | ||
| 450 | if (($currentNode->type ne 'p') && | ||
| 451 | ($currentNode->type ne 'li') && | ||
| 452 | ($currentNode->type ne 'dd') && | ||
| 453 | (!$isBracePre)) { | ||
| 454 | $currentNode = &_resetList($currentNode, \$listDepth, \$indentDepth); | ||
| 455 | $currentNode = &_terminateNode($currentNode, | ||
| 456 | \$nodeContent, | ||
| 457 | %params); | ||
| 458 | $currentNode = $currentNode->insertChild('type'=>'p'); | ||
| 459 | } | ||
| 460 | $nodeContent .= "$line\n"; | ||
| 461 | $isStart = 0 if ($isStart); | ||
| 462 | } | ||
| 463 | } | ||
| 464 | $currentNode = &_terminateNode($currentNode, \$nodeContent, | ||
| 465 | %params); | ||
| 466 | if (scalar @authors > 0) { | ||
| 467 | $tree->authors(\@authors); | ||
| 468 | } | ||
| 469 | |||
| 470 | if ($params{'add_node_ids'}) { | ||
| 471 | &_addNodeIds($tree->root); | ||
| 472 | } | ||
| 473 | return $tree; | ||
| 474 | } | ||
| 475 | |||
| 476 | ### private | ||
| 477 | |||
| 478 | sub _resetList { | ||
| 479 | # "Resets" lists. When a new node is about to be created, this | ||
| 480 | # routine makes sure that any previous lists or indentations are | ||
| 481 | # reset to the correct nesting depth. | ||
| 482 | |||
| 483 | my ($currentNode, $listDepthRef, $indentDepthRef) = @_; | ||
| 484 | |||
| 485 | if ($listDepthRef && ${$listDepthRef}) { | ||
| 486 | while (${$listDepthRef} > 1) { | ||
| 487 | $currentNode = $currentNode->parent->parent; | ||
| 488 | ${$listDepthRef}--; | ||
| 489 | } | ||
| 490 | $currentNode = $currentNode->parent; | ||
| 491 | ${$listDepthRef} = 0; | ||
| 492 | } | ||
| 493 | if ($indentDepthRef) { | ||
| 494 | while (${$indentDepthRef} > 0) { | ||
| 495 | $currentNode = $currentNode->parent; | ||
| 496 | ${$indentDepthRef}--; | ||
| 497 | } | ||
| 498 | } | ||
| 499 | return $currentNode; | ||
| 500 | } | ||
| 501 | |||
| 502 | sub _terminateNode { | ||
| 503 | # "Closes" nodes. When the parser knows that it is ready to add | ||
| 504 | # content to the node (i.e. when it sees a blank line), this | ||
| 505 | # routine does the adding. | ||
| 506 | |||
| 507 | my ($currentNode, $nodeContentRef, %params) = @_; | ||
| 508 | |||
| 509 | if (($currentNode->type eq 'p') || ($currentNode->type eq 'pre') || | ||
| 510 | ($currentNode->type eq 'li') || ($currentNode->type eq 'dd')) { | ||
| 511 | my $currentNid; | ||
| 512 | chomp ${$nodeContentRef}; | ||
| 513 | if (${$nodeContentRef} =~ s/\s*\{nid ([A-Z0-9]+)\}$//s) { | ||
| 514 | $currentNid = $1; | ||
| 515 | } | ||
| 516 | if (defined $currentNid && ($currentNid =~ /^[A-Z0-9]+$/)) { | ||
| 517 | $currentNode->id($currentNid); | ||
| 518 | } | ||
| 519 | $currentNode->content(&_parseInlineNode(${$nodeContentRef}, %params)); | ||
| 520 | undef ${$nodeContentRef}; | ||
| 521 | return $currentNode->parent; | ||
| 522 | } | ||
| 523 | return $currentNode; | ||
| 524 | } | ||
| 525 | |||
| 526 | sub _parseList { | ||
| 527 | # List parsing is mostly handled the same, which is why it gets | ||
| 528 | # its own subroutine. The main thing this routine does is handle | ||
| 529 | # the nesting properly. | ||
| 530 | |||
| 531 | my ($listType, $listLength, $listDepthRef, | ||
| 532 | $currentNode, $paramRef, $nodeContentRef, | ||
| 533 | @nodeContents) = @_; | ||
| 534 | |||
| 535 | if ($listLength == ${$listDepthRef} && $currentNode->type ne $listType) { | ||
| 536 | $currentNode = $currentNode->parent; | ||
| 537 | $currentNode = $currentNode->insertChild(type=>$listType); | ||
| 538 | } | ||
| 539 | while ($listLength > ${$listDepthRef}) { | ||
| 540 | # Nested lists are children of list items, not of other lists. | ||
| 541 | # We need to find the last list item (if it exists) and | ||
| 542 | # create a sublist there; otherwise, we need to create a list | ||
| 543 | # item and give it a sublist. | ||
| 544 | if ($currentNode->type eq 'ul' || $currentNode->type eq 'ol' || | ||
| 545 | $currentNode->type eq 'dl') { | ||
| 546 | my $kidsRef = $currentNode->children; | ||
| 547 | if ($kidsRef) { | ||
| 548 | $currentNode = $kidsRef->[scalar @{$kidsRef} - 1]; | ||
| 549 | } | ||
| 550 | else { | ||
| 551 | if ($listType eq 'dl') { | ||
| 552 | $currentNode = $currentNode->insertChild(type=>'dd'); | ||
| 553 | } | ||
| 554 | else { | ||
| 555 | $currentNode = $currentNode->insertChild(type=>'li'); | ||
| 556 | } | ||
| 557 | } | ||
| 558 | } | ||
| 559 | $currentNode = $currentNode->insertChild(type=>$listType); | ||
| 560 | ${$listDepthRef}++; | ||
| 561 | } | ||
| 562 | while ($listLength < ${$listDepthRef}) { # assert($listLength != 0) | ||
| 563 | $currentNode = $currentNode->parent->parent; | ||
| 564 | ${$listDepthRef}--; | ||
| 565 | } | ||
| 566 | if ($listType eq 'dl') { | ||
| 567 | $nodeContents[0] =~ s/\s*\{nid ([A-Z0-9]+)\}$//s; | ||
| 568 | my $currentNid = $1; | ||
| 569 | $currentNode = $currentNode->insertChild(type=>'dt', | ||
| 570 | content=>&_parseInlineNode($nodeContents[0], %{$paramRef})); | ||
| 571 | if (defined $currentNid && ($currentNid =~ /^[A-Z0-9]+$/)) { | ||
| 572 | $currentNode->id($currentNid); | ||
| 573 | } | ||
| 574 | $currentNode = $currentNode->parent; | ||
| 575 | ${$nodeContentRef} = $nodeContents[1] . "\n"; | ||
| 576 | $currentNode = $currentNode->insertChild('type'=>'dd'); | ||
| 577 | } | ||
| 578 | else { | ||
| 579 | ${$nodeContentRef} = $nodeContents[0] . "\n"; | ||
| 580 | $currentNode = $currentNode->insertChild('type'=>'li'); | ||
| 581 | } | ||
| 582 | return $currentNode; | ||
| 583 | } | ||
| 584 | |||
| 585 | sub _parseInlineNode { | ||
| 586 | my ($text, %params) = @_; | ||
| 587 | my (@inlineNodes); | ||
| 588 | |||
| 589 | # This used to be an extended regular expression, but it wasn't | ||
| 590 | # working in some cases. | ||
| 591 | my $rx = qq{$rxNowiki|$rxTransclusion|$rxTt|$rxFippleQuotes|$rxB|}; | ||
| 592 | $rx .= qq{$rxTripleQuotes|$rxI|$rxDoubleQuotes|}; | ||
| 593 | $rx .= qq{\\\[$rxProtocols$rxAddress\\s*.*?\\\]|$rxProtocols$rxAddress}; | ||
| 594 | if ($params{wikiword}) { | ||
| 595 | $rx .= qq{|(?:$rxWikiWord)?\\\/$rxSubpage(?:\\\#[A-Z0-9]+)?}; | ||
| 596 | $rx .= qq{$rxQuoteDelim|[A-Z]\\w+:[^\\\]\\\#\\s"<>\:]+}; | ||
| 597 | $rx .= qq{(?:\\\#[A-Z0-9]+)?$rxQuoteDelim|$rxWikiWord}; | ||
| 598 | $rx .= qq{(?:\\\#[A-Z0-9]+)?$rxQuoteDelim}; | ||
| 599 | } | ||
| 600 | if ($params{freelink}) { | ||
| 601 | $rx .= qq{|$rxDoubleBracketed}; | ||
| 602 | } | ||
| 603 | my @nodes = split(/($rx)/s, $text); | ||
| 604 | foreach my $node (@nodes) { | ||
| 605 | if ($node =~ /^$rxNowiki$/s) { | ||
| 606 | $node =~ s/^<nowiki>//; | ||
| 607 | $node =~ s/<\/nowiki>$//; | ||
| 608 | push @inlineNodes, PurpleWiki::InlineNode->new('type'=>'nowiki', | ||
| 609 | 'content'=>$node); | ||
| 610 | } | ||
| 611 | elsif ($node =~ /^$rxTransclusion$/s) { | ||
| 612 | # transclusion | ||
| 613 | my ($content) = ($node =~ /([A-Z0-9]+)/); | ||
| 614 | push @inlineNodes, PurpleWiki::InlineNode->new( | ||
| 615 | 'type' => 'transclusion', | ||
| 616 | 'content' => $content); | ||
| 617 | } | ||
| 618 | elsif ($node =~ /^$rxTt$/s) { | ||
| 619 | $node =~ s/^<tt>//; | ||
| 620 | $node =~ s/<\/tt>$//; | ||
| 621 | $node =~ s/^\{\{\{//; | ||
| 622 | $node =~ s/\}\}\}$//; | ||
| 623 | push @inlineNodes, PurpleWiki::InlineNode->new('type'=>'tt', | ||
| 624 | 'children'=>&_parseInlineNode($node, %params)); | ||
| 625 | } | ||
| 626 | elsif ($node =~ /^$rxFippleQuotes$/s) { | ||
| 627 | $node =~ s/^'''//; | ||
| 628 | $node =~ s/'''$//; | ||
| 629 | push @inlineNodes, PurpleWiki::InlineNode->new('type'=>'b', | ||
| 630 | 'children'=>&_parseInlineNode($node, %params)); | ||
| 631 | } | ||
| 632 | elsif ($node =~ /^$rxB$/s) { | ||
| 633 | $node =~ s/^<b>//; | ||
| 634 | $node =~ s/<\/b>$//; | ||
| 635 | push @inlineNodes, PurpleWiki::InlineNode->new('type'=>'b', | ||
| 636 | 'children'=>&_parseInlineNode($node, %params)); | ||
| 637 | } | ||
| 638 | elsif ($node =~ /^$rxTripleQuotes$/s) { | ||
| 639 | $node =~ s/^'''//; | ||
| 640 | $node =~ s/'''$//; | ||
| 641 | push @inlineNodes, PurpleWiki::InlineNode->new('type'=>'b', | ||
| 642 | 'children'=>&_parseInlineNode($node, %params)); | ||
| 643 | } | ||
| 644 | elsif ($node =~ /^$rxI$/s) { | ||
| 645 | $node =~ s/^<i>//; | ||
| 646 | $node =~ s/<\/i>$//; | ||
| 647 | push @inlineNodes, PurpleWiki::InlineNode->new('type'=>'i', | ||
| 648 | 'children'=>&_parseInlineNode($node, %params)); | ||
| 649 | } | ||
| 650 | elsif ($node =~ /^$rxDoubleQuotes$/s) { | ||
| 651 | $node =~ s/^''//; | ||
| 652 | $node =~ s/''$//; | ||
| 653 | push @inlineNodes, PurpleWiki::InlineNode->new('type'=>'i', | ||
| 654 | 'children'=>&_parseInlineNode($node, %params)); | ||
| 655 | } | ||
| 656 | elsif ($node =~ /^\[($rxProtocols$rxAddress)\s*(.*?)\]$/s) { | ||
| 657 | # bracketed link | ||
| 658 | push @inlineNodes, PurpleWiki::InlineNode->new('type'=>'link', | ||
| 659 | 'href'=>$1, | ||
| 660 | 'content'=>$2); | ||
| 661 | } | ||
| 662 | elsif ($node =~ /^$rxProtocols$rxAddress$/s) { | ||
| 663 | # URL | ||
| 664 | if ($node =~ /\.(?:jpg|gif|png|bmp|jpeg)$/i) { | ||
| 665 | push @inlineNodes, | ||
| 666 | PurpleWiki::InlineNode->new('type'=>'image', | ||
| 667 | 'href'=>$node, | ||
| 668 | 'content'=>$node); | ||
| 669 | } | ||
| 670 | else { | ||
| 671 | push @inlineNodes, | ||
| 672 | PurpleWiki::InlineNode->new('type'=>'url', | ||
| 673 | 'href'=>$node, | ||
| 674 | 'content'=>$node); | ||
| 675 | } | ||
| 676 | } | ||
| 677 | elsif ($params{freelink} && ($node =~ /$rxDoubleBracketed/s)) { | ||
| 678 | $node =~ s/^\[\[//; | ||
| 679 | $node =~ s/\]\]$//; | ||
| 680 | push @inlineNodes, PurpleWiki::InlineNode->new('type'=>'freelink', | ||
| 681 | 'content'=>$node); | ||
| 682 | } | ||
| 683 | elsif ($params{wikiword} && | ||
| 684 | ($node =~ /^(?:$rxWikiWord)?\/$rxSubpage(?:\#[A-Z0-9]+)?$rxQuoteDelim$/s)) { | ||
| 685 | $node =~ s/""$//; | ||
| 686 | push @inlineNodes, PurpleWiki::InlineNode->new('type'=>'wikiword', | ||
| 687 | 'content'=>$node); | ||
| 688 | } | ||
| 689 | elsif ($params{wikiword} && | ||
| 690 | ($node =~ /^([A-Z]\w+):([^\]\#\:\s"<>]+(?:\#[A-Z0-9]+)?)$rxQuoteDelim$/s)) { | ||
| 691 | my $site = $1; | ||
| 692 | my $page = $2; | ||
| 693 | if (&PurpleWiki::Page::siteExists($site)) { | ||
| 694 | $node =~ s/""$//; | ||
| 695 | push @inlineNodes, | ||
| 696 | PurpleWiki::InlineNode->new('type'=>'wikiword', | ||
| 697 | 'content'=>$node); | ||
| 698 | } | ||
| 699 | else { | ||
| 700 | if ($site =~ /^$rxWikiWord$/) { | ||
| 701 | push @inlineNodes, | ||
| 702 | PurpleWiki::InlineNode->new('type'=>'wikiword', | ||
| 703 | 'content'=>$site); | ||
| 704 | if ( ($page =~ /^$rxWikiWord(?:\#[A-Z0-9]+)?$/) || | ||
| 705 | ($page =~ /^$rxWikiWord\/$rxSubpage(?:\#[A-Z0-9]+)?$/) ) { | ||
| 706 | push @inlineNodes, | ||
| 707 | PurpleWiki::InlineNode->new('type'=>'text', | ||
| 708 | 'content'=>':'); | ||
| 709 | push @inlineNodes, | ||
| 710 | PurpleWiki::InlineNode->new('type'=>'wikiword', | ||
| 711 | 'content'=>$page); | ||
| 712 | } | ||
| 713 | elsif ($page =~ /(.*)(\/$rxSubpage(?:\#[A-Z0-9]+)?)$/) { | ||
| 714 | push @inlineNodes, | ||
| 715 | PurpleWiki::InlineNode->new('type'=>'text', | ||
| 716 | 'content'=>":$1"); | ||
| 717 | push @inlineNodes, | ||
| 718 | PurpleWiki::InlineNode->new('type'=>'wikiword', | ||
| 719 | 'content'=>$2); | ||
| 720 | } | ||
| 721 | else { | ||
| 722 | push @inlineNodes, | ||
| 723 | PurpleWiki::InlineNode->new('type'=>'text', | ||
| 724 | 'content'=>":$page"); | ||
| 725 | } | ||
| 726 | } | ||
| 727 | else { | ||
| 728 | if ( ($page =~ /^$rxWikiWord(?:\#[A-Z0-9]+)?$/) || | ||
| 729 | ($page =~ /^$rxWikiWord\/$rxSubpage(?:\#[A-Z0-9]+)?$/) ) { | ||
| 730 | push @inlineNodes, | ||
| 731 | PurpleWiki::InlineNode->new('type'=>'text', | ||
| 732 | 'content'=>"$site:"); | ||
| 733 | push @inlineNodes, | ||
| 734 | PurpleWiki::InlineNode->new('type'=>'wikiword', | ||
| 735 | 'content'=>$page); | ||
| 736 | } | ||
| 737 | elsif ($page =~ /(.*)(\/$rxSubpage(?:\#[A-Z0-9]+)?)$/) { | ||
| 738 | push @inlineNodes, | ||
| 739 | PurpleWiki::InlineNode->new('type'=>'text', | ||
| 740 | 'content'=>"$site:$1"); | ||
| 741 | push @inlineNodes, | ||
| 742 | PurpleWiki::InlineNode->new('type'=>'wikiword', | ||
| 743 | 'content'=>$2); | ||
| 744 | } | ||
| 745 | else { | ||
| 746 | push @inlineNodes, | ||
| 747 | PurpleWiki::InlineNode->new('type'=>'text', | ||
| 748 | 'content'=>"$site:$page"); | ||
| 749 | } | ||
| 750 | } | ||
| 751 | } | ||
| 752 | } | ||
| 753 | elsif ($params{wikiword} && | ||
| 754 | ($node =~ /$rxWikiWord(?:\#[A-Z0-9]+)?$rxQuoteDelim/s)) { | ||
| 755 | $node =~ s/""$//; | ||
| 756 | push @inlineNodes, PurpleWiki::InlineNode->new('type'=>'wikiword', | ||
| 757 | 'content'=>$node); | ||
| 758 | } | ||
| 759 | elsif ($node ne '') { | ||
| 760 | push @inlineNodes, PurpleWiki::InlineNode->new('type'=>'text', | ||
| 761 | 'content'=>$node); | ||
| 762 | } | ||
| 763 | } | ||
| 764 | return \@inlineNodes; | ||
| 765 | } | ||
| 766 | |||
| 767 | sub _addNodeIds { | ||
| 768 | my ($rootNode) = @_; | ||
| 769 | |||
| 770 | &_traverseAndAddNids($rootNode->children) | ||
| 771 | if ($rootNode->children); | ||
| 772 | } | ||
| 773 | |||
| 774 | sub _traverseAndAddNids { | ||
| 775 | my ($nodeListRef) = @_; | ||
| 776 | |||
| 777 | foreach my $node (@{$nodeListRef}) { | ||
| 778 | if (($node->type eq 'h' || $node->type eq 'p' || | ||
| 779 | $node->type eq 'li' || $node->type eq 'pre' || | ||
| 780 | $node->type eq 'dt' || $node->type eq 'dd') && | ||
| 781 | !$node->id) { | ||
| 782 | $node->id($sequence->getNext($url)); | ||
| 783 | } | ||
| 784 | my $childrenRef = $node->children; | ||
| 785 | &_traverseAndAddNids($childrenRef) | ||
| 786 | if ($childrenRef); | ||
| 787 | } | ||
| 788 | } | ||
| 789 | |||
| 790 | 1; | ||
| 791 | __END__ | ||