lib/PurpleWiki/View/wikitext.pm untested

filter: Statementstmt 0.0% 0 / 204 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 63 Podpod 0.0% 0 / 58 totaltotal 0.0% 0 / 393 CC 25 SCAR 24.8
CC
25
Cov
0%
_header
72.0
_liRecurse
30.0
_hardRule
20.0
CRAP 12.0
1 # PurpleWiki::View::wikitext.pm
2 #
3 # $Id: wikitext.pm 381 2004-05-31 19:21:37Z 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::wikitext;
31 use 5.005;
32 use strict;
33 use warnings;
34 use Carp;
35 use PurpleWiki::View::Driver;
36  
37 ############### Package Globals ###############
38  
39 our $VERSION;
40 $VERSION = sprintf("%d", q$Id: wikitext.pm 381 2004-05-31 19:21:37Z 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->{outputString} = "";
54 $self->{indentDepth} = 0;
55 $self->{listStack} = [];
56  
57 # standard flag for determining whether or not a hard rule should
58 # be printed
59 $self->{isPrevSection} = 0;
60  
61 # special case flag for handling hard rules (or not) at the
62 # beginning of a document
63 $self->{isStart} = 1;
64 $self->{emptyFirstSection} = 0;
65  
66 # used for determining whether there should be hard rules with
67 # nested sections
68 $self->{sectionDepth} = 0;
69 $self->{depthLastClosedSection} = 0;
70  
71 # needed for determining whether a WikiWord needs to be delimited
72 # from subsequent text
73 $self->{lastInlineProcessed} = "";
74  
75 bless($self, $class);
76 return $self;
77 }
78  
79 sub view {
80 my ($self, $wikiTree) = @_;
81 $self->SUPER::view($wikiTree);
82 $self->{outputString} = $self->_header($wikiTree) . $self->{outputString};
83 chomp $self->{outputString};
84 return $self->{outputString};
85 }
86  
87 # The most general case for generating hard rules is as follows: If
88 # the first structured node of a new section (with the exception of
89 # the very first section) does not begin with an h node, then print a
90 # hard rule. The trickiness is dealing with the exceptions: sections
91 # at the beginning of a document and nested subsections.
92 #
93 #
94 #
95 # When you create headers that are more than one level deeper than the
96 # previous section, you get things like:
97 #
98 # section:
99 # h:section 1
100 # section:
101 # section:
102 # h:section 1.1.1
103 #
104 # Note the two sections immediately nested after each other. A
105 # similar effect is possible when you have a hard rule followed by a
106 # header that is one or more levels deeper:
107 #
108 # section:
109 # h:section 1
110 # section:
111 # section:
112 # h:section 2.1
113 #
114 # Because of these two possible scenarios, it's not enough to simply
115 # print a hard rule whenever you see nested sections one right after
116 # the other.
117  
118 sub sectionPre {
119 my $self = shift;
120 $self->{sectionDepth}++;
121 $self->_hardRule(1);
122 $self->{isPrevSection} = 1;
123 }
124  
125 sub sectionPost {
126 my $self = shift;
127 $self->{depthLastClosedSection} = $self->{sectionDepth};
128 $self->{sectionDepth}--;
129 $self->{lastInlineProcessed} = '';
130 $self->{emptyFirstSection} = 1
131 if ($self->{isStart} && $self->{isPrevSection});
132 $self->_hardRule(0);
133 $self->{isStart} = 0;
134 }
135  
136 sub indentPre {
137 my $self = shift;
138 $self->_hardRule(0);
139 $self->{indentDepth}++;
140 }
141  
142 sub indentPost {
143 my $self = shift;
144 $self->{indentDepth}--;
145 $self->{lastInlineProcessed} = '';
146 $self->{outputString} .= "\n" if $self->{indentDepth} == 0;
147 }
148  
149 sub ulPre {
150 my ($self, $nodeRef) = @_;
151 $self->_hardRule(0);
152 push @{$self->{listStack}}, $nodeRef->type;
153 }
154  
155 sub olPre {
156 my ($self, $nodeRef) = @_;
157 $self->_hardRule(0);
158 push @{$self->{listStack}}, $nodeRef->type;
159 }
160  
161 sub dlPre {
162 my ($self, $nodeRef) = @_;
163 $self->_hardRule(0);
164 push @{$self->{listStack}}, $nodeRef->type;
165 }
166  
167 sub ulPost { shift->_endList(@_) }
168 sub olPost { shift->_endList(@_) }
169 sub dlPost { shift->_endList(@_) }
170  
171 sub hPre {
172 my $self = shift;
173 if ($self->{emptyFirstSection}) {
174 $self->{isPrevSection} = 1;
175 $self->{emptyFirstSection} = 0;
176 $self->_hardRule(0);
177 }
178 else {
179 $self->{isPrevSection} = 0;
180 }
181 $self->{outputString} .= '=' x $self->{sectionDepth}. ' ';
182 }
183  
184 sub hPost {
185 my ($self, $nodeRef) = @_;
186 $self->{lastInlineProcessed} = '';
187 $self->{outputString} .= $self->_nid($nodeRef->id) . " ";
188 $self->{outputString} .= '=' x $self->{sectionDepth} . "\n\n";
189 }
190  
191 sub pPre {
192 my $self = shift;
193 $self->_hardRule(0);
194 $self->{outputString} .= ':' x $self->{indentDepth};
195 }
196  
197 sub pPost {
198 my ($self, $nodeRef) = @_;
199 $self->{lastInlineProcessed} = '';
200 $self->{outputString} .= $self->_nid($nodeRef->id) . "\n";
201 $self->{outputString} .= "\n" if $self->{indentDepth} == 0;
202 }
203  
204 sub liPre {
205 my $self = shift;
206 my $bullet = ($self->{listStack}->[-1] eq 'ul') ? '*' : '#';
207 $self->{outputString} .= $bullet x scalar(@{$self->{listStack}}) . ' ';
208 }
209  
210 sub dtPre {
211 my $self = shift;
212 $self->{outputString} .= ';' x scalar(@{$self->{listStack}});
213 }
214  
215 sub ddPre {
216 shift->{outputString} .= ':'
217 }
218  
219 sub liMain { shift->_liRecurse(@_) }
220 sub ddMain { shift->_liRecurse(@_) }
221  
222 sub dtPost { shift->_showNID(@_) }
223  
224 sub prePre {
225 my $self = shift;
226 $self->_hardRule(0);
227 $self->{outputString} .= "{{{\n";
228 }
229  
230 sub prePost {
231 my $self = shift;
232 $self->_showNID(@_);
233 $self->{outputString} .= "}}}\n\n";
234 }
235  
236 sub sketchMain {
237 shift->{outputString} .= "{sketch}\n\n";
238 }
239  
240 sub bPre {
241 shift->{outputString} .= "'''";
242 }
243  
244 sub bPost {
245 my $self = shift;
246 $self->{lastInlineProcessed} = 'b';
247 $self->{outputString} .= "'''";
248 }
249  
250 sub iPre {
251 shift->{outputString} .= "''";
252 }
253  
254 sub iPost {
255 my $self = shift;
256 $self->{lastInlineProcessed} = 'i';
257 $self->{outputString} .= "''";
258 }
259  
260 sub ttPre {
261 shift->{outputString} .= "{{{";
262 }
263  
264 sub ttPost {
265 my $self = shift;
266 $self->{lastInlineProcessed} = 'tt';
267 $self->{outputString} .= "}}}";
268 }
269  
270 sub textPre {
271 my ($self, $nodeRef) = @_;
272 if ($self->{lastInlineProcessed} eq 'wikiword') {
273 if ($nodeRef->content =~ /^\w/) {
274 $self->{outputString} .= '""';
275 }
276 }
277 }
278  
279 sub textMain { shift->{outputString} .= shift->content }
280 sub nowikiMain { shift->{outputString} .= shift->content }
281 sub transclusionMain { shift->{outputString} .= shift->content }
282 sub linkMain { shift->{outputString} .= shift->content }
283 sub urlMain { shift->{outputString} .= shift->content }
284 sub wikiwordMain { shift->{outputString} .= shift->content }
285 sub freelinkMain { shift->{outputString} .= shift->content }
286 sub imageMain { shift->{outputString} .= shift->content }
287  
288 sub textPost { shift->{lastInlineProcessed} = 'text' }
289  
290 sub nowikiPre { shift->{outputString} .= '<nowiki>' }
291  
292 sub nowikiPost {
293 my $self = shift;
294 $self->{lastInlineProcessed} = 'nowiki';
295 $self->{outputString} .= '</nowiki>';
296 }
297  
298 sub transclusionPre {
299 shift->{outputString} .= '[t ';
300 }
301  
302 sub transclusionPost {
303 my $self = shift;
304 $self->{lastInlineProcessed} = 'transclusion';
305 $self->{outputString} .= ']';
306 }
307  
308 sub linkPre {
309 my ($self, $nodeRef) = @_;
310 $self->{outputString} .= '[' . $nodeRef->href . ' ';
311 }
312  
313 sub linkPost {
314 my $self = shift;
315 $self->{lastInlineProcessed} = 'link';
316 $self->{outputString} .= ']';
317 }
318  
319 sub urlPost {
320 shift->{lastInlineProcessed} = 'url';
321 }
322  
323 sub wikiwordPost {
324 shift->{lastInlineProcessed} = 'wikiword';
325 }
326  
327 sub freelinkPre {
328 shift->{outputString} .= '[[';
329 }
330  
331 sub freelinkPost {
332 my $self = shift;
333 $self->{lastInlineProcessed} = 'freelink';
334 $self->{outputString} .= ']]';
335 }
336  
337 sub imagePost {
338 shift->{lastInlineProcessed} = 'image';
339 }
340  
341  
342 ############### Private Methods ###############
343  
344 sub _hardRule {
345 my ($self, $isSection) = @_;
346  
347 if ($self->{isPrevSection}) {
348 if (!$self->{isStart}) {
349 if (!$isSection || ($isSection &&
350 $self->{sectionDepth} == $self->{depthLastClosedSection} + 1) ) {
351 $self->{outputString} .= "----\n\n";
352 }
353 }
354 $self->{isPrevSection} = 0;
355 }
356 }
357  
358 sub _liRecurse { # also used for dd
359 my ($self, $nodeRef) = @_;
360  
361 if (!defined $nodeRef) {
362 carp "Warning: tried to recurse on an undefined node\n";
363 return;
364 }
365 if ($nodeRef->isa('PurpleWiki::StructuralNode')) {
366 $self->traverse($nodeRef->content) if defined $nodeRef->content;
367 }
368 # display NID here
369 $self->_showNID($nodeRef);
370 $self->traverse($nodeRef->children) if defined $nodeRef->children;
371 }
372  
373 sub _endList {
374 my $self = shift;
375 pop @{$self->{listStack}};
376 $self->{lastInlineProcessed} = '';
377 $self->{outputString} .= "\n" if scalar @{$self->{listStack}} == 0;
378 }
379  
380 sub _showNID {
381 my ($self, $nodeRef) = @_;
382 my $nidString = $self->_nid($nodeRef->id);
383  
384 $self->{lastInlineProcessed} = '';
385  
386 if ($nodeRef->type eq 'dt') {
387 $self->{outputString} .= $nidString;
388 # } elsif ($nodeRef->type eq 'pre') {
389 # $self->{outputString} .= $nidString . "\n\n";
390 } else {
391 $self->{outputString} .= $nidString . "\n";
392 }
393 }
394  
395 sub _nid {
396 my ($self, $nid) = @_;
397 my $string = ''; # avoid a unitialized value warning
398 if ($nid) {
399 $string = " {nid $nid}";
400 }
401 return $string;
402 }
403  
404 sub _header {
405 my ($self, $wikiTree) = @_;
406 my $header = '';
407  
408 # FIXME: this can be a loop
409 if ($wikiTree->title) {
410 $header .= '{title ' . $wikiTree->title . "}\n";
411 }
412  
413 if ($wikiTree->subtitle) {
414 $header .= '{subtitle ' . $wikiTree->subtitle . "}\n";
415 }
416  
417 if ($wikiTree->authors) {
418 foreach my $author (@{$wikiTree->authors}) {
419 $header .= '{author ' . $author->[0];
420 if (scalar @{$author} > 1) {
421 $header .= ' ' . $author->[1];
422 }
423 $header .= "}\n";
424 }
425 }
426  
427 if ($wikiTree->id) {
428 $header .= '{docid ' . $wikiTree->id . "}\n";
429 }
430  
431 if ($wikiTree->version) {
432 $header .= '{version ' . $wikiTree->version . "}\n";
433 }
434  
435 if ($wikiTree->date) {
436 $header .= '{date ' . $wikiTree->date . "}\n";
437 }
438  
439 return $header;
440 }
441 1;
442 __END__