| 1 | # PurpleWiki::View::rawtext.pm | ||
| 2 | # | ||
| 3 | # $Id: rawtext.pm 366 2004-05-19 19:22:17Z 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::rawtext; | ||
| 31 | use 5.005; | ||
| 32 | use strict; | ||
| 33 | use warnings; | ||
| 34 | use PurpleWiki::Transclusion; | ||
| 35 | use PurpleWiki::View::Driver; | ||
| 36 | |||
| 37 | ############### Package Globals ############### | ||
| 38 | |||
| 39 | our $VERSION; | ||
| 40 | $VERSION = sprintf("%d", q$Id: rawtext.pm 366 2004-05-19 19:22:17Z eekim $ =~ /\s(\d+)\s/); | ||
| 41 | |||
| 42 | our @ISA = qw(PurpleWiki::View::Driver); | ||
| 43 | |||
| 44 | |||
| 45 | ############### Overloaded Methods ############### | ||
| 46 | |||
| 47 | sub new { | ||
| 48 | my $proto = shift; | ||
| 49 | my $class = ref($proto) || $proto; | ||
| 50 | my $self = $class->SUPER::new(@_); | ||
| 51 | |||
| 52 | # Object State | ||
| 53 | $self->{indentLevel} = 0; | ||
| 54 | $self->{outputString} = ""; | ||
| 55 | $self->{listType} = ""; | ||
| 56 | $self->{initialIndent} = ""; | ||
| 57 | $self->{subsequentIndent} = ""; | ||
| 58 | $self->{listNumber} = 1; | ||
| 59 | $self->{prevDefType} = ""; | ||
| 60 | $self->{links} = []; | ||
| 61 | $self->{transcluder} = new PurpleWiki::Transclusion( | ||
| 62 | config => $self->{config}, | ||
| 63 | url => $self->{url}); | ||
| 64 | |||
| 65 | bless($self, $class); | ||
| 66 | return $self; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub view { | ||
| 70 | my ($self, $wikiTree) = @_; | ||
| 71 | |||
| 72 | $self->SUPER::view($wikiTree); | ||
| 73 | |||
| 74 | return $self->{outputString}; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub Pre { | ||
| 78 | my ($self, $nodeRef) = @_; | ||
| 79 | if ($nodeRef->type =~ /^(ul|ol|dl|indent|section)$/) { | ||
| 80 | $self->{indentLevel}++; | ||
| 81 | } | ||
| 82 | $self->SUPER::Pre($nodeRef); | ||
| 83 | } | ||
| 84 | |||
| 85 | sub Post { | ||
| 86 | my ($self, $nodeRef) = @_; | ||
| 87 | if ($nodeRef->type =~ /^(ul|ol|dl|indent|section)$/) { | ||
| 88 | $self->{indentLevel}--; | ||
| 89 | } | ||
| 90 | $self->SUPER::Post($nodeRef); | ||
| 91 | } | ||
| 92 | |||
| 93 | sub sectionPre { shift->_setIndent(@_) } | ||
| 94 | sub indentPre { shift->_setIndent(@_) } | ||
| 95 | sub ulPre { shift->_setIndent(@_) } | ||
| 96 | sub olPre { shift->_setIndent(@_) } | ||
| 97 | sub dlPre { shift->_setIndent(@_) } | ||
| 98 | |||
| 99 | sub ulMain { shift->_recurseList(@_) } | ||
| 100 | sub olMain { shift->_recurseList(@_) } | ||
| 101 | |||
| 102 | sub hPre { shift->_newLineSetIndent(@_) } | ||
| 103 | sub pPre { shift->_newLineSetIndent(@_) } | ||
| 104 | sub liPre { shift->_newLineSetIndent(@_) } | ||
| 105 | sub dtPre { shift->_newLineSetIndent(@_) } | ||
| 106 | sub prePre { shift->_newLineSetIndent(@_) } | ||
| 107 | |||
| 108 | sub hMain { shift->_structuralContent(@_) } | ||
| 109 | sub pMain { shift->_structuralContent(@_) } | ||
| 110 | sub liMain { shift->_structuralContent(@_) } | ||
| 111 | sub dtMain { shift->_structuralContent(@_) } | ||
| 112 | sub ddMain { shift->_structuralContent(@_) } | ||
| 113 | sub preMain { shift->_structuralContent(@_) } | ||
| 114 | |||
| 115 | sub hPost { shift->{outputString} .= "\n" } | ||
| 116 | sub pPost { shift->{outputString} .= "\n" } | ||
| 117 | sub liPost { shift->{outputString} .= "\n" } | ||
| 118 | sub prePost { shift->{outputString} .= "\n" } | ||
| 119 | |||
| 120 | sub dtPost { | ||
| 121 | my $self = shift; | ||
| 122 | $self->{prevDefType} = 'dt'; | ||
| 123 | $self->{outputString} .= "\n"; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub ddPre { | ||
| 127 | my $self = shift; | ||
| 128 | $self->_setIndent(@_); | ||
| 129 | if ($self->{prevDefType} eq 'dd') { | ||
| 130 | $self->{outputString} .= "\n"; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | sub ddPost { | ||
| 135 | my $self = shift; | ||
| 136 | $self->{prevDefType} = 'dd'; | ||
| 137 | $self->{outputString} .= "\n"; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub bPre { shift->{outputString} .= "*" } | ||
| 141 | sub bPost { shift->{outputString} .= "*" } | ||
| 142 | |||
| 143 | sub iPre { shift->{outputString} .= "_" } | ||
| 144 | sub iPost { shift->{outputString} .= "_" } | ||
| 145 | |||
| 146 | sub textMain { shift->{outputString} .= shift->content } | ||
| 147 | sub nowikiMain { shift->{outputString} .= shift->content } | ||
| 148 | sub transclusionMain { | ||
| 149 | my ($self, $nodeRef) = @_; | ||
| 150 | my $transcluded = $self->{transcluder}->get($nodeRef->content); | ||
| 151 | if (ref $transcluded) { | ||
| 152 | # Add the transcluded content into our tree if it's a reference | ||
| 153 | $self->traverse($transcluded->content); | ||
| 154 | } else { | ||
| 155 | # Add the transcluded string to our output if it isn't a reference | ||
| 156 | $self->{outputString} .= $transcluded; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | sub linkMain { shift->{outputString} .= shift->content } | ||
| 160 | |||
| 161 | sub transclusionPre { shift->{outputString} .= "transclude: " } | ||
| 162 | |||
| 163 | sub urlPre { shift->{outputString} .= shift->content } | ||
| 164 | sub wikiwordPre { shift->{outputString} .= shift->content } | ||
| 165 | sub freelinkPre { shift->{outputString} .= shift->content } | ||
| 166 | sub imagePre { shift->{outputString} .= shift->content } | ||
| 167 | |||
| 168 | |||
| 169 | ############### Private Methods ############### | ||
| 170 | |||
| 171 | sub _recurseList { | ||
| 172 | my ($self, $nodeRef) = @_; | ||
| 173 | $self->{listType} = $nodeRef->type; | ||
| 174 | $self->{listNumber} = 1 if $nodeRef->type eq 'ol'; | ||
| 175 | $self->recurse($nodeRef); | ||
| 176 | } | ||
| 177 | |||
| 178 | sub _newLineSetIndent { | ||
| 179 | my $self = shift; | ||
| 180 | $self->_setIndent(@_); | ||
| 181 | $self->{outputString} .= "\n"; | ||
| 182 | } | ||
| 183 | |||
| 184 | sub _structuralContent { | ||
| 185 | my ($self, $nodeRef) = @_; | ||
| 186 | |||
| 187 | if ($nodeRef->content) { | ||
| 188 | my $tmp = $self->{outputString}; | ||
| 189 | $self->{outputString} = ""; | ||
| 190 | $self->traverse($nodeRef->content); | ||
| 191 | my $nodeString = $self->{outputString}; | ||
| 192 | $self->{outputString} = $tmp; | ||
| 193 | if ($nodeRef->type eq 'li') { | ||
| 194 | if ($self->{listType} eq 'ul') { | ||
| 195 | $nodeString = "* $nodeString"; | ||
| 196 | } | ||
| 197 | elsif ($self->{listType} eq 'ol') { | ||
| 198 | $nodeString = $self->{listNumber}.". $nodeString"; | ||
| 199 | $self->{listNumber}++; | ||
| 200 | } | ||
| 201 | } | ||
| 202 | if ($nodeRef->type eq 'pre') { | ||
| 203 | $self->{outputString} .= $nodeString; | ||
| 204 | } else { | ||
| 205 | $self->{outputString} .= $nodeString; | ||
| 206 | } | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | sub _setIndent { | ||
| 211 | my ($self, $nodeRef) = @_; | ||
| 212 | |||
| 213 | my $indent; | ||
| 214 | my $initialOffset = 1; | ||
| 215 | my $subsequentOffset = 1; | ||
| 216 | my $subsequentMore = 0; | ||
| 217 | my $listMore = 0; | ||
| 218 | |||
| 219 | if ($nodeRef->type eq 'li') { | ||
| 220 | $initialOffset = 2; | ||
| 221 | $subsequentOffset = 2; | ||
| 222 | $listMore = 2; | ||
| 223 | |||
| 224 | if ($self->{listType} eq 'ul') { | ||
| 225 | $subsequentMore = 2; | ||
| 226 | } elsif ($self->{listType} eq 'ol') { | ||
| 227 | $subsequentMore = 3; | ||
| 228 | } | ||
| 229 | } elsif ($nodeRef->type eq 'dt') { | ||
| 230 | $initialOffset = 2; | ||
| 231 | $subsequentOffset = 2; | ||
| 232 | } | ||
| 233 | |||
| 234 | $indent = 4*$self->{indentLevel} - 4*$initialOffset + $listMore; | ||
| 235 | $self->{initialIndent} = ' ' x $indent; | ||
| 236 | |||
| 237 | $indent = 4*$self->{indentLevel} - 4*$subsequentOffset + $subsequentMore | ||
| 238 | + $listMore; | ||
| 239 | $self->{subsequentIndent} = ' ' x $indent; | ||
| 240 | } | ||
| 241 | 1; | ||
| 242 | __END__ |