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 {
59 * The text following the matched text.
64 * Index of the regular expression which matched.
70 * A set of regular expressions.
72 * This class is probably only useful for InlineTransformer.
74 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 AnchoredRegexpSet ($regexps) {
84 $this->_regexps = $regexps;
85 $this->_re = "/((" . join(")|(", $regexps) . "))/Ax";
89 * Search text for the next matching regexp from the Regexp Set.
91 * @param $text string The text to search.
93 * @return object A RegexpSet_match object, or false if no match.
95 function match ($text) {
96 if (!is_string($text)) return false;
97 if (! preg_match($this->_re, $text, $m)) {
101 $match = new AnchoredRegexpSet_match;
102 $match->postmatch = substr($text, strlen($m[0]));
103 $match->match = $m[1];
104 $match->regexp_ind = count($m) - 3;
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 $text string Text to search.
120 * @param $prevMatch A RegexpSet_match object
122 * $prevMatch should be a match object obtained by a previous
123 * match upon the same value of $text.
125 * @return object A RegexpSet_match object, or false if no match.
127 function nextMatch ($text, $prevMatch) {
128 // Try to find match at same position.
129 $regexps = array_slice($this->_regexps, $prevMatch->regexp_ind + 1);
134 $pat= "/ ( (" . join(')|(', $regexps) . ") ) /Axs";
136 if (! preg_match($pat, $text, $m)) {
140 $match = new AnchoredRegexpSet_match;
141 $match->postmatch = substr($text, strlen($m[0]));
142 $match->match = $m[1];
143 $match->regexp_ind = count($m) - 3 + $prevMatch->regexp_ind + 1;;
148 class BlockParser_Input {
150 function BlockParser_Input ($text) {
152 // Expand leading tabs.
153 // FIXME: do this better.
155 // We want to ensure the only characters matching \s are ' ' and "\n".
157 $text = preg_replace('/(?![ \n])\s/', ' ', $text);
158 assert(!preg_match('/(?![ \n])\s/', $text));
160 $this->_lines = preg_split('/[^\S\n]*\n/', $text);
163 // Strip leading blank lines.
164 while ($this->_lines and ! $this->_lines[0])
165 array_shift($this->_lines);
166 $this->_atSpace = false;
169 function skipSpace () {
170 $nlines = count($this->_lines);
172 if ($this->_pos >= $nlines) {
173 $this->_atSpace = false;
176 if ($this->_lines[$this->_pos] != '')
179 $this->_atSpace = true;
181 return $this->_atSpace;
184 function currentLine () {
185 if ($this->_pos >= count($this->_lines)) {
188 return $this->_lines[$this->_pos];
191 function nextLine () {
192 $this->_atSpace = $this->_lines[$this->_pos++] === '';
193 if ($this->_pos >= count($this->_lines)) {
196 return $this->_lines[$this->_pos];
199 function advance () {
200 $this->_atSpace = ($this->_lines[$this->_pos] === '');
205 return array($this->_pos, $this->_atSpace);
208 function setPos ($pos) {
209 list($this->_pos, $this->_atSpace) = $pos;
212 function getPrefix () {
216 function getDepth () {
221 if ($this->_pos < count($this->_lines))
222 return $this->_lines[$this->_pos];
227 function _debug ($tab, $msg) {
229 $where = $this->where();
230 $tab = str_repeat('____', $this->getDepth() ) . $tab;
231 printXML(HTML::div("$tab $msg: at: '",
238 class BlockParser_InputSubBlock extends BlockParser_Input
240 function BlockParser_InputSubBlock (&$input, $prefix_re, $initial_prefix = false) {
241 $this->_input = &$input;
242 $this->_prefix_pat = "/$prefix_re|\\s*\$/Ax";
243 $this->_atSpace = false;
245 if (($line = $input->currentLine()) === false)
246 $this->_line = false;
247 elseif ($initial_prefix) {
248 assert(substr($line, 0, strlen($initial_prefix)) == $initial_prefix);
249 $this->_line = (string) substr($line, strlen($initial_prefix));
250 $this->_atBlank = ! ltrim($line);
252 elseif (preg_match($this->_prefix_pat, $line, $m)) {
253 $this->_line = (string) substr($line, strlen($m[0]));
254 $this->_atBlank = ! ltrim($line);
257 $this->_line = false;
260 function skipSpace () {
261 // In contrast to the case for top-level blocks,
262 // for sub-blocks, there never appears to be any trailing space.
263 // (The last block in the sub-block should always be of class tight-bottom.)
264 while ($this->_line === '')
267 if ($this->_line === false)
268 return $this->_atSpace == 'strong_space';
270 return $this->_atSpace;
273 function currentLine () {
277 function nextLine () {
278 if ($this->_line === '')
279 $this->_atSpace = $this->_atBlank ? 'weak_space' : 'strong_space';
281 $this->_atSpace = false;
283 $line = $this->_input->nextLine();
284 if ($line !== false && preg_match($this->_prefix_pat, $line, $m)) {
285 $this->_line = (string) substr($line, strlen($m[0]));
286 $this->_atBlank = ! ltrim($line);
289 $this->_line = false;
294 function advance () {
299 return array($this->_line, $this->_atSpace, $this->_input->getPos());
302 function setPos ($pos) {
303 $this->_line = $pos[0];
304 $this->_atSpace = $pos[1];
305 $this->_input->setPos($pos[2]);
308 function getPrefix () {
309 assert ($this->_line !== false);
310 $line = $this->_input->currentLine();
311 assert ($line !== false && strlen($line) >= strlen($this->_line));
312 return substr($line, 0, strlen($line) - strlen($this->_line));
315 function getDepth () {
316 return $this->_input->getDepth() + 1;
320 return $this->_input->where();
324 class Block_HtmlElement extends HtmlElement
326 function Block_HtmlElement($tag /*, ... */) {
327 $this->_init(func_get_args());
330 function setTightness($top, $bottom) {
334 class ParsedBlock extends Block_HtmlElement {
336 function ParsedBlock (&$input, $tag = 'div', $attr = false) {
337 $this->Block_HtmlElement($tag, $attr);
338 $this->_initBlockTypes();
339 $this->_parse($input);
342 function _parse (&$input) {
343 // php5 failed to advance the block. php5 copies objects by ref.
344 // nextBlock == block, both are the same objects. So we have to clone it.
345 for ($block = $this->_getBlock($input);
347 $block = (is_object($nextBlock) ? clone($nextBlock) : $nextBlock))
349 while ($nextBlock = $this->_getBlock($input)) {
350 // Attempt to merge current with following block.
351 if (! ($merged = $block->merge($nextBlock)) ) {
352 break; // can't merge
356 $this->pushContent($block->finish());
360 // FIXME: hackish. This should only be called once.
361 function _initBlockTypes () {
362 // better static or global?
363 static $_regexpset, $_block_types;
365 if (!is_object($_regexpset)) {
366 // nowiki_wikicreole must be before template_plugin
368 ('nowiki_wikicreole', 'template_plugin', 'placeholder', 'oldlists', 'list', 'dl',
369 'table_dl', 'table_wikicreole', 'table_mediawiki',
370 'blockquote', 'heading', 'heading_wikicreole', 'hr', 'pre',
371 'email_blockquote', 'wikicreole_indented',
372 'plugin', 'plugin_wikicreole', 'p');
373 // insert it before p!
374 if (ENABLE_MARKUP_DIVSPAN) {
375 array_pop($Block_types);
376 $Block_types[] = 'divspan';
377 $Block_types[] = 'p';
379 foreach ($Block_types as $type) {
380 $class = "Block_$type";
382 $this->_block_types[] = $proto;
383 $this->_regexps[] = $proto->_re;
385 $this->_regexpset = new AnchoredRegexpSet($this->_regexps);
386 $_regexpset = $this->_regexpset;
387 $_block_types = $this->_block_types;
390 $this->_regexpset = $_regexpset;
391 $this->_block_types = $_block_types;
395 function _getBlock (&$input) {
396 $this->_atSpace = $input->skipSpace();
398 $line = $input->currentLine();
399 if ($line === false or $line === '') { // allow $line === '0'
402 $tight_top = !$this->_atSpace;
403 $re_set = &$this->_regexpset;
404 //FIXME: php5 fails to advance here!
405 for ($m = $re_set->match($line); $m; $m = $re_set->nextMatch($line, $m)) {
406 $block = clone($this->_block_types[$m->regexp_ind]);
407 if (DEBUG & _DEBUG_PARSER)
408 $input->_debug('>', get_class($block));
410 if ($block->_match($input, $m)) {
411 //$block->_text = $line;
412 if (DEBUG & _DEBUG_PARSER)
413 $input->_debug('<', get_class($block));
414 $tight_bottom = ! $input->skipSpace();
415 $block->_setTightness($tight_top, $tight_bottom);
418 if (DEBUG & _DEBUG_PARSER)
419 $input->_debug('[', "_match failed");
421 if ($line === false or $line === '') // allow $line === '0'
424 trigger_error("Couldn't match block: '$line'", E_USER_NOTICE);
429 class WikiText extends ParsedBlock {
430 function WikiText ($text) {
431 $input = new BlockParser_Input($text);
432 $this->ParsedBlock($input);
436 class SubBlock extends ParsedBlock {
437 function SubBlock (&$input, $indent_re, $initial_indent = false,
438 $tag = 'div', $attr = false) {
439 $subinput = new BlockParser_InputSubBlock($input, $indent_re, $initial_indent);
440 $this->ParsedBlock($subinput, $tag, $attr);
445 * TightSubBlock is for use in parsing lists item bodies.
447 * If the sub-block consists of a single paragraph, it omits
448 * the paragraph element.
450 * We go to this trouble so that "tight" lists look somewhat reasonable
451 * in older (non-CSS) browsers. (If you don't do this, then, without
452 * CSS, you only get "loose" lists.
454 class TightSubBlock extends SubBlock {
455 function TightSubBlock (&$input, $indent_re, $initial_indent = false,
456 $tag = 'div', $attr = false) {
457 $this->SubBlock($input, $indent_re, $initial_indent, $tag, $attr);
459 // If content is a single paragraph, eliminate the paragraph...
460 if (count($this->_content) == 1) {
461 $elem = $this->_content[0];
462 if (isa($elem, 'XmlElement') and $elem->getTag() == 'p') {
463 $this->setContent($elem->getContent());
472 function _match (&$input, $match) {
473 trigger_error('pure virtual', E_USER_ERROR);
476 function _setTightness ($top, $bot) {
479 function merge ($followingBlock) {
484 return $this->_element;
488 class Block_blockquote extends BlockMarkup
491 var $_re = '\ +(?=\S)';
493 function _match (&$input, $m) {
494 $this->_depth = strlen($m->match);
495 $indent = sprintf("\\ {%d}", $this->_depth);
496 $this->_element = new SubBlock($input, $indent, $m->match,
501 function merge ($nextBlock) {
502 if (get_class($nextBlock) == get_class($this)) {
503 assert ($nextBlock->_depth < $this->_depth);
504 $nextBlock->_element->unshiftContent($this->_element);
505 if (!empty($this->_tight_top))
506 $nextBlock->_tight_top = $this->_tight_top;
513 class Block_list extends BlockMarkup
515 //var $_tag = 'ol' or 'ul';
521 | [*]\ (?!(?=\S)[^*]*(?<=\S)[*](?:\\s|[-)}>"\'\\/:.,;!?_*=]) )
523 var $_content = array();
525 function _match (&$input, $m) {
526 // A list as the first content in a list is not allowed.
529 // Should markup as <ul><li>* Item</li></ul>,
530 // not <ul><li><ul><li>Item</li></ul>/li></ul>.
532 if (preg_match('/[*#+-o]/', $input->getPrefix())) {
537 $indent = sprintf("\\ {%d}", strlen($prefix));
539 $bullet = trim($m->match);
540 $this->_tag = $bullet == '#' ? 'ol' : 'ul';
541 $this->_content[] = new TightSubBlock($input, $indent, $m->match, 'li');
545 function _setTightness($top, $bot) {
546 $li = &$this->_content[0];
547 $li->setTightness($top, $bot);
550 function merge ($nextBlock) {
551 if (isa($nextBlock, 'Block_list') and $this->_tag == $nextBlock->_tag) {
552 array_splice($this->_content, count($this->_content), 0,
553 $nextBlock->_content);
560 return new Block_HtmlElement($this->_tag, false, $this->_content);
564 class Block_dl extends Block_list
568 function Block_dl () {
569 $this->_re = '\ {0,4}\S.*(?<!'.ESCAPE_CHAR.'):\s*$';
572 function _match (&$input, $m) {
573 if (!($p = $this->_do_match($input, $m)))
575 list ($term, $defn, $loose) = $p;
577 $this->_content[] = new Block_HtmlElement('dt', false, $term);
578 $this->_content[] = $defn;
579 $this->_tight_defn = !$loose;
583 function _setTightness($top, $bot) {
584 $dt = &$this->_content[0];
585 $dd = &$this->_content[1];
587 $dt->setTightness($top, $this->_tight_defn);
588 $dd->setTightness($this->_tight_defn, $bot);
591 function _do_match (&$input, $m) {
592 $pos = $input->getPos();
594 $firstIndent = strspn($m->match, ' ');
595 $pat = sprintf('/\ {%d,%d}(?=\s*\S)/A', $firstIndent + 1, $firstIndent + 5);
598 $loose = $input->skipSpace();
599 $line = $input->currentLine();
601 if (!$line || !preg_match($pat, $line, $mm)) {
602 $input->setPos($pos);
603 return false; // No body found.
606 $indent = strlen($mm[0]);
607 $term = TransformInline(rtrim(substr(trim($m->match),0,-1)));
608 $defn = new TightSubBlock($input, sprintf("\\ {%d}", $indent), false, 'dd');
609 return array($term, $defn, $loose);
613 class Block_table_dl_defn extends XmlContent
618 function Block_table_dl_defn ($term, $defn) {
620 if (!is_array($defn))
621 $defn = $defn->getContent();
623 $this->_next_tight_top = false; // value irrelevant - gets fixed later
624 $this->_ncols = $this->_ComputeNcols($defn);
627 foreach ($defn as $item) {
628 if ($this->_IsASubtable($item))
629 $this->_addSubtable($item);
631 $this->_addToRow($item);
635 $th = HTML::th($term);
636 if ($this->_nrows > 1)
637 $th->setAttr('rowspan', $this->_nrows);
638 $this->_setTerm($th);
641 function setTightness($tight_top, $tight_bot) {
642 $this->_tight_top = $tight_top;
643 $this->_tight_bot = $tight_bot;
646 function _addToRow ($item) {
647 if (empty($this->_accum)) {
648 $this->_accum = HTML::td();
649 if ($this->_ncols > 2)
650 $this->_accum->setAttr('colspan', $this->_ncols - 1);
652 $this->_accum->pushContent($item);
655 function _flushRow ($tight_bottom=false) {
656 if (!empty($this->_accum)) {
657 $row = new Block_HtmlElement('tr', false, $this->_accum);
659 $row->setTightness($this->_next_tight_top, $tight_bottom);
660 $this->_next_tight_top = $tight_bottom;
662 $this->pushContent($row);
663 $this->_accum = false;
668 function _addSubtable ($table) {
669 if (!($table_rows = $table->getContent()))
672 $this->_flushRow($table_rows[0]->_tight_top);
674 foreach ($table_rows as $subdef) {
675 $this->pushContent($subdef);
676 $this->_nrows += $subdef->nrows();
677 $this->_next_tight_top = $subdef->_tight_bot;
681 function _setTerm ($th) {
682 $first_row = &$this->_content[0];
683 if (isa($first_row, 'Block_table_dl_defn'))
684 $first_row->_setTerm($th);
686 $first_row->unshiftContent($th);
689 function _ComputeNcols ($defn) {
691 foreach ($defn as $item) {
692 if ($this->_IsASubtable($item)) {
693 $row = $this->_FirstDefn($item);
694 $ncols = max($ncols, $row->ncols() + 1);
700 function _IsASubtable ($item) {
701 return isa($item, 'HtmlElement')
702 && $item->getTag() == 'table'
703 && $item->getAttr('class') == 'wiki-dl-table';
706 function _FirstDefn ($subtable) {
707 $defs = $subtable->getContent();
712 return $this->_ncols;
716 return $this->_nrows;
719 function & firstTR() {
720 $first = &$this->_content[0];
721 if (isa($first, 'Block_table_dl_defn'))
722 return $first->firstTR();
726 function & lastTR() {
727 $last = &$this->_content[$this->_nrows - 1];
728 if (isa($last, 'Block_table_dl_defn'))
729 return $last->lastTR();
733 function setWidth ($ncols) {
734 assert($ncols >= $this->_ncols);
735 if ($ncols <= $this->_ncols)
737 $rows = &$this->_content;
738 for ($i = 0; $i < count($rows); $i++) {
740 if (isa($row, 'Block_table_dl_defn'))
741 $row->setWidth($ncols - 1);
743 $n = count($row->_content);
744 $lastcol = &$row->_content[$n - 1];
745 if (!empty($lastcol))
746 $lastcol->setAttr('colspan', $ncols - 1);
752 class Block_table_dl extends Block_dl
754 var $_tag = 'dl-table'; // phony.
756 function Block_table_dl() {
757 $this->_re = '\ {0,4} (?:\S.*)? (?<!'.ESCAPE_CHAR.') \| \s* $';
760 function _match (&$input, $m) {
761 if (!($p = $this->_do_match($input, $m)))
763 list ($term, $defn, $loose) = $p;
765 $this->_content[] = new Block_table_dl_defn($term, $defn);
769 function _setTightness($top, $bot) {
770 $this->_content[0]->setTightness($top, $bot);
775 $defs = &$this->_content;
778 foreach ($defs as $defn)
779 $ncols = max($ncols, $defn->ncols());
781 foreach ($defs as $key => $defn)
782 $defs[$key]->setWidth($ncols);
784 return HTML::table(array('class' => 'wiki-dl-table',
792 class Block_oldlists extends Block_list
794 //var $_tag = 'ol', 'ul', or 'dl';
795 var $_re = '(?: [*]\ (?!(?=\S)[^*]*(?<=\S)[*](?:\\s|[-)}>"\'\\/:.,;!?_*=]))
796 | [#]\ (?! \[ .*? \] )
800 function _match (&$input, $m) {
802 if (!preg_match('/[*#;]*$/A', $input->getPrefix())) {
807 $oldindent = '[*#;](?=[#*]|;.*:.*\S)';
808 $newindent = sprintf('\\ {%d}', strlen($prefix));
809 $indent = "(?:$oldindent|$newindent)";
811 $bullet = $prefix[0];
812 if ($bullet == '*') {
816 elseif ($bullet == '#') {
822 list ($term,) = explode(':', substr($prefix, 1), 2);
825 $this->_content[] = new Block_HtmlElement('dt', false,
826 TransformInline($term));
830 $this->_content[] = new TightSubBlock($input, $indent, $m->match, $itemtag);
834 function _setTightness($top, $bot) {
835 if (count($this->_content) == 1) {
836 $li = &$this->_content[0];
837 $li->setTightness($top, $bot);
840 // This is where php5 usually brakes.
841 // wrong duplicated <li> contents
842 if (DEBUG and DEBUG & _DEBUG_PARSER and check_php_version(5)) {
843 if (count($this->_content) != 2) {
846 $class = new Reflection_Class('XmlElement');
847 // Print out basic information
849 "===> The %s%s%s %s '%s' [extends %s]\n".
852 " having the modifiers %d [%s]\n",
853 $class->isInternal() ? 'internal' : 'user-defined',
854 $class->isAbstract() ? ' abstract' : '',
855 $class->isFinal() ? ' final' : '',
856 $class->isInterface() ? 'interface' : 'class',
858 var_export($class->getParentClass(), 1),
859 $class->getFileName(),
860 $class->getStartLine(),
861 $class->getEndline(),
862 $class->getModifiers(),
863 implode(' ', Reflection::getModifierNames($class->getModifiers()))
865 // Print class properties
866 printf("---> Properties: %s\n", var_export($class->getProperties(), 1));
868 echo 'count($this->_content): ', count($this->_content),"\n";
869 echo "\$this->_content[0]: "; var_dump ($this->_content[0]);
871 for ($i=1; $i < min(5, count($this->_content)); $i++) {
872 $c =& $this->_content[$i];
873 echo '$this->_content[',$i,"]: \n";
874 echo "_tag: "; var_dump ($c->_tag);
875 echo "_content: "; var_dump ($c->_content);
876 echo "_properties: "; var_dump ($c->_properties);
878 debug_print_backtrace();
879 if (DEBUG & _DEBUG_APD) {
880 if (function_exists("xdebug_get_function_stack")) {
881 var_dump (xdebug_get_function_stack());
887 if (!check_php_version(5))
888 assert(count($this->_content) == 2);
889 $dt = &$this->_content[0];
890 $dd = &$this->_content[1];
891 $dt->setTightness($top, false);
892 $dd->setTightness(false, $bot);
897 class Block_pre extends BlockMarkup
899 var $_re = '<(?:pre|verbatim|nowiki|noinclude)>';
901 function _match (&$input, $m) {
902 $endtag = '</' . substr($m->match, 1);
904 $pos = $input->getPos();
906 $line = $m->postmatch;
907 while (ltrim($line) != $endtag) {
909 if (($line = $input->nextLine()) === false) {
910 $input->setPos($pos);
916 if ($m->match == '<nowiki>')
917 $text = join("<br>\n", $text);
919 $text = join("\n", $text);
921 // FIXME: no <img>, <big>, <small>, <sup>, or <sub>'s allowed
923 if ($m->match == '<pre>') {
924 $text = TransformInline($text);
926 if ($m->match == '<noinclude>') {
927 $text = TransformText($text);
928 $this->_element = new Block_HtmlElement('div', false, $text);
929 } elseif ($m->match == '<nowiki>') {
930 $text = TransformInlineNowiki($text);
931 $this->_element = new Block_HtmlElement('p', false, $text);
933 $this->_element = new Block_HtmlElement('pre', false, $text);
939 // Wikicreole placeholder
941 class Block_placeholder extends BlockMarkup
945 function _match (&$input, $m) {
948 $pos = $input->getPos();
950 $line = $m->postmatch;
951 while (ltrim($line) != $endtag) {
953 if (($line = $input->nextLine()) === false) {
954 $input->setPos($pos);
960 $text = join("\n", $text);
961 $text = '<<<' . $text . '>>>';
962 $this->_element = new Block_HtmlElement('div', false, $text);
967 class Block_nowiki_wikicreole extends BlockMarkup
971 function _match (&$input, $m) {
974 $pos = $input->getPos();
976 $line = $m->postmatch;
977 while (ltrim($line) != $endtag) {
979 if (($line = $input->nextLine()) === false) {
980 $input->setPos($pos);
986 $text = join("\n", $text);
987 $this->_element = new Block_HtmlElement('pre', false, $text);
992 class Block_plugin extends Block_pre
994 var $_re = '<\?plugin(?:-form)?(?!\S)';
997 /* <?plugin Backlinks
999 /* <?plugin ListPages pages=<!plugin-list Backlinks!>
1000 * exclude=<!plugin-list TitleSearch s=T*!> ?>
1004 function _match (&$input, $m) {
1005 $pos = $input->getPos();
1006 $pi = $m->match . $m->postmatch;
1007 while (!preg_match('/(?<!'.ESCAPE_CHAR.')\?>\s*$/', $pi)) {
1008 if (($line = $input->nextLine()) === false) {
1009 $input->setPos($pos);
1016 $this->_element = new Cached_PluginInvocation($pi);
1021 class Block_plugin_wikicreole extends Block_pre
1023 // var $_re = '<<(?!\S)';
1026 function _match (&$input, $m) {
1027 $pos = $input->getPos();
1028 $pi = $m->postmatch;
1029 if ($pi[0] == '<') {
1032 $pi = "<?plugin " . $pi;
1033 while (!preg_match('/(?<!'.ESCAPE_CHAR.')>>\s*$/', $pi)) {
1034 if (($line = $input->nextLine()) === false) {
1035 $input->setPos($pos);
1042 $pi = str_replace(">>", "?>", $pi);
1044 $this->_element = new Cached_PluginInvocation($pi);
1049 class Block_table_wikicreole extends Block_pre
1053 function _match (&$input, $m) {
1054 $pos = $input->getPos();
1055 $pi = "|" . $m->postmatch;
1059 if ((($line = $input->nextLine()) === false) && !$intable) {
1060 $input->setPos($pos);
1067 $trimline = trim($line);
1068 if ($trimline[0] != "|") {
1072 $pi .= "\n$trimline";
1075 $pi = '<'.'?plugin WikicreoleTable ' . $pi . '?'.'>';
1077 $this->_element = new Cached_PluginInvocation($pi);
1083 * Table syntax similar to Mediawiki
1085 * => <?plugin MediawikiTable
1089 class Block_table_mediawiki extends Block_pre
1093 function _match (&$input, $m) {
1094 $pos = $input->getPos();
1095 $pi = $m->postmatch;
1096 while (!preg_match('/(?<!'.ESCAPE_CHAR.')\|}\s*$/', $pi)) {
1097 if (($line = $input->nextLine()) === false) {
1098 $input->setPos($pos);
1105 $pi = str_replace("\|}", "", $pi);
1106 $pi = '<'.'?plugin MediawikiTable ' . $pi . '?'.'>';
1107 $this->_element = new Cached_PluginInvocation($pi);
1113 * Template syntax similar to Mediawiki
1115 * => < ? plugin Template page=template ? >
1116 * {{template|var1=value1|var2=value|...}}
1117 * => < ? plugin Template page=template var=value ... ? >
1119 * The {{...}} syntax is also used for:
1120 * - Wikicreole images
1123 class Block_template_plugin extends Block_pre
1127 function _match (&$input, $m) {
1128 // If we find "}}", this is an inline template.
1129 if (strpos($m->postmatch, "}}") !== false) {
1132 $pos = $input->getPos();
1133 $pi = $m->postmatch;
1134 if ($pi[0] == '{') {
1137 while (!preg_match('/(?<!'.ESCAPE_CHAR.')}}\s*$/', $pi)) {
1138 if (($line = $input->nextLine()) === false) {
1139 $input->setPos($pos);
1147 $pi = trim($pi, "}}");
1149 if (strpos($pi, "|") === false) {
1153 $imagename = substr($pi, 0, strpos($pi, "|"));
1154 $alt = ltrim(strstr($pi, "|"), "|");
1157 // It's not a Mediawiki template, it's a Wikicreole image
1158 if (is_image($imagename)) {
1159 $this->_element = LinkImage(getUploadDataPath() . $imagename, $alt);
1164 if (is_video($imagename)) {
1165 $pi = '<'.'?plugin Video file="' . $pi . '" ?>';
1166 $this->_element = new Cached_PluginInvocation($pi);
1170 $pi = str_replace("\n", "", $pi);
1172 // The argument value might contain a double quote (")
1173 // We have to encode that.
1174 $pi = htmlspecialchars($pi);
1178 if (preg_match('/^(\S+?)\|(.*)$/', $pi, $_m)) {
1180 $vars = '"' . preg_replace('/\|/', '" "', $_m[2]) . '"';
1181 $vars = preg_replace('/"(\S+)=([^"]*)"/', '\\1="\\2"', $vars);
1184 // pi may contain a version number
1185 // {{foo?version=5}}
1186 // in that case, output is "page=foo rev=5"
1187 if (strstr($pi, "?")) {
1188 $pi = str_replace("?version=", "\" rev=\"", $pi);
1192 $pi = '<'.'?plugin Template page="'.$pi.'" '.$vars . ' ?>';
1194 $pi = '<'.'?plugin Template page="' . $pi . '" ?>';
1195 $this->_element = new Cached_PluginInvocation($pi);
1200 class Block_email_blockquote extends BlockMarkup
1202 var $_attr = array('class' => 'mail-style-quote');
1205 function _match (&$input, $m) {
1206 //$indent = str_replace(' ', '\\ ', $m->match) . '|>$';
1207 $indent = $this->_re;
1208 $this->_element = new SubBlock($input, $indent, $m->match,
1209 'blockquote', $this->_attr);
1214 class Block_wikicreole_indented extends BlockMarkup
1216 var $_attr = array('style' => 'margin-left:2em');
1219 function _match (&$input, $m) {
1220 $indent = $this->_re;
1221 $this->_element = new SubBlock($input, $indent, $m->match,
1222 'div', $this->_attr);
1227 class Block_hr extends BlockMarkup
1229 var $_re = '-{4,}\s*$';
1231 function _match (&$input, $m) {
1233 $this->_element = new Block_HtmlElement('hr');
1238 class Block_heading extends BlockMarkup
1240 var $_re = '!{1,3}';
1242 function _match (&$input, $m) {
1243 $tag = "h" . (5 - strlen($m->match));
1244 $text = TransformInline(trim($m->postmatch));
1247 $this->_element = new Block_HtmlElement($tag, false, $text);
1253 class Block_heading_wikicreole extends BlockMarkup
1255 var $_re = '={2,6}';
1257 function _match (&$input, $m) {
1258 $tag = "h" . strlen($m->match);
1260 $header = trim($m->postmatch);
1261 // Remove '='s at the end so that Mediawiki syntax is recognized
1262 $header = trim($header, "=");
1263 $text = TransformInline(trim($header));
1266 $this->_element = new Block_HtmlElement($tag, false, $text);
1272 class Block_p extends BlockMarkup
1278 function _match (&$input, $m) {
1279 $this->_text = $m->match;
1284 function _setTightness ($top, $bot) {
1285 $this->_tight_top = $top;
1286 $this->_tight_bot = $bot;
1289 function merge ($nextBlock) {
1290 $class = get_class($nextBlock);
1291 if (strtolower($class) == 'block_p' and $this->_tight_bot) {
1292 $this->_text .= "\n" . $nextBlock->_text;
1293 $this->_tight_bot = $nextBlock->_tight_bot;
1299 function finish () {
1300 $content = TransformInline(trim($this->_text));
1301 $p = new Block_HtmlElement('p', false, $content);
1302 $p->setTightness($this->_tight_top, $this->_tight_bot);
1307 class Block_divspan extends BlockMarkup
1309 var $_re = '<(?im)(?: div|span)(?:[^>]*)?>';
1311 function _match (&$input, $m) {
1312 if (substr($m->match,1,4) == 'span') {
1318 $argstr = substr(trim(substr($m->match,strlen($tag)+1)),0,-1);
1319 $pos = $input->getPos();
1320 $pi = $content = $m->postmatch;
1321 while (!preg_match('/^(.*)\<\/'.$tag.'\>(.*)$/i', $pi, $me)) {
1322 if ($pi != $content)
1323 $content .= "\n$pi";
1324 if (($pi = $input->nextLine()) === false) {
1325 $input->setPos($pos);
1329 if ($pi != $content)
1330 $content .= $me[1]; // prematch
1334 if (strstr($content, "\n"))
1335 $content = TransformText($content);
1337 $content = TransformInline($content);
1342 while (preg_match("/(\w+)=(.+)/", $argstr, $m)) {
1343 $k = $m[1]; $v = $m[2];
1344 if (preg_match("/^\"(.+?)\"(.*)$/", $v, $m)) {
1348 preg_match("/^(\s+)(.*)$/", $v, $m);
1352 if (trim($k) and trim($v)) $args[$k] = $v;
1355 $this->_element = new Block_HtmlElement($tag, $args, $content);
1361 ////////////////////////////////////////////////////////////////
1365 * Transform the text of a page, and return a parse tree.
1367 function TransformTextPre ($text, $markup = 2.0, $basepage=false) {
1368 if (isa($text, 'WikiDB_PageRevision')) {
1370 $text = $rev->getPackedContent();
1371 $markup = $rev->get('markup');
1373 // NEW: default markup is new, to increase stability
1374 if (!empty($markup) && $markup < 2.0) {
1375 $text = ConvertOldMarkup($text);
1377 // Expand leading tabs.
1378 $text = expand_tabs($text);
1379 $output = new WikiText($text);
1385 * Transform the text of a page, and return an XmlContent,
1386 * suitable for printXml()-ing.
1388 function TransformText ($text, $markup = 2.0, $basepage = false) {
1389 $output = TransformTextPre($text, $markup, $basepage);
1391 // This is for immediate consumption.
1392 // We must bind the contents to a base pagename so that
1393 // relative page links can be properly linkified...
1394 return new CacheableMarkup($output->getContent(), $basepage);
1396 return new XmlContent($output->getContent());
1402 // c-basic-offset: 4
1403 // c-hanging-comment-ender-p: nil
1404 // indent-tabs-mode: nil