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 = false)
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 = false)
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 = false)
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 (isa($elem, 'XmlElement') and $elem->getTag() == 'p') {
498 $this->setContent($elem->getContent());
508 function _match(&$input, $match)
510 trigger_error('pure virtual', E_USER_ERROR);
513 function _setTightness($top, $bot)
517 function merge($followingBlock)
524 return $this->_element;
528 class Block_blockquote extends BlockMarkup
531 public $_re = '\ +(?=\S)';
533 function _match(&$input, $m)
535 $this->_depth = strlen($m->match);
536 $indent = sprintf("\\ {%d}", $this->_depth);
537 $this->_element = new SubBlock($input, $indent, $m->match,
542 function merge($nextBlock)
544 if (get_class($nextBlock) == get_class($this)) {
545 assert($nextBlock->_depth < $this->_depth);
546 $nextBlock->_element->unshiftContent($this->_element);
547 if (!empty($this->_tight_top))
548 $nextBlock->_tight_top = $this->_tight_top;
555 class Block_list extends BlockMarkup
557 //public $_tag = 'ol' or 'ul';
558 public $_re = '\ {0,4}
563 | [*]\ (?!(?=\S)[^*]*(?<=\S)[*](?:\\s|[-)}>"\'\\/:.,;!?_*=]) )
565 public $_content = array();
567 function _match(&$input, $m)
569 // A list as the first content in a list is not allowed.
572 // Should markup as <ul><li>* Item</li></ul>,
573 // not <ul><li><ul><li>Item</li></ul>/li></ul>.
575 if (preg_match('/[*#+-o]/', $input->getPrefix())) {
580 $indent = sprintf("\\ {%d}", strlen($prefix));
582 $bullet = trim($m->match);
583 $this->_tag = $bullet == '#' ? 'ol' : 'ul';
584 $this->_content[] = new TightSubBlock($input, $indent, $m->match, 'li');
588 function _setTightness($top, $bot)
590 $li = &$this->_content[0];
591 $li->setTightness($top, $bot);
594 function merge($nextBlock)
596 if (isa($nextBlock, 'Block_list') and $this->_tag == $nextBlock->_tag) {
597 array_splice($this->_content, count($this->_content), 0,
598 $nextBlock->_content);
606 return new Block_HtmlElement($this->_tag, false, $this->_content);
610 class Block_dl extends Block_list
614 function __construct()
616 $this->_re = '\ {0,4}\S.*(?<!' . ESCAPE_CHAR . '):\s*$';
619 function _match(&$input, $m)
621 if (!($p = $this->_do_match($input, $m)))
623 list ($term, $defn, $loose) = $p;
625 $this->_content[] = new Block_HtmlElement('dt', false, $term);
626 $this->_content[] = $defn;
627 $this->_tight_defn = !$loose;
631 function _setTightness($top, $bot)
633 $dt = &$this->_content[0];
634 $dd = &$this->_content[1];
636 $dt->setTightness($top, $this->_tight_defn);
637 $dd->setTightness($this->_tight_defn, $bot);
640 function _do_match(&$input, $m)
642 $pos = $input->getPos();
644 $firstIndent = strspn($m->match, ' ');
645 $pat = sprintf('/\ {%d,%d}(?=\s*\S)/A', $firstIndent + 1, $firstIndent + 5);
648 $loose = $input->skipSpace();
649 $line = $input->currentLine();
651 if (!$line || !preg_match($pat, $line, $mm)) {
652 $input->setPos($pos);
653 return false; // No body found.
656 $indent = strlen($mm[0]);
657 $term = TransformInline(rtrim(substr(trim($m->match), 0, -1)));
658 $defn = new TightSubBlock($input, sprintf("\\ {%d}", $indent), false, 'dd');
659 return array($term, $defn, $loose);
663 class Block_table_dl_defn extends XmlContent
668 function __construct($term, $defn)
671 if (!is_array($defn))
672 $defn = $defn->getContent();
674 $this->_next_tight_top = false; // value irrelevant - gets fixed later
675 $this->_ncols = $this->_ComputeNcols($defn);
678 foreach ($defn as $item) {
679 if ($this->_IsASubtable($item))
680 $this->_addSubtable($item);
682 $this->_addToRow($item);
686 $th = HTML::th($term);
687 if ($this->_nrows > 1)
688 $th->setAttr('rowspan', $this->_nrows);
689 $this->_setTerm($th);
692 function setTightness($tight_top, $tight_bot)
694 $this->_tight_top = $tight_top;
695 $this->_tight_bot = $tight_bot;
698 function _addToRow($item)
700 if (empty($this->_accum)) {
701 $this->_accum = HTML::td();
702 if ($this->_ncols > 2)
703 $this->_accum->setAttr('colspan', $this->_ncols - 1);
705 $this->_accum->pushContent($item);
708 function _flushRow($tight_bottom = false)
710 if (!empty($this->_accum)) {
711 $row = new Block_HtmlElement('tr', false, $this->_accum);
713 $row->setTightness($this->_next_tight_top, $tight_bottom);
714 $this->_next_tight_top = $tight_bottom;
716 $this->pushContent($row);
717 $this->_accum = false;
722 function _addSubtable($table)
724 if (!($table_rows = $table->getContent()))
727 $this->_flushRow($table_rows[0]->_tight_top);
729 foreach ($table_rows as $subdef) {
730 $this->pushContent($subdef);
731 $this->_nrows += $subdef->nrows();
732 $this->_next_tight_top = $subdef->_tight_bot;
736 function _setTerm($th)
738 $first_row = &$this->_content[0];
739 if (isa($first_row, 'Block_table_dl_defn'))
740 $first_row->_setTerm($th);
742 $first_row->unshiftContent($th);
745 function _ComputeNcols($defn)
748 foreach ($defn as $item) {
749 if ($this->_IsASubtable($item)) {
750 $row = $this->_FirstDefn($item);
751 $ncols = max($ncols, $row->ncols() + 1);
757 function _IsASubtable($item)
759 return isa($item, 'HtmlElement')
760 && $item->getTag() == 'table'
761 && $item->getAttr('class') == 'wiki-dl-table';
764 function _FirstDefn($subtable)
766 $defs = $subtable->getContent();
772 return $this->_ncols;
777 return $this->_nrows;
782 $first = &$this->_content[0];
783 if (isa($first, 'Block_table_dl_defn'))
784 return $first->firstTR();
790 $last = &$this->_content[$this->_nrows - 1];
791 if (isa($last, 'Block_table_dl_defn'))
792 return $last->lastTR();
796 function setWidth($ncols)
798 assert($ncols >= $this->_ncols);
799 if ($ncols <= $this->_ncols)
801 $rows = &$this->_content;
802 for ($i = 0; $i < count($rows); $i++) {
804 if (isa($row, 'Block_table_dl_defn'))
805 $row->setWidth($ncols - 1);
807 $n = count($row->_content);
808 $lastcol = &$row->_content[$n - 1];
809 if (!empty($lastcol))
810 $lastcol->setAttr('colspan', $ncols - 1);
816 class Block_table_dl extends Block_dl
818 public $_tag = 'dl-table'; // phony.
820 function __construct()
822 $this->_re = '\ {0,4} (?:\S.*)? (?<!' . ESCAPE_CHAR . ') \| \s* $';
825 function _match(&$input, $m)
827 if (!($p = $this->_do_match($input, $m)))
829 list ($term, $defn, $loose) = $p;
831 $this->_content[] = new Block_table_dl_defn($term, $defn);
835 function _setTightness($top, $bot)
837 $this->_content[0]->setTightness($top, $bot);
842 $defs = &$this->_content;
845 foreach ($defs as $defn)
846 $ncols = max($ncols, $defn->ncols());
848 foreach ($defs as $key => $defn)
849 $defs[$key]->setWidth($ncols);
851 return HTML::table(array('class' => 'wiki-dl-table'), $defs);
855 class Block_oldlists extends Block_list
857 //public $_tag = 'ol', 'ul', or 'dl';
858 public $_re = '(?: [*]\ (?!(?=\S)[^*]*(?<=\S)[*](?:\\s|[-)}>"\'\\/:.,;!?_*=]))
859 | [#]\ (?! \[ .*? \] )
863 function _match(&$input, $m)
866 if (!preg_match('/[*#;]*$/A', $input->getPrefix())) {
871 $oldindent = '[*#;](?=[#*]|;.*:.*\S)';
872 $newindent = sprintf('\\ {%d}', strlen($prefix));
873 $indent = "(?:$oldindent|$newindent)";
875 $bullet = $prefix[0];
876 if ($bullet == '*') {
879 } elseif ($bullet == '#') {
884 list ($term,) = explode(':', substr($prefix, 1), 2);
887 $this->_content[] = new Block_HtmlElement('dt', false,
888 TransformInline($term));
892 $this->_content[] = new TightSubBlock($input, $indent, $m->match, $itemtag);
896 function _setTightness($top, $bot)
898 if (count($this->_content) == 1) {
899 $li = &$this->_content[0];
900 $li->setTightness($top, $bot);
902 $dt = &$this->_content[0];
903 $dd = &$this->_content[1];
904 $dt->setTightness($top, false);
905 $dd->setTightness(false, $bot);
910 class Block_pre extends BlockMarkup
912 public $_re = '<(?:pre|verbatim|nowiki|noinclude)>';
914 function _match(&$input, $m)
916 $endtag = '</' . substr($m->match, 1);
918 $pos = $input->getPos();
920 $line = $m->postmatch;
921 while (ltrim($line) != $endtag) {
923 if (($line = $input->nextLine()) === false) {
924 $input->setPos($pos);
930 if ($m->match == '<nowiki>')
931 $text = join("<br>\n", $text);
933 $text = join("\n", $text);
935 // FIXME: no <img>, <big>, <small>, <sup>, or <sub>'s allowed
937 if ($m->match == '<pre>') {
938 $text = TransformInline($text);
940 if ($m->match == '<noinclude>') {
941 $text = TransformText($text);
942 $this->_element = new Block_HtmlElement('div', false, $text);
943 } elseif ($m->match == '<nowiki>') {
944 $text = TransformInlineNowiki($text);
945 $this->_element = new Block_HtmlElement('p', false, $text);
947 $this->_element = new Block_HtmlElement('pre', false, $text);
953 // Wikicreole placeholder
955 class Block_placeholder extends BlockMarkup
959 function _match(&$input, $m)
963 $pos = $input->getPos();
965 $line = $m->postmatch;
966 while (ltrim($line) != $endtag) {
968 if (($line = $input->nextLine()) === false) {
969 $input->setPos($pos);
975 $text = join("\n", $text);
976 $text = '<<<' . $text . '>>>';
977 $this->_element = new Block_HtmlElement('div', false, $text);
982 class Block_nowiki_wikicreole extends BlockMarkup
986 function _match(&$input, $m)
990 $pos = $input->getPos();
992 $line = $m->postmatch;
993 while (ltrim($line) != $endtag) {
995 if (($line = $input->nextLine()) === false) {
996 $input->setPos($pos);
1002 $text = join("\n", $text);
1003 $this->_element = new Block_HtmlElement('pre', false, $text);
1008 class Block_plugin extends Block_pre
1010 public $_re = '<\?plugin(?:-form)?(?!\S)';
1013 /* <?plugin Backlinks
1015 /* <?plugin ListPages pages=<!plugin-list Backlinks!>
1016 * exclude=<!plugin-list TitleSearch s=T*!> ?>
1020 function _match(&$input, $m)
1022 $pos = $input->getPos();
1023 $pi = $m->match . $m->postmatch;
1024 while (!preg_match('/(?<!' . ESCAPE_CHAR . ')\?>\s*$/', $pi)) {
1025 if (($line = $input->nextLine()) === false) {
1026 $input->setPos($pos);
1033 $this->_element = new Cached_PluginInvocation($pi);
1038 class Block_plugin_wikicreole extends Block_pre
1040 // public $_re = '<<(?!\S)';
1043 function _match(&$input, $m)
1045 $pos = $input->getPos();
1046 $pi = $m->postmatch;
1047 if ($pi[0] == '<') {
1050 $pi = "<?plugin " . $pi;
1051 while (!preg_match('/(?<!' . ESCAPE_CHAR . ')>>\s*$/', $pi)) {
1052 if (($line = $input->nextLine()) === false) {
1053 $input->setPos($pos);
1060 $pi = str_replace(">>", "?>", $pi);
1062 $this->_element = new Cached_PluginInvocation($pi);
1067 class Block_table_wikicreole extends Block_pre
1069 public $_re = '\s*\|';
1071 function _match(&$input, $m)
1073 $pos = $input->getPos();
1074 $pi = "|" . $m->postmatch;
1078 if ((($line = $input->nextLine()) === false) && !$intable) {
1079 $input->setPos($pos);
1086 $trimline = trim($line);
1087 if ($trimline[0] != "|") {
1091 $pi .= "\n$trimline";
1094 $pi = '<' . '?plugin WikicreoleTable ' . $pi . '?' . '>';
1096 $this->_element = new Cached_PluginInvocation($pi);
1102 * Table syntax similar to Mediawiki
1104 * => <?plugin MediawikiTable
1108 class Block_table_mediawiki extends Block_pre
1110 public $_re = '{\|';
1112 function _match(&$input, $m)
1114 $pos = $input->getPos();
1115 $pi = $m->postmatch;
1116 while (!preg_match('/(?<!' . ESCAPE_CHAR . ')\|}\s*$/', $pi)) {
1117 if (($line = $input->nextLine()) === false) {
1118 $input->setPos($pos);
1125 $pi = str_replace("\|}", "", $pi);
1126 $pi = '<' . '?plugin MediawikiTable ' . $pi . '?' . '>';
1127 $this->_element = new Cached_PluginInvocation($pi);
1133 * Template syntax similar to Mediawiki
1135 * => < ? plugin Template page=template ? >
1136 * {{template|var1=value1|var2=value|...}}
1137 * => < ? plugin Template page=template var=value ... ? >
1139 * The {{...}} syntax is also used for:
1140 * - Wikicreole images
1143 class Block_template_plugin extends Block_pre
1147 function _match(&$input, $m)
1149 // If we find "}}", this is an inline template.
1150 if (strpos($m->postmatch, "}}") !== false) {
1153 $pos = $input->getPos();
1154 $pi = $m->postmatch;
1155 if ($pi[0] == '{') {
1158 while (!preg_match('/(?<!' . ESCAPE_CHAR . ')}}\s*$/', $pi)) {
1159 if (($line = $input->nextLine()) === false) {
1160 $input->setPos($pos);
1168 $pi = trim($pi, "}}");
1170 if (strpos($pi, "|") === false) {
1174 $imagename = substr($pi, 0, strpos($pi, "|"));
1175 $alt = ltrim(strstr($pi, "|"), "|");
1178 // It's not a Mediawiki template, it's a Wikicreole image
1179 if (is_image($imagename)) {
1180 $this->_element = LinkImage(getUploadDataPath() . $imagename, $alt);
1185 if (is_video($imagename)) {
1186 $pi = '<' . '?plugin Video file="' . $pi . '" ?>';
1187 $this->_element = new Cached_PluginInvocation($pi);
1191 $pi = str_replace("\n", "", $pi);
1193 // The argument value might contain a double quote (")
1194 // We have to encode that.
1195 $pi = htmlspecialchars($pi);
1199 if (preg_match('/^(\S+?)\|(.*)$/', $pi, $_m)) {
1201 $vars = '"' . preg_replace('/\|/', '" "', $_m[2]) . '"';
1202 $vars = preg_replace('/"(\S+)=([^"]*)"/', '\\1="\\2"', $vars);
1205 // pi may contain a version number
1206 // {{foo?version=5}}
1207 // in that case, output is "page=foo rev=5"
1208 if (strstr($pi, "?")) {
1209 $pi = str_replace("?version=", "\" rev=\"", $pi);
1213 $pi = '<' . '?plugin Template page="' . $pi . '" ' . $vars . ' ?>';
1215 $pi = '<' . '?plugin Template page="' . $pi . '" ?>';
1216 $this->_element = new Cached_PluginInvocation($pi);
1221 class Block_email_blockquote extends BlockMarkup
1223 public $_attr = array('class' => 'mail-style-quote');
1224 public $_re = '>\ ?';
1226 function _match(&$input, $m)
1228 //$indent = str_replace(' ', '\\ ', $m->match) . '|>$';
1229 $indent = $this->_re;
1230 $this->_element = new SubBlock($input, $indent, $m->match,
1231 'blockquote', $this->_attr);
1236 class Block_wikicreole_indented extends BlockMarkup
1238 public $_attr = array('style' => 'margin-left:2em');
1239 public $_re = ':\ ?';
1241 function _match(&$input, $m)
1243 $indent = $this->_re;
1244 $this->_element = new SubBlock($input, $indent, $m->match,
1245 'div', $this->_attr);
1250 class Block_hr extends BlockMarkup
1252 public $_re = '-{4,}\s*$';
1254 function _match(&$input, $m)
1257 $this->_element = new Block_HtmlElement('hr');
1262 class Block_heading extends BlockMarkup
1264 public $_re = '!{1,3}';
1266 function _match(&$input, $m)
1268 $tag = "h" . (5 - strlen($m->match));
1269 $text = TransformInline(trim($m->postmatch));
1272 $this->_element = new Block_HtmlElement($tag, false, $text);
1278 class Block_heading_wikicreole extends BlockMarkup
1280 public $_re = '={2,6}';
1282 function _match(&$input, $m)
1284 $tag = "h" . strlen($m->match);
1286 $header = trim($m->postmatch);
1287 // Remove '='s at the end so that Mediawiki syntax is recognized
1288 $header = trim($header, "=");
1289 $text = TransformInline(trim($header));
1292 $this->_element = new Block_HtmlElement($tag, false, $text);
1298 class Block_p extends BlockMarkup
1301 public $_re = '\S.*';
1304 function _match(&$input, $m)
1306 $this->_text = $m->match;
1311 function _setTightness($top, $bot)
1313 $this->_tight_top = $top;
1314 $this->_tight_bot = $bot;
1317 function merge($nextBlock)
1319 $class = get_class($nextBlock);
1320 if (strtolower($class) == 'block_p' and $this->_tight_bot) {
1321 $this->_text .= "\n" . $nextBlock->_text;
1322 $this->_tight_bot = $nextBlock->_tight_bot;
1330 $content = TransformInline(trim($this->_text));
1331 $p = new Block_HtmlElement('p', false, $content);
1332 $p->setTightness($this->_tight_top, $this->_tight_bot);
1337 class Block_divspan extends BlockMarkup
1339 public $_re = '<(?im)(?: div|span)(?:[^>]*)?>';
1341 function _match(&$input, $m)
1343 if (substr($m->match, 1, 4) == 'span') {
1349 $argstr = substr(trim(substr($m->match, strlen($tag) + 1)), 0, -1);
1350 $pos = $input->getPos();
1351 $pi = $content = $m->postmatch;
1352 while (!preg_match('/^(.*)\<\/' . $tag . '\>(.*)$/i', $pi, $me)) {
1353 if ($pi != $content)
1354 $content .= "\n$pi";
1355 if (($pi = $input->nextLine()) === false) {
1356 $input->setPos($pos);
1360 if ($pi != $content)
1361 $content .= $me[1]; // prematch
1365 if (strstr($content, "\n"))
1366 $content = TransformText($content);
1368 $content = TransformInline($content);
1373 while (preg_match("/(\w+)=(.+)/", $argstr, $m)) {
1376 if (preg_match("/^\"(.+?)\"(.*)$/", $v, $m)) {
1380 preg_match("/^(\s+)(.*)$/", $v, $m);
1384 if (trim($k) and trim($v)) $args[$k] = $v;
1387 $this->_element = new Block_HtmlElement($tag, $args, $content);
1392 ////////////////////////////////////////////////////////////////
1396 * Transform the text of a page, and return a parse tree.
1398 function TransformTextPre($text, $basepage = false)
1400 if (isa($text, 'WikiDB_PageRevision')) {
1402 $text = $rev->getPackedContent();
1404 // Expand leading tabs.
1405 $text = expand_tabs($text);
1406 $output = new WikiText($text);
1412 * Transform the text of a page, and return an XmlContent,
1413 * suitable for printXml()-ing.
1415 function TransformText($text, $basepage = false)
1417 $output = TransformTextPre($text, $basepage);
1419 // This is for immediate consumption.
1420 // We must bind the contents to a base pagename so that
1421 // relative page links can be properly linkified...
1422 return new CacheableMarkup($output->getContent(), $basepage);
1424 return new XmlContent($output->getContent());
1430 // c-basic-offset: 4
1431 // c-hanging-comment-ender-p: nil
1432 // indent-tabs-mode: nil