2 /* Copyright (C) 2002 Geoffrey T. Dairiki <dairiki@dairiki.org>
3 * Copyright (C) 2004,2005 Reini Urban
4 * Copyright (C) 2008-2009 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
19 * along with PhpWiki; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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;;
150 class BlockParser_Input {
152 function BlockParser_Input ($text) {
154 // Expand leading tabs.
155 // FIXME: do this better.
157 // We want to ensure the only characters matching \s are ' ' and "\n".
159 $text = preg_replace('/(?![ \n])\s/', ' ', $text);
160 assert(!preg_match('/(?![ \n])\s/', $text));
162 $this->_lines = preg_split('/[^\S\n]*\n/', $text);
165 // Strip leading blank lines.
166 while ($this->_lines and ! $this->_lines[0])
167 array_shift($this->_lines);
168 $this->_atSpace = false;
171 function skipSpace () {
172 $nlines = count($this->_lines);
174 if ($this->_pos >= $nlines) {
175 $this->_atSpace = false;
178 if ($this->_lines[$this->_pos] != '')
181 $this->_atSpace = true;
183 return $this->_atSpace;
186 function currentLine () {
187 if ($this->_pos >= count($this->_lines)) {
190 return $this->_lines[$this->_pos];
193 function nextLine () {
194 $this->_atSpace = $this->_lines[$this->_pos++] === '';
195 if ($this->_pos >= count($this->_lines)) {
198 return $this->_lines[$this->_pos];
201 function advance () {
202 $this->_atSpace = ($this->_lines[$this->_pos] === '');
207 return array($this->_pos, $this->_atSpace);
210 function setPos ($pos) {
211 list($this->_pos, $this->_atSpace) = $pos;
214 function getPrefix () {
218 function getDepth () {
223 if ($this->_pos < count($this->_lines))
224 return $this->_lines[$this->_pos];
229 function _debug ($tab, $msg) {
231 $where = $this->where();
232 $tab = str_repeat('____', $this->getDepth() ) . $tab;
233 printXML(HTML::div("$tab $msg: at: '",
240 class BlockParser_InputSubBlock extends BlockParser_Input
242 function BlockParser_InputSubBlock (&$input, $prefix_re, $initial_prefix = false) {
243 $this->_input = &$input;
244 $this->_prefix_pat = "/$prefix_re|\\s*\$/Ax";
245 $this->_atSpace = false;
247 if (($line = $input->currentLine()) === false)
248 $this->_line = false;
249 elseif ($initial_prefix) {
250 assert(substr($line, 0, strlen($initial_prefix)) == $initial_prefix);
251 $this->_line = (string) substr($line, strlen($initial_prefix));
252 $this->_atBlank = ! ltrim($line);
254 elseif (preg_match($this->_prefix_pat, $line, $m)) {
255 $this->_line = (string) substr($line, strlen($m[0]));
256 $this->_atBlank = ! ltrim($line);
259 $this->_line = false;
262 function skipSpace () {
263 // In contrast to the case for top-level blocks,
264 // for sub-blocks, there never appears to be any trailing space.
265 // (The last block in the sub-block should always be of class tight-bottom.)
266 while ($this->_line === '')
269 if ($this->_line === false)
270 return $this->_atSpace == 'strong_space';
272 return $this->_atSpace;
275 function currentLine () {
279 function nextLine () {
280 if ($this->_line === '')
281 $this->_atSpace = $this->_atBlank ? 'weak_space' : 'strong_space';
283 $this->_atSpace = false;
285 $line = $this->_input->nextLine();
286 if ($line !== false && preg_match($this->_prefix_pat, $line, $m)) {
287 $this->_line = (string) substr($line, strlen($m[0]));
288 $this->_atBlank = ! ltrim($line);
291 $this->_line = false;
296 function advance () {
301 return array($this->_line, $this->_atSpace, $this->_input->getPos());
304 function setPos ($pos) {
305 $this->_line = $pos[0];
306 $this->_atSpace = $pos[1];
307 $this->_input->setPos($pos[2]);
310 function getPrefix () {
311 assert ($this->_line !== false);
312 $line = $this->_input->currentLine();
313 assert ($line !== false && strlen($line) >= strlen($this->_line));
314 return substr($line, 0, strlen($line) - strlen($this->_line));
317 function getDepth () {
318 return $this->_input->getDepth() + 1;
322 return $this->_input->where();
327 class Block_HtmlElement extends HtmlElement
329 function Block_HtmlElement($tag /*, ... */) {
330 $this->_init(func_get_args());
333 function setTightness($top, $bottom) {
337 class ParsedBlock extends Block_HtmlElement {
339 function ParsedBlock (&$input, $tag = 'div', $attr = false) {
340 $this->Block_HtmlElement($tag, $attr);
341 $this->_initBlockTypes();
342 $this->_parse($input);
345 function _parse (&$input) {
346 // php5 failed to advance the block. php5 copies objects by ref.
347 // nextBlock == block, both are the same objects. So we have to clone it.
348 for ($block = $this->_getBlock($input);
350 $block = (is_object($nextBlock) ? clone($nextBlock) : $nextBlock))
352 while ($nextBlock = $this->_getBlock($input)) {
353 // Attempt to merge current with following block.
354 if (! ($merged = $block->merge($nextBlock)) ) {
355 break; // can't merge
359 $this->pushContent($block->finish());
363 // FIXME: hackish. This should only be called once.
364 function _initBlockTypes () {
365 // better static or global?
366 static $_regexpset, $_block_types;
368 if (!is_object($_regexpset)) {
369 // nowiki_wikicreole must be before template_plugin
371 ('nowiki_wikicreole', 'template_plugin', 'placeholder', 'oldlists', 'list', 'dl',
372 'table_dl', 'table_wikicreole', 'table_mediawiki',
373 'blockquote', 'heading', 'heading_wikicreole', 'hr', 'pre', 'email_blockquote',
374 'plugin', 'plugin_wikicreole', 'p');
375 // insert it before p!
376 if (ENABLE_MARKUP_DIVSPAN) {
377 array_pop($Block_types);
378 $Block_types[] = 'divspan';
379 $Block_types[] = 'p';
381 foreach ($Block_types as $type) {
382 $class = "Block_$type";
384 $this->_block_types[] = $proto;
385 $this->_regexps[] = $proto->_re;
387 $this->_regexpset = new AnchoredRegexpSet($this->_regexps);
388 $_regexpset = $this->_regexpset;
389 $_block_types = $this->_block_types;
392 $this->_regexpset = $_regexpset;
393 $this->_block_types = $_block_types;
397 function _getBlock (&$input) {
398 $this->_atSpace = $input->skipSpace();
400 $line = $input->currentLine();
401 if ($line === false or $line === '') { // allow $line === '0'
404 $tight_top = !$this->_atSpace;
405 $re_set = &$this->_regexpset;
406 //FIXME: php5 fails to advance here!
407 for ($m = $re_set->match($line); $m; $m = $re_set->nextMatch($line, $m)) {
408 $block = clone($this->_block_types[$m->regexp_ind]);
409 if (DEBUG & _DEBUG_PARSER)
410 $input->_debug('>', get_class($block));
412 if ($block->_match($input, $m)) {
413 //$block->_text = $line;
414 if (DEBUG & _DEBUG_PARSER)
415 $input->_debug('<', get_class($block));
416 $tight_bottom = ! $input->skipSpace();
417 $block->_setTightness($tight_top, $tight_bottom);
420 if (DEBUG & _DEBUG_PARSER)
421 $input->_debug('[', "_match failed");
423 if ($line === false or $line === '') // allow $line === '0'
426 trigger_error("Couldn't match block: '$line'", E_USER_NOTICE);
431 class WikiText extends ParsedBlock {
432 function WikiText ($text) {
433 $input = new BlockParser_Input($text);
434 $this->ParsedBlock($input);
438 class SubBlock extends ParsedBlock {
439 function SubBlock (&$input, $indent_re, $initial_indent = false,
440 $tag = 'div', $attr = false) {
441 $subinput = new BlockParser_InputSubBlock($input, $indent_re, $initial_indent);
442 $this->ParsedBlock($subinput, $tag, $attr);
447 * TightSubBlock is for use in parsing lists item bodies.
449 * If the sub-block consists of a single paragraph, it omits
450 * the paragraph element.
452 * We go to this trouble so that "tight" lists look somewhat reasonable
453 * in older (non-CSS) browsers. (If you don't do this, then, without
454 * CSS, you only get "loose" lists.
456 class TightSubBlock extends SubBlock {
457 function TightSubBlock (&$input, $indent_re, $initial_indent = false,
458 $tag = 'div', $attr = false) {
459 $this->SubBlock($input, $indent_re, $initial_indent, $tag, $attr);
461 // If content is a single paragraph, eliminate the paragraph...
462 if (count($this->_content) == 1) {
463 $elem = $this->_content[0];
464 if (isa($elem, 'XmlElement') and $elem->getTag() == 'p') {
465 $this->setContent($elem->getContent());
474 function _match (&$input, $match) {
475 trigger_error('pure virtual', E_USER_ERROR);
478 function _setTightness ($top, $bot) {
479 // $this->_element->setTightness($top, $bot);
482 function merge ($followingBlock) {
487 return $this->_element;
491 class Block_blockquote extends BlockMarkup
494 var $_re = '\ +(?=\S)';
496 function _match (&$input, $m) {
497 $this->_depth = strlen($m->match);
498 $indent = sprintf("\\ {%d}", $this->_depth);
499 $this->_element = new SubBlock($input, $indent, $m->match,
504 function merge ($nextBlock) {
505 if (get_class($nextBlock) == get_class($this)) {
506 assert ($nextBlock->_depth < $this->_depth);
507 $nextBlock->_element->unshiftContent($this->_element);
508 if (!empty($this->_tight_top))
509 $nextBlock->_tight_top = $this->_tight_top;
516 class Block_list extends BlockMarkup
518 //var $_tag = 'ol' or 'ul';
524 | [*]\ (?!(?=\S)[^*]*(?<=\S)[*](?:\\s|[-)}>"\'\\/:.,;!?_*=]) )
526 var $_content = array();
528 function _match (&$input, $m) {
529 // A list as the first content in a list is not allowed.
532 // Should markup as <ul><li>* Item</li></ul>,
533 // not <ul><li><ul><li>Item</li></ul>/li></ul>.
535 if (preg_match('/[*#+-o]/', $input->getPrefix())) {
540 $indent = sprintf("\\ {%d}", strlen($prefix));
542 $bullet = trim($m->match);
543 $this->_tag = $bullet == '#' ? 'ol' : 'ul';
544 $this->_content[] = new TightSubBlock($input, $indent, $m->match, 'li');
548 function _setTightness($top, $bot) {
549 $li = &$this->_content[0];
550 $li->setTightness($top, $bot);
553 function merge ($nextBlock) {
554 if (isa($nextBlock, 'Block_list') and $this->_tag == $nextBlock->_tag) {
555 if ($nextBlock->_content === $this->_content) {
556 trigger_error("Internal Error: no block advance", E_USER_NOTICE);
559 array_splice($this->_content, count($this->_content), 0,
560 $nextBlock->_content);
567 return new Block_HtmlElement($this->_tag, false, $this->_content);
571 class Block_dl extends Block_list
575 function Block_dl () {
576 $this->_re = '\ {0,4}\S.*(?<!'.ESCAPE_CHAR.'):\s*$';
579 function _match (&$input, $m) {
580 if (!($p = $this->_do_match($input, $m)))
582 list ($term, $defn, $loose) = $p;
584 $this->_content[] = new Block_HtmlElement('dt', false, $term);
585 $this->_content[] = $defn;
586 $this->_tight_defn = !$loose;
590 function _setTightness($top, $bot) {
591 $dt = &$this->_content[0];
592 $dd = &$this->_content[1];
594 $dt->setTightness($top, $this->_tight_defn);
595 $dd->setTightness($this->_tight_defn, $bot);
598 function _do_match (&$input, $m) {
599 $pos = $input->getPos();
601 $firstIndent = strspn($m->match, ' ');
602 $pat = sprintf('/\ {%d,%d}(?=\s*\S)/A', $firstIndent + 1, $firstIndent + 5);
605 $loose = $input->skipSpace();
606 $line = $input->currentLine();
608 if (!$line || !preg_match($pat, $line, $mm)) {
609 $input->setPos($pos);
610 return false; // No body found.
613 $indent = strlen($mm[0]);
614 $term = TransformInline(rtrim(substr(trim($m->match),0,-1)));
615 $defn = new TightSubBlock($input, sprintf("\\ {%d}", $indent), false, 'dd');
616 return array($term, $defn, $loose);
622 class Block_table_dl_defn extends XmlContent
627 function Block_table_dl_defn ($term, $defn) {
629 if (!is_array($defn))
630 $defn = $defn->getContent();
632 $this->_next_tight_top = false; // value irrelevant - gets fixed later
633 $this->_ncols = $this->_ComputeNcols($defn);
636 foreach ($defn as $item) {
637 if ($this->_IsASubtable($item))
638 $this->_addSubtable($item);
640 $this->_addToRow($item);
644 $th = HTML::th($term);
645 if ($this->_nrows > 1)
646 $th->setAttr('rowspan', $this->_nrows);
647 $this->_setTerm($th);
650 function setTightness($tight_top, $tight_bot) {
651 $this->_tight_top = $tight_top;
652 $this->_tight_bot = $tight_bot;
653 $first = &$this->firstTR();
654 $last = &$this->lastTR();
655 $first->setInClass('top', $tight_top);
657 $last->setInClass('bottom', $tight_bot);
659 trigger_error(sprintf("no lastTR: %s",AsXML($this->_content[0])), E_USER_WARNING);
663 function _addToRow ($item) {
664 if (empty($this->_accum)) {
665 $this->_accum = HTML::td();
666 if ($this->_ncols > 2)
667 $this->_accum->setAttr('colspan', $this->_ncols - 1);
669 $this->_accum->pushContent($item);
672 function _flushRow ($tight_bottom=false) {
673 if (!empty($this->_accum)) {
674 $row = new Block_HtmlElement('tr', false, $this->_accum);
676 $row->setTightness($this->_next_tight_top, $tight_bottom);
677 $this->_next_tight_top = $tight_bottom;
679 $this->pushContent($row);
680 $this->_accum = false;
685 function _addSubtable ($table) {
686 if (!($table_rows = $table->getContent()))
689 $this->_flushRow($table_rows[0]->_tight_top);
691 foreach ($table_rows as $subdef) {
692 $this->pushContent($subdef);
693 $this->_nrows += $subdef->nrows();
694 $this->_next_tight_top = $subdef->_tight_bot;
698 function _setTerm ($th) {
699 $first_row = &$this->_content[0];
700 if (isa($first_row, 'Block_table_dl_defn'))
701 $first_row->_setTerm($th);
703 $first_row->unshiftContent($th);
706 function _ComputeNcols ($defn) {
708 foreach ($defn as $item) {
709 if ($this->_IsASubtable($item)) {
710 $row = $this->_FirstDefn($item);
711 $ncols = max($ncols, $row->ncols() + 1);
717 function _IsASubtable ($item) {
718 return isa($item, 'HtmlElement')
719 && $item->getTag() == 'table'
720 && $item->getAttr('class') == 'wiki-dl-table';
723 function _FirstDefn ($subtable) {
724 $defs = $subtable->getContent();
729 return $this->_ncols;
733 return $this->_nrows;
736 function & firstTR() {
737 $first = &$this->_content[0];
738 if (isa($first, 'Block_table_dl_defn'))
739 return $first->firstTR();
743 function & lastTR() {
744 $last = &$this->_content[$this->_nrows - 1];
745 if (isa($last, 'Block_table_dl_defn'))
746 return $last->lastTR();
750 function setWidth ($ncols) {
751 assert($ncols >= $this->_ncols);
752 if ($ncols <= $this->_ncols)
754 $rows = &$this->_content;
755 for ($i = 0; $i < count($rows); $i++) {
757 if (isa($row, 'Block_table_dl_defn'))
758 $row->setWidth($ncols - 1);
760 $n = count($row->_content);
761 $lastcol = &$row->_content[$n - 1];
762 if (!empty($lastcol))
763 $lastcol->setAttr('colspan', $ncols - 1);
769 class Block_table_dl extends Block_dl
771 var $_tag = 'dl-table'; // phony.
773 function Block_table_dl() {
774 $this->_re = '\ {0,4} (?:\S.*)? (?<!'.ESCAPE_CHAR.') \| \s* $';
777 function _match (&$input, $m) {
778 if (!($p = $this->_do_match($input, $m)))
780 list ($term, $defn, $loose) = $p;
782 $this->_content[] = new Block_table_dl_defn($term, $defn);
786 function _setTightness($top, $bot) {
787 $this->_content[0]->setTightness($top, $bot);
792 $defs = &$this->_content;
795 foreach ($defs as $defn)
796 $ncols = max($ncols, $defn->ncols());
798 foreach ($defs as $key => $defn)
799 $defs[$key]->setWidth($ncols);
801 return HTML::table(array('class' => 'wiki-dl-table',
809 class Block_oldlists extends Block_list
811 //var $_tag = 'ol', 'ul', or 'dl';
812 var $_re = '(?: [*]\ (?!(?=\S)[^*]*(?<=\S)[*](?:\\s|[-)}>"\'\\/:.,;!?_*=]))
813 | [#]\ (?! \[ .*? \] )
817 function _match (&$input, $m) {
819 if (!preg_match('/[*#;]*$/A', $input->getPrefix())) {
825 $oldindent = '[*#;](?=[#*]|;.*:.*\S)';
826 $newindent = sprintf('\\ {%d}', strlen($prefix));
827 $indent = "(?:$oldindent|$newindent)";
829 $bullet = $prefix[0];
830 if ($bullet == '*') {
834 elseif ($bullet == '#') {
840 list ($term,) = explode(':', substr($prefix, 1), 2);
843 $this->_content[] = new Block_HtmlElement('dt', false,
844 TransformInline($term));
848 $this->_content[] = new TightSubBlock($input, $indent, $m->match, $itemtag);
852 function _setTightness($top, $bot) {
853 if (count($this->_content) == 1) {
854 $li = &$this->_content[0];
855 $li->setTightness($top, $bot);
858 // This is where php5 usually brakes.
859 // wrong duplicated <li> contents
860 if (DEBUG and DEBUG & _DEBUG_PARSER and check_php_version(5)) {
861 if (count($this->_content) != 2) {
864 $class = new Reflection_Class('XmlElement');
865 // Print out basic information
867 "===> The %s%s%s %s '%s' [extends %s]\n".
870 " having the modifiers %d [%s]\n",
871 $class->isInternal() ? 'internal' : 'user-defined',
872 $class->isAbstract() ? ' abstract' : '',
873 $class->isFinal() ? ' final' : '',
874 $class->isInterface() ? 'interface' : 'class',
876 var_export($class->getParentClass(), 1),
877 $class->getFileName(),
878 $class->getStartLine(),
879 $class->getEndline(),
880 $class->getModifiers(),
881 implode(' ', Reflection::getModifierNames($class->getModifiers()))
883 // Print class properties
884 printf("---> Properties: %s\n", var_export($class->getProperties(), 1));
886 echo 'count($this->_content): ', count($this->_content),"\n";
887 echo "\$this->_content[0]: "; var_dump ($this->_content[0]);
889 for ($i=1; $i < min(5, count($this->_content)); $i++) {
890 $c =& $this->_content[$i];
891 echo '$this->_content[',$i,"]: \n";
892 echo "_tag: "; var_dump ($c->_tag);
893 echo "_content: "; var_dump ($c->_content);
894 echo "_properties: "; var_dump ($c->_properties);
896 debug_print_backtrace();
897 if (DEBUG & _DEBUG_APD) {
898 if (function_exists("xdebug_get_function_stack")) {
899 var_dump (xdebug_get_function_stack());
905 if (!check_php_version(5))
906 assert(count($this->_content) == 2);
907 $dt = &$this->_content[0];
908 $dd = &$this->_content[1];
909 $dt->setTightness($top, false);
910 $dd->setTightness(false, $bot);
915 class Block_pre extends BlockMarkup
917 var $_re = '<(?:pre|verbatim|nowiki|noinclude)>';
919 function _match (&$input, $m) {
920 $endtag = '</' . substr($m->match, 1);
922 $pos = $input->getPos();
924 $line = $m->postmatch;
925 while (ltrim($line) != $endtag) {
927 if (($line = $input->nextLine()) === false) {
928 $input->setPos($pos);
934 if ($m->match == '<nowiki>')
935 $text = join("<br>\n", $text);
937 $text = join("\n", $text);
939 // FIXME: no <img>, <big>, <small>, <sup>, or <sub>'s allowed
941 if ($m->match == '<pre>') {
942 $text = TransformInline($text);
944 if ($m->match == '<noinclude>') {
945 $text = TransformText($text);
946 $this->_element = new Block_HtmlElement('div', false, $text);
947 } else if ($m->match == '<nowiki>') {
948 $text = TransformInlineNowiki($text);
949 $this->_element = new Block_HtmlElement('p', false, $text);
951 $this->_element = new Block_HtmlElement('pre', false, $text);
957 // Wikicreole placeholder
959 class Block_placeholder extends BlockMarkup
963 function _match (&$input, $m) {
966 $pos = $input->getPos();
968 $line = $m->postmatch;
969 while (ltrim($line) != $endtag) {
971 if (($line = $input->nextLine()) === false) {
972 $input->setPos($pos);
978 $text = join("\n", $text);
979 $text = '<<<' . $text . '>>>';
980 $this->_element = new Block_HtmlElement('div', false, $text);
985 class Block_nowiki_wikicreole extends BlockMarkup
989 function _match (&$input, $m) {
992 $pos = $input->getPos();
994 $line = $m->postmatch;
995 while (ltrim($line) != $endtag) {
997 if (($line = $input->nextLine()) === false) {
998 $input->setPos($pos);
1004 $text = join("\n", $text);
1005 $this->_element = new Block_HtmlElement('pre', false, $text);
1010 class Block_plugin extends Block_pre
1012 var $_re = '<\?plugin(?:-form)?(?!\S)';
1015 /* <?plugin Backlinks
1017 /* <?plugin ListPages pages=<!plugin-list Backlinks!>
1018 * exclude=<!plugin-list TitleSearch s=T*!> ?>
1022 function _match (&$input, $m) {
1023 $pos = $input->getPos();
1024 $pi = $m->match . $m->postmatch;
1025 while (!preg_match('/(?<!'.ESCAPE_CHAR.')\?>\s*$/', $pi)) {
1026 if (($line = $input->nextLine()) === false) {
1027 $input->setPos($pos);
1034 $this->_element = new Cached_PluginInvocation($pi);
1039 class Block_plugin_wikicreole extends Block_pre
1041 // var $_re = '<<(?!\S)';
1044 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
1071 function _match (&$input, $m) {
1072 $pos = $input->getPos();
1073 $pi = "|" . $m->postmatch;
1077 if ((($line = $input->nextLine()) === false) && !$intable) {
1078 $input->setPos($pos);
1085 $trimline = trim($line);
1086 if ($trimline[0] != "|") {
1090 $pi .= "\n$trimline";
1093 $pi = '<'.'?plugin WikicreoleTable ' . $pi . '?'.'>';
1095 $this->_element = new Cached_PluginInvocation($pi);
1100 /** ENABLE_MARKUP_MEDIAWIKI_TABLE
1101 * Table syntax similar to Mediawiki
1103 * => <?plugin MediawikiTable
1107 class Block_table_mediawiki extends Block_pre
1111 function _match (&$input, $m) {
1112 $pos = $input->getPos();
1113 $pi = $m->postmatch;
1114 while (!preg_match('/(?<!'.ESCAPE_CHAR.')\|}\s*$/', $pi)) {
1115 if (($line = $input->nextLine()) === false) {
1116 $input->setPos($pos);
1123 $pi = str_replace("\|}", "", $pi);
1124 $pi = '<'.'?plugin MediawikiTable ' . $pi . '?'.'>';
1125 $this->_element = new Cached_PluginInvocation($pi);
1130 class Block_template_plugin extends Block_pre
1134 function _match (&$input, $m) {
1135 $pos = $input->getPos();
1136 $pi = $m->postmatch;
1137 if ($pi[0] == '{') {
1140 while (!preg_match('/(?<!'.ESCAPE_CHAR.')}}\s*$/', $pi)) {
1141 if (($line = $input->nextLine()) === false) {
1142 $input->setPos($pos);
1150 $pi = trim($pi, "}}");
1152 if (strpos($pi, "|") === false) {
1156 $imagename = substr($pi, 0, strpos($pi, "|"));
1157 $alt = ltrim(strstr($pi, "|"), "|");
1160 // It's not a Mediawiki template, it's a Wikicreole image
1161 if (is_image($imagename)) {
1162 $this->_element = LinkImage(UPLOAD_DATA_PATH . $imagename, $alt);
1166 $pi = str_replace("\n", "", $pi);
1169 if (preg_match('/^(\S+?)\|(.*)$/', $pi, $_m)) {
1171 $vars = '"' . preg_replace('/\|/', '" "', $_m[2]) . '"';
1172 $vars = preg_replace('/"(\S+)=([^"]*)"/', '\\1="\\2"', $vars);
1175 // pi may contain a version number
1176 // {{foo?version=5}}
1177 // in that case, output is "page=foo rev=5"
1178 if (strstr($pi, "?")) {
1179 $pi = str_replace("?version=", "\" rev=\"", $page);
1183 $pi = '<'.'?plugin Template page="'.$pi.'" '.$vars . ' ?>';
1185 $pi = '<'.'?plugin Template page="' . $pi . '" ?>';
1186 $this->_element = new Cached_PluginInvocation($pi);
1191 class Block_email_blockquote extends BlockMarkup
1193 var $_attr = array('class' => 'mail-style-quote');
1196 function _match (&$input, $m) {
1197 //$indent = str_replace(' ', '\\ ', $m->match) . '|>$';
1198 $indent = $this->_re;
1199 $this->_element = new SubBlock($input, $indent, $m->match,
1200 'blockquote', $this->_attr);
1205 class Block_hr extends BlockMarkup
1207 var $_re = '-{4,}\s*$';
1209 function _match (&$input, $m) {
1211 $this->_element = new Block_HtmlElement('hr');
1215 function _setTightness($top, $bot) {
1216 // Don't tighten <hr/>s
1220 class Block_heading extends BlockMarkup
1222 var $_re = '!{1,3}';
1224 function _match (&$input, $m) {
1225 $tag = "h" . (5 - strlen($m->match));
1226 $text = TransformInline(trim($m->postmatch));
1229 $this->_element = new Block_HtmlElement($tag, false, $text);
1234 function _setTightness($top, $bot) {
1235 // Don't tighten headers.
1239 class Block_heading_wikicreole extends BlockMarkup
1241 var $_re = '={2,6}';
1243 function _match (&$input, $m) {
1244 $tag = "h" . strlen($m->match);
1246 $header = trim($m->postmatch);
1247 // Remove '='s at the end so that Mediawiki syntax is recognized
1248 $header = trim($header, "=");
1249 $text = TransformInline(trim($header));
1252 $this->_element = new Block_HtmlElement($tag, false, $text);
1257 function _setTightness($top, $bot) {
1258 // Don't tighten headers.
1262 class Block_p extends BlockMarkup
1268 function _match (&$input, $m) {
1269 $this->_text = $m->match;
1274 function _setTightness ($top, $bot) {
1275 $this->_tight_top = $top;
1276 $this->_tight_bot = $bot;
1279 function merge ($nextBlock) {
1280 $class = get_class($nextBlock);
1281 if (strtolower($class) == 'block_p' and $this->_tight_bot) {
1282 $this->_text .= "\n" . $nextBlock->_text;
1283 $this->_tight_bot = $nextBlock->_tight_bot;
1289 function finish () {
1290 $content = TransformInline(trim($this->_text));
1291 $p = new Block_HtmlElement('p', false, $content);
1292 $p->setTightness($this->_tight_top, $this->_tight_bot);
1297 class Block_divspan extends BlockMarkup
1299 var $_re = '<(?im)(?: div|span)(?:[^>]*)?>';
1301 function _match (&$input, $m) {
1302 if (substr($m->match,1,4) == 'span') {
1308 $argstr = substr(trim(substr($m->match,strlen($tag)+1)),0,-1);
1309 $pos = $input->getPos();
1310 $pi = $content = $m->postmatch;
1311 while (!preg_match('/^(.*)\<\/'.$tag.'\>(.*)$/i', $pi, $me)) {
1312 if ($pi != $content)
1313 $content .= "\n$pi";
1314 if (($pi = $input->nextLine()) === false) {
1315 $input->setPos($pos);
1319 if ($pi != $content)
1320 $content .= $me[1]; // prematch
1324 if (strstr($content, "\n"))
1325 $content = TransformText($content);
1327 $content = TransformInline($content);
1332 while (preg_match("/(\w+)=(.+)/", $argstr, $m)) {
1333 $k = $m[1]; $v = $m[2];
1334 if (preg_match("/^\"(.+?)\"(.*)$/", $v, $m)) {
1338 preg_match("/^(\s+)(.*)$/", $v, $m);
1342 if (trim($k) and trim($v)) $args[$k] = $v;
1345 $this->_element = new Block_HtmlElement($tag, $args, $content);
1346 //$this->_element->setTightness($tag == 'span', $tag == 'span');
1349 function _setTightness($top, $bot) {
1350 // Don't tighten user <div|span>
1355 ////////////////////////////////////////////////////////////////
1359 * Transform the text of a page, and return a parse tree.
1361 function TransformTextPre ($text, $markup = 2.0, $basepage=false) {
1362 if (isa($text, 'WikiDB_PageRevision')) {
1364 $text = $rev->getPackedContent();
1365 $markup = $rev->get('markup');
1367 // NEW: default markup is new, to increase stability
1368 if (!empty($markup) && $markup < 2.0) {
1369 $text = ConvertOldMarkup($text);
1372 /*if (!empty($markup) && $markup == 3) {
1373 $text = ConvertFromCreole($text);
1375 // Expand leading tabs.
1376 $text = expand_tabs($text);
1377 //set_time_limit(3);
1378 $output = new WikiText($text);
1384 * Transform the text of a page, and return an XmlContent,
1385 * suitable for printXml()-ing.
1387 function TransformText ($text, $markup = 2.0, $basepage = false) {
1388 $output = TransformTextPre($text, $markup, $basepage);
1390 // This is for immediate consumption.
1391 // We must bind the contents to a base pagename so that
1392 // relative page links can be properly linkified...
1393 return new CacheableMarkup($output->getContent(), $basepage);
1395 return new XmlContent($output->getContent());
1398 // (c-file-style: "gnu")
1402 // c-basic-offset: 4
1403 // c-hanging-comment-ender-p: nil
1404 // indent-tabs-mode: nil