]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/InlineParser.php
add Log
[SourceForge/phpwiki.git] / lib / InlineParser.php
1 <?php 
2 rcs_id('$Id: InlineParser.php,v 1.65 2005-03-27 18:24:17 rurban Exp $');
3 /* Copyright (C) 2002 Geoffrey T. Dairiki <dairiki@dairiki.org>
4  * Copyright (C) 2004,2005 Reini Urban
5  *
6  * This file is part of PhpWiki.
7  * 
8  * PhpWiki is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  * 
13  * PhpWiki is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with PhpWiki; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22 /**
23  * This is the code which deals with the inline part of the (new-style)
24  * wiki-markup.
25  *
26  * @package Markup
27  * @author Geoffrey T. Dairiki
28  */
29 /**
30  */
31
32 /**
33  * This is the character used in wiki markup to escape characters with
34  * special meaning.
35  */
36 define('ESCAPE_CHAR', '~');
37
38 require_once(dirname(__FILE__).'/HtmlElement.php');
39 require_once('lib/CachedMarkup.php');
40 require_once(dirname(__FILE__).'/stdlib.php');
41
42
43 function WikiEscape($text) {
44     return str_replace('#', ESCAPE_CHAR . '#', $text);
45 }
46
47 function UnWikiEscape($text) {
48     return preg_replace('/' . ESCAPE_CHAR . '(.)/', '\1', $text);
49 }
50
51 /**
52  * Return type from RegexpSet::match and RegexpSet::nextMatch.
53  *
54  * @see RegexpSet
55  */
56 class RegexpSet_match {
57     /**
58      * The text leading up the the next match.
59      */
60     var $prematch;
61     /**
62      * The matched text.
63      */
64     var $match;
65     /**
66      * The text following the matched text.
67      */
68     var $postmatch;
69     /**
70      * Index of the regular expression which matched.
71      */
72     var $regexp_ind;
73 }
74
75 /**
76  * A set of regular expressions.
77  *
78  * This class is probably only useful for InlineTransformer.
79  */
80 class RegexpSet
81 {
82     /** Constructor
83      *
84      * @param array $regexps A list of regular expressions.  The
85      * regular expressions should not include any sub-pattern groups
86      * "(...)".  (Anonymous groups, like "(?:...)", as well as
87      * look-ahead and look-behind assertions are okay.)
88      */
89     function RegexpSet ($regexps) {
90         assert($regexps);
91         $this->_regexps = array_unique($regexps);
92         if (!defined('_INLINE_OPTIMIZATION')) define('_INLINE_OPTIMIZATION',0);
93     }
94
95     /**
96      * Search text for the next matching regexp from the Regexp Set.
97      *
98      * @param string $text The text to search.
99      *
100      * @return RegexpSet_match  A RegexpSet_match object, or false if no match.
101      */
102     function match ($text) {
103         return $this->_match($text, $this->_regexps, '*?');
104     }
105
106     /**
107      * Search for next matching regexp.
108      *
109      * Here, 'next' has two meanings:
110      *
111      * Match the next regexp(s) in the set, at the same position as the last match.
112      *
113      * If that fails, match the whole RegexpSet, starting after the position of the
114      * previous match.
115      *
116      * @param string $text Text to search.
117      *
118      * @param RegexpSet_match $prevMatch A RegexpSet_match object.
119      * $prevMatch should be a match object obtained by a previous
120      * match upon the same value of $text.
121      *
122      * @return RegexpSet_match A RegexpSet_match object, or false if no match.
123      */
124     function nextMatch ($text, $prevMatch) {
125         // Try to find match at same position.
126         $pos = strlen($prevMatch->prematch);
127         $regexps = array_slice($this->_regexps, $prevMatch->regexp_ind + 1);
128         if ($regexps) {
129             $repeat = sprintf('{%d}', $pos);
130             if ( ($match = $this->_match($text, $regexps, $repeat)) ) {
131                 $match->regexp_ind += $prevMatch->regexp_ind + 1;
132                 return $match;
133             }
134             
135         }
136         
137         // Failed.  Look for match after current position.
138         $repeat = sprintf('{%d,}?', $pos + 1);
139         return $this->_match($text, $this->_regexps, $repeat);
140     }
141
142     // Syntax: http://www.pcre.org/pcre.txt
143     //   x - EXTENDED, ignore whitespace
144     //   s - DOTALL
145     //   A - ANCHORED
146     //   S - STUDY
147     function _match ($text, $regexps, $repeat) {
148         // If one of the regexps is an empty string, php will crash here: 
149         // sf.net: Fatal error: Allowed memory size of 8388608 bytes exhausted 
150         //         (tried to allocate 634 bytes)
151         if (_INLINE_OPTIMIZATION) { // disabled, wrong
152         // So we try to minize memory usage, by looping explicitly,
153         // and storing only those regexp which actually match. 
154         // There may be more than one, so we have to find the longest, 
155         // and match inside until the shortest is empty.
156         $matched = array(); $matched_ind = array();
157         for ($i=0; $i<count($regexps); $i++) {
158             if (!trim($regexps[$i])) {
159                 trigger_error("empty regexp $i", E_USER_WARNING);
160                 continue;
161             }
162             $pat= "/ ( . $repeat ) ( " . $regexps[$i] . " ) /x";
163             if (preg_match($pat, $text, $_m)) {
164                 $m = $_m; // FIXME: prematch, postmatch is wrong
165                 $matched[] = $regexps[$i];
166                 $matched_ind[] = $i;
167                 $regexp_ind = $i;
168             }
169         }
170         // To overcome ANCHORED:
171         // We could sort by longest match and iterate over these.
172         if (empty($matched)) return false;
173         }
174         $match = new RegexpSet_match;
175         
176         // Optimization: if the matches are only "$" and another, then omit "$"
177         if (! _INLINE_OPTIMIZATION or count($matched) > 2) {
178             assert(!empty($repeat));
179             assert(!empty($regexps));
180             for ($i=0; $i<count($regexps); $i++) {
181                 if (!trim($regexps[$i])) {
182                     trigger_error("empty regexp $i", E_USER_WARNING);
183                     $regexps[$i] = '\Wxxxx\w\W\w\W\w\W\w\W\w\W\w'; // some placeholder
184                 }
185             }
186             // We could do much better, if we would know the matching markup for the 
187             // longest regexp match:
188             $hugepat= "/ ( . $repeat ) ( (" . join(')|(', $regexps) . ") ) /Asx";
189             // Proposed premature optimization 1:
190             //$hugepat= "/ ( . $repeat ) ( (" . join(')|(', array_values($matched)) . ") ) /Asx";
191             if (! preg_match($hugepat, $text, $m)) {
192                 return false;
193             }
194             // Proposed premature optimization 1:
195             //$match->regexp_ind = $matched_ind[count($m) - 4];
196             $match->regexp_ind = count($m) - 4;
197         } else {
198             $match->regexp_ind = $regexp_ind;
199         }
200         
201         $match->postmatch = substr($text, strlen($m[0]));
202         $match->prematch = $m[1];
203         $match->match = $m[2];
204
205         /* DEBUGGING */
206         /*
207         if (DEBUG & 4) {
208           var_dump($regexps); var_dump($matched); var_dump($matched_inc); 
209         PrintXML(HTML::dl(HTML::dt("input"),
210                           HTML::dd(HTML::pre($text)),
211                           HTML::dt("regexp"),
212                           HTML::dd(HTML::pre($match->regexp_ind, ":", $regexps[$match->regexp_ind])),
213                           HTML::dt("prematch"),
214                           HTML::dd(HTML::pre($match->prematch)),
215                           HTML::dt("match"),
216                           HTML::dd(HTML::pre($match->match)),
217                           HTML::dt("postmatch"),
218                           HTML::dd(HTML::pre($match->postmatch))
219                           ));
220         }
221         */
222         return $match;
223     }
224 }
225
226
227
228 /**
229  * A simple markup rule (i.e. terminal token).
230  *
231  * These are defined by a regexp.
232  *
233  * When a match is found for the regexp, the matching text is replaced.
234  * The replacement content is obtained by calling the SimpleMarkup::markup method.
235  */ 
236 class SimpleMarkup
237 {
238     var $_match_regexp;
239
240     /** Get regexp.
241      *
242      * @return string Regexp which matches this token.
243      */
244     function getMatchRegexp () {
245         return $this->_match_regexp;
246     }
247
248     /** Markup matching text.
249      *
250      * @param string $match The text which matched the regexp
251      * (obtained from getMatchRegexp).
252      *
253      * @return mixed The expansion of the matched text.
254      */
255     function markup ($match /*, $body */) {
256         trigger_error("pure virtual", E_USER_ERROR);
257     }
258 }
259
260 /**
261  * A balanced markup rule.
262  *
263  * These are defined by a start regexp, and an end regexp.
264  */ 
265 class BalancedMarkup
266 {
267     var $_start_regexp;
268
269     /** Get the starting regexp for this rule.
270      *
271      * @return string The starting regexp.
272      */
273     function getStartRegexp () {
274         return $this->_start_regexp;
275     }
276     
277     /** Get the ending regexp for this rule.
278      *
279      * @param string $match The text which matched the starting regexp.
280      *
281      * @return string The ending regexp.
282      */
283     function getEndRegexp ($match) {
284         return $this->_end_regexp;
285     }
286
287     /** Get expansion for matching input.
288      *
289      * @param string $match The text which matched the starting regexp.
290      *
291      * @param mixed $body Transformed text found between the starting
292      * and ending regexps.
293      *
294      * @return mixed The expansion of the matched text.
295      */
296     function markup ($match, $body) {
297         trigger_error("pure virtual", E_USER_ERROR);
298     }
299 }
300
301 class Markup_escape  extends SimpleMarkup
302 {
303     function getMatchRegexp () {
304         return ESCAPE_CHAR . '(?: [[:alnum:]]+ | .)';
305     }
306     
307     function markup ($match) {
308         assert(strlen($match) >= 2);
309         return substr($match, 1);
310     }
311 }
312
313 /**
314  * [image.jpg size=50% border=5], [image.jpg size=50x30]
315  * Support for the following attributes: see stdlib.php:LinkImage()
316  *   size=<precent>%, size=<width>x<height>
317  *   border=n, align=\w+, hspace=n, vspace=n
318  */
319 function isImageLink($link) {
320     if (!$link) return false;
321     assert(defined('INLINE_IMAGES'));
322     return preg_match("/\\.(" . INLINE_IMAGES . ")$/i", $link)
323         or preg_match("/\\.(" . INLINE_IMAGES . ")\s+(size|border|align|hspace|vspace)=/i", $link);
324 }
325
326 function LinkBracketLink($bracketlink) {
327
328     // $bracketlink will start and end with brackets; in between will
329     // be either a page name, a URL or both separated by a pipe.
330     
331     // strip brackets and leading space
332     // FIXME: \n inside [] will lead to errors
333     preg_match('/(\#?) \[\s* (?: (.*?) \s* (?<!' . ESCAPE_CHAR . ')(\|) )? \s* (.+?) \s*\]/x',
334                $bracketlink, $matches);
335     if (count($matches) < 4) {
336         trigger_error(_("Invalid [] syntax ignored").": ".$bracketlink, E_USER_NOTICE);
337         return new Cached_Link;
338     }
339     list (, $hash, $label, $bar, $rawlink) = $matches;
340
341     $label = UnWikiEscape($label);
342     /*
343      * Check if the user has typed a explicit URL. This solves the
344      * problem where the URLs have a ~ character, which would be stripped away.
345      *   "[http:/server/~name/]" will work as expected
346      *   "http:/server/~name/"   will NOT work as expected, will remove the ~
347      */
348     if (strstr($rawlink, "http://") or strstr($rawlink, "https://")) {
349         $link = $rawlink;
350         // Mozilla Browser URI Obfuscation Weakness 2004-06-14
351         //   http://www.securityfocus.com/bid/10532/
352         //   goodurl+"%2F%20%20%20."+badurl
353         if (preg_match("/%2F(%20)+\./i", $rawlink)) {
354             $rawlink = preg_replace("/%2F(%20)+\./i","%2F.",$rawlink);
355         }
356     } else
357         $link  = UnWikiEscape($rawlink);
358
359     // [label|link]
360     // if label looks like a url to an image, we want an image link.
361     if (isImageLink($label)) {
362         $imgurl = $label;
363         $intermap = getInterwikiMap();
364         if (preg_match("/^" . $intermap->getRegexp() . ":/", $label)) {
365             $imgurl = $intermap->link($label);
366             $imgurl = $imgurl->getAttr('href');
367         } elseif (! preg_match("#^(" . ALLOWED_PROTOCOLS . "):#", $imgurl)) {
368             // local theme linkname like 'images/next.gif'.
369             global $WikiTheme;
370             $imgurl = $WikiTheme->getImageURL($imgurl);
371         }
372         $label = LinkImage($imgurl, $link);
373     }
374
375     if ($hash) {
376         // It's an anchor, not a link...
377         $id = MangleXmlIdentifier($link);
378         return HTML::a(array('name' => $id, 'id' => $id),
379                        $bar ? $label : $link);
380     }
381
382     if (preg_match("#^(" . ALLOWED_PROTOCOLS . "):#", $link)) {
383         // if it's an image, embed it; otherwise, it's a regular link
384         if (isImageLink($link))
385             return LinkImage($link, $label);
386         else
387             return new Cached_ExternalLink($link, $label);
388     }
389     elseif (preg_match("/^phpwiki:/", $link))
390         return new Cached_PhpwikiURL($link, $label);
391     /*
392      * Inline images in Interwiki urls's:
393      * [File:my_image.gif] inlines the image,
394      * File:my_image.gif shows a plain inter-wiki link,
395      * [what a pic|File:my_image.gif] shows a named inter-wiki link to the gif
396      * [File:my_image.gif|what a pic] shows a inlimed image linked to the page "what a pic"
397      */
398     elseif (strstr($link,':') and 
399             ($intermap = getInterwikiMap()) and 
400             preg_match("/^" . $intermap->getRegexp() . ":/", $link)) {
401         if (empty($label) && isImageLink($link)) {
402             // if without label => inlined image [File:xx.gif]
403             $imgurl = $intermap->link($link);
404             return LinkImage($imgurl->getAttr('href'), $label);
405         }
406         return new Cached_InterwikiLink($link, $label);
407     } else {
408         // Split anchor off end of pagename.
409         if (preg_match('/\A(.*)(?<!'.ESCAPE_CHAR.')#(.*?)\Z/', $rawlink, $m)) {
410             list(,$rawlink,$anchor) = $m;
411             $pagename = UnWikiEscape($rawlink);
412             $anchor = UnWikiEscape($anchor);
413             if (!$label)
414                 $label = $link;
415         }
416         else {
417             $pagename = $link;
418             $anchor = false;
419         }
420         return new Cached_WikiLink($pagename, $label, $anchor);
421     }
422 }
423
424 class Markup_bracketlink  extends SimpleMarkup
425 {
426     var $_match_regexp = "\\#? \\[ .*? [^]\\s] .*? \\]";
427     
428     function markup ($match) {
429         $link = LinkBracketLink($match);
430         assert($link->isInlineElement());
431         return $link;
432     }
433 }
434
435 class Markup_url extends SimpleMarkup
436 {
437     function getMatchRegexp () {
438         return "(?<![[:alnum:]]) (?:" . ALLOWED_PROTOCOLS . ") : [^\s<>\"']+ (?<![ ,.?; \] \) ])";
439     }
440     
441     function markup ($match) {
442         return new Cached_ExternalLink(UnWikiEscape($match));
443     }
444 }
445
446
447 class Markup_interwiki extends SimpleMarkup
448 {
449     function getMatchRegexp () {
450         global $request;
451         $map = getInterwikiMap();
452         return "(?<! [[:alnum:]])" . $map->getRegexp(). ": \S+ (?<![ ,.?;! \] \) \" \' ])";
453     }
454
455     function markup ($match) {
456         //$map = getInterwikiMap();
457         return new Cached_InterwikiLink(UnWikiEscape($match));
458     }
459 }
460
461 class Markup_wikiword extends SimpleMarkup
462 {
463     function getMatchRegexp () {
464         global $WikiNameRegexp;
465         if (!trim($WikiNameRegexp)) return " " . WIKI_NAME_REGEXP;
466         return " $WikiNameRegexp";
467     }
468
469     function markup ($match) {
470         if (!$match) return false;
471         if ($this->_isWikiUserPage($match))
472             return new Cached_UserLink($match); //$this->_UserLink($match);
473         else
474             return new Cached_WikiLink($match);
475     }
476
477     // FIXME: there's probably a more useful place to put these two functions    
478     function _isWikiUserPage ($page) {
479         global $request;
480         $dbi = $request->getDbh();
481         $page_handle = $dbi->getPage($page);
482         if ($page_handle and $page_handle->get('pref'))
483             return true;
484         else
485             return false;
486     }
487
488     function _UserLink($PageName) {
489         $link = HTML::a(array('href' => $PageName));
490         $link->pushContent(PossiblyGlueIconToText('wikiuser', $PageName));
491         $link->setAttr('class', 'wikiuser');
492         return $link;
493     }
494 }
495
496 class Markup_linebreak extends SimpleMarkup
497 {
498     //var $_match_regexp = "(?: (?<! %) %%% (?! %) | <(?:br|BR)> | <(?:br|BR) \/> )";
499     var $_match_regexp = "(?: (?<! %) %%% (?! %) | <(?:br|BR)> )";
500
501     function markup ($match) {
502         return HTML::br();
503     }
504 }
505
506 class Markup_old_emphasis  extends BalancedMarkup
507 {
508     var $_start_regexp = "''|__";
509
510     function getEndRegexp ($match) {
511         return $match;
512     }
513     
514     function markup ($match, $body) {
515         $tag = $match == "''" ? 'em' : 'strong';
516         return new HtmlElement($tag, $body);
517     }
518 }
519
520 class Markup_nestled_emphasis extends BalancedMarkup
521 {
522     function getStartRegexp() {
523         static $start_regexp = false;
524
525         if (!$start_regexp) {
526             // The three possible delimiters
527             // (none of which can be followed by itself.)
528             $i = "_ (?! _)";
529             $b = "\\* (?! \\*)";
530             $tt = "= (?! =)";
531
532             $any = "(?: ${i}|${b}|${tt})"; // any of the three.
533
534             // Any of [_*=] is okay if preceded by space or one of [-"'/:]
535             $start[] = "(?<= \\s|^|[-\"'\\/:]) ${any}";
536
537             // _ or * is okay after = as long as not immediately followed by =
538             $start[] = "(?<= =) (?: ${i}|${b}) (?! =)";
539             // etc...
540             $start[] = "(?<= _) (?: ${b}|${tt}) (?! _)";
541             $start[] = "(?<= \\*) (?: ${i}|${tt}) (?! \\*)";
542
543
544             // any delimiter okay after an opening brace ( [{<(] )
545             // as long as it's not immediately followed by the matching closing
546             // brace.
547             $start[] = "(?<= { ) ${any} (?! } )";
548             $start[] = "(?<= < ) ${any} (?! > )";
549             $start[] = "(?<= \\( ) ${any} (?! \\) )";
550             
551             $start = "(?:" . join('|', $start) . ")";
552             
553             // Any of the above must be immediately followed by non-whitespace.
554             $start_regexp = $start . "(?= \S)";
555         }
556
557         return $start_regexp;
558     }
559
560     function getEndRegexp ($match) {
561         $chr = preg_quote($match);
562         return "(?<= \S | ^ ) (?<! $chr) $chr (?! $chr) (?= \s | [-)}>\"'\\/:.,;!? _*=] | $)";
563     }
564     
565     function markup ($match, $body) {
566         switch ($match) {
567         case '*': return new HtmlElement('b', $body);
568         case '=': return new HtmlElement('tt', $body);
569         case '_': return new HtmlElement('i', $body);
570         }
571     }
572 }
573
574 class Markup_html_emphasis extends BalancedMarkup
575 {
576     var $_start_regexp = 
577         "<(?: b|big|i|small|tt|em|strong|cite|code|dfn|kbd|samp|var|sup|sub )>";
578
579     function getEndRegexp ($match) {
580         return "<\\/" . substr($match, 1);
581     }
582     
583     function markup ($match, $body) {
584         $tag = substr($match, 1, -1);
585         return new HtmlElement($tag, $body);
586     }
587 }
588
589 class Markup_html_abbr extends BalancedMarkup
590 {
591     //rurban: abbr|acronym need an optional title tag.
592     //sf.net bug #728595
593     var $_start_regexp = "<(?: abbr|acronym )(?: \stitle=[^>]*)?>";
594
595     function getEndRegexp ($match) {
596         if (substr($match,1,4) == 'abbr')
597             $tag = 'abbr';
598         else
599             $tag = 'acronym';
600         return "<\\/" . $tag . '>';
601     }
602     
603     function markup ($match, $body) {
604         if (substr($match,1,4) == 'abbr')
605             $tag = 'abbr';
606         else
607             $tag = 'acronym';
608         $rest = substr($match,1+strlen($tag),-1);
609         if (!empty($rest)) {
610             list($key,$val) = explode("=",$rest);
611             $args = array($key => $val);
612         } else $args = array();
613         return new HtmlElement($tag, $args, $body);
614     }
615 }
616
617 // Special version for single-line plugins formatting, 
618 //  like: '<small>< ?plugin PopularNearby ? ></small>'
619 class Markup_plugin extends SimpleMarkup
620 {
621     var $_match_regexp = '<\?plugin(?:-form)?\s[^\n]+?\?>';
622
623     function markup ($match) {
624         //$xml = new Cached_PluginInvocation($match);
625         //$xml->setTightness(true,true);
626         return new Cached_PluginInvocation($match);
627     }
628 }
629
630
631 // TODO: "..." => "&#133;"  browser specific display (not cached?)
632 // TODO: "--" => "&emdash;" browser specific display (not cached?)
633 // TODO: Support more HTML::Entities: (C) for copy, --- for mdash, -- for ndash
634
635 class Markup_html_entities  extends SimpleMarkup {
636     var $_match_regexp = '(: \.\.\.|\-\-|\-\-\-|\(C\) )';
637    
638     function markup ($match) {
639         static $entities = array('...'  => '&#133;',
640                                  '--'   => '&ndash;',
641                                  '---'  => '&mdash;',
642                                  '(C)'  => '&copy;',
643                                  );
644         return HTML::Raw($entities[$match]);
645     }
646 }
647
648 class Markup_isonumchars  extends SimpleMarkup {
649     var $_match_regexp = '\&\#\d{2,5};';
650     
651     function markup ($match) {
652         return HTML::Raw($match);
653     }
654 }
655
656 class Markup_isohexchars extends SimpleMarkup {
657     // hexnums, like &#x00A4; <=> &curren;
658     var $_match_regexp = '\&\#x[0-9a-fA-F]{2,4};';
659     
660     function markup ($match) {
661         return HTML::Raw($match);
662     }
663 }
664
665 // FIXME: Do away with magic phpwiki forms.  (Maybe phpwiki: links too?)
666 // FIXME: Do away with plugin-links.  They seem not to be used.
667 //Plugin link
668
669 class InlineTransformer
670 {
671     var $_regexps = array();
672     var $_markup = array();
673     
674     function InlineTransformer ($markup_types = false) {
675         if (!$markup_types)
676             $markup_types = array('escape', 'bracketlink', 'url',
677                                   'interwiki', 'wikiword', 'linebreak',
678                                   'old_emphasis', 'nestled_emphasis',
679                                   'html_emphasis', 'html_abbr', 'plugin',
680                                   'isonumchars', 'isohexchars', /*'html_entities',*/
681                                   );
682         foreach ($markup_types as $mtype) {
683             $class = "Markup_$mtype";
684             $this->_addMarkup(new $class);
685         }
686     }
687
688     function _addMarkup ($markup) {
689         if (isa($markup, 'SimpleMarkup'))
690             $regexp = $markup->getMatchRegexp();
691         else
692             $regexp = $markup->getStartRegexp();
693
694         assert(!isset($this->_markup[$regexp]));
695         $this->_regexps[] = $regexp;
696         $this->_markup[] = $markup;
697     }
698         
699     function parse (&$text, $end_regexps = array('$')) {
700         $regexps = $this->_regexps;
701
702         // $end_re takes precedence: "favor reduce over shift"
703         array_unshift($regexps, $end_regexps[0]);
704         //array_push($regexps, $end_regexps[0]);
705         $regexps = new RegexpSet($regexps);
706         
707         $input = $text;
708         $output = new XmlContent;
709
710         $match = $regexps->match($input);
711         
712         while ($match) {
713             if ($match->regexp_ind == 0) {
714                 // No start pattern found before end pattern.
715                 // We're all done!
716                 if (isset($markup) and is_object($markup) and isa($markup,'Markup_plugin')) {
717                     $current =& $output->_content[count($output->_content)-1];
718                     $current->setTightness(true,true);
719                 }
720                 $output->pushContent($match->prematch);
721                 $text = $match->postmatch;
722                 return $output;
723             }
724
725             $markup = $this->_markup[$match->regexp_ind - 1];
726             $body = $this->_parse_markup_body($markup, $match->match, $match->postmatch, $end_regexps);
727             if (!$body) {
728                 // Couldn't match balanced expression.
729                 // Ignore and look for next matching start regexp.
730                 $match = $regexps->nextMatch($input, $match);
731                 continue;
732             }
733
734             // Matched markup.  Eat input, push output.
735             // FIXME: combine adjacent strings.
736             if (isa($markup, 'SimpleMarkup'))
737                 $current = $markup->markup($match->match);
738             else
739                 $current = $markup->markup($match->match, $body);
740             $input = $match->postmatch;
741             if (isset($markup) and is_object($markup) and isa($markup,'Markup_plugin')) {
742                 $current->setTightness(true,true);
743             }
744             $output->pushContent($match->prematch, $current);
745
746             $match = $regexps->match($input);
747         }
748
749         // No pattern matched, not even the end pattern.
750         // Parse fails.
751         return false;
752     }
753
754     function _parse_markup_body ($markup, $match, &$text, $end_regexps) {
755         if (isa($markup, 'SimpleMarkup'))
756             return true;        // Done. SimpleMarkup is simple.
757
758         if (!is_object($markup)) return false; // Some error: Should assert
759         array_unshift($end_regexps, $markup->getEndRegexp($match));
760
761         // Optimization: if no end pattern in text, we know the
762         // parse will fail.  This is an important optimization,
763         // e.g. when text is "*lots *of *start *delims *with
764         // *no *matching *end *delims".
765         $ends_pat = "/(?:" . join(").*(?:", $end_regexps) . ")/xs";
766         if (!preg_match($ends_pat, $text))
767             return false;
768         return $this->parse($text, $end_regexps);
769     }
770 }
771
772 class LinkTransformer extends InlineTransformer
773 {
774     function LinkTransformer () {
775         $this->InlineTransformer(array('escape', 'bracketlink', 'url',
776                                        'interwiki', 'wikiword'));
777     }
778 }
779
780 function TransformInline($text, $markup = 2.0, $basepage=false) {
781     static $trfm;
782     
783     if (empty($trfm)) {
784         $trfm = new InlineTransformer;
785     }
786     
787     if ($markup < 2.0) {
788         $text = ConvertOldMarkup($text, 'inline');
789     }
790
791     if ($basepage) {
792         return new CacheableMarkup($trfm->parse($text), $basepage);
793     }
794     return $trfm->parse($text);
795 }
796
797 function TransformLinks($text, $markup = 2.0, $basepage = false) {
798     static $trfm;
799     
800     if (empty($trfm)) {
801         $trfm = new LinkTransformer;
802     }
803
804     if ($markup < 2.0) {
805         $text = ConvertOldMarkup($text, 'links');
806     }
807     
808     if ($basepage) {
809         return new CacheableMarkup($trfm->parse($text), $basepage);
810     }
811     return $trfm->parse($text);
812 }
813
814 // $Log: not supported by cvs2svn $
815
816 // (c-file-style: "gnu")
817 // Local Variables:
818 // mode: php
819 // tab-width: 8
820 // c-basic-offset: 4
821 // c-hanging-comment-ender-p: nil
822 // indent-tabs-mode: nil
823 // End:   
824 ?>