1 <?php rcs_id('$Id: BlockParser.php,v 1.32 2003-02-18 02:49:20 dairiki Exp $');
2 /* Copyright (C) 2002, Geoffrey T. Dairiki <dairiki@dairiki.org>
4 * This file is part of PhpWiki.
6 * PhpWiki is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * PhpWiki is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with PhpWiki; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 require_once('lib/HtmlElement.php');
21 require_once('lib/InlineParser.php');
23 ////////////////////////////////////////////////////////////////
33 // FIXME: unify this with the RegexpSet in InlinePArser.
36 * Return type from RegexpSet::match and RegexpSet::nextMatch.
40 class AnchoredRegexpSet_match {
47 * The text following the matched text.
52 * Index of the regular expression which matched.
58 * A set of regular expressions.
60 * This class is probably only useful for InlineTransformer.
62 class AnchoredRegexpSet
66 * @param $regexps array A list of regular expressions. The
67 * regular expressions should not include any sub-pattern groups
68 * "(...)". (Anonymous groups, like "(?:...)", as well as
69 * look-ahead and look-behind assertions are fine.)
71 function AnchoredRegexpSet ($regexps) {
72 $this->_regexps = $regexps;
73 $this->_re = "/((" . join(")|(", $regexps) . "))/Ax";
77 * Search text for the next matching regexp from the Regexp Set.
79 * @param $text string The text to search.
81 * @return object A RegexpSet_match object, or false if no match.
83 function match ($text) {
84 if (! preg_match($this->_re, $text, $m)) {
88 $match = new AnchoredRegexpSet_match;
89 $match->postmatch = substr($text, strlen($m[0]));
90 $match->match = $m[1];
91 $match->regexp_ind = count($m) - 3;
96 * Search for next matching regexp.
98 * Here, 'next' has two meanings:
100 * Match the next regexp(s) in the set, at the same position as the last match.
102 * If that fails, match the whole RegexpSet, starting after the position of the
105 * @param $text string Text to search.
107 * @param $prevMatch A RegexpSet_match object
109 * $prevMatch should be a match object obtained by a previous
110 * match upon the same value of $text.
112 * @return object A RegexpSet_match object, or false if no match.
114 function nextMatch ($text, $prevMatch) {
115 // Try to find match at same position.
116 $regexps = array_slice($this->_regexps, $prevMatch->regexp_ind + 1);
121 $pat= "/ ( (" . join(')|(', $regexps) . ") ) /Axs";
123 if (! preg_match($pat, $text, $m)) {
127 $match = new AnchoredRegexpSet_match;
128 $match->postmatch = substr($text, strlen($m[0]));
129 $match->match = $m[1];
130 $match->regexp_ind = count($m) - 3 + $prevMatch->regexp_ind + 1;;
137 class BlockParser_Input {
139 function BlockParser_Input ($text) {
141 // Expand leading tabs.
142 // FIXME: do this better.
144 // We want to ensure the only characters matching \s are ' ' and "\n".
146 $text = preg_replace('/(?![ \n])\s/', ' ', $text);
147 assert(!preg_match('/(?![ \n])\s/', $text));
149 $this->_lines = preg_split('/[^\S\n]*\n/', $text);
151 $this->_atSpace = true;
154 function skipSpace () {
155 // For top-level input, the end of file looks like a space.
156 // (The last block is not of class tight-bottom.)
157 $nlines = count($this->_lines);
159 if ($this->_pos >= $nlines) {
160 $this->_atSpace = true;
163 if ($this->_lines[$this->_pos] != '')
166 $this->_atSpace = true;
168 return $this->_atSpace;
171 function currentLine () {
172 if ($this->_pos >= count($this->_lines)) {
175 return $this->_lines[$this->_pos];
178 function nextLine () {
179 $this->_atSpace = $this->_lines[$this->_pos++] === '';
180 if ($this->_pos >= count($this->_lines)) {
183 return $this->_lines[$this->_pos];
186 function advance () {
187 $this->_atSpace = $this->_lines[$this->_pos++] === '';
191 return array($this->_pos, $this->_atSpace);
194 function setPos ($pos) {
195 list($this->_pos, $this->_atSpace) = $pos;
198 function getPrefix () {
202 function getDepth () {
207 if ($this->_pos < count($this->_lines))
208 return $this->_lines[$this->_pos];
213 function _debug ($tab, $msg) {
215 $where = $this->where();
216 $tab = str_repeat('____', $this->getDepth() ) . $tab;
217 printXML(HTML::div("$tab $msg: at: '",
223 class BlockParser_InputSubBlock extends BlockParser_Input
225 function BlockParser_InputSubBlock (&$input, $prefix_re, $initial_prefix = false) {
226 $this->_input = &$input;
227 $this->_prefix_pat = "/$prefix_re|\\s*\$/Ax";
228 $this->_atSpace = false;
230 if (($line = $input->currentLine()) === false)
231 $this->_line = false;
232 elseif ($initial_prefix) {
233 assert(substr($line, 0, strlen($initial_prefix)) == $initial_prefix);
234 $this->_line = (string) substr($line, strlen($initial_prefix));
235 $this->_atBlank = ! ltrim($line);
237 elseif (preg_match($this->_prefix_pat, $line, $m)) {
238 $this->_line = (string) substr($line, strlen($m[0]));
239 $this->_atBlank = ! ltrim($line);
242 $this->_line = false;
245 function skipSpace () {
246 // In contrast to the case for top-level blocks,
247 // for sub-blocks, there never appears to be any trailing space.
248 // (The last block in the sub-block should always be of class tight-bottom.)
249 while ($this->_line === '')
252 if ($this->_line === false)
253 return $this->_atSpace == 'strong_space';
255 return $this->_atSpace;
258 function currentLine () {
262 function nextLine () {
263 if ($this->_line === '')
264 $this->_atSpace = $this->_atBlank ? 'weak_space' : 'strong_space';
266 $this->_atSpace = false;
268 $line = $this->_input->nextLine();
269 if ($line !== false && preg_match($this->_prefix_pat, $line, $m)) {
270 $this->_line = (string) substr($line, strlen($m[0]));
271 $this->_atBlank = ! ltrim($line);
274 $this->_line = false;
279 function advance () {
284 return array($this->_line, $this->_atSpace, $this->_input->getPos());
287 function setPos ($pos) {
288 $this->_line = $pos[0];
289 $this->_atSpace = $pos[1];
290 $this->_input->setPos($pos[2]);
293 function getPrefix () {
294 assert ($this->_line !== false);
295 $line = $this->_input->currentLine();
296 assert ($line !== false && strlen($line) >= strlen($this->_line));
297 return substr($line, 0, strlen($line) - strlen($this->_line));
300 function getDepth () {
301 return $this->_input->getDepth() + 1;
305 return $this->_input->where();
310 class Block_HtmlElement extends HtmlElement
312 function Block_HtmlElement($tag /*, ... */) {
313 $this->_init(func_get_args());
317 function setTightness($top, $bottom) {
318 $class = (string) $this->getAttr('class');
320 $class .= " tight-top";
322 $class .= " tight-bottom";
323 $class = ltrim($class);
325 $this->setAttr('class', $class);
329 class ParsedBlock extends Block_HtmlElement {
331 function ParsedBlock (&$input, $tag = 'div', $attr = false) {
332 $this->Block_HtmlElement($tag, $attr);
333 $this->_initBlockTypes();
334 $this->_parse($input);
337 function _parse (&$input) {
338 for ($block = $this->_getBlock($input); $block; $block = $nextBlock) {
339 while ($nextBlock = $this->_getBlock($input)) {
340 // Attempt to merge current with following block.
341 if (! ($merged = $block->merge($nextBlock)) ) {
342 break; // can't merge
346 $this->pushContent($block->finish());
351 function _initBlockTypes () {
352 foreach (array('oldlists', 'list', 'dl', 'table_dl',
353 'blockquote', 'heading', 'hr', 'pre', 'email_blockquote',
356 $class = "Block_$type";
358 $this->_block_types[] = $proto;
359 $this->_regexps[] = $proto->_re;
361 $this->_regexpset = new AnchoredRegexpSet($this->_regexps);
364 function _getBlock (&$input) {
365 $this->_atSpace = $input->skipSpace();
367 if (! ($line = $input->currentLine()) )
370 $tight_top = !$this->_atSpace;
371 $re_set = &$this->_regexpset;
372 for ($m = $re_set->match($line); $m; $m = $re_set->nextMatch($line, $m)) {
373 $block = $this->_block_types[$m->regexp_ind];
374 //$input->_debug('>', get_class($block));
376 if ($block->_match($input, $m)) {
377 //$input->_debug('<', get_class($block));
378 $tight_bottom = ! $input->skipSpace();
379 $block->_setTightness($tight_top, $tight_bottom);
382 //$input->_debug('[', "_match failed");
385 trigger_error("Couldn't match block: '$line'", E_USER_NOTICE);
390 class WikiText extends ParsedBlock {
391 function WikiText ($text) {
392 $input = new BlockParser_Input($text);
393 $this->ParsedBlock($input);
397 class SubBlock extends ParsedBlock {
398 function SubBlock (&$input, $indent_re, $initial_indent = false,
399 $tag = 'div', $attr = false) {
400 $subinput = new BlockParser_InputSubBlock($input, $indent_re, $initial_indent);
401 $this->ParsedBlock($subinput, $tag, $attr);
406 * TightSubBlock is for use in parsing lists item bodies.
408 * If the sub-block consists of a single paragraph, it omits
409 * the paragraph element.
411 * We go to this trouble so that "tight" lists look somewhat reasonable
412 * in older (non-CSS) browsers. (If you don't do this, then, without
413 * CSS, you only get "loose" lists.
415 class TightSubBlock extends SubBlock {
416 function TightSubBlock (&$input, $indent_re, $initial_indent = false,
417 $tag = 'div', $attr = false) {
418 $this->SubBlock($input, $indent_re, $initial_indent, $tag, $attr);
420 // If content is a single paragraph, eliminate the paragraph...
421 if (count($this->_content) == 1) {
422 $elem = $this->_content[0];
423 if ($elem->getTag() == 'p') {
424 assert($elem->getAttr('class') == 'tight-top tight-bottom');
425 $this->setContent($elem->getContent());
434 function _match (&$input, $match) {
435 trigger_error('pure virtual', E_USER_ERROR);
438 function _setTightness ($top, $bot) {
439 $this->_tight_top = $top;
440 $this->_tight_bot = $bot;
443 function merge ($followingBlock) {
448 $this->_element->setTightness($this->_tight_top, $this->_tight_bot);
449 return $this->_element;
453 class Block_blockquote extends BlockMarkup
457 var $_re = '\ +(?=\S)';
459 function _match (&$input, $m) {
460 $this->_depth = strlen($m->match);
461 $indent = sprintf("\\ {%d}", $this->_depth);
462 $this->_element = new SubBlock($input, $indent, $m->match,
467 function merge ($nextBlock) {
468 if (get_class($nextBlock) == get_class($this)) {
469 assert ($nextBlock->_depth < $this->_depth);
470 $nextBlock->_element->unshiftContent($this->_element);
471 $nextBlock->_tight_top = $this->_tight_top;
478 class Block_list extends BlockMarkup
480 //var $_tag = 'ol' or 'ul';
486 | [*] (?! \S[^*]*(?<=\S)[*](?!\S) )
489 var $_content = array();
491 function _match (&$input, $m) {
492 // A list as the first content in a list is not allowed.
495 // Should markup as <ul><li>* Item</li></ul>,
496 // not <ul><li><ul><li>Item</li></ul>/li></ul>.
498 if (preg_match('/[*#+-o]/', $input->getPrefix())) {
503 $indent = sprintf("\\ {%d}", strlen($prefix));
505 $bullet = trim($m->match);
506 $this->_tag = $bullet == '#' ? 'ol' : 'ul';
507 $this->_content[] = new TightSubBlock($input, $indent, $m->match, 'li');
511 function _setTightness($top, $bot) {
512 $li = &$this->_content[0];
513 $li->setTightness($top, $bot);
516 function merge ($nextBlock) {
517 if (isa($nextBlock, 'Block_list') && $this->_tag == $nextBlock->_tag) {
518 array_splice($this->_content, count($this->_content), 0,
519 $nextBlock->_content);
526 return new Block_HtmlElement($this->_tag, false, $this->_content);
530 class Block_dl extends Block_list
533 var $_re = '\ {0,4}\S.*(?<! ~):\s*$';
535 function _match (&$input, $m) {
536 if (!($p = $this->_do_match($input, $m)))
538 list ($term, $defn) = $p;
540 $this->_content[] = new Block_HtmlElement('dt', false, $term);
541 $this->_content[] = $defn;
545 function _setTightness($top, $bot) {
546 $dt = &$this->_content[0];
547 $dd = &$this->_content[1];
549 $dt->setTightness($top, false);
550 $dd->setTightness(false, $bot);
553 function _do_match (&$input, $m) {
554 $pos = $input->getPos();
556 $firstIndent = strspn($m->match, ' ');
557 $pat = sprintf('/\ {%d,%d}(?=\s*\S)/A', $firstIndent + 1, $firstIndent + 5);
561 $line = $input->currentLine();
563 if (!$line || !preg_match($pat, $line, $mm)) {
564 $input->setPos($pos);
565 return false; // No body found.
568 $indent = strlen($mm[0]);
569 $term = TransformInline(rtrim(substr(trim($m->match),0,-1)));
570 $defn = new TightSubBlock($input, sprintf("\\ {%d}", $indent), false, 'dd');
571 return array($term, $defn);
577 class Block_table_dl_defn extends XmlContent
582 function Block_table_dl_defn ($term, $defn) {
584 if (!is_array($defn))
585 $defn = $defn->getContent();
587 $this->_ncols = $this->_ComputeNcols($defn);
589 $this->_tight_top = false;
590 $this->_tight_bot = false;
591 $this->_atSpace = true;
593 foreach ($defn as $item) {
594 if ($this->_IsASubtable($item))
595 $this->_addSubtable($item);
597 $this->_addToRow($item);
601 $th = HTML::th($term);
602 if ($this->_nrows > 1)
603 $th->setAttr('rowspan', $this->_nrows);
604 $this->_setTerm($th);
607 function setTightness($top, $bot) {
608 $this->_content[0]->setTightness($top, false);
609 $this->_content[$this->_nrows-1]->setTightness(false, $bot);
610 $this->_tight_top = $top;
611 $this->_tight_bot = $bot;
614 function _addToRow ($item) {
615 if (empty($this->_accum)) {
616 $this->_accum = HTML::td();
617 if ($this->_ncols > 2)
618 $this->_accum->setAttr('colspan', $this->_ncols - 1);
620 $this->_accum->pushContent($item);
623 function _flushRow ($tight_bottom=false) {
624 if (!empty($this->_accum)) {
625 $row = new Block_HtmlElement('tr', false, $this->_accum);
627 $row->setTightness(!$this->_atSpace, $tight_bottom);
628 $this->_atSpace = !$tight_bottom;
630 $this->pushContent($row);
631 $this->_accum = false;
636 function _addSubtable ($table) {
637 if (!($table_rows = $table->getContent()))
640 $this->_flushRow($table_rows[0]->_tight_top);
642 foreach ($table_rows as $subdef) {
643 $this->pushContent($subdef);
644 $this->_nrows += $subdef->nrows();
645 $this->_atSpace = ! $subdef->_tight_bot;
649 function _setTerm ($th) {
650 $first_row = &$this->_content[0];
651 if (isa($first_row, 'Block_table_dl_defn'))
652 $first_row->_setTerm($th);
654 $first_row->unshiftContent($th);
657 function _ComputeNcols ($defn) {
659 foreach ($defn as $item) {
660 if ($this->_IsASubtable($item)) {
661 $row = $this->_FirstDefn($item);
662 $ncols = max($ncols, $row->ncols() + 1);
668 function _IsASubtable ($item) {
669 return isa($item, 'HtmlElement')
670 && $item->getTag() == 'table'
671 && $item->getAttr('class') == 'wiki-dl-table';
674 function _FirstDefn ($subtable) {
675 $defs = $subtable->getContent();
680 return $this->_ncols;
684 return $this->_nrows;
687 function setWidth ($ncols) {
688 assert($ncols >= $this->_ncols);
689 if ($ncols <= $this->_ncols)
691 $rows = &$this->_content;
692 for ($i = 0; $i < count($rows); $i++) {
694 if (isa($row, 'Block_table_dl_defn'))
695 $row->setWidth($ncols - 1);
697 $n = count($row->_content);
698 $lastcol = &$row->_content[$n - 1];
699 $lastcol->setAttr('colspan', $ncols - 1);
705 class Block_table_dl extends Block_dl
707 var $_tag = 'dl-table'; // phony.
709 var $_re = '\ {0,4} (?:\S.*)? (?<! ~) \| \s* $';
711 function _match (&$input, $m) {
712 if (!($p = $this->_do_match($input, $m)))
714 list ($term, $defn) = $p;
716 $this->_content[] = new Block_table_dl_defn($term, $defn);
720 function _setTightness($top, $bot) {
721 $this->_content[0]->setTightness($top, $bot);
726 $defs = &$this->_content;
729 foreach ($defs as $defn)
730 $ncols = max($ncols, $defn->ncols());
732 foreach ($defs as $key => $defn)
733 $defs[$key]->setWidth($ncols);
735 return HTML::table(array('class' => 'wiki-dl-table',
743 class Block_oldlists extends Block_list
745 //var $_tag = 'ol', 'ul', or 'dl';
746 var $_re = '(?: [*] (?! \S[^*]* (?<=\S) [*](?!\S) )
747 | [#] (?! \[ .*? \] )
751 function _match (&$input, $m) {
753 if (!preg_match('/[*#;]*$/A', $input->getPrefix())) {
759 $oldindent = '[*#;](?=[#*]|;.*:.*\S)';
760 $newindent = sprintf('\\ {%d}', strlen($prefix));
761 $indent = "(?:$oldindent|$newindent)";
763 $bullet = $prefix[0];
764 if ($bullet == '*') {
768 elseif ($bullet == '#') {
774 list ($term,) = explode(':', substr($prefix, 1), 2);
777 $this->_content[] = new Block_HtmlElement('dt', false,
778 TransformInline($term));
782 $this->_content[] = new TightSubBlock($input, $indent, $m->match, $itemtag);
786 function _setTightness($top, $bot) {
787 if (count($this->_content) == 1) {
788 $li = &$this->_content[0];
789 $li->setTightness($top, $bot);
792 assert(count($this->_content) == 2);
793 $dt = &$this->_content[0];
794 $dd = &$this->_content[1];
795 $dt->setTightness($top, false);
796 $dd->setTightness(false, $bot);
801 class Block_pre extends BlockMarkup
803 var $_re = '<(?:pre|verbatim)>';
805 function _match (&$input, $m) {
806 $endtag = '</' . substr($m->match, 1);
808 $pos = $input->getPos();
810 $line = $m->postmatch;
811 while (ltrim($line) != $endtag) {
813 if (($line = $input->nextLine()) === false) {
814 $input->setPos($pos);
820 $text = join("\n", $text);
822 // FIXME: no <img>, <big>, <small>, <sup>, or <sub>'s allowed
824 if ($m->match == '<pre>')
825 $text = TransformInline($text);
827 $this->_element = new Block_HtmlElement('pre', false, $text);
833 class Block_plugin extends Block_pre
835 var $_re = '<\?plugin(?:-form)?(?!\S)';
838 /* <?plugin Backlinks
842 function _match (&$input, $m) {
843 $pos = $input->getPos();
844 $pi = $m->match . $m->postmatch;
845 while (!preg_match('/(?<!~)\?>\s*$/', $pi)) {
846 if (($line = $input->nextLine()) === false) {
847 $input->setPos($pos);
855 $loader = new WikiPluginLoader;
856 $expansion = $loader->expandPI($pi, $request);
857 $this->_element = new Block_HtmlElement('div', array('class' => 'plugin'),
863 class Block_email_blockquote extends BlockMarkup
865 var $_attr = array('class' => 'mail-style-quote');
868 function _match (&$input, $m) {
869 //$indent = str_replace(' ', '\\ ', $m->match) . '|>$';
870 $indent = $this->_re;
871 $this->_element = new SubBlock($input, $indent, $m->match,
872 'blockquote', $this->_attr);
877 class Block_hr extends BlockMarkup
879 var $_re = '-{4,}\s*$';
881 function _match (&$input, $m) {
883 $this->_element = new Block_HtmlElement('hr');
888 class Block_heading extends BlockMarkup
892 function _match (&$input, $m) {
893 $tag = "h" . (5 - strlen($m->match));
894 $text = TransformInline(trim($m->postmatch));
897 $this->_element = new Block_HtmlElement($tag, false, $text);
903 class Block_p extends BlockMarkup
908 function _match (&$input, $m) {
909 $this->_text = $m->match;
914 function merge ($nextBlock) {
915 $class = get_class($nextBlock);
916 if ($class == 'block_p' && $this->_tight_bot) {
917 $this->_text .= "\n" . $nextBlock->_text;
918 $this->_tight_bot = $nextBlock->_tight_bot;
925 $content = TransformInline(trim($this->_text));
926 $p = new Block_HtmlElement('p', false, $content);
927 $p->setTightness($this->_tight_top, $this->_tight_bot);
932 ////////////////////////////////////////////////////////////////
935 function TransformText ($text, $markup = 2.0) {
936 if (isa($text, 'WikiDB_PageRevision')) {
938 $text = $rev->getPackedContent();
939 $markup = $rev->get('markup');
942 if (empty($markup) || $markup < 2.0) {
943 //include_once("lib/transform.php");
944 //return do_transform($text);
945 $text = ConvertOldMarkup($text);
948 // Expand leading tabs.
949 $text = expand_tabs($text);
953 $output = new WikiText($text);
954 return new XmlContent($output->getContent());
957 // (c-file-style: "gnu")
962 // c-hanging-comment-ender-p: nil
963 // indent-tabs-mode: nil