2 rcs_id('$Id: InlineParser.php,v 1.79 2006-10-08 12:38:11 rurban Exp $');
3 /* Copyright (C) 2002 Geoffrey T. Dairiki <dairiki@dairiki.org>
4 * Copyright (C) 2004,2005,2006 Reini Urban
6 * This file is part of PhpWiki.
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.
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.
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
23 * This is the code which deals with the inline part of the (new-style)
27 * @author Geoffrey T. Dairiki
33 * This is the character used in wiki markup to escape characters with
36 define('ESCAPE_CHAR', '~');
38 require_once(dirname(__FILE__).'/HtmlElement.php');
39 require_once('lib/CachedMarkup.php');
40 require_once(dirname(__FILE__).'/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.
66 * The text following the matched text.
70 * Index of the regular expression which matched.
76 * A set of regular expressions.
78 * This class is probably only useful for InlineTransformer.
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.)
89 function RegexpSet ($regexps) {
91 $this->_regexps = array_unique($regexps);
92 if (!defined('_INLINE_OPTIMIZATION')) define('_INLINE_OPTIMIZATION',0);
96 * Search text for the next matching regexp from the Regexp Set.
98 * @param string $text The text to search.
100 * @return RegexpSet_match A RegexpSet_match object, or false if no match.
102 function match ($text) {
103 return $this->_match($text, $this->_regexps, '*?');
107 * Search for next matching regexp.
109 * Here, 'next' has two meanings:
111 * Match the next regexp(s) in the set, at the same position as the last match.
113 * If that fails, match the whole RegexpSet, starting after the position of the
116 * @param string $text Text to search.
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.
122 * @return RegexpSet_match A RegexpSet_match object, or false if no match.
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);
129 $repeat = sprintf('{%d}', $pos);
130 if ( ($match = $this->_match($text, $regexps, $repeat)) ) {
131 $match->regexp_ind += $prevMatch->regexp_ind + 1;
137 // Failed. Look for match after current position.
138 $repeat = sprintf('{%d,}?', $pos + 1);
139 return $this->_match($text, $this->_regexps, $repeat);
142 // Syntax: http://www.pcre.org/pcre.txt
143 // x - EXTENDED, ignore whitespace
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);
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];
170 // To overcome ANCHORED:
171 // We could sort by longest match and iterate over these.
172 if (empty($matched)) return false;
174 $match = new RegexpSet_match;
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
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)) {
194 // Proposed premature optimization 1:
195 //$match->regexp_ind = $matched_ind[count($m) - 4];
196 $match->regexp_ind = count($m) - 4;
198 $match->regexp_ind = $regexp_ind;
201 $match->postmatch = substr($text, strlen($m[0]));
202 $match->prematch = $m[1];
203 $match->match = $m[2];
208 var_dump($regexps); var_dump($matched); var_dump($matched_inc);
209 PrintXML(HTML::dl(HTML::dt("input"),
210 HTML::dd(HTML::pre($text)),
212 HTML::dd(HTML::pre($match->regexp_ind, ":", $regexps[$match->regexp_ind])),
213 HTML::dt("prematch"),
214 HTML::dd(HTML::pre($match->prematch)),
216 HTML::dd(HTML::pre($match->match)),
217 HTML::dt("postmatch"),
218 HTML::dd(HTML::pre($match->postmatch))
229 * A simple markup rule (i.e. terminal token).
231 * These are defined by a regexp.
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.
242 * @return string Regexp which matches this token.
244 function getMatchRegexp () {
245 return $this->_match_regexp;
248 /** Markup matching text.
250 * @param string $match The text which matched the regexp
251 * (obtained from getMatchRegexp).
253 * @return mixed The expansion of the matched text.
255 function markup ($match /*, $body */) {
256 trigger_error("pure virtual", E_USER_ERROR);
261 * A balanced markup rule.
263 * These are defined by a start regexp, and an end regexp.
269 /** Get the starting regexp for this rule.
271 * @return string The starting regexp.
273 function getStartRegexp () {
274 return $this->_start_regexp;
277 /** Get the ending regexp for this rule.
279 * @param string $match The text which matched the starting regexp.
281 * @return string The ending regexp.
283 function getEndRegexp ($match) {
284 return $this->_end_regexp;
287 /** Get expansion for matching input.
289 * @param string $match The text which matched the starting regexp.
291 * @param mixed $body Transformed text found between the starting
292 * and ending regexps.
294 * @return mixed The expansion of the matched text.
296 function markup ($match, $body) {
297 trigger_error("pure virtual", E_USER_ERROR);
301 class Markup_escape extends SimpleMarkup
303 function getMatchRegexp () {
304 return ESCAPE_CHAR . '(?: [[:alnum:]]+ | .)';
307 function markup ($match) {
308 assert(strlen($match) >= 2);
309 return substr($match, 1);
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
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);
326 function LinkBracketLink($bracketlink) {
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.
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_WARNING);
337 return new Cached_Link;
339 list (, $hash, $label, $bar, $rawlink) = $matches;
341 $label = UnWikiEscape($label);
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 ~
348 if (strstr($rawlink, "http://") or strstr($rawlink, "https://")) {
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);
357 $link = UnWikiEscape($rawlink);
359 /* Relatives links by Joel Schaubert.
360 * Recognize [../bla] or [/bla] as relative links, without needing http://
361 * but [ /link ] only if SUBPAGE_SEPERATOR is not "/".
362 * Normally /Page links to the subpage /Page.
364 if (SUBPAGE_SEPARATOR == '/') {
365 if (preg_match('/^\.\.\//', $link)) {
366 return new Cached_ExternalLink($link, $label);
368 } else if (preg_match('/^(\.\.\/|\/)/', $link)) {
369 return new Cached_ExternalLink($link, $label);
372 // if label looks like a url to an image, we want an image link.
373 if (isImageLink($label)) {
375 $intermap = getInterwikiMap();
376 if (preg_match("/^" . $intermap->getRegexp() . ":/", $label)) {
377 $imgurl = $intermap->link($label);
378 $imgurl = $imgurl->getAttr('href');
379 } elseif (! preg_match("#^(" . ALLOWED_PROTOCOLS . "):#", $imgurl)) {
380 // local theme linkname like 'images/next.gif'.
382 $imgurl = $WikiTheme->getImageURL($imgurl);
384 $label = LinkImage($imgurl, $link);
388 // It's an anchor, not a link...
389 $id = MangleXmlIdentifier($link);
390 return HTML::a(array('name' => $id, 'id' => $id),
391 $bar ? $label : $link);
394 if (preg_match("#^(" . ALLOWED_PROTOCOLS . "):#", $link)) {
395 // if it's an image, embed it; otherwise, it's a regular link
396 if (isImageLink($link) and empty($label)) // patch #1348996 by Robert Litwiniec
397 return LinkImage($link, $label);
399 return new Cached_ExternalLink($link, $label);
401 elseif (preg_match("/^phpwiki:/", $link))
402 return new Cached_PhpwikiURL($link, $label);
403 /* Semantic relations and attributes */
404 elseif (preg_match("/:[:-]/", $link) and !isImageLink($link))
405 return new Cached_SemanticLink($link, $label);
406 /* Do not store the link */
407 elseif (substr($link,0,1) == ':')
408 return new Cached_InterwikiLink($link, $label);
410 * Inline images in Interwiki urls's:
411 * [File:my_image.gif] inlines the image,
412 * File:my_image.gif shows a plain inter-wiki link,
413 * [what a pic|File:my_image.gif] shows a named inter-wiki link to the gif
414 * [File:my_image.gif|what a pic] shows a inlimed image linked to the page "what a pic"
416 * Note that for simplicity we will accept embedded object tags (non-images)
417 * here also, and seperate them later in LinkImage()
419 elseif (strstr($link,':')
420 and ($intermap = getInterwikiMap())
421 and preg_match("/^" . $intermap->getRegexp() . ":/", $link))
423 // trigger_error("label: $label link: $link", E_USER_WARNING);
424 if (empty($label) and isImageLink($link)) {
425 // if without label => inlined image [File:xx.gif]
426 $imgurl = $intermap->link($link);
427 return LinkImage($imgurl->getAttr('href'), $label);
429 return new Cached_InterwikiLink($link, $label);
431 // Split anchor off end of pagename.
432 if (preg_match('/\A(.*)(?<!'.ESCAPE_CHAR.')#(.*?)\Z/', $rawlink, $m)) {
433 list(,$rawlink,$anchor) = $m;
434 $pagename = UnWikiEscape($rawlink);
435 $anchor = UnWikiEscape($anchor);
443 return new Cached_WikiLink($pagename, $label, $anchor);
447 class Markup_bracketlink extends SimpleMarkup
449 var $_match_regexp = "\\#? \\[ .*? [^]\\s] .*? \\]";
451 function markup ($match) {
452 $link = LinkBracketLink($match);
453 assert($link->isInlineElement());
458 class Markup_url extends SimpleMarkup
460 function getMatchRegexp () {
461 return "(?<![[:alnum:]]) (?:" . ALLOWED_PROTOCOLS . ") : [^\s<>\"']+ (?<![ ,.?; \] \) ])";
464 function markup ($match) {
465 return new Cached_ExternalLink(UnWikiEscape($match));
470 class Markup_interwiki extends SimpleMarkup
472 function getMatchRegexp () {
474 $map = getInterwikiMap();
475 return "(?<! [[:alnum:]])" . $map->getRegexp(). ": \S+ (?<![ ,.?;! \] \) \" \' ])";
478 function markup ($match) {
479 //$map = getInterwikiMap();
480 return new Cached_InterwikiLink(UnWikiEscape($match));
484 class Markup_wikiword extends SimpleMarkup
486 function getMatchRegexp () {
487 global $WikiNameRegexp;
488 if (!trim($WikiNameRegexp)) return " " . WIKI_NAME_REGEXP;
489 return " $WikiNameRegexp";
492 function markup ($match) {
493 if (!$match) return false;
494 if ($this->_isWikiUserPage($match))
495 return new Cached_UserLink($match); //$this->_UserLink($match);
497 return new Cached_WikiLink($match);
500 // FIXME: there's probably a more useful place to put these two functions
501 function _isWikiUserPage ($page) {
503 $dbi = $request->getDbh();
504 $page_handle = $dbi->getPage($page);
505 if ($page_handle and $page_handle->get('pref'))
511 function _UserLink($PageName) {
512 $link = HTML::a(array('href' => $PageName));
513 $link->pushContent(PossiblyGlueIconToText('wikiuser', $PageName));
514 $link->setAttr('class', 'wikiuser');
519 class Markup_linebreak extends SimpleMarkup
521 //var $_match_regexp = "(?: (?<! %) %%% (?! %) | <(?:br|BR)> | <(?:br|BR) \/> )";
522 var $_match_regexp = "(?: (?<! %) %%% (?! %) | <(?:br|BR)> )";
524 function markup ($match) {
529 class Markup_old_emphasis extends BalancedMarkup
531 var $_start_regexp = "''|__";
533 function getEndRegexp ($match) {
537 function markup ($match, $body) {
538 $tag = $match == "''" ? 'em' : 'strong';
539 return new HtmlElement($tag, $body);
543 class Markup_nestled_emphasis extends BalancedMarkup
545 function getStartRegexp() {
546 static $start_regexp = false;
548 if (!$start_regexp) {
549 // The three possible delimiters
550 // (none of which can be followed by itself.)
555 $any = "(?: ${i}|${b}|${tt})"; // any of the three.
557 // Any of [_*=] is okay if preceded by space or one of [-"'/:]
558 $start[] = "(?<= \\s|^|[-\"'\\/:]) ${any}";
560 // _ or * is okay after = as long as not immediately followed by =
561 $start[] = "(?<= =) (?: ${i}|${b}) (?! =)";
563 $start[] = "(?<= _) (?: ${b}|${tt}) (?! _)";
564 $start[] = "(?<= \\*) (?: ${i}|${tt}) (?! \\*)";
567 // any delimiter okay after an opening brace ( [{<(] )
568 // as long as it's not immediately followed by the matching closing
570 $start[] = "(?<= { ) ${any} (?! } )";
571 $start[] = "(?<= < ) ${any} (?! > )";
572 $start[] = "(?<= \\( ) ${any} (?! \\) )";
574 $start = "(?:" . join('|', $start) . ")";
576 // Any of the above must be immediately followed by non-whitespace.
577 $start_regexp = $start . "(?= \S)";
580 return $start_regexp;
583 function getEndRegexp ($match) {
584 $chr = preg_quote($match);
585 return "(?<= \S | ^ ) (?<! $chr) $chr (?! $chr) (?= \s | [-)}>\"'\\/:.,;!? _*=] | $)";
588 function markup ($match, $body) {
590 case '*': return new HtmlElement('b', $body);
591 case '=': return new HtmlElement('tt', $body);
592 case '_': return new HtmlElement('i', $body);
597 class Markup_html_emphasis extends BalancedMarkup
600 "<(?: b|big|i|small|tt|em|strong|cite|code|dfn|kbd|samp|strike|del|var|sup|sub )>";
602 function getEndRegexp ($match) {
603 return "<\\/" . substr($match, 1);
606 function markup ($match, $body) {
607 $tag = substr($match, 1, -1);
608 return new HtmlElement($tag, $body);
612 class Markup_html_abbr extends BalancedMarkup
614 //rurban: abbr|acronym need an optional title tag.
616 var $_start_regexp = "<(?: abbr|acronym )(?: \stitle=[^>]*)?>";
618 function getEndRegexp ($match) {
619 if (substr($match,1,4) == 'abbr')
623 return "<\\/" . $tag . '>';
626 function markup ($match, $body) {
627 if (substr($match,1,4) == 'abbr')
631 $rest = substr($match,1+strlen($tag),-1);
633 list($key,$val) = explode("=",$rest);
634 $args = array($key => $val);
635 } else $args = array();
636 return new HtmlElement($tag, $args, $body);
640 /** ENABLE_MARKUP_COLOR
641 * See http://www.pmwiki.org/wiki/PmWiki/WikiStyles and
642 * http://www.flexwiki.com/default.aspx/FlexWiki/FormattingRules.html
644 class Markup_color extends BalancedMarkup {
645 // %color=blue% blue text %% and back to normal
646 var $_start_regexp = "%color=(?: [^%]*)%";
647 var $_end_regexp = "%%";
649 function markup ($match, $body) {
650 $color = strtoupper(substr($match, 7, -1));
651 if (strlen($color) != 7
652 and in_array($color, array('RED', 'BLUE', 'GREY', 'YELLOW', 'GREEN', 'CYAN', 'BLACK')))
653 { // must be a valid color name
654 return new HtmlElement('font', array('color' => $color), $body);
655 } elseif ((substr($color,0,1) == '#')
656 and (strspn(substr($color,1),'0123456789ABCDEF') == strlen($color)-1)) {
657 return new HtmlElement('font', array('color' => $color), $body);
659 trigger_error(sprintf(_("unknown color %s ignored"), substr($match, 7, -1)), E_USER_WARNING);
665 // Special version for single-line plugins formatting,
666 // like: '<small>< ?plugin PopularNearby ? ></small>'
667 class Markup_plugin extends SimpleMarkup
669 var $_match_regexp = '<\?plugin(?:-form)?\s[^\n]+?\?>';
671 function markup ($match) {
672 //$xml = new Cached_PluginInvocation($match);
673 //$xml->setTightness(true,true);
674 return new Cached_PluginInvocation($match);
678 // Special version for plugins in xml syntax
679 // <name arg=value>body</name> or <name /> => < ? plugin pluginname arg=value body ? >
680 // PLUGIN_MARKUP_MAP = "html:RawHtml dot:GraphViz toc:CreateToc amath:AsciiMath richtable:RichTable include:IncludePage tex:TexToPng"
681 class Markup_xml_plugin extends BalancedMarkup
683 //var $_start_regexp = "<(?: ".join('|',PLUGIN_MARKUP_MAP)." )(?: \s[^>]*)>";
685 function getStartRegexp ($match) {
686 static $_start_regexp;
687 if ($_start_regexp) return $_start_regexp;
688 if (!defined('PLUGIN_MARKUP_MAP'))
690 $pairs = split(' ', PLUGIN_MARKUP_MAP);
691 $this->_map = array();
692 foreach ($pairs as $pair) {
693 list($xml,$plugin) = split(':',$pair);
694 $this->_map[$xml] = $plugin;
696 //"<(?: html|dot|toc|amath|richtable|include|tex )(?: \s[^>]*)>"
697 return "<(?: ".join('|',array_keys($this->_map))." )(?:(?:\s[^>]*|/))>";;
699 function getEndRegexp ($match) {
700 return "<\\/" . $match . '>';
702 function markup ($match, $body) {
703 $name = substr($match,2,-2); $vars = '';
704 if (preg_match('/^(\S+)\|(.*)$/', $name, $_m)) {
706 $vars = $_m[2]; //str_replace(' ', '&', $_m[2]);
708 if (!isset($this->_map[$name])) {
709 trigger_error("No plugin for $ name $ vars defined.", E_USER_WARNING);
712 $plugin = $this->_map[$name];
713 return new Cached_PluginInvocation("<"."?plugin $plugin $vars $body ?".">");
717 /** ENABLE_MARKUP_TEMPLATE
718 * Template syntax similar to mediawiki
720 * => < ? plugin Template page=template ? >
721 * {{template|var=value|...}}
722 * => < ? plugin Template page=template vars="var=value&..." ? >
724 class Markup_template_plugin extends SimpleMarkup
726 var $_match_regexp = '\{\{\w[^\n]+\}\}';
728 function markup ($match) {
729 $page = substr($match,2,-2); $vars = '';
730 if (preg_match('/^(\S+)\|(.*)$/', $page, $_m)) {
732 $vars = str_replace('|', '&', $_m[2]);
735 $s = '<'.'?plugin Template page=' . $page . ' vars="' . $vars . '"?'.'>';
737 $s = '<'.'?plugin Template page=' . $page . '?'.'>';
738 return new Cached_PluginInvocation($s);
742 // "..." => "…" browser specific display (not cached?)
743 // Support some HTML::Entities: (C) for copy, --- for mdash, -- for ndash
744 // TODO: "--" => "&emdash;" browser specific display (not cached?)
746 class Markup_html_entities extends SimpleMarkup {
747 //var $_match_regexp = '(: \.\.\.|\-\-|\-\-\-|\(C\) )';
749 function Markup_html_entities() {
750 $this->_entities = array('...' => '…',
754 '©' => '©',
755 '™' => '™',
757 $this->_match_regexp =
759 join('|', array_map('preg_quote', array_keys($this->_entities))) .
763 function markup ($match) {
764 return HTML::Raw($this->_entities[$match]);
768 class Markup_isonumchars extends SimpleMarkup {
769 var $_match_regexp = '\&\#\d{2,5};';
771 function markup ($match) {
772 return HTML::Raw($match);
776 class Markup_isohexchars extends SimpleMarkup {
777 // hexnums, like ¤ <=> ¤
778 var $_match_regexp = '\&\#x[0-9a-fA-F]{2,4};';
780 function markup ($match) {
781 return HTML::Raw($match);
785 // FIXME: Do away with magic phpwiki forms. (Maybe phpwiki: links too?)
786 // FIXME: Do away with plugin-links. They seem not to be used.
789 class InlineTransformer
791 var $_regexps = array();
792 var $_markup = array();
794 function InlineTransformer ($markup_types = false) {
795 if (!$markup_types) {
796 $non_default = false;
797 if (DISABLE_MARKUP_WIKIWORD)
798 // TODO: easily and portably remove wikiword from array
799 $markup_types = array
800 ('escape', 'bracketlink', 'url',
801 'interwiki', /* 'wikiword', */ 'linebreak',
802 'old_emphasis', 'nestled_emphasis',
803 'html_emphasis', 'html_abbr', 'plugin',
804 'isonumchars', 'isohexchars', /*'html_entities'*/
807 $markup_types = array
808 ('escape', 'bracketlink', 'url',
809 'interwiki', 'wikiword', 'linebreak',
810 'old_emphasis', 'nestled_emphasis',
811 'html_emphasis', 'html_abbr', 'plugin',
812 'isonumchars', 'isohexchars', /*'html_entities'*/
817 foreach ($markup_types as $mtype) {
818 $class = "Markup_$mtype";
819 $this->_addMarkup(new $class);
821 if (ENABLE_MARKUP_COLOR and !$non_default)
822 $this->_addMarkup(new Markup_color);
823 if (ENABLE_MARKUP_TEMPLATE and !$non_default)
824 $this->_addMarkup(new Markup_template_plugin);
827 function _addMarkup ($markup) {
828 if (isa($markup, 'SimpleMarkup'))
829 $regexp = $markup->getMatchRegexp();
831 $regexp = $markup->getStartRegexp();
833 assert(!isset($this->_markup[$regexp]));
834 $this->_regexps[] = $regexp;
835 $this->_markup[] = $markup;
838 function parse (&$text, $end_regexps = array('$')) {
839 $regexps = $this->_regexps;
841 // $end_re takes precedence: "favor reduce over shift"
842 array_unshift($regexps, $end_regexps[0]);
843 //array_push($regexps, $end_regexps[0]);
844 $regexps = new RegexpSet($regexps);
847 $output = new XmlContent;
849 $match = $regexps->match($input);
852 if ($match->regexp_ind == 0) {
853 // No start pattern found before end pattern.
855 if (isset($markup) and is_object($markup)
856 and isa($markup,'Markup_plugin'))
858 $current =& $output->_content[count($output->_content)-1];
859 $current->setTightness(true,true);
861 $output->pushContent($match->prematch);
862 $text = $match->postmatch;
866 $markup = $this->_markup[$match->regexp_ind - 1];
867 $body = $this->_parse_markup_body($markup, $match->match,
868 $match->postmatch, $end_regexps);
870 // Couldn't match balanced expression.
871 // Ignore and look for next matching start regexp.
872 $match = $regexps->nextMatch($input, $match);
876 // Matched markup. Eat input, push output.
877 // FIXME: combine adjacent strings.
878 if (isa($markup, 'SimpleMarkup'))
879 $current = $markup->markup($match->match);
881 $current = $markup->markup($match->match, $body);
882 $input = $match->postmatch;
883 if (isset($markup) and is_object($markup)
884 and isa($markup,'Markup_plugin'))
886 $current->setTightness(true,true);
888 $output->pushContent($match->prematch, $current);
890 $match = $regexps->match($input);
893 // No pattern matched, not even the end pattern.
898 function _parse_markup_body ($markup, $match, &$text, $end_regexps) {
899 if (isa($markup, 'SimpleMarkup'))
900 return true; // Done. SimpleMarkup is simple.
902 if (!is_object($markup)) return false; // Some error: Should assert
903 array_unshift($end_regexps, $markup->getEndRegexp($match));
905 // Optimization: if no end pattern in text, we know the
906 // parse will fail. This is an important optimization,
907 // e.g. when text is "*lots *of *start *delims *with
908 // *no *matching *end *delims".
909 $ends_pat = "/(?:" . join(").*(?:", $end_regexps) . ")/xs";
910 if (!preg_match($ends_pat, $text))
912 return $this->parse($text, $end_regexps);
916 class LinkTransformer extends InlineTransformer
918 function LinkTransformer () {
919 $this->InlineTransformer(array('escape', 'bracketlink', 'url',
920 'interwiki', 'wikiword'));
924 class NowikiTransformer extends InlineTransformer
926 function NowikiTransformer () {
927 $this->InlineTransformer
929 'html_emphasis', 'html_abbr', 'plugin',
930 'isonumchars', 'isohexchars', /*'html_entities',*/
935 function TransformInline($text, $markup = 2.0, $basepage=false) {
939 $trfm = new InlineTransformer;
943 $text = ConvertOldMarkup($text, 'inline');
947 return new CacheableMarkup($trfm->parse($text), $basepage);
949 return $trfm->parse($text);
952 function TransformLinks($text, $markup = 2.0, $basepage = false) {
956 $trfm = new LinkTransformer;
960 $text = ConvertOldMarkup($text, 'links');
964 return new CacheableMarkup($trfm->parse($text), $basepage);
966 return $trfm->parse($text);
970 * Transform only html markup and entities.
972 function TransformInlineNowiki($text, $markup = 2.0, $basepage=false) {
976 $trfm = new NowikiTransformer;
979 return new CacheableMarkup($trfm->parse($text), $basepage);
981 return $trfm->parse($text);
985 // $Log: not supported by cvs2svn $
986 // Revision 1.78 2006/09/03 09:53:52 rurban
987 // more colors, case-insensitive color names
989 // Revision 1.77 2006/08/25 19:02:02 rurban
990 // patch #1348996 by Robert Litwiniec: fix show image semantics if label is given
992 // Revision 1.76 2006/08/19 11:02:35 rurban
993 // add strike and del to html emphasis: Patch #1542894 by Kai Krakow
995 // Revision 1.75 2006/08/15 13:43:10 rurban
996 // add Markup_xml_plugin (untested) and fix Markup_template_plugin
998 // Revision 1.74 2006/07/23 14:03:18 rurban
999 // add new feature: DISABLE_MARKUP_WIKIWORD
1001 // Revision 1.73 2006/04/15 12:20:36 rurban
1002 // fix relatives links patch by Joel Schaubert for [/
1004 // Revision 1.72 2006/03/07 20:43:29 rurban
1005 // relative external link, if no internal subpage. by joel Schaubert
1007 // Revision 1.71 2005/11/14 22:31:12 rurban
1008 // add SemanticWeb support
1010 // Revision 1.70 2005/10/31 16:45:23 rurban
1011 // added cfg-able markups only for default TextTransformation, not for links and others
1013 // Revision 1.69 2005/09/14 05:57:19 rurban
1014 // make ENABLE_MARKUP_TEMPLATE optional
1016 // Revision 1.68 2005/09/10 21:24:32 rurban
1017 // optionally support {{Template|vars}} syntax
1019 // Revision 1.67 2005/06/06 17:41:20 rurban
1020 // support new ENABLE_MARKUP_COLOR
1022 // Revision 1.66 2005/04/23 11:15:49 rurban
1023 // handle allowed inlined objects within INLINE_IMAGES
1025 // Revision 1.65 2005/03/27 18:24:17 rurban
1029 // (c-file-style: "gnu")
1033 // c-basic-offset: 4
1034 // c-hanging-comment-ender-p: nil
1035 // indent-tabs-mode: nil