]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/InlineParser.php
warn if invalid [] syntax
[SourceForge/phpwiki.git] / lib / InlineParser.php
1 <?php 
2 rcs_id('$Id: InlineParser.php,v 1.55 2004-06-18 14:37:47 rurban Exp $');
3 /* Copyright (C) 2002 Geoffrey T. Dairiki <dairiki@dairiki.org>
4  * Copyright (C) 2004 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             // We could do much better, if we would know the matching markup for the 
179             // longest regexp match:
180             $hugepat= "/ ( . $repeat ) ( (" . join(')|(', $regexps) . ") ) /Asx";
181             // Proposed premature optimization 1:
182             //$hugepat= "/ ( . $repeat ) ( (" . join(')|(', array_values($matched)) . ") ) /Asx";
183             if (! preg_match($hugepat, $text, $m)) {
184                 return false;
185             }
186             // Proposed premature optimization 1:
187             //$match->regexp_ind = $matched_ind[count($m) - 4];
188             $match->regexp_ind = count($m) - 4;
189         } else {
190             $match->regexp_ind = $regexp_ind;
191         }
192         
193         $match->postmatch = substr($text, strlen($m[0]));
194         $match->prematch = $m[1];
195         $match->match = $m[2];
196
197         /* DEBUGGING */
198         /*
199         if (DEBUG == 4) {
200           var_dump($regexps); var_dump($matched); var_dump($matched_inc); 
201         PrintXML(HTML::dl(HTML::dt("input"),
202                           HTML::dd(HTML::pre($text)),
203                           HTML::dt("regexp"),
204                           HTML::dd(HTML::pre($match->regexp_ind, ":", $regexps[$match->regexp_ind])),
205                           HTML::dt("prematch"),
206                           HTML::dd(HTML::pre($match->prematch)),
207                           HTML::dt("match"),
208                           HTML::dd(HTML::pre($match->match)),
209                           HTML::dt("postmatch"),
210                           HTML::dd(HTML::pre($match->postmatch))
211                           ));
212         }
213         */
214         return $match;
215     }
216 }
217
218
219
220 /**
221  * A simple markup rule (i.e. terminal token).
222  *
223  * These are defined by a regexp.
224  *
225  * When a match is found for the regexp, the matching text is replaced.
226  * The replacement content is obtained by calling the SimpleMarkup::markup method.
227  */ 
228 class SimpleMarkup
229 {
230     var $_match_regexp;
231
232     /** Get regexp.
233      *
234      * @return string Regexp which matches this token.
235      */
236     function getMatchRegexp () {
237         return $this->_match_regexp;
238     }
239
240     /** Markup matching text.
241      *
242      * @param string $match The text which matched the regexp
243      * (obtained from getMatchRegexp).
244      *
245      * @return mixed The expansion of the matched text.
246      */
247     function markup ($match /*, $body */) {
248         trigger_error("pure virtual", E_USER_ERROR);
249     }
250 }
251
252 /**
253  * A balanced markup rule.
254  *
255  * These are defined by a start regexp, and an end regexp.
256  */ 
257 class BalancedMarkup
258 {
259     var $_start_regexp;
260
261     /** Get the starting regexp for this rule.
262      *
263      * @return string The starting regexp.
264      */
265     function getStartRegexp () {
266         return $this->_start_regexp;
267     }
268     
269     /** Get the ending regexp for this rule.
270      *
271      * @param string $match The text which matched the starting regexp.
272      *
273      * @return string The ending regexp.
274      */
275     function getEndRegexp ($match) {
276         return $this->_end_regexp;
277     }
278
279     /** Get expansion for matching input.
280      *
281      * @param string $match The text which matched the starting regexp.
282      *
283      * @param mixed $body Transformed text found between the starting
284      * and ending regexps.
285      *
286      * @return mixed The expansion of the matched text.
287      */
288     function markup ($match, $body) {
289         trigger_error("pure virtual", E_USER_ERROR);
290     }
291 }
292
293 class Markup_escape  extends SimpleMarkup
294 {
295     function getMatchRegexp () {
296         return ESCAPE_CHAR . '(?: [[:alnum:]]+ | .)';
297     }
298     
299     function markup ($match) {
300         assert(strlen($match) >= 2);
301         return substr($match, 1);
302     }
303 }
304
305 /**
306  * [image.jpg size=50% border=5], [image.jpg size=50x30]
307  * Support for the following attributes: see stdlib.php:LinkImage()
308  *   size=<precent>%, size=<width>x<height>
309  *   border=n, align=\w+, hspace=n, vspace=n
310  */
311 function isImageLink($link) {
312     if (!$link) return false;
313     return preg_match("/\\.(" . INLINE_IMAGES . ")$/i", $link)
314         or preg_match("/\\.(" . INLINE_IMAGES . ")\s+(size|border|align|hspace|vspace)=/i", $link);
315 }
316
317 function LinkBracketLink($bracketlink) {
318
319     // $bracketlink will start and end with brackets; in between will
320     // be either a page name, a URL or both separated by a pipe.
321     
322     // strip brackets and leading space
323     preg_match('/(\#?) \[\s* (?: (.*?) \s* (?<!' . ESCAPE_CHAR . ')(\|) )? \s* (.+?) \s*\]/x',
324                $bracketlink, $matches);
325     if (count($matches) < 4) {
326         trigger_error(_("Invalid [] syntax ignored").": ".$bracketlink, E_USER_NOTICE);
327         return new Cached_Link;
328     }
329     list (, $hash, $label, $bar, $rawlink) = $matches;
330
331     $label = UnWikiEscape($label);
332     /*
333      * Check if the user has typed a explicit URL. This solves the
334      * problem where the URLs have a ~ character, which would be stripped away.
335      *   "[http:/server/~name/]" will work as expected
336      *   "http:/server/~name/"   will NOT work as expected, will remove the ~
337      */
338     if (strstr($rawlink, "http://") or strstr($rawlink, "https://"))
339         $link = $rawlink;
340     else
341         $link  = UnWikiEscape($rawlink);
342
343     // [label|link]
344     // if label looks like a url to an image, we want an image link.
345     if (isImageLink($label)) {
346         $imgurl = $label;
347         $intermap = getInterwikiMap();
348         if (preg_match("/^" . $intermap->getRegexp() . ":/", $label)) {
349             $imgurl = $intermap->link($label);
350             $imgurl = $imgurl->getAttr('href');
351         } elseif (! preg_match("#^(" . ALLOWED_PROTOCOLS . "):#", $imgurl)) {
352             // local theme linkname like 'images/next.gif'.
353             global $WikiTheme;
354             $imgurl = $WikiTheme->getImageURL($imgurl);
355         }
356         $label = LinkImage($imgurl, $link);
357     }
358
359     if ($hash) {
360         // It's an anchor, not a link...
361         $id = MangleXmlIdentifier($link);
362         return HTML::a(array('name' => $id, 'id' => $id),
363                        $bar ? $label : $link);
364     }
365
366     if (preg_match("#^(" . ALLOWED_PROTOCOLS . "):#", $link)) {
367         // if it's an image, embed it; otherwise, it's a regular link
368         if (isImageLink($link))
369             return LinkImage($link, $label);
370         else
371             return new Cached_ExternalLink($link, $label);
372     }
373     elseif (preg_match("/^phpwiki:/", $link))
374         return new Cached_PhpwikiURL($link, $label);
375     /*
376      * Inline images in Interwiki urls's:
377      * [File:my_image.gif] inlines the image,
378      * File:my_image.gif shows a plain inter-wiki link,
379      * [what a pic|File:my_image.gif] shows a named inter-wiki link to the gif
380      * [File:my_image.gif|what a pic] shows a inlimed image linked to the page "what a pic"
381      */
382     elseif (strstr($link,':') and 
383             ($intermap = getInterwikiMap()) and 
384             preg_match("/^" . $intermap->getRegexp() . ":/", $link)) {
385         if (empty($label) && isImageLink($link)) {
386             // if without label => inlined image [File:xx.gif]
387             $imgurl = $intermap->link($link);
388             return LinkImage($imgurl->getAttr('href'), $label);
389         }
390         return new Cached_InterwikiLink($link, $label);
391     } else {
392         // Split anchor off end of pagename.
393         if (preg_match('/\A(.*)(?<!'.ESCAPE_CHAR.')#(.*?)\Z/', $rawlink, $m)) {
394             list(,$rawlink,$anchor) = $m;
395             $pagename = UnWikiEscape($rawlink);
396             $anchor = UnWikiEscape($anchor);
397             if (!$label)
398                 $label = $link;
399         }
400         else {
401             $pagename = $link;
402             $anchor = false;
403         }
404         return new Cached_WikiLink($pagename, $label, $anchor);
405     }
406 }
407
408 class Markup_bracketlink  extends SimpleMarkup
409 {
410     var $_match_regexp = "\\#? \\[ .*? [^]\\s] .*? \\]";
411     
412     function markup ($match) {
413         $link = LinkBracketLink($match);
414         assert($link->isInlineElement());
415         return $link;
416     }
417 }
418
419 class Markup_url extends SimpleMarkup
420 {
421     function getMatchRegexp () {
422         return "(?<![[:alnum:]]) (?:" . ALLOWED_PROTOCOLS . ") : [^\s<>\"']+ (?<![ ,.?; \] \) ])";
423     }
424     
425     function markup ($match) {
426         return new Cached_ExternalLink(UnWikiEscape($match));
427     }
428 }
429
430
431 class Markup_interwiki extends SimpleMarkup
432 {
433     function getMatchRegexp () {
434         global $request;
435         $map = getInterwikiMap();
436         return "(?<! [[:alnum:]])" . $map->getRegexp(). ": \S+ (?<![ ,.?;! \] \) \" \' ])";
437     }
438
439     function markup ($match) {
440         //$map = getInterwikiMap();
441         return new Cached_InterwikiLink(UnWikiEscape($match));
442     }
443 }
444
445 class Markup_wikiword extends SimpleMarkup
446 {
447     function getMatchRegexp () {
448         global $WikiNameRegexp;
449         if (!trim($WikiNameRegexp)) return " " . WIKI_NAME_REGEXP;
450         return " $WikiNameRegexp";
451     }
452
453     function markup ($match) {
454         if (!$match) return false;
455         if ($this->_isWikiUserPage($match))
456             return new Cached_UserLink($match); //$this->_UserLink($match);
457         else
458             return new Cached_WikiLink($match);
459     }
460
461     // FIXME: there's probably a more useful place to put these two functions    
462     function _isWikiUserPage ($page) {
463         global $request;
464         $dbi = $request->getDbh();
465         $page_handle = $dbi->getPage($page);
466         if ($page_handle and $page_handle->get('pref'))
467             return true;
468         else
469             return false;
470     }
471
472     function _UserLink($PageName) {
473         $link = HTML::a(array('href' => $PageName));
474         $link->pushContent(PossiblyGlueIconToText('wikiuser', $PageName));
475         $link->setAttr('class', 'wikiuser');
476         return $link;
477     }
478 }
479
480 class Markup_linebreak extends SimpleMarkup
481 {
482     //var $_match_regexp = "(?: (?<! %) %%% (?! %) | <(?:br|BR)> | <(?:br|BR) \/> )";
483     var $_match_regexp = "(?: (?<! %) %%% (?! %) | <(?:br|BR)> )";
484
485     function markup ($match) {
486         return HTML::br();
487     }
488 }
489
490 class Markup_old_emphasis  extends BalancedMarkup
491 {
492     var $_start_regexp = "''|__";
493
494     function getEndRegexp ($match) {
495         return $match;
496     }
497     
498     function markup ($match, $body) {
499         $tag = $match == "''" ? 'em' : 'strong';
500         return new HtmlElement($tag, $body);
501     }
502 }
503
504 class Markup_nestled_emphasis extends BalancedMarkup
505 {
506     function getStartRegexp() {
507         static $start_regexp = false;
508
509         if (!$start_regexp) {
510             // The three possible delimiters
511             // (none of which can be followed by itself.)
512             $i = "_ (?! _)";
513             $b = "\\* (?! \\*)";
514             $tt = "= (?! =)";
515
516             $any = "(?: ${i}|${b}|${tt})"; // any of the three.
517
518             // Any of [_*=] is okay if preceded by space or one of [-"'/:]
519             $start[] = "(?<= \\s|^|[-\"'\\/:]) ${any}";
520
521             // _ or * is okay after = as long as not immediately followed by =
522             $start[] = "(?<= =) (?: ${i}|${b}) (?! =)";
523             // etc...
524             $start[] = "(?<= _) (?: ${b}|${tt}) (?! _)";
525             $start[] = "(?<= \\*) (?: ${i}|${tt}) (?! \\*)";
526
527
528             // any delimiter okay after an opening brace ( [{<(] )
529             // as long as it's not immediately followed by the matching closing
530             // brace.
531             $start[] = "(?<= { ) ${any} (?! } )";
532             $start[] = "(?<= < ) ${any} (?! > )";
533             $start[] = "(?<= \\( ) ${any} (?! \\) )";
534             
535             $start = "(?:" . join('|', $start) . ")";
536             
537             // Any of the above must be immediately followed by non-whitespace.
538             $start_regexp = $start . "(?= \S)";
539         }
540
541         return $start_regexp;
542     }
543
544     function getEndRegexp ($match) {
545         $chr = preg_quote($match);
546         return "(?<= \S | ^ ) (?<! $chr) $chr (?! $chr) (?= \s | [-)}>\"'\\/:.,;!? _*=] | $)";
547     }
548     
549     function markup ($match, $body) {
550         switch ($match) {
551         case '*': return new HtmlElement('b', $body);
552         case '=': return new HtmlElement('tt', $body);
553         case '_': return new HtmlElement('i', $body);
554         }
555     }
556 }
557
558 class Markup_html_emphasis extends BalancedMarkup
559 {
560     var $_start_regexp = 
561         "<(?: b|big|i|small|tt|em|strong|cite|code|dfn|kbd|samp|var|sup|sub )>";
562
563     function getEndRegexp ($match) {
564         return "<\\/" . substr($match, 1);
565     }
566     
567     function markup ($match, $body) {
568         $tag = substr($match, 1, -1);
569         return new HtmlElement($tag, $body);
570     }
571 }
572
573 class Markup_html_abbr extends BalancedMarkup
574 {
575     //rurban: abbr|acronym need an optional title tag.
576     //sf.net bug #728595
577     var $_start_regexp = "<(?: abbr|acronym )(?: \stitle=[^>]*)?>";
578
579     function getEndRegexp ($match) {
580         if (substr($match,1,4) == 'abbr')
581             $tag = 'abbr';
582         else
583             $tag = 'acronym';
584         return "<\\/" . $tag . '>';
585     }
586     
587     function markup ($match, $body) {
588         if (substr($match,1,4) == 'abbr')
589             $tag = 'abbr';
590         else
591             $tag = 'acronym';
592         $rest = substr($match,1+strlen($tag),-1);
593         if (!empty($rest)) {
594             list($key,$val) = explode("=",$rest);
595             $args = array($key => $val);
596         } else $args = array();
597         return new HtmlElement($tag, $args, $body);
598     }
599 }
600
601 // Special version for single-line plugins formatting, 
602 //  like: '<small>< ?plugin PopularNearby ? ></small>'
603 class Markup_plugin extends SimpleMarkup
604 {
605     var $_match_regexp = '<\?plugin(?:-form)?\s[^\n]+?\?>';
606
607     function markup ($match) {
608         //$xml = new Cached_PluginInvocation($match);
609         //$xml->setTightness(true,true);
610         return new Cached_PluginInvocation($match);
611     }
612 }
613
614
615 // TODO: "..." => "&#133;"  browser specific display (not cached?)
616 // TODO: "--" => "&emdash;" browser specific display (not cached?)
617
618 // FIXME: Do away with magic phpwiki forms.  (Maybe phpwiki: links too?)
619 // FIXME: Do away with plugin-links.  They seem not to be used.
620 //Plugin link
621
622
623 class InlineTransformer
624 {
625     var $_regexps = array();
626     var $_markup = array();
627     
628     function InlineTransformer ($markup_types = false) {
629         if (!$markup_types)
630             $markup_types = array('escape', 'bracketlink', 'url',
631                                   'interwiki', 'wikiword', 'linebreak',
632                                   'old_emphasis', 'nestled_emphasis',
633                                   'html_emphasis', 'html_abbr', 'plugin');
634         foreach ($markup_types as $mtype) {
635             $class = "Markup_$mtype";
636             $this->_addMarkup(new $class);
637         }
638     }
639
640     function _addMarkup ($markup) {
641         if (isa($markup, 'SimpleMarkup'))
642             $regexp = $markup->getMatchRegexp();
643         else
644             $regexp = $markup->getStartRegexp();
645
646         assert(!isset($this->_markup[$regexp]));
647         $this->_regexps[] = $regexp;
648         $this->_markup[] = $markup;
649     }
650         
651     function parse (&$text, $end_regexps = array('$')) {
652         $regexps = $this->_regexps;
653
654         // $end_re takes precedence: "favor reduce over shift"
655         array_unshift($regexps, $end_regexps[0]);
656         //array_push($regexps, $end_regexps[0]);
657         $regexps = new RegexpSet($regexps);
658         
659         $input = $text;
660         $output = new XmlContent;
661
662         $match = $regexps->match($input);
663         
664         while ($match) {
665             if ($match->regexp_ind == 0) {
666                 // No start pattern found before end pattern.
667                 // We're all done!
668                 if (isset($markup) and is_object($markup) and isa($markup,'Markup_plugin')) {
669                     $current =& $output->_content[count($output->_content)-1];
670                     $current->setTightness(true,true);
671                 }
672                 $output->pushContent($match->prematch);
673                 $text = $match->postmatch;
674                 return $output;
675             }
676
677             $markup = $this->_markup[$match->regexp_ind - 1];
678             $body = $this->_parse_markup_body($markup, $match->match, $match->postmatch, $end_regexps);
679             if (!$body) {
680                 // Couldn't match balanced expression.
681                 // Ignore and look for next matching start regexp.
682                 $match = $regexps->nextMatch($input, $match);
683                 continue;
684             }
685
686             // Matched markup.  Eat input, push output.
687             // FIXME: combine adjacent strings.
688             $current = $markup->markup($match->match, $body);
689             $input = $match->postmatch;
690             if (isset($markup) and is_object($markup) and isa($markup,'Markup_plugin')) {
691                 $current->setTightness(true,true);
692             }
693             $output->pushContent($match->prematch, $current);
694
695             $match = $regexps->match($input);
696         }
697
698         // No pattern matched, not even the end pattern.
699         // Parse fails.
700         return false;
701     }
702
703     function _parse_markup_body ($markup, $match, &$text, $end_regexps) {
704         if (isa($markup, 'SimpleMarkup'))
705             return true;        // Done. SimpleMarkup is simple.
706
707         if (!is_object($markup)) return false; // Some error: Should assert
708         array_unshift($end_regexps, $markup->getEndRegexp($match));
709
710         // Optimization: if no end pattern in text, we know the
711         // parse will fail.  This is an important optimization,
712         // e.g. when text is "*lots *of *start *delims *with
713         // *no *matching *end *delims".
714         $ends_pat = "/(?:" . join(").*(?:", $end_regexps) . ")/xs";
715         if (!preg_match($ends_pat, $text))
716             return false;
717         return $this->parse($text, $end_regexps);
718     }
719 }
720
721 class LinkTransformer extends InlineTransformer
722 {
723     function LinkTransformer () {
724         $this->InlineTransformer(array('escape', 'bracketlink', 'url',
725                                        'interwiki', 'wikiword'));
726     }
727 }
728
729 function TransformInline($text, $markup = 2.0, $basepage=false) {
730     static $trfm;
731     
732     if (empty($trfm)) {
733         $trfm = new InlineTransformer;
734     }
735     
736     if ($markup < 2.0) {
737         $text = ConvertOldMarkup($text, 'inline');
738     }
739
740     if ($basepage) {
741         return new CacheableMarkup($trfm->parse($text), $basepage);
742     }
743     return $trfm->parse($text);
744 }
745
746 function TransformLinks($text, $markup = 2.0, $basepage = false) {
747     static $trfm;
748     
749     if (empty($trfm)) {
750         $trfm = new LinkTransformer;
751     }
752
753     if ($markup < 2.0) {
754         $text = ConvertOldMarkup($text, 'links');
755     }
756     
757     if ($basepage) {
758         return new CacheableMarkup($trfm->parse($text), $basepage);
759     }
760     return $trfm->parse($text);
761 }
762
763 // (c-file-style: "gnu")
764 // Local Variables:
765 // mode: php
766 // tab-width: 8
767 // c-basic-offset: 4
768 // c-hanging-comment-ender-p: nil
769 // indent-tabs-mode: nil
770 // End:   
771 ?>