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>
5 * This file is part of PhpWiki.
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.
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.
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
22 * This is the code which deals with the inline part of the (new-style)
26 * @author Geoffrey T. Dairiki
32 * This is the character used in wiki markup to escape characters with
35 define('ESCAPE_CHAR', '~');
37 require_once('lib/HtmlElement.php');
38 require_once('lib/CachedMarkup.php');
39 //require_once('lib/interwiki.php');
40 require_once('lib/stdlib.php');
43 function WikiEscape($text) {
44 return str_replace('#', ESCAPE_CHAR . '#', $text);
47 function UnWikiEscape($text) {
48 return preg_replace('/' . ESCAPE_CHAR . '(.)/', '\1', $text);
52 * Return type from RegexpSet::match and RegexpSet::nextMatch.
56 class RegexpSet_match {
58 * The text leading up the the next match.
68 * The text following the matched text.
73 * Index of the regular expression which matched.
79 * A set of regular expressions.
81 * This class is probably only useful for InlineTransformer.
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.)
92 function RegexpSet ($regexps) {
94 $this->_regexps = array_unique($regexps);
98 * Search text for the next matching regexp from the Regexp Set.
100 * @param string $text The text to search.
102 * @return RegexpSet_match A RegexpSet_match object, or false if no match.
104 function match ($text) {
105 return $this->_match($text, $this->_regexps, '*?');
109 * Search for next matching regexp.
111 * Here, 'next' has two meanings:
113 * Match the next regexp(s) in the set, at the same position as the last match.
115 * If that fails, match the whole RegexpSet, starting after the position of the
118 * @param string $text Text to search.
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.
124 * @return RegexpSet_match A RegexpSet_match object, or false if no match.
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);
131 $repeat = sprintf('{%d}', $pos);
132 if ( ($match = $this->_match($text, $regexps, $repeat)) ) {
133 $match->regexp_ind += $prevMatch->regexp_ind + 1;
139 // Failed. Look for match after current position.
140 $repeat = sprintf('{%d,}?', $pos + 1);
141 return $this->_match($text, $this->_regexps, $repeat);
145 function _match ($text, $regexps, $repeat) {
146 $pat= "/ ( . $repeat ) ( (" . join(')|(', $regexps) . ") ) /Axs";
148 if (! preg_match($pat, $text, $m)) {
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;
159 PrintXML(HTML::dl(HTML::dt("input"),
160 HTML::dd(HTML::pre($text)),
162 HTML::dd(HTML::pre($match->match)),
164 HTML::dd(HTML::pre($regexps[$match->regexp_ind])),
165 HTML::dt("prematch"),
166 HTML::dd(HTML::pre($match->prematch))));
175 * A simple markup rule (i.e. terminal token).
177 * These are defined by a regexp.
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.
188 * @return string Regexp which matches this token.
190 function getMatchRegexp () {
191 return $this->_match_regexp;
194 /** Markup matching text.
196 * @param string $match The text which matched the regexp
197 * (obtained from getMatchRegexp).
199 * @return mixed The expansion of the matched text.
201 function markup ($match /*, $body */) {
202 trigger_error("pure virtual", E_USER_ERROR);
207 * A balanced markup rule.
209 * These are defined by a start regexp, and an end regexp.
215 /** Get the starting regexp for this rule.
217 * @return string The starting regexp.
219 function getStartRegexp () {
220 return $this->_start_regexp;
223 /** Get the ending regexp for this rule.
225 * @param string $match The text which matched the starting regexp.
227 * @return string The ending regexp.
229 function getEndRegexp ($match) {
230 return $this->_end_regexp;
233 /** Get expansion for matching input.
235 * @param string $match The text which matched the starting regexp.
237 * @param mixed $body Transformed text found between the starting
238 * and ending regexps.
240 * @return mixed The expansion of the matched text.
242 function markup ($match, $body) {
243 trigger_error("pure virtual", E_USER_ERROR);
247 class Markup_escape extends SimpleMarkup
249 function getMatchRegexp () {
250 return ESCAPE_CHAR . '(?: [[:alnum:]]+ | .)';
253 function markup ($match) {
254 assert(strlen($match) >= 2);
255 return substr($match, 1);
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
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);
271 function LinkBracketLink($bracketlink) {
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.
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;
281 $label = UnWikiEscape($label);
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 ~
288 if (strstr($rawlink, "http://") or strstr($rawlink, "https://"))
291 $link = UnWikiEscape($rawlink);
294 // if label looks like a url to an image, we want an image link.
295 if (isImageLink($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'.
303 $imgurl = $Theme->getImageURL($imgurl);
305 $label = LinkImage($imgurl, $link);
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);
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);
320 return new Cached_ExternalLink($link, $label);
322 elseif (preg_match("/^phpwiki:/", $link))
323 return new Cached_PhpwikiURL($link, $label);
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"
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);
339 return new Cached_InterwikiLink($link, $label);
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);
353 return new Cached_WikiLink($pagename, $label, $anchor);
357 class Markup_bracketlink extends SimpleMarkup
359 var $_match_regexp = "\\#? \\[ .*? [^]\\s] .*? \\]";
361 function markup ($match) {
362 $link = LinkBracketLink($match);
363 assert($link->isInlineElement());
368 class Markup_url extends SimpleMarkup
370 function getMatchRegexp () {
371 return "(?<![[:alnum:]]) (?:" . ALLOWED_PROTOCOLS . ") : [^\s<>\"']+ (?<![ ,.?; \] \) ])";
374 function markup ($match) {
375 return new Cached_ExternalLink(UnWikiEscape($match));
380 class Markup_interwiki extends SimpleMarkup
382 function getMatchRegexp () {
384 $map = getInterwikiMap();
385 return "(?<! [[:alnum:]])" . $map->getRegexp(). ": \S+ (?<![ ,.?;! \] \) \" \' ])";
388 function markup ($match) {
389 //$map = getInterwikiMap();
390 return new Cached_InterwikiLink(UnWikiEscape($match));
394 class Markup_wikiword extends SimpleMarkup
396 function getMatchRegexp () {
397 global $WikiNameRegexp;
398 return " $WikiNameRegexp";
401 function markup ($match) {
402 if (!$match) return false;
403 if ($this->_isWikiUserPage($match))
404 return new Cached_UserLink($match); //$this->_UserLink($match);
406 return new Cached_WikiLink($match);
409 // FIXME: there's probably a more useful place to put these two functions
410 function _isWikiUserPage ($page) {
412 $dbi = $request->getDbh();
413 $page_handle = $dbi->getPage($page);
414 if ($page_handle and $page_handle->get('pref'))
420 function _UserLink($PageName) {
421 $link = HTML::a(array('href' => $PageName));
422 $link->pushContent(PossiblyGlueIconToText('wikiuser', $PageName));
423 $link->setAttr('class', 'wikiuser');
428 class Markup_linebreak extends SimpleMarkup
430 //var $_match_regexp = "(?: (?<! %) %%% (?! %) | <(?:br|BR)> | <(?:br|BR) \/> )";
431 var $_match_regexp = "(?: (?<! %) %%% (?! %) | <(?:br|BR)> )";
433 function markup ($match) {
438 class Markup_old_emphasis extends BalancedMarkup
440 var $_start_regexp = "''|__";
442 function getEndRegexp ($match) {
446 function markup ($match, $body) {
447 $tag = $match == "''" ? 'em' : 'strong';
448 return new HtmlElement($tag, $body);
452 class Markup_nestled_emphasis extends BalancedMarkup
454 function getStartRegexp() {
455 static $start_regexp = false;
457 if (!$start_regexp) {
458 // The three possible delimiters
459 // (none of which can be followed by itself.)
464 $any = "(?: ${i}|${b}|${tt})"; // any of the three.
466 // Any of [_*=] is okay if preceded by space or one of [-"'/:]
467 $start[] = "(?<= \\s|^|[-\"'\\/:]) ${any}";
469 // _ or * is okay after = as long as not immediately followed by =
470 $start[] = "(?<= =) (?: ${i}|${b}) (?! =)";
472 $start[] = "(?<= _) (?: ${b}|${tt}) (?! _)";
473 $start[] = "(?<= \\*) (?: ${i}|${tt}) (?! \\*)";
476 // any delimiter okay after an opening brace ( [{<(] )
477 // as long as it's not immediately followed by the matching closing
479 $start[] = "(?<= { ) ${any} (?! } )";
480 $start[] = "(?<= < ) ${any} (?! > )";
481 $start[] = "(?<= \\( ) ${any} (?! \\) )";
483 $start = "(?:" . join('|', $start) . ")";
485 // Any of the above must be immediately followed by non-whitespace.
486 $start_regexp = $start . "(?= \S)";
489 return $start_regexp;
492 function getEndRegexp ($match) {
493 $chr = preg_quote($match);
494 return "(?<= \S | ^ ) (?<! $chr) $chr (?! $chr) (?= \s | [-)}>\"'\\/:.,;!? _*=] | $)";
497 function markup ($match, $body) {
499 case '*': return new HtmlElement('b', $body);
500 case '=': return new HtmlElement('tt', $body);
501 case '_': return new HtmlElement('i', $body);
506 class Markup_html_emphasis extends BalancedMarkup
508 var $_start_regexp = "<(?: b|big|i|small|tt|
510 cite|code|dfn|kbd|samp|var|
513 function getEndRegexp ($match) {
514 return "<\\/" . substr($match, 1);
517 function markup ($match, $body) {
518 $tag = substr($match, 1, -1);
519 return new HtmlElement($tag, $body);
523 class Markup_html_abbr extends BalancedMarkup
525 //rurban: abbr|acronym need an optional title tag.
527 var $_start_regexp = "<(?: abbr|acronym )(?: \stitle=[^>]*)?>";
529 function getEndRegexp ($match) {
530 if (substr($match,1,4) == 'abbr')
534 return "<\\/" . $tag . '>';
537 function markup ($match, $body) {
538 if (substr($match,1,4) == 'abbr')
542 $rest = substr($match,1+strlen($tag),-1);
544 list($key,$val) = explode("=",$rest);
545 $args = array($key => $val);
546 } else $args = array();
547 return new HtmlElement($tag, $args, $body);
551 // Special version for single-line plugins formatting,
552 // like: '<small>< ?plugin PopularNearby ? ></small>'
553 class Markup_plugin extends SimpleMarkup
555 var $_match_regexp = '<\?plugin(?:-form)?\s[^\n]+?\?>';
557 function markup ($match) {
558 return new Cached_PluginInvocation($match);
563 // TODO: "..." => "…" browser specific display (not cached?)
564 // TODO: "--" => "&emdash;" browser specific display (not cached?)
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.
571 class InlineTransformer
573 var $_regexps = array();
574 var $_markup = array();
576 function InlineTransformer ($markup_types = false) {
578 $markup_types = array('escape', 'bracketlink', 'url',
579 'interwiki', 'wikiword', 'linebreak',
580 'old_emphasis', 'nestled_emphasis',
581 'html_emphasis', 'html_abbr', 'plugin');
583 foreach ($markup_types as $mtype) {
584 $class = "Markup_$mtype";
585 $this->_addMarkup(new $class);
589 function _addMarkup ($markup) {
590 if (isa($markup, 'SimpleMarkup'))
591 $regexp = $markup->getMatchRegexp();
593 $regexp = $markup->getStartRegexp();
595 assert(!isset($this->_markup[$regexp]));
596 $this->_regexps[] = $regexp;
597 $this->_markup[] = $markup;
600 function parse (&$text, $end_regexps = array('$')) {
601 $regexps = $this->_regexps;
603 // $end_re takes precedence: "favor reduce over shift"
604 array_unshift($regexps, $end_regexps[0]);
605 $regexps = new RegexpSet($regexps);
608 $output = new XmlContent;
610 $match = $regexps->match($input);
613 if ($match->regexp_ind == 0) {
614 // No start pattern found before end pattern.
616 $output->pushContent($match->prematch);
617 $text = $match->postmatch;
621 $markup = $this->_markup[$match->regexp_ind - 1];
622 $body = $this->_parse_markup_body($markup, $match->match, $match->postmatch, $end_regexps);
624 // Couldn't match balanced expression.
625 // Ignore and look for next matching start regexp.
626 $match = $regexps->nextMatch($input, $match);
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));
636 $match = $regexps->match($input);
639 // No pattern matched, not even the end pattern.
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));
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))
657 return $this->parse($text, $end_regexps);
661 class LinkTransformer extends InlineTransformer
663 function LinkTransformer () {
664 $this->InlineTransformer(array('escape', 'bracketlink', 'url',
665 'interwiki', 'wikiword'));
669 function TransformInline($text, $markup = 2.0, $basepage=false) {
673 $trfm = new InlineTransformer;
677 $text = ConvertOldMarkup($text, 'inline');
681 return new CacheableMarkup($trfm->parse($text), $basepage);
683 return $trfm->parse($text);
686 function TransformLinks($text, $markup = 2.0, $basepage = false) {
690 $trfm = new LinkTransformer;
694 $text = ConvertOldMarkup($text, 'links');
698 return new CacheableMarkup($trfm->parse($text), $basepage);
700 return $trfm->parse($text);
703 // (c-file-style: "gnu")
708 // c-hanging-comment-ender-p: nil
709 // indent-tabs-mode: nil