2 /* Copyright (C) 2002 Geoffrey T. Dairiki <dairiki@dairiki.org>
3 * Copyright (C) 2004,2005 Reini Urban
4 * Copyright (C) 2008-2010 Marc-Etienne Vargenau, Alcatel-Lucent
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 along
19 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 //require_once('lib/HtmlElement.php');
23 require_once 'lib/CachedMarkup.php';
24 require_once 'lib/InlineParser.php';
27 * Deal with paragraphs and proper, recursive block indents
28 * for the new style markup (version 2)
30 * Everything which goes over more than line:
31 * automatic lists, UL, OL, DL, table, blockquote, verbatim,
37 * FIXME: unify this with the RegexpSet in InlineParser.
39 * FIXME: This is very php5 sensitive: It was fixed for 1.3.9,
40 * but is again broken with the 1.3.11
41 * allow_call_time_pass_reference clean fixes
44 * @author: Geoffrey T. Dairiki
48 * Return type from RegexpSet::match and RegexpSet::nextMatch.
52 class AnchoredRegexpSet_match
60 * The text following the matched text.
65 * Index of the regular expression which matched.
71 * A set of regular expressions.
73 * This class is probably only useful for InlineTransformer.
75 class AnchoredRegexpSet
78 * @param $regexps array A list of regular expressions. The
79 * regular expressions should not include any sub-pattern groups
80 * "(...)". (Anonymous groups, like "(?:...)", as well as
81 * look-ahead and look-behind assertions are fine.)
83 function __construct($regexps)
85 $this->_regexps = $regexps;
86 $this->_re = "/((" . join(")|(", $regexps) . "))/Ax";
90 * Search text for the next matching regexp from the Regexp Set.
92 * @param $text string The text to search.
94 * @return object A RegexpSet_match object, or false if no match.
98 if (!is_string($text)) return false;
99 if (!preg_match($this->_re, $text, $m)) {
103 $match = new AnchoredRegexpSet_match;
104 $match->postmatch = substr($text, strlen($m[0]));
105 $match->match = $m[1];
106 $match->regexp_ind = count($m) - 3;
111 * Search for next matching regexp.
113 * Here, 'next' has two meanings:
115 * Match the next regexp(s) in the set, at the same position as the last match.
117 * If that fails, match the whole RegexpSet, starting after the position of the
120 * @param $text string Text to search.
122 * @param $prevMatch A RegexpSet_match object
124 * $prevMatch should be a match object obtained by a previous
125 * match upon the same value of $text.
127 * @return object A RegexpSet_match object, or false if no match.
129 function nextMatch($text, $prevMatch)
131 // Try to find match at same position.
132 $regexps = array_slice($this->_regexps, $prevMatch->regexp_ind + 1);
137 $pat = "/ ( (" . join(')|(', $regexps) . ") ) /Axs";
139 if (!preg_match($pat, $text, $m)) {
143 $match = new AnchoredRegexpSet_match;
144 $match->postmatch = substr($text, strlen($m[0]));
145 $match->match = $m[1];
146 $match->regexp_ind = count($m) - 3 + $prevMatch->regexp_ind + 1;
152 class BlockParser_Input
155 function BlockParser_Input($text)
158 // Expand leading tabs.
159 // FIXME: do this better.
161 // We want to ensure the only characters matching \s are ' ' and "\n".
163 $text = preg_replace('/(?![ \n])\s/', ' ', $text);
164 assert(!preg_match('/(?![ \n])\s/', $text));
166 $this->_lines = preg_split('/[^\S\n]*\n/', $text);
169 // Strip leading blank lines.
170 while ($this->_lines and !$this->_lines[0])
171 array_shift($this->_lines);
172 $this->_atSpace = false;
177 $nlines = count($this->_lines);
179 if ($this->_pos >= $nlines) {
180 $this->_atSpace = false;
183 if ($this->_lines[$this->_pos] != '')
186 $this->_atSpace = true;
188 return $this->_atSpace;
191 function currentLine()
193 if ($this->_pos >= count($this->_lines)) {
196 return $this->_lines[$this->_pos];
201 $this->_atSpace = $this->_lines[$this->_pos++] === '';
202 if ($this->_pos >= count($this->_lines)) {
205 return $this->_lines[$this->_pos];
210 $this->_atSpace = ($this->_lines[$this->_pos] === '');
216 return array($this->_pos, $this->_atSpace);
219 function setPos($pos)
221 list($this->_pos, $this->_atSpace) = $pos;
236 if ($this->_pos < count($this->_lines))
237 return $this->_lines[$this->_pos];
242 function _debug($tab, $msg)
245 $where = $this->where();
246 $tab = str_repeat('____', $this->getDepth()) . $tab;
247 printXML(HTML::div("$tab $msg: at: '",
254 class BlockParser_InputSubBlock extends BlockParser_Input
256 function __construct(&$input, $prefix_re, $initial_prefix = false)
258 $this->_input = &$input;
259 $this->_prefix_pat = "/$prefix_re|\\s*\$/Ax";
260 $this->_atSpace = false;
262 if (($line = $input->currentLine()) === false)
263 $this->_line = false;
264 elseif ($initial_prefix) {
265 assert(substr($line, 0, strlen($initial_prefix)) == $initial_prefix);
266 $this->_line = (string)substr($line, strlen($initial_prefix));
267 $this->_atBlank = !ltrim($line);
268 } elseif (preg_match($this->_prefix_pat, $line, $m)) {
269 $this->_line = (string)substr($line, strlen($m[0]));
270 $this->_atBlank = !ltrim($line);
272 $this->_line = false;
277 // In contrast to the case for top-level blocks,
278 // for sub-blocks, there never appears to be any trailing space.
279 // (The last block in the sub-block should always be of class tight-bottom.)
280 while ($this->_line === '')
283 if ($this->_line === false)
284 return $this->_atSpace == 'strong_space';
286 return $this->_atSpace;
289 function currentLine()
296 if ($this->_line === '')
297 $this->_atSpace = $this->_atBlank ? 'weak_space' : 'strong_space';
299 $this->_atSpace = false;
301 $line = $this->_input->nextLine();
302 if ($line !== false && preg_match($this->_prefix_pat, $line, $m)) {
303 $this->_line = (string)substr($line, strlen($m[0]));
304 $this->_atBlank = !ltrim($line);
306 $this->_line = false;
318 return array($this->_line, $this->_atSpace, $this->_input->getPos());
321 function setPos($pos)
323 $this->_line = $pos[0];
324 $this->_atSpace = $pos[1];
325 $this->_input->setPos($pos[2]);
330 assert($this->_line !== false);
331 $line = $this->_input->currentLine();
332 assert($line !== false && strlen($line) >= strlen($this->_line));
333 return substr($line, 0, strlen($line) - strlen($this->_line));
338 return $this->_input->getDepth() + 1;
343 return $this->_input->where();
347 class Block_HtmlElement extends HtmlElement
349 function Block_HtmlElement($tag /*, ... */)
351 $this->_init(func_get_args());
354 function setTightness($top, $bottom)
359 class ParsedBlock extends Block_HtmlElement
362 function ParsedBlock(&$input, $tag = 'div', $attr = array())
364 $this->Block_HtmlElement($tag, $attr);
365 $this->_initBlockTypes();
366 $this->_parse($input);
369 function _parse(&$input)
371 // php5 failed to advance the block. php5 copies objects by ref.
372 // nextBlock == block, both are the same objects. So we have to clone it.
373 for ($block = $this->_getBlock($input);
375 $block = (is_object($nextBlock) ? clone($nextBlock) : $nextBlock)) {
376 while ($nextBlock = $this->_getBlock($input)) {
377 // Attempt to merge current with following block.
378 if (!($merged = $block->merge($nextBlock))) {
379 break; // can't merge
383 $this->pushContent($block->finish());
387 // FIXME: hackish. This should only be called once.
388 function _initBlockTypes()
390 // better static or global?
391 static $_regexpset, $_block_types;
393 if (!is_object($_regexpset)) {
394 // nowiki_wikicreole must be before template_plugin
396 ('nowiki_wikicreole', 'template_plugin', 'placeholder', 'oldlists', 'list', 'dl',
397 'table_dl', 'table_wikicreole', 'table_mediawiki',
398 'blockquote', 'heading', 'heading_wikicreole', 'hr', 'pre',
399 'email_blockquote', 'wikicreole_indented',
400 'plugin', 'plugin_wikicreole', 'p');
401 // insert it before p!
402 if (ENABLE_MARKUP_DIVSPAN) {
403 array_pop($Block_types);
404 $Block_types[] = 'divspan';
405 $Block_types[] = 'p';
407 foreach ($Block_types as $type) {
408 $class = "Block_$type";
410 $this->_block_types[] = $proto;
411 $this->_regexps[] = $proto->_re;
413 $this->_regexpset = new AnchoredRegexpSet($this->_regexps);
414 $_regexpset = $this->_regexpset;
415 $_block_types = $this->_block_types;
418 $this->_regexpset = $_regexpset;
419 $this->_block_types = $_block_types;
423 function _getBlock(&$input)
425 $this->_atSpace = $input->skipSpace();
427 $line = $input->currentLine();
428 if ($line === false or $line === '') { // allow $line === '0'
431 $tight_top = !$this->_atSpace;
432 $re_set = &$this->_regexpset;
433 //FIXME: php5 fails to advance here!
434 for ($m = $re_set->match($line); $m; $m = $re_set->nextMatch($line, $m)) {
435 $block = clone($this->_block_types[$m->regexp_ind]);
436 if (DEBUG & _DEBUG_PARSER)
437 $input->_debug('>', get_class($block));
439 if ($block->_match($input, $m)) {
440 //$block->_text = $line;
441 if (DEBUG & _DEBUG_PARSER)
442 $input->_debug('<', get_class($block));
443 $tight_bottom = !$input->skipSpace();
444 $block->_setTightness($tight_top, $tight_bottom);
447 if (DEBUG & _DEBUG_PARSER)
448 $input->_debug('[', "_match failed");
450 if ($line === false or $line === '') // allow $line === '0'
453 trigger_error("Couldn't match block: '$line'", E_USER_NOTICE);
458 class WikiText extends ParsedBlock
460 function WikiText($text)
462 $input = new BlockParser_Input($text);
463 $this->ParsedBlock($input);
467 class SubBlock extends ParsedBlock
469 function SubBlock(&$input, $indent_re, $initial_indent = false,
470 $tag = 'div', $attr = array())
472 $subinput = new BlockParser_InputSubBlock($input, $indent_re, $initial_indent);
473 $this->ParsedBlock($subinput, $tag, $attr);
478 * TightSubBlock is for use in parsing lists item bodies.
480 * If the sub-block consists of a single paragraph, it omits
481 * the paragraph element.
483 * We go to this trouble so that "tight" lists look somewhat reasonable
484 * in older (non-CSS) browsers. (If you don't do this, then, without
485 * CSS, you only get "loose" lists.
487 class TightSubBlock extends SubBlock
489 function __construct(&$input, $indent_re, $initial_indent = false,
490 $tag = 'div', $attr = array())
492 $this->SubBlock($input, $indent_re, $initial_indent, $tag, $attr);
494 // If content is a single paragraph, eliminate the paragraph...
495 if (count($this->_content) == 1) {
496 $elem = $this->_content[0];
497 if (is_a($elem, 'XmlElement') and $elem->getTag() == 'p') {
498 $this->setContent($elem->getContent());
504 abstract class BlockMarkup
508 abstract function _match(&$input, $match);
510 function _setTightness($top, $bot)
514 function merge($followingBlock)
521 return $this->_element;
525 class Block_blockquote extends BlockMarkup
528 public $_re = '\ +(?=\S)';
530 function _match(&$input, $m)
532 $this->_depth = strlen($m->match);
533 $indent = sprintf("\\ {%d}", $this->_depth);
534 $this->_element = new SubBlock($input, $indent, $m->match,
539 function merge($nextBlock)
541 if (get_class($nextBlock) == get_class($this)) {
542 assert($nextBlock->_depth < $this->_depth);
543 $nextBlock->_element->unshiftContent($this->_element);
544 if (!empty($this->_tight_top))
545 $nextBlock->_tight_top = $this->_tight_top;
552 class Block_list extends BlockMarkup
554 //public $_tag = 'ol' or 'ul';
555 public $_re = '\ {0,4}
560 | [*]\ (?!(?=\S)[^*]*(?<=\S)[*](?:\\s|[-)}>"\'\\/:.,;!?_*=]) )
562 public $_content = array();
564 function _match(&$input, $m)
566 // A list as the first content in a list is not allowed.
569 // Should markup as <ul><li>* Item</li></ul>,
570 // not <ul><li><ul><li>Item</li></ul>/li></ul>.
572 if (preg_match('/[*#+-o]/', $input->getPrefix())) {
577 $indent = sprintf("\\ {%d}", strlen($prefix));
579 $bullet = trim($m->match);
580 $this->_tag = $bullet == '#' ? 'ol' : 'ul';
581 $this->_content[] = new TightSubBlock($input, $indent, $m->match, 'li');
585 function _setTightness($top, $bot)
587 $li = &$this->_content[0];
588 $li->setTightness($top, $bot);
591 function merge($nextBlock)
593 if (is_a($nextBlock, 'Block_list') and $this->_tag == $nextBlock->_tag) {
594 array_splice($this->_content, count($this->_content), 0,
595 $nextBlock->_content);
603 return new Block_HtmlElement($this->_tag, false, $this->_content);
607 class Block_dl extends Block_list
611 function __construct()
613 $this->_re = '\ {0,4}\S.*(?<!' . ESCAPE_CHAR . '):\s*$';
616 function _match(&$input, $m)
618 if (!($p = $this->_do_match($input, $m)))
620 list ($term, $defn, $loose) = $p;
622 $this->_content[] = new Block_HtmlElement('dt', false, $term);
623 $this->_content[] = $defn;
624 $this->_tight_defn = !$loose;
628 function _setTightness($top, $bot)
630 $dt = &$this->_content[0];
631 $dd = &$this->_content[1];
633 $dt->setTightness($top, $this->_tight_defn);
634 $dd->setTightness($this->_tight_defn, $bot);
637 function _do_match(&$input, $m)
639 $pos = $input->getPos();
641 $firstIndent = strspn($m->match, ' ');
642 $pat = sprintf('/\ {%d,%d}(?=\s*\S)/A', $firstIndent + 1, $firstIndent + 5);
645 $loose = $input->skipSpace();
646 $line = $input->currentLine();
648 if (!$line || !preg_match($pat, $line, $mm)) {
649 $input->setPos($pos);
650 return false; // No body found.
653 $indent = strlen($mm[0]);
654 $term = TransformInline(rtrim(substr(trim($m->match), 0, -1)));
655 $defn = new TightSubBlock($input, sprintf("\\ {%d}", $indent), false, 'dd');
656 return array($term, $defn, $loose);
660 class Block_table_dl_defn extends XmlContent
665 function __construct($term, $defn)
668 if (!is_array($defn))
669 $defn = $defn->getContent();
671 $this->_next_tight_top = false; // value irrelevant - gets fixed later
672 $this->_ncols = $this->_ComputeNcols($defn);
675 foreach ($defn as $item) {
676 if ($this->_IsASubtable($item))
677 $this->_addSubtable($item);
679 $this->_addToRow($item);
683 $th = HTML::th($term);
684 if ($this->_nrows > 1)
685 $th->setAttr('rowspan', $this->_nrows);
686 $this->_setTerm($th);
689 function setTightness($tight_top, $tight_bot)
691 $this->_tight_top = $tight_top;
692 $this->_tight_bot = $tight_bot;
695 function _addToRow($item)
697 if (empty($this->_accum)) {
698 $this->_accum = HTML::td();
699 if ($this->_ncols > 2)
700 $this->_accum->setAttr('colspan', $this->_ncols - 1);
702 $this->_accum->pushContent($item);
705 function _flushRow($tight_bottom = false)
707 if (!empty($this->_accum)) {
708 $row = new Block_HtmlElement('tr', false, $this->_accum);
710 $row->setTightness($this->_next_tight_top, $tight_bottom);
711 $this->_next_tight_top = $tight_bottom;
713 $this->pushContent($row);
714 $this->_accum = false;
719 function _addSubtable($table)
721 if (!($table_rows = $table->getContent()))
724 $this->_flushRow($table_rows[0]->_tight_top);
726 foreach ($table_rows as $subdef) {
727 $this->pushContent($subdef);
728 $this->_nrows += $subdef->nrows();
729 $this->_next_tight_top = $subdef->_tight_bot;
733 function _setTerm($th)
735 $first_row = &$this->_content[0];
736 if (is_a($first_row, 'Block_table_dl_defn'))
737 $first_row->_setTerm($th);
739 $first_row->unshiftContent($th);
742 function _ComputeNcols($defn)
745 foreach ($defn as $item) {
746 if ($this->_IsASubtable($item)) {
747 $row = $this->_FirstDefn($item);
748 $ncols = max($ncols, $row->ncols() + 1);
754 function _IsASubtable($item)
756 return is_a($item, 'HtmlElement')
757 && $item->getTag() == 'table'
758 && $item->getAttr('class') == 'wiki-dl-table';
761 function _FirstDefn($subtable)
763 $defs = $subtable->getContent();
769 return $this->_ncols;
774 return $this->_nrows;
779 $first = &$this->_content[0];
780 if (is_a($first, 'Block_table_dl_defn'))
781 return $first->firstTR();
787 $last = &$this->_content[$this->_nrows - 1];
788 if (is_a($last, 'Block_table_dl_defn'))
789 return $last->lastTR();
793 function setWidth($ncols)
795 assert($ncols >= $this->_ncols);
796 if ($ncols <= $this->_ncols)
798 $rows = &$this->_content;
799 for ($i = 0; $i < count($rows); $i++) {
801 if (is_a($row, 'Block_table_dl_defn'))
802 $row->setWidth($ncols - 1);
804 $n = count($row->_content);
805 $lastcol = &$row->_content[$n - 1];
806 if (!empty($lastcol))
807 $lastcol->setAttr('colspan', $ncols - 1);
813 class Block_table_dl extends Block_dl
815 public $_tag = 'dl-table'; // phony.
817 function __construct()
819 $this->_re = '\ {0,4} (?:\S.*)? (?<!' . ESCAPE_CHAR . ') \| \s* $';
822 function _match(&$input, $m)
824 if (!($p = $this->_do_match($input, $m)))
826 list ($term, $defn, $loose) = $p;
828 $this->_content[] = new Block_table_dl_defn($term, $defn);
832 function _setTightness($top, $bot)
834 $this->_content[0]->setTightness($top, $bot);
839 $defs = &$this->_content;
842 foreach ($defs as $defn)
843 $ncols = max($ncols, $defn->ncols());
845 foreach ($defs as $key => $defn)
846 $defs[$key]->setWidth($ncols);
848 return HTML::table(array('class' => 'wiki-dl-table'), $defs);
852 class Block_oldlists extends Block_list
854 //public $_tag = 'ol', 'ul', or 'dl';
855 public $_re = '(?: [*]\ (?!(?=\S)[^*]*(?<=\S)[*](?:\\s|[-)}>"\'\\/:.,;!?_*=]))
856 | [#]\ (?! \[ .*? \] )
860 function _match(&$input, $m)
863 if (!preg_match('/[*#;]*$/A', $input->getPrefix())) {
868 $oldindent = '[*#;](?=[#*]|;.*:.*\S)';
869 $newindent = sprintf('\\ {%d}', strlen($prefix));
870 $indent = "(?:$oldindent|$newindent)";
872 $bullet = $prefix[0];
873 if ($bullet == '*') {
876 } elseif ($bullet == '#') {
881 list ($term,) = explode(':', substr($prefix, 1), 2);
884 $this->_content[] = new Block_HtmlElement('dt', false,
885 TransformInline($term));
889 $this->_content[] = new TightSubBlock($input, $indent, $m->match, $itemtag);
893 function _setTightness($top, $bot)
895 if (count($this->_content) == 1) {
896 $li = &$this->_content[0];
897 $li->setTightness($top, $bot);
899 $dt = &$this->_content[0];
900 $dd = &$this->_content[1];
901 $dt->setTightness($top, false);
902 $dd->setTightness(false, $bot);
907 class Block_pre extends BlockMarkup
909 public $_re = '<(?:pre|verbatim|nowiki|noinclude|includeonly)>';
911 function _match(&$input, $m)
913 $endtag = '</' . substr($m->match, 1);
915 $pos = $input->getPos();
917 $line = $m->postmatch;
918 while (ltrim($line) != $endtag) {
920 if (($line = $input->nextLine()) === false) {
921 $input->setPos($pos);
927 if ($m->match == '<includeonly>') {
928 $this->_element = new Block_HtmlElement('div', false, '');
932 if ($m->match == '<nowiki>')
933 $text = join("<br>\n", $text);
935 $text = join("\n", $text);
937 if ($m->match == '<noinclude>') {
938 $text = TransformText($text);
939 $this->_element = new Block_HtmlElement('div', false, $text);
940 } elseif ($m->match == '<nowiki>') {
941 $text = TransformInlineNowiki($text);
942 $this->_element = new Block_HtmlElement('p', false, $text);
944 $this->_element = new Block_HtmlElement('pre', false, $text);
950 // Wikicreole placeholder
952 class Block_placeholder extends BlockMarkup
956 function _match(&$input, $m)
960 $pos = $input->getPos();
962 $line = $m->postmatch;
963 while (ltrim($line) != $endtag) {
965 if (($line = $input->nextLine()) === false) {
966 $input->setPos($pos);
972 $text = join("\n", $text);
973 $text = '<<<' . $text . '>>>';
974 $this->_element = new Block_HtmlElement('div', false, $text);
979 class Block_nowiki_wikicreole extends BlockMarkup
983 function _match(&$input, $m)
987 $pos = $input->getPos();
989 $line = $m->postmatch;
990 while (ltrim($line) != $endtag) {
992 if (($line = $input->nextLine()) === false) {
993 $input->setPos($pos);
999 $text = join("\n", $text);
1000 $this->_element = new Block_HtmlElement('pre', false, $text);
1005 class Block_plugin extends Block_pre
1007 public $_re = '<\?plugin(?:-form)?(?!\S)';
1010 /* <?plugin Backlinks
1012 /* <?plugin ListPages pages=<!plugin-list Backlinks!>
1013 * exclude=<!plugin-list TitleSearch s=T*!> ?>
1017 function _match(&$input, $m)
1019 $pos = $input->getPos();
1020 $pi = $m->match . $m->postmatch;
1021 while (!preg_match('/(?<!' . ESCAPE_CHAR . ')\?>\s*$/', $pi)) {
1022 if (($line = $input->nextLine()) === false) {
1023 $input->setPos($pos);
1030 $this->_element = new Cached_PluginInvocation($pi);
1035 class Block_plugin_wikicreole extends Block_pre
1037 // public $_re = '<<(?!\S)';
1040 function _match(&$input, $m)
1042 $pos = $input->getPos();
1043 $pi = $m->postmatch;
1044 if ($pi[0] == '<') {
1047 $pi = "<?plugin " . $pi;
1048 while (!preg_match('/(?<!' . ESCAPE_CHAR . ')>>\s*$/', $pi)) {
1049 if (($line = $input->nextLine()) === false) {
1050 $input->setPos($pos);
1057 $pi = str_replace(">>", "?>", $pi);
1059 $this->_element = new Cached_PluginInvocation($pi);
1064 class Block_table_wikicreole extends Block_pre
1066 public $_re = '\s*\|';
1068 function _match(&$input, $m)
1070 $pos = $input->getPos();
1071 $pi = "|" . $m->postmatch;
1075 if ((($line = $input->nextLine()) === false) && !$intable) {
1076 $input->setPos($pos);
1083 $trimline = trim($line);
1084 if ($trimline[0] != "|") {
1088 $pi .= "\n$trimline";
1091 $pi = '<' . '?plugin WikicreoleTable ' . $pi . '?' . '>';
1093 $this->_element = new Cached_PluginInvocation($pi);
1099 * Table syntax similar to Mediawiki
1101 * => <?plugin MediawikiTable
1105 class Block_table_mediawiki extends Block_pre
1107 public $_re = '{\|';
1109 function _match(&$input, $m)
1111 $pos = $input->getPos();
1112 $pi = $m->postmatch;
1113 while (!preg_match('/(?<!' . ESCAPE_CHAR . ')\|}\s*$/', $pi)) {
1114 if (($line = $input->nextLine()) === false) {
1115 $input->setPos($pos);
1122 $pi = str_replace("\|}", "", $pi);
1123 $pi = '<' . '?plugin MediawikiTable ' . $pi . '?' . '>';
1124 $this->_element = new Cached_PluginInvocation($pi);
1130 * Template syntax similar to Mediawiki
1132 * => < ? plugin Template page=template ? >
1133 * {{template|var1=value1|var2=value|...}}
1134 * => < ? plugin Template page=template var=value ... ? >
1136 * The {{...}} syntax is also used for:
1137 * - Wikicreole images
1140 class Block_template_plugin extends Block_pre
1144 function _match(&$input, $m)
1146 // If we find "}}", this is an inline template.
1147 if (strpos($m->postmatch, "}}") !== false) {
1150 $pos = $input->getPos();
1151 $pi = $m->postmatch;
1152 if ($pi[0] == '{') {
1155 while (!preg_match('/(?<!' . ESCAPE_CHAR . ')}}\s*$/', $pi)) {
1156 if (($line = $input->nextLine()) === false) {
1157 $input->setPos($pos);
1165 $pi = trim($pi, "}}");
1167 if (strpos($pi, "|") === false) {
1171 $imagename = substr($pi, 0, strpos($pi, "|"));
1172 $alt = ltrim(strstr($pi, "|"), "|");
1175 // It's not a Mediawiki template, it's a Wikicreole image
1176 if (is_image($imagename)) {
1177 $this->_element = LinkImage(getUploadDataPath() . $imagename, $alt);
1182 if (is_video($imagename)) {
1183 $pi = '<' . '?plugin Video file="' . $pi . '" ?>';
1184 $this->_element = new Cached_PluginInvocation($pi);
1188 $pi = str_replace("\n", "", $pi);
1190 // The argument value might contain a double quote (")
1191 // We have to encode that.
1192 $pi = htmlspecialchars($pi);
1196 if (preg_match('/^(\S+?)\|(.*)$/', $pi, $_m)) {
1198 $vars = '"' . preg_replace('/\|/', '" "', $_m[2]) . '"';
1199 $vars = preg_replace('/"(\S+)=([^"]*)"/', '\\1="\\2"', $vars);
1202 // pi may contain a version number
1203 // {{foo?version=5}}
1204 // in that case, output is "page=foo rev=5"
1205 if (strstr($pi, "?")) {
1206 $pi = str_replace("?version=", "\" rev=\"", $pi);
1210 $pi = '<' . '?plugin Template page="' . $pi . '" ' . $vars . ' ?>';
1212 $pi = '<' . '?plugin Template page="' . $pi . '" ?>';
1213 $this->_element = new Cached_PluginInvocation($pi);
1218 class Block_email_blockquote extends BlockMarkup
1220 public $_attr = array('class' => 'mail-style-quote');
1221 public $_re = '>\ ?';
1223 function _match(&$input, $m)
1225 //$indent = str_replace(' ', '\\ ', $m->match) . '|>$';
1226 $indent = $this->_re;
1227 $this->_element = new SubBlock($input, $indent, $m->match, 'blockquote', $this->_attr);
1232 class Block_wikicreole_indented extends BlockMarkup
1234 public $_attr = array('style' => 'margin-left:2em');
1235 public $_re = ':\ ?';
1237 function _match(&$input, $m)
1239 $indent = $this->_re;
1240 $this->_element = new SubBlock($input, $indent, $m->match,
1241 'div', $this->_attr);
1246 class Block_hr extends BlockMarkup
1248 public $_re = '-{4,}\s*$';
1250 function _match(&$input, $m)
1253 $this->_element = new Block_HtmlElement('hr');
1258 class Block_heading extends BlockMarkup
1260 public $_re = '!{1,3}';
1262 function _match(&$input, $m)
1264 $tag = "h" . (5 - strlen($m->match));
1265 $text = TransformInline(trim($m->postmatch));
1268 $this->_element = new Block_HtmlElement($tag, false, $text);
1274 class Block_heading_wikicreole extends BlockMarkup
1276 public $_re = '={2,6}';
1278 function _match(&$input, $m)
1280 $tag = "h" . strlen($m->match);
1282 $header = trim($m->postmatch);
1283 // Remove '='s at the end so that Mediawiki syntax is recognized
1284 $header = trim($header, "=");
1285 $text = TransformInline(trim($header));
1288 $this->_element = new Block_HtmlElement($tag, false, $text);
1294 class Block_p extends BlockMarkup
1297 public $_re = '\S.*';
1300 function _match(&$input, $m)
1302 $this->_text = $m->match;
1307 function _setTightness($top, $bot)
1309 $this->_tight_top = $top;
1310 $this->_tight_bot = $bot;
1313 function merge($nextBlock)
1315 $class = get_class($nextBlock);
1316 if (strtolower($class) == 'block_p' and $this->_tight_bot) {
1317 $this->_text .= "\n" . $nextBlock->_text;
1318 $this->_tight_bot = $nextBlock->_tight_bot;
1326 $content = TransformInline(trim($this->_text));
1327 $p = new Block_HtmlElement('p', false, $content);
1328 $p->setTightness($this->_tight_top, $this->_tight_bot);
1333 class Block_divspan extends BlockMarkup
1335 public $_re = '<(?im)(?: div|span)(?:[^>]*)?>';
1337 function _match(&$input, $m)
1339 if (substr($m->match, 1, 4) == 'span') {
1345 $argstr = substr(trim(substr($m->match, strlen($tag) + 1)), 0, -1);
1346 $pos = $input->getPos();
1347 $pi = $content = $m->postmatch;
1348 while (!preg_match('/^(.*)\<\/' . $tag . '\>(.*)$/i', $pi, $me)) {
1349 if ($pi != $content)
1350 $content .= "\n$pi";
1351 if (($pi = $input->nextLine()) === false) {
1352 $input->setPos($pos);
1356 if ($pi != $content)
1357 $content .= $me[1]; // prematch
1361 if (strstr($content, "\n"))
1362 $content = TransformText($content);
1364 $content = TransformInline($content);
1369 while (preg_match("/(\w+)=(.+)/", $argstr, $m)) {
1372 if (preg_match("/^\"(.+?)\"(.*)$/", $v, $m)) {
1376 preg_match("/^(\s+)(.*)$/", $v, $m);
1380 if (trim($k) and trim($v)) $args[$k] = $v;
1383 $this->_element = new Block_HtmlElement($tag, $args, $content);
1388 ////////////////////////////////////////////////////////////////
1392 * Transform the text of a page, and return a parse tree.
1394 function TransformTextPre($text, $basepage = false)
1396 if (is_a($text, 'WikiDB_PageRevision')) {
1398 $text = $rev->getPackedContent();
1400 // Expand leading tabs.
1401 $text = expand_tabs($text);
1402 $output = new WikiText($text);
1408 * Transform the text of a page, and return an XmlContent,
1409 * suitable for printXml()-ing.
1411 function TransformText($text, $basepage = false)
1413 $output = TransformTextPre($text, $basepage);
1415 // This is for immediate consumption.
1416 // We must bind the contents to a base pagename so that
1417 // relative page links can be properly linkified...
1418 return new CacheableMarkup($output->getContent(), $basepage);
1420 return new XmlContent($output->getContent());
1426 // c-basic-offset: 4
1427 // c-hanging-comment-ender-p: nil
1428 // indent-tabs-mode: nil