| 1 | # PurpleWiki::View::wikihtml.pm | ||
| 2 | # | ||
| 3 | # $Id: wikihtml.pm 469 2004-08-09 05:08:03Z eekim $ | ||
| 4 | # | ||
| 5 | # Copyright (c) Blue Oxen Associates 2002-2004. All rights reserved. | ||
| 6 | # | ||
| 7 | # This file is part of PurpleWiki. PurpleWiki is derived from: | ||
| 8 | # | ||
| 9 | # UseModWiki v0.92 (c) Clifford A. Adams 2000-2001 | ||
| 10 | # AtisWiki v0.3 (c) Markus Denker 1998 | ||
| 11 | # CVWiki CVS-patches (c) Peter Merel 1997 | ||
| 12 | # The Original WikiWikiWeb (c) Ward Cunningham | ||
| 13 | # | ||
| 14 | # PurpleWiki is free software; you can redistribute it and/or modify | ||
| 15 | # it under the terms of the GNU General Public License as published by | ||
| 16 | # the Free Software Foundation; either version 2 of the License, or | ||
| 17 | # (at your option) any later version. | ||
| 18 | # | ||
| 19 | # This program is distributed in the hope that it will be useful, | ||
| 20 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 22 | # GNU General Public License for more details. | ||
| 23 | # | ||
| 24 | # You should have received a copy of the GNU General Public License | ||
| 25 | # along with this program; if not, write to the | ||
| 26 | # Free Software Foundation, Inc. | ||
| 27 | # 59 Temple Place, Suite 330 | ||
| 28 | # Boston, MA 02111-1307 USA | ||
| 29 | |||
| 30 | package PurpleWiki::View::wikihtml; | ||
| 31 | use 5.005; | ||
| 32 | use strict; | ||
| 33 | use warnings; | ||
| 34 | use Carp; | ||
| 35 | use PurpleWiki::Transclusion; | ||
| 36 | use PurpleWiki::View::Driver; | ||
| 37 | |||
| 38 | ############### Package Globals ############### | ||
| 39 | |||
| 40 | our $VERSION; | ||
| 41 | $VERSION = sprintf("%d", q$Id: wikihtml.pm 469 2004-08-09 05:08:03Z eekim $ =~ /\s(\d+)\s/); | ||
| 42 | |||
| 43 | our @ISA = qw(PurpleWiki::View::Driver); | ||
| 44 | |||
| 45 | |||
| 46 | ############### Overloaded Methods ############### | ||
| 47 | |||
| 48 | sub new { | ||
| 49 | my $proto = shift; | ||
| 50 | my $class = ref($proto) || $proto; | ||
| 51 | my $self = $class->SUPER::new(@_); | ||
| 52 | |||
| 53 | ### Object State | ||
| 54 | $self->{outputString} = ""; | ||
| 55 | $self->{pageName} = ""; | ||
| 56 | $self->{url} = $self->{url} || ""; | ||
| 57 | $self->{transcluder} = new PurpleWiki::Transclusion( | ||
| 58 | url => $self->{url}); | ||
| 59 | |||
| 60 | # standard flag for determining whether or not a hard rule should | ||
| 61 | # be printed | ||
| 62 | $self->{isPrevSection} = 0; | ||
| 63 | |||
| 64 | # special case flag for handling hard rules (or not) at the | ||
| 65 | # beginning of a document | ||
| 66 | $self->{isStart} = 1; | ||
| 67 | $self->{emptyFirstSection} = 0; | ||
| 68 | |||
| 69 | # used for determining whether there should be hard rules with | ||
| 70 | # nested sections | ||
| 71 | $self->{sectionDepth} = 0; | ||
| 72 | $self->{depthLastClosedSection} = 0; | ||
| 73 | |||
| 74 | bless($self, $class); | ||
| 75 | return $self; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub view { | ||
| 79 | my ($self, $wikiTree) = @_; | ||
| 80 | $self->SUPER::view($wikiTree); | ||
| 81 | return $self->{outputString}; | ||
| 82 | } | ||
| 83 | |||
| 84 | # See PurpleWiki::View::wikitext.pm for an explanation of hard rules. | ||
| 85 | |||
| 86 | sub sectionPre { | ||
| 87 | my $self = shift; | ||
| 88 | $self->{sectionDepth}++; | ||
| 89 | $self->_hardRule(1); | ||
| 90 | $self->{isPrevSection} = 1; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub sectionPost { | ||
| 94 | my $self = shift; | ||
| 95 | $self->{depthLastClosedSection} = $self->{sectionDepth}; | ||
| 96 | $self->{sectionDepth}--; | ||
| 97 | $self->{emptyFirstSection} = 1 | ||
| 98 | if ($self->{isStart} && $self->{isPrevSection}); | ||
| 99 | $self->_hardRule(0); | ||
| 100 | $self->{isStart} = 0; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub indentPre { | ||
| 104 | my $self = shift; | ||
| 105 | $self->_hardRule(0); | ||
| 106 | $self->{outputString} .= "<div class=\"indent\">\n"; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub indentPost { | ||
| 110 | shift->{outputString} .= "</div>\n"; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub ulPre { | ||
| 114 | my ($self, $nodeRef) = @_; | ||
| 115 | |||
| 116 | $self->_hardRule(0); | ||
| 117 | $self->{outputString} .= '<' . $nodeRef->type . ">\n"; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub olPre { | ||
| 121 | my ($self, $nodeRef) = @_; | ||
| 122 | |||
| 123 | $self->_hardRule(0); | ||
| 124 | $self->{outputString} .= '<' . $nodeRef->type . ">\n"; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub dlPre { | ||
| 128 | my ($self, $nodeRef) = @_; | ||
| 129 | |||
| 130 | $self->_hardRule(0); | ||
| 131 | $self->{outputString} .= '<' . $nodeRef->type . ">\n"; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub bPre { shift->{outputString} .= '<' . shift->type . '>' } | ||
| 135 | sub iPre { shift->{outputString} .= '<' . shift->type . '>' } | ||
| 136 | sub ttPre { shift->{outputString} .= '<' . shift->type . '>' } | ||
| 137 | |||
| 138 | sub ulPost { shift->{outputString} .= '</' . shift->type . ">\n" } | ||
| 139 | sub olPost { shift->{outputString} .= '</' . shift->type . ">\n" } | ||
| 140 | sub dlPost { shift->{outputString} .= '</' . shift->type . ">\n" } | ||
| 141 | |||
| 142 | sub hPre { | ||
| 143 | my ($self, $node) = @_; | ||
| 144 | if ($self->{emptyFirstSection}) { | ||
| 145 | $self->{isPrevSection} = 1; | ||
| 146 | $self->{emptyFirstSection} = 0; | ||
| 147 | $self->_hardRule(0); | ||
| 148 | } | ||
| 149 | else { | ||
| 150 | $self->{isPrevSection} = 0; | ||
| 151 | } | ||
| 152 | $self->{outputString} .= '<h' . $self->_headerLevel(); | ||
| 153 | $self->{outputString} .= '>' . $self->_anchor($node->id); | ||
| 154 | } | ||
| 155 | |||
| 156 | sub hPost { | ||
| 157 | my ($self, $node) = @_; | ||
| 158 | $self->{outputString} .= $self->_nid($node->id); | ||
| 159 | $self->{outputString} .= '</h' . $self->_headerLevel() . ">\n"; | ||
| 160 | } | ||
| 161 | |||
| 162 | sub pPre { | ||
| 163 | my ($self, $node) = @_; | ||
| 164 | |||
| 165 | $self->_hardRule(0); | ||
| 166 | $self->_openTagWithNID($node); | ||
| 167 | } | ||
| 168 | |||
| 169 | sub liPre { shift->_openTagWithNID(@_) } | ||
| 170 | sub ddPre { shift->_openTagWithNID(@_) } | ||
| 171 | sub dtPre { shift->_openTagWithNID(@_) } | ||
| 172 | |||
| 173 | sub prePre { | ||
| 174 | my $self = shift; | ||
| 175 | |||
| 176 | $self->_hardRule(0); | ||
| 177 | $self->_openTagWithNID(@_); | ||
| 178 | } | ||
| 179 | |||
| 180 | sub liMain { shift->_liRecurse(@_) } | ||
| 181 | sub ddMain { shift->_liRecurse(@_) } | ||
| 182 | |||
| 183 | sub liPost { | ||
| 184 | my ($self, $nodeRef) = @_; | ||
| 185 | |||
| 186 | $self->{outputString} .= '</' . $nodeRef->type . ">\n"; | ||
| 187 | } | ||
| 188 | |||
| 189 | sub ddPost { | ||
| 190 | my ($self, $nodeRef) = @_; | ||
| 191 | |||
| 192 | $self->{outputString} .= '</' . $nodeRef->type . ">\n"; | ||
| 193 | } | ||
| 194 | |||
| 195 | sub prePost { | ||
| 196 | my $self = shift; | ||
| 197 | |||
| 198 | $self->_closeTagWithNID(@_); | ||
| 199 | } | ||
| 200 | |||
| 201 | sub pPost { shift->_closeTagWithNID(@_) } | ||
| 202 | sub dtPost { shift->_closeTagWithNID(@_) } | ||
| 203 | |||
| 204 | sub sketchMain { | ||
| 205 | my $self = shift; | ||
| 206 | |||
| 207 | $self->{outputString} .= q{<form name="SvgForm" action="/cgi-bin/wikiwhiteboard.pl" method="POST" onsubmit="frm=document.forms['SvgForm'];frm.svg.value= window.getSVG(); return true;">} . "\n" . | ||
| 208 | '<input type="submit" value="Save" />' . "\n" . | ||
| 209 | '<input type="hidden" name="pageName" value="' . $self->{pageName} . | ||
| 210 | '" />' . "\n" . | ||
| 211 | '<input type="hidden" name="svg" value="" />' . "\n" . | ||
| 212 | '<input type="submit" name="submit" value="Clear" />' . "\n" . | ||
| 213 | "</form>\n" . | ||
| 214 | '<embed src="/cgi-bin/wikiwhiteboard.pl?' . $self->{pageName} . | ||
| 215 | '" width="500" height="300" pluginspage="http://www.adobe.com/svg/viewer/install" />' . "\n"; | ||
| 216 | } | ||
| 217 | |||
| 218 | sub bPost { shift->{outputString} .= '</' . shift->type . '>' } | ||
| 219 | sub iPost { shift->{outputString} .= '</' . shift->type . '>' } | ||
| 220 | sub ttPost { shift->{outputString} .= '</' . shift->type . '>' } | ||
| 221 | |||
| 222 | sub textMain { shift->_quoteHtml(@_) } | ||
| 223 | sub nowikiMain { shift->_quoteHtml(@_) } | ||
| 224 | sub linkMain { shift->_quoteHtml(@_) } | ||
| 225 | sub urlMain { shift->_quoteHtml(@_) } | ||
| 226 | |||
| 227 | sub imageMain { | ||
| 228 | my ($self, $nodeRef) = @_; | ||
| 229 | my $href = $nodeRef->href; | ||
| 230 | $self->{outputString} .= '<img alt="' . $href . | ||
| 231 | '" src="' . $href . '" />'; | ||
| 232 | } | ||
| 233 | |||
| 234 | sub transclusionMain { | ||
| 235 | my ($self, $nodeRef) = @_; | ||
| 236 | my $transcluded = $self->{transcluder}->get($nodeRef->content); | ||
| 237 | if (ref($transcluded)) { | ||
| 238 | my $node = new PurpleWiki::InlineNode( | ||
| 239 | type => 'link', | ||
| 240 | class => 'nid', | ||
| 241 | href => $self->{transcluder}->getURL($nodeRef->content) . | ||
| 242 | '#nid' . $nodeRef->content, | ||
| 243 | content => 'T'); | ||
| 244 | my $textNode = new PurpleWiki::InlineNode( | ||
| 245 | type => 'text', | ||
| 246 | content => ' '); | ||
| 247 | $transcluded->content([@{$transcluded->content}, $textNode, $node]); | ||
| 248 | $self->traverse($transcluded->content); | ||
| 249 | } else { | ||
| 250 | $self->{outputString} .= $transcluded; | ||
| 251 | } | ||
| 252 | } | ||
| 253 | |||
| 254 | sub linkPre { shift->_openLinkTag(@_) } | ||
| 255 | sub urlPre { shift->_openLinkTag(@_) } | ||
| 256 | |||
| 257 | sub linkPost { shift->{outputString} .= '</a>' } | ||
| 258 | sub urlPost { shift->{outputString} .= '</a>' } | ||
| 259 | |||
| 260 | sub freelinkMain { shift->_wikiLink(@_) } | ||
| 261 | sub wikiwordMain { shift->_wikiLink(@_) } | ||
| 262 | |||
| 263 | |||
| 264 | ############### Private Methods ############### | ||
| 265 | |||
| 266 | sub _hardRule { | ||
| 267 | my ($self, $isSection) = @_; | ||
| 268 | |||
| 269 | if ($self->{isPrevSection}) { | ||
| 270 | if (!$self->{isStart}) { | ||
| 271 | if (!$isSection || ($isSection && | ||
| 272 | $self->{sectionDepth} == $self->{depthLastClosedSection} + 1) ) { | ||
| 273 | $self->{outputString} .= "<hr />\n\n"; | ||
| 274 | } | ||
| 275 | } | ||
| 276 | $self->{isPrevSection} = 0; | ||
| 277 | } | ||
| 278 | } | ||
| 279 | |||
| 280 | sub _liRecurse { # also used for dd | ||
| 281 | my ($self, $nodeRef) = @_; | ||
| 282 | |||
| 283 | if (!defined $nodeRef) { | ||
| 284 | carp "Warning: tried to recurse on an undefined node\n"; | ||
| 285 | return; | ||
| 286 | } | ||
| 287 | if ($nodeRef->isa('PurpleWiki::StructuralNode')) { | ||
| 288 | $self->traverse($nodeRef->content) if defined $nodeRef->content; | ||
| 289 | } | ||
| 290 | # display NID here | ||
| 291 | $self->{outputString} .= $self->_nid($nodeRef->id); | ||
| 292 | $self->traverse($nodeRef->children) if defined $nodeRef->children; | ||
| 293 | } | ||
| 294 | |||
| 295 | sub _openTagWithNID { | ||
| 296 | my ($self, $nodeRef) = @_; | ||
| 297 | $self->{outputString} .= '<'.$nodeRef->type.'>'; | ||
| 298 | $self->{outputString} .= $self->_anchor($nodeRef->id); | ||
| 299 | } | ||
| 300 | |||
| 301 | sub _closeTagWithNID { | ||
| 302 | my ($self, $nodeRef) = @_; | ||
| 303 | $self->{outputString} .= $self->_nid($nodeRef->id); | ||
| 304 | $self->{outputString} .= '</' . $nodeRef->type . ">\n"; | ||
| 305 | } | ||
| 306 | |||
| 307 | sub _openLinkTag { | ||
| 308 | my ($self, $nodeRef) = @_; | ||
| 309 | my $class = $nodeRef->class || 'extlink'; | ||
| 310 | my $href = $nodeRef->href; | ||
| 311 | $href =~ s/&/&/g; # for XHTML compliance | ||
| 312 | $self->{outputString} .= '<a href="'; | ||
| 313 | $self->{outputString} .= $href . qq(" class="$class">); | ||
| 314 | } | ||
| 315 | |||
| 316 | sub _wikiLink { | ||
| 317 | my ($self, $nodeRef) = @_; | ||
| 318 | my $pageName = $nodeRef->content; | ||
| 319 | my $linkString = ""; | ||
| 320 | my $pageNid; | ||
| 321 | |||
| 322 | if ($pageName =~ s/\#([A-Z0-9]+)$//) { | ||
| 323 | $pageNid = $1; | ||
| 324 | } | ||
| 325 | |||
| 326 | if ($nodeRef->content =~ /:/) { | ||
| 327 | $linkString .= '<a href="' . | ||
| 328 | PurpleWiki::Page::getInterWikiLink($pageName); | ||
| 329 | $linkString .= "#nid$pageNid" if $pageNid; | ||
| 330 | $linkString .= '" class="interwiki">' . $nodeRef->content . '</a>'; | ||
| 331 | } | ||
| 332 | elsif (&PurpleWiki::Page::exists($pageName)) { | ||
| 333 | if ($nodeRef->type eq 'freelink') { | ||
| 334 | $linkString .= '<a href="' . | ||
| 335 | PurpleWiki::Page::getFreeLink($nodeRef->content) . | ||
| 336 | '" class="freelink">'; | ||
| 337 | } else { | ||
| 338 | $linkString .= '<a href="' . | ||
| 339 | PurpleWiki::Page::getWikiWordLink($pageName); | ||
| 340 | $linkString .= "#nid$pageNid" if $pageNid; | ||
| 341 | $linkString .= '" class="wikiword">'; | ||
| 342 | } | ||
| 343 | $linkString .= $nodeRef->content . '</a>'; | ||
| 344 | } | ||
| 345 | else { | ||
| 346 | my $createLinkText = $self->{config}->CreateLinkText; | ||
| 347 | if ($nodeRef->type eq 'freelink') { | ||
| 348 | if ($createLinkText) { | ||
| 349 | my $linkText .= '[' . $nodeRef->content . ']'; | ||
| 350 | my $createLink = '<a href="' . | ||
| 351 | PurpleWiki::Page::getFreeLink($nodeRef->content) . | ||
| 352 | '" class="freelink">' . "$createLinkText</a>"; | ||
| 353 | if ($self->{config}->CreateLinkBefore) { | ||
| 354 | $linkString .= $createLink . $linkText; | ||
| 355 | } | ||
| 356 | else { | ||
| 357 | $linkString .= $linkText . $createLink; | ||
| 358 | } | ||
| 359 | } | ||
| 360 | else { | ||
| 361 | # the bracket syntax isn't necessary, because the | ||
| 362 | # entire text is linked | ||
| 363 | $linkString .= '<a href="' . | ||
| 364 | PurpleWiki::Page::getFreeLink($nodeRef->content) . | ||
| 365 | '" class="create">' . $nodeRef->content . | ||
| 366 | '</a>'; | ||
| 367 | } | ||
| 368 | } | ||
| 369 | else { | ||
| 370 | if ($createLinkText) { | ||
| 371 | my $linkText .= $nodeRef->content; | ||
| 372 | my $createLink .= '<a href="' . | ||
| 373 | &PurpleWiki::Page::getWikiWordLink($pageName) . | ||
| 374 | '" class="wikiword">' . "$createLinkText</a>"; | ||
| 375 | if ($self->{config}->CreateLinkBefore) { | ||
| 376 | $linkString .= $createLink . $linkText; | ||
| 377 | } | ||
| 378 | else { | ||
| 379 | $linkString .= $linkText . $createLink; | ||
| 380 | } | ||
| 381 | } | ||
| 382 | else { | ||
| 383 | $linkString .= '<a href="' . | ||
| 384 | &PurpleWiki::Page::getWikiWordLink($pageName) . | ||
| 385 | '" class="create">' . $nodeRef->content . '</a>'; | ||
| 386 | } | ||
| 387 | } | ||
| 388 | } | ||
| 389 | |||
| 390 | $self->{outputString} .= $linkString; | ||
| 391 | } | ||
| 392 | |||
| 393 | sub _quoteHtml { | ||
| 394 | my ($self, $nodeRef) = @_; | ||
| 395 | my $html = $nodeRef->content || ''; | ||
| 396 | |||
| 397 | $html =~ s/&/&/g; | ||
| 398 | $html =~ s/</</g; | ||
| 399 | $html =~ s/>/>/g; | ||
| 400 | |||
| 401 | if (1) { # Make an official option? | ||
| 402 | $html =~ s/&([#a-zA-Z0-9]+);/&$1;/g; # Allow character references | ||
| 403 | } | ||
| 404 | |||
| 405 | $self->{outputString} .= $html; | ||
| 406 | } | ||
| 407 | |||
| 408 | sub _headerLevel { | ||
| 409 | my $self = shift; | ||
| 410 | my $headerLevel = $self->{sectionDepth} + 1; | ||
| 411 | |||
| 412 | $headerLevel = 6 if $headerLevel > 6; | ||
| 413 | return $headerLevel; | ||
| 414 | } | ||
| 415 | |||
| 416 | # FIXME: goes to too much effort to avoid a void return | ||
| 417 | sub _anchor { | ||
| 418 | my ($self, $nid) = @_; | ||
| 419 | my $string = ''; | ||
| 420 | |||
| 421 | if ($nid) { | ||
| 422 | $string = '<a name="nid' . $nid . '" id="nid' . $nid . '"></a>'; | ||
| 423 | } | ||
| 424 | |||
| 425 | return $string; | ||
| 426 | } | ||
| 427 | |||
| 428 | # FIXME: goes to too much effort to avoid a void return | ||
| 429 | sub _nid { | ||
| 430 | my ($self, $nid) = @_; | ||
| 431 | my $string = ''; | ||
| 432 | |||
| 433 | |||
| 434 | if ($nid) { | ||
| 435 | my $nidFace = '#'; | ||
| 436 | if ($self->{config}->ShowNid) { | ||
| 437 | $nidFace = "($nid)"; | ||
| 438 | } | ||
| 439 | |||
| 440 | if ($self->{config}->ShowNid) { | ||
| 441 | $nidFace = "($nid)"; | ||
| 442 | } | ||
| 443 | $string = ' <a class="nid" ' . | ||
| 444 | 'title="' . "$nid" . '" href="' . | ||
| 445 | $self->{url} . '#nid' . | ||
| 446 | $nid . '">' . $nidFace . '</a>'; | ||
| 447 | } | ||
| 448 | |||
| 449 | return $string; | ||
| 450 | } | ||
| 451 | 1; | ||
| 452 | __END__ |