]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/BlockParser.php
Changes for cached markup.
[SourceForge/phpwiki.git] / lib / BlockParser.php
1 <?php rcs_id('$Id: BlockParser.php,v 1.35 2003-02-21 04:09:11 dairiki Exp $');
2 /* Copyright (C) 2002, Geoffrey T. Dairiki <dairiki@dairiki.org>
3  *
4  * This file is part of PhpWiki.
5  * 
6  * PhpWiki is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * 
11  * PhpWiki is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License
17  * along with PhpWiki; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 require_once('lib/HtmlElement.php');
21 require_once('lib/CachedMarkup.php');
22 require_once('lib/InlineParser.php');
23
24 ////////////////////////////////////////////////////////////////
25 //
26 //
27
28 /**
29  * FIXME:
30  *  Still to do:
31  *    (old-style) tables
32  */
33
34 // FIXME: unify this with the RegexpSet in InlinePArser.
35
36 /**
37  * Return type from RegexpSet::match and RegexpSet::nextMatch.
38  *
39  * @see RegexpSet
40  */
41 class AnchoredRegexpSet_match {
42     /**
43      * The matched text.
44      */
45     var $match;
46
47     /**
48      * The text following the matched text.
49      */
50     var $postmatch;
51
52     /**
53      * Index of the regular expression which matched.
54      */
55     var $regexp_ind;
56 }
57
58 /**
59  * A set of regular expressions.
60  *
61  * This class is probably only useful for InlineTransformer.
62  */
63 class AnchoredRegexpSet
64 {
65     /** Constructor
66      *
67      * @param $regexps array A list of regular expressions.  The
68      * regular expressions should not include any sub-pattern groups
69      * "(...)".  (Anonymous groups, like "(?:...)", as well as
70      * look-ahead and look-behind assertions are fine.)
71      */
72     function AnchoredRegexpSet ($regexps) {
73         $this->_regexps = $regexps;
74         $this->_re = "/((" . join(")|(", $regexps) . "))/Ax";
75     }
76
77     /**
78      * Search text for the next matching regexp from the Regexp Set.
79      *
80      * @param $text string The text to search.
81      *
82      * @return object  A RegexpSet_match object, or false if no match.
83      */
84     function match ($text) {
85         if (! preg_match($this->_re, $text, $m)) {
86             return false;
87         }
88         
89         $match = new AnchoredRegexpSet_match;
90         $match->postmatch = substr($text, strlen($m[0]));
91         $match->match = $m[1];
92         $match->regexp_ind = count($m) - 3;
93         return $match;
94     }
95
96     /**
97      * Search for next matching regexp.
98      *
99      * Here, 'next' has two meanings:
100      *
101      * Match the next regexp(s) in the set, at the same position as the last match.
102      *
103      * If that fails, match the whole RegexpSet, starting after the position of the
104      * previous match.
105      *
106      * @param $text string Text to search.
107      *
108      * @param $prevMatch A RegexpSet_match object
109      *
110      * $prevMatch should be a match object obtained by a previous
111      * match upon the same value of $text.
112      *
113      * @return object  A RegexpSet_match object, or false if no match.
114      */
115     function nextMatch ($text, $prevMatch) {
116         // Try to find match at same position.
117         $regexps = array_slice($this->_regexps, $prevMatch->regexp_ind + 1);
118         if (!$regexps) {
119             return false;
120         }
121
122         $pat= "/ ( (" . join(')|(', $regexps) . ") ) /Axs";
123
124         if (! preg_match($pat, $text, $m)) {
125             return false;
126         }
127         
128         $match = new AnchoredRegexpSet_match;
129         $match->postmatch = substr($text, strlen($m[0]));
130         $match->match = $m[1];
131         $match->regexp_ind = count($m) - 3 + $prevMatch->regexp_ind + 1;;
132         return $match;
133     }
134 }
135
136
137     
138 class BlockParser_Input {
139
140     function BlockParser_Input ($text) {
141         
142         // Expand leading tabs.
143         // FIXME: do this better.
144         //
145         // We want to ensure the only characters matching \s are ' ' and "\n".
146         //
147         $text = preg_replace('/(?![ \n])\s/', ' ', $text);
148         assert(!preg_match('/(?![ \n])\s/', $text));
149
150         $this->_lines = preg_split('/[^\S\n]*\n/', $text);
151         $this->_pos = 0;
152
153         // Strip leading blank lines.
154         while ($this->_lines and ! $this->_lines[0])
155             array_shift($this->_lines);
156         $this->_atSpace = false;
157     }
158
159     function skipSpace () {
160         $nlines = count($this->_lines);
161         while (1) {
162             if ($this->_pos >= $nlines) {
163                 $this->_atSpace = false;
164                 break;
165             }
166             if ($this->_lines[$this->_pos] != '')
167                 break;
168             $this->_pos++;
169             $this->_atSpace = true;
170         }
171         return $this->_atSpace;
172     }
173         
174     function currentLine () {
175         if ($this->_pos >= count($this->_lines)) {
176             return false;
177         }
178         return $this->_lines[$this->_pos];
179     }
180         
181     function nextLine () {
182         $this->_atSpace = $this->_lines[$this->_pos++] === '';
183         if ($this->_pos >= count($this->_lines)) {
184             return false;
185         }
186         return $this->_lines[$this->_pos];
187     }
188
189     function advance () {
190         $this->_atSpace = $this->_lines[$this->_pos++] === '';
191     }
192     
193     function getPos () {
194         return array($this->_pos, $this->_atSpace);
195     }
196
197     function setPos ($pos) {
198         list($this->_pos, $this->_atSpace) = $pos;
199     }
200
201     function getPrefix () {
202         return '';
203     }
204
205     function getDepth () {
206         return 0;
207     }
208
209     function where () {
210         if ($this->_pos < count($this->_lines))
211             return $this->_lines[$this->_pos];
212         else
213             return "<EOF>";
214     }
215     
216     function _debug ($tab, $msg) {
217         //return ;
218         $where = $this->where();
219         $tab = str_repeat('____', $this->getDepth() ) . $tab;
220         printXML(HTML::div("$tab $msg: at: '",
221                            HTML::tt($where),
222                            "'"));
223     }
224 }
225
226 class BlockParser_InputSubBlock extends BlockParser_Input
227 {
228     function BlockParser_InputSubBlock (&$input, $prefix_re, $initial_prefix = false) {
229         $this->_input = &$input;
230         $this->_prefix_pat = "/$prefix_re|\\s*\$/Ax";
231         $this->_atSpace = false;
232
233         if (($line = $input->currentLine()) === false)
234             $this->_line = false;
235         elseif ($initial_prefix) {
236             assert(substr($line, 0, strlen($initial_prefix)) == $initial_prefix);
237             $this->_line = (string) substr($line, strlen($initial_prefix));
238             $this->_atBlank = ! ltrim($line);
239         }
240         elseif (preg_match($this->_prefix_pat, $line, $m)) {
241             $this->_line = (string) substr($line, strlen($m[0]));
242             $this->_atBlank = ! ltrim($line);
243         }
244         else
245             $this->_line = false;
246     }
247
248     function skipSpace () {
249         // In contrast to the case for top-level blocks,
250         // for sub-blocks, there never appears to be any trailing space.
251         // (The last block in the sub-block should always be of class tight-bottom.)
252         while ($this->_line === '')
253             $this->advance();
254
255         if ($this->_line === false)
256             return $this->_atSpace == 'strong_space';
257         else
258             return $this->_atSpace;
259     }
260         
261     function currentLine () {
262         return $this->_line;
263     }
264
265     function nextLine () {
266         if ($this->_line === '')
267             $this->_atSpace = $this->_atBlank ? 'weak_space' : 'strong_space';
268         else
269             $this->_atSpace = false;
270
271         $line = $this->_input->nextLine();
272         if ($line !== false && preg_match($this->_prefix_pat, $line, $m)) {
273             $this->_line = (string) substr($line, strlen($m[0]));
274             $this->_atBlank = ! ltrim($line);
275         }
276         else
277             $this->_line = false;
278
279         return $this->_line;
280     }
281
282     function advance () {
283         $this->nextLine();
284     }
285         
286     function getPos () {
287         return array($this->_line, $this->_atSpace, $this->_input->getPos());
288     }
289
290     function setPos ($pos) {
291         $this->_line = $pos[0];
292         $this->_atSpace = $pos[1];
293         $this->_input->setPos($pos[2]);
294     }
295     
296     function getPrefix () {
297         assert ($this->_line !== false);
298         $line = $this->_input->currentLine();
299         assert ($line !== false && strlen($line) >= strlen($this->_line));
300         return substr($line, 0, strlen($line) - strlen($this->_line));
301     }
302
303     function getDepth () {
304         return $this->_input->getDepth() + 1;
305     }
306
307     function where () {
308         return $this->_input->where();
309     }
310 }
311     
312
313 class Block_HtmlElement extends HtmlElement
314 {
315     function Block_HtmlElement($tag /*, ... */) {
316         $this->_init(func_get_args());
317     }
318
319
320     function setTightness($top, $bottom) {
321         $this->setInClass('tightenable');
322         $this->setInClass('top', $top);
323         $this->setInClass('bottom', $bottom);
324     }
325 }
326
327 class ParsedBlock extends Block_HtmlElement {
328     
329     function ParsedBlock (&$input, $tag = 'div', $attr = false) {
330         $this->Block_HtmlElement($tag, $attr);
331         $this->_initBlockTypes();
332         $this->_parse($input);
333     }
334
335     function _parse (&$input) {
336         for ($block = $this->_getBlock($input); $block; $block = $nextBlock) {
337             while ($nextBlock = $this->_getBlock($input)) {
338                 // Attempt to merge current with following block.
339                 if (! ($merged = $block->merge($nextBlock)) ) {
340                     break;      // can't merge
341                 }
342                 $block = $merged;
343             }
344             $this->pushContent($block->finish());
345         }
346     }
347
348     // FIXME: hackish
349     function _initBlockTypes () {
350         foreach (array('oldlists', 'list', 'dl', 'table_dl',
351                        'blockquote', 'heading', 'hr', 'pre', 'email_blockquote',
352                        'plugin', 'p')
353                  as $type) {
354             $class = "Block_$type";
355             $proto = new $class;
356             $this->_block_types[] = $proto;
357             $this->_regexps[] = $proto->_re;
358         }
359         $this->_regexpset = new AnchoredRegexpSet($this->_regexps);
360     }
361
362     function _getBlock (&$input) {
363         $this->_atSpace = $input->skipSpace();
364
365         if (! ($line = $input->currentLine()) )
366             return false;
367
368         $tight_top = !$this->_atSpace;
369         $re_set = &$this->_regexpset;
370         for ($m = $re_set->match($line); $m; $m = $re_set->nextMatch($line, $m)) {
371             $block = $this->_block_types[$m->regexp_ind];
372             //$input->_debug('>', get_class($block));
373             
374             if ($block->_match($input, $m)) {
375                 //$input->_debug('<', get_class($block));
376                 $tight_bottom = ! $input->skipSpace();
377                 $block->_setTightness($tight_top, $tight_bottom);
378                 return $block;
379             }
380             //$input->_debug('[', "_match failed");
381         }
382
383         trigger_error("Couldn't match block: '$line'", E_USER_NOTICE);
384         return false;
385     }
386 }
387
388 class WikiText extends ParsedBlock {
389     function WikiText ($text) {
390         $input = new BlockParser_Input($text);
391         $this->ParsedBlock($input);
392     }
393 }
394
395 class SubBlock extends ParsedBlock {
396     function SubBlock (&$input, $indent_re, $initial_indent = false,
397                        $tag = 'div', $attr = false) {
398         $subinput = new BlockParser_InputSubBlock($input, $indent_re, $initial_indent);
399         $this->ParsedBlock($subinput, $tag, $attr);
400     }
401 }
402
403 /**
404  * TightSubBlock is for use in parsing lists item bodies.
405  *
406  * If the sub-block consists of a single paragraph, it omits
407  * the paragraph element.
408  *
409  * We go to this trouble so that "tight" lists look somewhat reasonable
410  * in older (non-CSS) browsers.  (If you don't do this, then, without
411  * CSS, you only get "loose" lists.
412  */
413 class TightSubBlock extends SubBlock {
414     function TightSubBlock (&$input, $indent_re, $initial_indent = false,
415                             $tag = 'div', $attr = false) {
416         $this->SubBlock($input, $indent_re, $initial_indent, $tag, $attr);
417
418         // If content is a single paragraph, eliminate the paragraph...
419         if (count($this->_content) == 1) {
420             $elem = $this->_content[0];
421             if ($elem->getTag() == 'p') {
422                 assert($elem->getAttr('class') == 'tightenable top bottom');
423                 $this->setContent($elem->getContent());
424             }
425         }
426     }
427 }
428
429 class BlockMarkup {
430     var $_re;
431
432     function _match (&$input, $match) {
433         trigger_error('pure virtual', E_USER_ERROR);
434     }
435
436     function _setTightness ($top, $bot) {
437         $this->_element->setTightness($top, $bot);
438     }
439
440     function merge ($followingBlock) {
441         return false;
442     }
443
444     function finish () {
445         return $this->_element;
446     }
447 }
448
449 class Block_blockquote extends BlockMarkup
450 {
451     var $_depth;
452
453     var $_re = '\ +(?=\S)';
454
455     function _match (&$input, $m) {
456         $this->_depth = strlen($m->match);
457         $indent = sprintf("\\ {%d}", $this->_depth);
458         $this->_element = new SubBlock($input, $indent, $m->match,
459                                        'blockquote');
460         return true;
461     }
462     
463     function merge ($nextBlock) {
464         if (get_class($nextBlock) == get_class($this)) {
465             assert ($nextBlock->_depth < $this->_depth);
466             $nextBlock->_element->unshiftContent($this->_element);
467             $nextBlock->_tight_top = $this->_tight_top;
468             return $nextBlock;
469         }
470         return false;
471     }
472 }
473
474 class Block_list extends BlockMarkup
475 {
476     //var $_tag = 'ol' or 'ul';
477     var $_re = '\ {0,4}
478                 (?: \+
479                   | \\# (?!\[.*\])
480                   | -(?!-)
481                   | [o](?=\ )
482                   | [*] (?! \S[^*]*(?<=\S)[*](?!\S) )
483                 )\ *(?=\S)';
484
485     var $_content = array();
486
487     function _match (&$input, $m) {
488         // A list as the first content in a list is not allowed.
489         // E.g.:
490         //   *  * Item
491         // Should markup as <ul><li>* Item</li></ul>,
492         // not <ul><li><ul><li>Item</li></ul>/li></ul>.
493         //
494         if (preg_match('/[*#+-o]/', $input->getPrefix())) {
495             return false;
496         }
497         
498         $prefix = $m->match;
499         $indent = sprintf("\\ {%d}", strlen($prefix));
500
501         $bullet = trim($m->match);
502         $this->_tag = $bullet == '#' ? 'ol' : 'ul';
503         $this->_content[] = new TightSubBlock($input, $indent, $m->match, 'li');
504         return true;
505     }
506
507     function _setTightness($top, $bot) {
508         $li = &$this->_content[0];
509         $li->setTightness($top, $bot);
510     }
511     
512     function merge ($nextBlock) {
513         if (isa($nextBlock, 'Block_list') && $this->_tag == $nextBlock->_tag) {
514             array_splice($this->_content, count($this->_content), 0,
515                          $nextBlock->_content);
516             return $this;
517         }
518         return false;
519     }
520
521     function finish () {
522         return new Block_HtmlElement($this->_tag, false, $this->_content);
523     }
524 }
525
526 class Block_dl extends Block_list
527 {
528     var $_tag = 'dl';
529
530     function Block_dl () {
531         $this->_re = '\ {0,4}\S.*(?<!'.ESCAPE_CHAR.'):\s*$';
532     }
533
534     function _match (&$input, $m) {
535         if (!($p = $this->_do_match($input, $m)))
536             return false;
537         list ($term, $defn, $loose) = $p;
538
539         $this->_content[] = new Block_HtmlElement('dt', false, $term);
540         $this->_content[] = $defn;
541         $this->_tight_defn = !$loose;
542         return true;
543     }
544
545     function _setTightness($top, $bot) {
546         $dt = &$this->_content[0];
547         $dd = &$this->_content[1];
548
549         $dt->setTightness($top, $this->_tight_defn);
550         $dd->setTightness($this->_tight_defn, $bot);
551     }
552
553     function _do_match (&$input, $m) {
554         $pos = $input->getPos();
555
556         $firstIndent = strspn($m->match, ' ');
557         $pat = sprintf('/\ {%d,%d}(?=\s*\S)/A', $firstIndent + 1, $firstIndent + 5);
558
559         $input->advance();
560         $loose = $input->skipSpace();
561         $line = $input->currentLine();
562
563         if (!$line || !preg_match($pat, $line, $mm)) {
564             $input->setPos($pos);
565             return false;       // No body found.
566         }
567
568         $indent = strlen($mm[0]);
569         $term = TransformInline(rtrim(substr(trim($m->match),0,-1)));
570         $defn = new TightSubBlock($input, sprintf("\\ {%d}", $indent), false, 'dd');
571         return array($term, $defn, $loose);
572     }
573 }
574
575
576
577 class Block_table_dl_defn extends XmlContent
578 {
579     var $nrows;
580     var $ncols;
581     
582     function Block_table_dl_defn ($term, $defn) {
583         $this->XmlContent();
584         if (!is_array($defn))
585             $defn = $defn->getContent();
586
587         $this->_next_tight_top = false; // value irrelevant - gets fixed later
588         $this->_ncols = $this->_ComputeNcols($defn);
589         $this->_nrows = 0;
590
591         foreach ($defn as $item) {
592             if ($this->_IsASubtable($item))
593                 $this->_addSubtable($item);
594             else
595                 $this->_addToRow($item);
596         }
597         $this->_flushRow();
598
599         $th = HTML::th($term);
600         if ($this->_nrows > 1)
601             $th->setAttr('rowspan', $this->_nrows);
602         $this->_setTerm($th);
603     }
604
605     function setTightness($tight_top, $tight_bot) {
606         $this->_tight_top = $tight_top;
607         $this->_tight_bot = $tight_bot;
608         $first = &$this->firstTR();
609         $last = &$this->lastTR();
610         $first->setInClass('top', $tight_top);
611         $last->setInClass('bottom', $tight_bot);
612     }
613     
614     function _addToRow ($item) {
615         if (empty($this->_accum)) {
616             $this->_accum = HTML::td();
617             if ($this->_ncols > 2)
618                 $this->_accum->setAttr('colspan', $this->_ncols - 1);
619         }
620         $this->_accum->pushContent($item);
621     }
622
623     function _flushRow ($tight_bottom=false) {
624         if (!empty($this->_accum)) {
625             $row = new Block_HtmlElement('tr', false, $this->_accum);
626
627             $row->setTightness($this->_next_tight_top, $tight_bottom);
628             $this->_next_tight_top = $tight_bottom;
629             
630             $this->pushContent($row);
631             $this->_accum = false;
632             $this->_nrows++;
633         }
634     }
635
636     function _addSubtable ($table) {
637         if (!($table_rows = $table->getContent()))
638             return;
639
640         $this->_flushRow($table_rows[0]->_tight_top);
641             
642         foreach ($table_rows as $subdef) {
643             $this->pushContent($subdef);
644             $this->_nrows += $subdef->nrows();
645             $this->_next_tight_top = $subdef->_tight_bot;
646         }
647     }
648
649     function _setTerm ($th) {
650         $first_row = &$this->_content[0];
651         if (isa($first_row, 'Block_table_dl_defn'))
652             $first_row->_setTerm($th);
653         else
654             $first_row->unshiftContent($th);
655     }
656     
657     function _ComputeNcols ($defn) {
658         $ncols = 2;
659         foreach ($defn as $item) {
660             if ($this->_IsASubtable($item)) {
661                 $row = $this->_FirstDefn($item);
662                 $ncols = max($ncols, $row->ncols() + 1);
663             }
664         }
665         return $ncols;
666     }
667
668     function _IsASubtable ($item) {
669         return isa($item, 'HtmlElement')
670             && $item->getTag() == 'table'
671             && $item->getAttr('class') == 'wiki-dl-table';
672     }
673
674     function _FirstDefn ($subtable) {
675         $defs = $subtable->getContent();
676         return $defs[0];
677     }
678
679     function ncols () {
680         return $this->_ncols;
681     }
682
683     function nrows () {
684         return $this->_nrows;
685     }
686
687     function & firstTR() {
688         $first = &$this->_content[0];
689         if (isa($first, 'Block_table_dl_defn'))
690             return $first->firstTR();
691         return $first;
692     }
693
694     function & lastTR() {
695         $last = &$this->_content[$this->_nrows - 1];
696         if (isa($last, 'Block_table_dl_defn'))
697             return $last->lastTR();
698         return $last;
699     }
700
701     function setWidth ($ncols) {
702         assert($ncols >= $this->_ncols);
703         if ($ncols <= $this->_ncols)
704             return;
705         $rows = &$this->_content;
706         for ($i = 0; $i < count($rows); $i++) {
707             $row = &$rows[$i];
708             if (isa($row, 'Block_table_dl_defn'))
709                 $row->setWidth($ncols - 1);
710             else {
711                 $n = count($row->_content);
712                 $lastcol = &$row->_content[$n - 1];
713                 $lastcol->setAttr('colspan', $ncols - 1);
714             }
715         }
716     }
717 }
718
719 class Block_table_dl extends Block_dl
720 {
721     var $_tag = 'dl-table';     // phony.
722
723     function Block_table_dl() {
724         $this->_re = '\ {0,4} (?:\S.*)? (?<!'.ESCAPE_CHAR.') \| \s* $';
725     }
726
727     function _match (&$input, $m) {
728         if (!($p = $this->_do_match($input, $m)))
729             return false;
730         list ($term, $defn, $loose) = $p;
731
732         $this->_content[] = new Block_table_dl_defn($term, $defn);
733         return true;
734     }
735
736     function _setTightness($top, $bot) {
737         $this->_content[0]->setTightness($top, $bot);
738     }
739     
740     function finish () {
741
742         $defs = &$this->_content;
743
744         $ncols = 0;
745         foreach ($defs as $defn)
746             $ncols = max($ncols, $defn->ncols());
747         
748         foreach ($defs as $key => $defn)
749             $defs[$key]->setWidth($ncols);
750
751         return HTML::table(array('class' => 'wiki-dl-table',
752                                  'border' => 1,
753                                  'cellspacing' => 0,
754                                  'cellpadding' => 6),
755                            $defs);
756     }
757 }
758
759 class Block_oldlists extends Block_list
760 {
761     //var $_tag = 'ol', 'ul', or 'dl';
762     var $_re = '(?: [*] (?! \S[^*]* (?<=\S) [*](?!\S) )
763                   | [#] (?! \[ .*? \] )
764                   | ; .*? :
765                 ) .*? (?=\S)';
766
767     function _match (&$input, $m) {
768         // FIXME:
769         if (!preg_match('/[*#;]*$/A', $input->getPrefix())) {
770             return false;
771         }
772         
773
774         $prefix = $m->match;
775         $oldindent = '[*#;](?=[#*]|;.*:.*\S)';
776         $newindent = sprintf('\\ {%d}', strlen($prefix));
777         $indent = "(?:$oldindent|$newindent)";
778
779         $bullet = $prefix[0];
780         if ($bullet == '*') {
781             $this->_tag = 'ul';
782             $itemtag = 'li';
783         }
784         elseif ($bullet == '#') {
785             $this->_tag = 'ol';
786             $itemtag = 'li';
787         }
788         else {
789             $this->_tag = 'dl';
790             list ($term,) = explode(':', substr($prefix, 1), 2);
791             $term = trim($term);
792             if ($term)
793                 $this->_content[] = new Block_HtmlElement('dt', false,
794                                                           TransformInline($term));
795             $itemtag = 'dd';
796         }
797
798         $this->_content[] = new TightSubBlock($input, $indent, $m->match, $itemtag);
799         return true;
800     }
801
802     function _setTightness($top, $bot) {
803         if (count($this->_content) == 1) {
804             $li = &$this->_content[0];
805             $li->setTightness($top, $bot);
806         }
807         else {
808             assert(count($this->_content) == 2);
809             $dt = &$this->_content[0];
810             $dd = &$this->_content[1];
811             $dt->setTightness($top, false);
812             $dd->setTightness(false, $bot);
813         }
814     }
815 }
816
817 class Block_pre extends BlockMarkup
818 {
819     var $_re = '<(?:pre|verbatim)>';
820
821     function _match (&$input, $m) {
822         $endtag = '</' . substr($m->match, 1);
823         $text = array();
824         $pos = $input->getPos();
825
826         $line = $m->postmatch;
827         while (ltrim($line) != $endtag) {
828             $text[] = $line;
829             if (($line = $input->nextLine()) === false) {
830                 $input->setPos($pos);
831                 return false;
832             }
833         }
834         $input->advance();
835         
836         $text = join("\n", $text);
837         
838         // FIXME: no <img>, <big>, <small>, <sup>, or <sub>'s allowed
839         // in a <pre>.
840         if ($m->match == '<pre>')
841             $text = TransformInline($text);
842
843         $this->_element = new Block_HtmlElement('pre', false, $text);
844         return true;
845     }
846 }
847
848
849 class Block_plugin extends Block_pre
850 {
851     var $_re = '<\?plugin(?:-form)?(?!\S)';
852
853     // FIXME:
854     /* <?plugin Backlinks
855      *       page=ThisPage ?>
856      *
857      * should work. */
858     function _match (&$input, $m) {
859         $pos = $input->getPos();
860         $pi = $m->match . $m->postmatch;
861         while (!preg_match('/(?<!'.ESCAPE_CHAR.')\?>\s*$/', $pi)) {
862             if (($line = $input->nextLine()) === false) {
863                 $input->setPos($pos);
864                 return false;
865             }
866             $pi .= "\n$line";
867         }
868         $input->advance();
869
870         $this->_element = new Cached_PluginInvocation($pi);
871         return true;
872     }
873 }
874
875 class Block_email_blockquote extends BlockMarkup
876 {
877     var $_attr = array('class' => 'mail-style-quote');
878     var $_re = '>\ ?';
879     
880     function _match (&$input, $m) {
881         //$indent = str_replace(' ', '\\ ', $m->match) . '|>$';
882         $indent = $this->_re;
883         $this->_element = new SubBlock($input, $indent, $m->match,
884                                        'blockquote', $this->_attr);
885         return true;
886     }
887 }
888
889 class Block_hr extends BlockMarkup
890 {
891     var $_re = '-{4,}\s*$';
892
893     function _match (&$input, $m) {
894         $input->advance();
895         $this->_element = new Block_HtmlElement('hr');
896         return true;
897     }
898
899     function _setTightness($top, $bot) {
900         // Don't tighten <hr/>s
901     }
902 }
903
904 class Block_heading extends BlockMarkup
905 {
906     var $_re = '!{1,3}';
907     
908     function _match (&$input, $m) {
909         $tag = "h" . (5 - strlen($m->match));
910         $text = TransformInline(trim($m->postmatch));
911         $input->advance();
912
913         $this->_element = new Block_HtmlElement($tag, false, $text);
914         
915         return true;
916     }
917
918     function _setTightness($top, $bot) {
919         // Don't tighten headers.
920     }
921 }
922
923 class Block_p extends BlockMarkup
924 {
925     var $_tag = 'p';
926     var $_re = '\S.*';
927
928     function _match (&$input, $m) {
929         $this->_text = $m->match;
930         $input->advance();
931         return true;
932     }
933
934     function _setTightness ($top, $bot) {
935         $this->_tight_top = $top;
936         $this->_tight_bot = $bot;
937     }
938
939     function merge ($nextBlock) {
940         $class = get_class($nextBlock);
941         if ($class == 'block_p' && $this->_tight_bot) {
942             $this->_text .= "\n" . $nextBlock->_text;
943             $this->_tight_bot = $nextBlock->_tight_bot;
944             return $this;
945         }
946         return false;
947     }
948             
949     function finish () {
950         $content = TransformInline(trim($this->_text));
951         $p = new Block_HtmlElement('p', false, $content);
952         $p->setTightness($this->_tight_top, $this->_tight_bot);
953         return $p;
954     }
955 }
956
957 ////////////////////////////////////////////////////////////////
958 //
959
960 function TransformText ($text, $markup = 2.0, $basepage=false) {
961     if (isa($text, 'WikiDB_PageRevision')) {
962         $rev = $text;
963         $text = $rev->getPackedContent();
964         $markup = $rev->get('markup');
965     }
966
967     if (empty($markup) || $markup < 2.0) {
968         //include_once("lib/transform.php");
969         //return do_transform($text);
970         $text = ConvertOldMarkup($text);
971     }
972     
973     // Expand leading tabs.
974     $text = expand_tabs($text);
975
976     //set_time_limit(3);
977
978     $output = new WikiText($text);
979
980     if ($basepage) {
981         // This is for immediate consumption.
982         // We must bind the contents to a base pagename so that
983         // relative page links can be properly linkified...
984         return new CacheableMarkup($output->getContent(), $basepage);
985     }
986     
987     return new XmlContent($output->getContent());
988 }
989
990 // (c-file-style: "gnu")
991 // Local Variables:
992 // mode: php
993 // tab-width: 8
994 // c-basic-offset: 4
995 // c-hanging-comment-ender-p: nil
996 // indent-tabs-mode: nil
997 // End:   
998 ?>