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