1 <?php rcs_id('$Id: BlockParser.php,v 1.31 2003-02-17 23:50:10 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 $nlines = count($this->_lines);
157 if ($this->_pos >= $nlines) {
158 $this->_atSpace = true;
161 if ($this->_lines[$this->_pos] != '')
164 $this->_atSpace = true;
166 return $this->_atSpace;
169 function currentLine () {
170 if ($this->_pos >= count($this->_lines)) {
173 return $this->_lines[$this->_pos];
176 function nextLine () {
177 $this->_atSpace = $this->_lines[$this->_pos++] === '';
178 if ($this->_pos >= count($this->_lines)) {
181 return $this->_lines[$this->_pos];
184 function advance () {
185 $this->_atSpace = $this->_lines[$this->_pos++] === '';
189 return array($this->_pos, $this->_atSpace);
192 function setPos ($pos) {
193 list($this->_pos, $this->_atSpace) = $pos;
196 function getPrefix () {
200 function getDepth () {
205 if ($this->_pos < count($this->_lines))
206 return $this->_lines[$this->_pos];
211 function _debug ($tab, $msg) {
213 $where = $this->where();
214 $tab = str_repeat('____', $this->getDepth() ) . $tab;
215 printXML(HTML::div("$tab $msg: at: '",
221 class BlockParser_InputSubBlock extends BlockParser_Input
223 function BlockParser_InputSubBlock (&$input, $prefix_re, $initial_prefix = false) {
224 $this->_input = &$input;
225 $this->_prefix_pat = "/$prefix_re|\\s*\$/Ax";
226 $this->_atSpace = $input->_atSpace;
227 //$this->_atSpace = false;
229 if (($line = $input->currentLine()) === false)
230 $this->_line = false;
231 elseif ($initial_prefix) {
232 assert(substr($line, 0, strlen($initial_prefix)) == $initial_prefix);
233 $this->_line = (string) substr($line, strlen($initial_prefix));
235 elseif (preg_match($this->_prefix_pat, $line, $m))
236 $this->_line = (string) substr($line, strlen($m[0]));
238 $this->_line = false;
241 function skipSpace () {
242 while (! $this->_line) {
243 if ($this->_line === false)
247 return $this->_atSpace;
250 function currentLine () {
254 function nextLine () {
255 $this->_atSpace = $this->_line === '';
256 $line = $this->_input->nextLine();
257 if ($line !== false && preg_match($this->_prefix_pat, $line, $m))
258 $this->_line = (string) substr($line, strlen($m[0]));
260 $this->_line = false;
264 function advance () {
265 $this->_atSpace = $this->_line === '';
266 $line = $this->_input->nextLine();
267 if ($line !== false && preg_match($this->_prefix_pat, $line, $m))
268 $this->_line = (string) substr($line, strlen($m[0]));
270 $this->_line = false;
274 return array($this->_line, $this->_atSpace, $this->_input->getPos());
277 function setPos ($pos) {
278 $this->_line = $pos[0];
279 $this->_atSpace = $pos[1];
280 $this->_input->setPos($pos[2]);
283 function getPrefix () {
284 assert ($this->_line !== false);
285 $line = $this->_input->currentLine();
286 assert ($line !== false && strlen($line) >= strlen($this->_line));
287 return substr($line, 0, strlen($line) - strlen($this->_line));
290 function getDepth () {
291 return $this->_input->getDepth() + 1;
295 return $this->_input->where();
300 class BlockParser_HtmlElement extends HtmlElement
302 function BlockHtmlElement($tag /*, ... */) {
303 $this->_init(func_get_args());
307 function setTightness($top, $bottom) {
308 $class = (string) $this->getAttr('class');
310 $class .= " tight-top";
312 $class .= " tight-bottom";
313 $class = ltrim($class);
315 $this->setAttr('class', $class);
319 class Tightenable extends BlockParser_HtmlElement {
320 var $_isTight = false;
322 function Tightenable ($tag /*, ...*/) {
323 $this->_init(func_get_args());
326 function tighten () {
327 if (! $this->_isTight) {
328 $content = &$this->_content;
329 for ($i = 0; $i < count($content); $i++) {
330 if (!isa($content[$i], 'Tightenable'))
332 $content[$i]->tighten();
334 $this->_isTight = true;
338 function canTighten () {
339 $content = &$this->_content;
340 for ($i = 0; $i < count($content); $i++) {
341 if (!isa($content[$i], 'Tightenable'))
343 if (!$content[$i]->canTighten())
350 class TightenableParagraph extends Tightenable {
351 function TightenableParagraph (/*...*/) {
353 $this->pushContent(func_get_args());
356 function tighten () {
357 $this->_isTight = true;
360 function canTighten () {
365 function printXML () {
367 return XmlContent::printXML();
369 return parent::printXML();
374 return XmlContent::asXML();
376 return parent::asXML();
380 class ParsedBlock extends Tightenable {
381 var $_isLoose = false;
383 function ParsedBlock (&$input, $tag = 'div', $attr = false) {
384 $this->Tightenable($tag, $attr);
385 $this->_initBlockTypes();
386 $this->_parse($input);
389 function canTighten () {
392 return parent::canTighten();
395 function _parse (&$input) {
396 for ($block = $this->_getBlock($input); $block; $block = $nextBlock) {
397 while ($nextBlock = $this->_getBlock($input)) {
398 // Attempt to merge current with following block.
399 if (! ($merged = $block->merge($nextBlock, $this->_atSpace)) ) {
400 break; // can't merge
404 $this->pushContent($block->finish());
409 function _initBlockTypes () {
410 foreach (array('oldlists', 'list', 'dl', 'table_dl',
411 'blockquote', 'heading', 'hr', 'pre', 'email_blockquote',
414 $class = "Block_$type";
416 $this->_block_types[] = $proto;
417 $this->_regexps[] = $proto->_re;
419 $this->_regexpset = new AnchoredRegexpSet($this->_regexps);
422 function _getBlock (&$input) {
423 $this->_atSpace = $input->skipSpace();
425 if (! ($line = $input->currentLine()) )
429 $this->_isLoose = true;
431 $tight_top = !$this->_atSpace;
432 $re_set = &$this->_regexpset;
433 for ($m = $re_set->match($line); $m; $m = $re_set->nextMatch($line, $m)) {
434 $block = $this->_block_types[$m->regexp_ind];
435 //$input->_debug('>', get_class($block));
437 if ($block->_match($input, $m)) {
438 //$input->_debug('<', get_class($block));
439 $tight_bottom = ! $input->skipSpace();
440 $block->_setTightness($tight_top, $tight_bottom);
443 //$input->_debug('[', "_match failed");
446 trigger_error("Couldn't match block: '$line'", E_USER_NOTICE);
451 class WikiText extends ParsedBlock {
452 function WikiText ($text) {
453 $input = new BlockParser_Input($text);
454 $this->ParsedBlock($input);
458 class SubBlock extends ParsedBlock {
459 function SubBlock (&$input, $indent_re, $initial_indent = false,
460 $tag = 'div', $attr = false) {
461 $subinput = new BlockParser_InputSubBlock($input, $indent_re, $initial_indent);
462 $this->ParsedBlock($subinput, $tag, $attr);
469 function _match (&$input, $match) {
470 trigger_error('pure virtual', E_USER_ERROR);
473 function _setTightness ($top, $bot) {
474 $this->_tight_top = $top;
475 $this->_tight_bot = $bot;
478 function merge ($followingBlock, $followsSpace) {
483 $this->_element->setTightness($this->_top_tight, $this->_bot_tight);
484 return $this->_element;
488 class Block_blockquote extends BlockMarkup
492 var $_re = '\ +(?=\S)';
494 // FIXME: tight top/bottom.
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, $followsSpace) {
505 if (get_class($nextBlock) == get_class($this)) {
506 assert ($nextBlock->_depth < $this->_depth);
507 $nextBlock->_element->unshiftContent($this->_element);
508 $nextBlock->_tight_top = $this->_tight_top;
515 class Block_list extends BlockMarkup
517 //var $_tag = 'ol' or 'ul';
523 | [*] (?! \S[^*]*(?<=\S)[*](?!\S) )
526 var $_isLoose = false;
527 var $_content = array();
529 function _match (&$input, $m) {
530 // A list as the first content in a list is not allowed.
533 // Should markup as <ul><li>* Item</li></ul>,
534 // not <ul><li><ul><li>Item</li></ul>/li></ul>.
536 if (preg_match('/[*#+-o]/', $input->getPrefix())) {
541 $indent = sprintf("\\ {%d}", strlen($prefix));
543 $bullet = trim($m->match);
544 $this->_tag = $bullet == '#' ? 'ol' : 'ul';
545 $this->_content[] = new SubBlock($input, $indent, $m->match, 'li');
549 function _setTightness($top, $bot) {
550 $li = &$this->_content[0];
551 $li->setTightness($top, $bot);
554 function merge ($nextBlock, $followsSpace) {
555 if (isa($nextBlock, 'Block_list') && $this->_tag == $nextBlock->_tag) {
556 array_splice($this->_content, count($this->_content), 0,
557 $nextBlock->_content);
559 $this->_isLoose = true;
566 $list = new Tightenable($this->_tag, false, $this->_content);
567 if (!$this->_isLoose && $list->canTighten())
573 class Block_dl extends Block_list
576 var $_re = '\ {0,4}\S.*(?<! ~):\s*$';
578 function _match (&$input, $m) {
579 if (!($p = $this->_do_match($input, $m)))
581 list ($term, $defn) = $p;
583 $this->_content[] = new BlockParser_HtmlElement('dt', false, $term);
584 $this->_content[] = $defn;
588 function _setTightness($top, $bot) {
589 $dt = &$this->_content[0];
590 $dd = &$this->_content[1];
592 $dt->setTightness($top, false);
593 $dd->setTightness(false, $bot);
596 function _do_match (&$input, $m) {
597 $pos = $input->getPos();
599 $firstIndent = strspn($m->match, ' ');
600 $pat = sprintf('/\ {%d,%d}(?=\s*\S)/A', $firstIndent + 1, $firstIndent + 5);
604 $line = $input->currentLine();
606 if (!$line || !preg_match($pat, $line, $mm)) {
607 $input->setPos($pos);
608 return false; // No body found.
611 $indent = strlen($mm[0]);
612 $term = TransformInline(rtrim(substr(trim($m->match),0,-1)));
613 $defn = new SubBlock($input, sprintf("\\ {%d}", $indent), false, 'dd');
614 return array($term, $defn);
620 class Block_table_dl_defn extends XmlContent
625 function Block_table_dl_defn ($term, $defn) {
627 if (!is_array($defn))
628 $defn = $defn->getContent();
630 $this->_ncols = $this->_ComputeNcols($defn);
633 foreach ($defn as $item) {
634 if ($this->_IsASubtable($item))
635 $this->_addSubtable($item);
637 $this->_addToRow($item);
641 $th = HTML::th($term);
642 if ($this->_nrows > 1)
643 $th->setAttr('rowspan', $this->_nrows);
644 $this->_setTerm($th);
647 function _addToRow ($item) {
648 if (empty($this->_accum)) {
649 $this->_accum = HTML::td();
650 if ($this->_ncols > 2)
651 $this->_accum->setAttr('colspan', $this->_ncols - 1);
653 $this->_accum->pushContent($item);
656 function _flushRow () {
657 if (!empty($this->_accum)) {
658 $this->pushContent(HTML::tr($this->_accum));
659 $this->_accum = false;
664 function _addSubtable ($table) {
666 foreach ($table->getContent() as $subdef) {
667 $this->pushContent($subdef);
668 $this->_nrows += $subdef->nrows();
672 function _setTerm ($th) {
673 $first_row = &$this->_content[0];
674 if (isa($first_row, 'Block_table_dl_defn'))
675 $first_row->_setTerm($th);
677 $first_row->unshiftContent($th);
680 function _ComputeNcols ($defn) {
682 foreach ($defn as $item) {
683 if ($this->_IsASubtable($item)) {
684 $row = $this->_FirstDefn($item);
685 $ncols = max($ncols, $row->ncols() + 1);
691 function _IsASubtable ($item) {
692 return isa($item, 'HtmlElement')
693 && $item->getTag() == 'table'
694 && $item->getAttr('class') == 'wiki-dl-table';
697 function _FirstDefn ($subtable) {
698 $defs = $subtable->getContent();
703 return $this->_ncols;
707 return $this->_nrows;
710 function setWidth ($ncols) {
711 assert($ncols >= $this->_ncols);
712 if ($ncols <= $this->_ncols)
714 $rows = &$this->_content;
715 for ($i = 0; $i < count($rows); $i++) {
717 if (isa($row, 'Block_table_dl_defn'))
718 $row->setWidth($ncols - 1);
720 $n = count($row->_content);
721 $lastcol = &$row->_content[$n - 1];
722 $lastcol->setAttr('colspan', $ncols - 1);
728 class Block_table_dl extends Block_dl
730 var $_tag = 'dl-table'; // phony.
732 var $_re = '\ {0,4} (?:\S.*)? (?<! ~) \| \s* $';
734 function _match (&$input, $m) {
735 if (!($p = $this->_do_match($input, $m)))
737 list ($term, $defn) = $p;
739 $this->_content[] = new Block_table_dl_defn($term, $defn);
743 function _setTightness($top, $bot) {
744 // FIXME: what to do?
749 $defs = &$this->_content;
752 foreach ($defs as $defn)
753 $ncols = max($ncols, $defn->ncols());
755 foreach ($defs as $key => $defn)
756 $defs[$key]->setWidth($ncols);
758 return HTML::table(array('class' => 'wiki-dl-table',
759 'border' => 2, // FIXME: CSS?
766 class Block_oldlists extends Block_list
768 //var $_tag = 'ol', 'ul', or 'dl';
769 var $_re = '(?: [*] (?! \S[^*]* (?<=\S) [*](?!\S) )
770 | [#] (?! \[ .*? \] )
774 function _match (&$input, $m) {
776 if (!preg_match('/[*#;]*$/A', $input->getPrefix())) {
782 $oldindent = '[*#;](?=[#*]|;.*:.*\S)';
783 $newindent = sprintf('\\ {%d}', strlen($prefix));
784 $indent = "(?:$oldindent|$newindent)";
786 $bullet = $prefix[0];
787 if ($bullet == '*') {
791 elseif ($bullet == '#') {
797 list ($term,) = explode(':', substr($prefix, 1), 2);
800 $this->_content[] = new BlockParser_HtmlElement('dt', false,
801 TransformInline($term));
805 $this->_content[] = new SubBlock($input, $indent, $m->match, $itemtag);
809 function _setTightness($top, $bot) {
810 if (count($this->_content) == 1) {
811 $li = &$this->_content[0];
812 $li->setTightness($top, $bot);
815 assert(count($this->_content) == 2);
816 $dt = &$this->_content[0];
817 $dd = &$this->_content[1];
818 $dt->setTightness($top, false);
819 $dd->setTightness(false, $bot);
824 class Block_pre extends BlockMarkup
826 var $_re = '<(?:pre|verbatim)>';
828 function _match (&$input, $m) {
829 $endtag = '</' . substr($m->match, 1);
831 $pos = $input->getPos();
833 $line = $m->postmatch;
834 while (ltrim($line) != $endtag) {
836 if (($line = $input->nextLine()) === false) {
837 $input->setPos($pos);
843 $text = join("\n", $text);
845 // FIXME: no <img>, <big>, <small>, <sup>, or <sub>'s allowed
847 if ($m->match == '<pre>')
848 $text = TransformInline($text);
850 $this->_element = new BlockParser_HtmlElement('pref', false, $text);
856 class Block_plugin extends Block_pre
858 var $_re = '<\?plugin(?:-form)?(?!\S)';
861 /* <?plugin Backlinks
865 function _match (&$input, $m) {
866 $pos = $input->getPos();
867 $pi = $m->match . $m->postmatch;
868 while (!preg_match('/(?<!~)\?>\s*$/', $pi)) {
869 if (($line = $input->nextLine()) === false) {
870 $input->setPos($pos);
878 $loader = new WikiPluginLoader;
879 $expansion = $loader->expandPI($pi, $request);
880 $this->_element = new BlockParser_HtmlElement('div', array('class' => 'plugin'),
886 class Block_email_blockquote extends BlockMarkup
888 // FIXME: move CSS to CSS.
889 var $_attr = array('style' => 'border-left-width: medium; border-left-color: #0f0; border-left-style: ridge; padding-left: 1em; margin-left: 0em; margin-right: 0em;');
893 function _match (&$input, $m) {
894 $indent = str_replace(' ', '\\ ', $m->match) . '|>$';
895 $this->_element = new SubBlock($input, $indent, $m->match,
896 'blockquote', $this->_attr);
901 class Block_hr extends BlockMarkup
903 var $_re = '-{4,}\s*$';
905 function _match (&$input, $m) {
907 $this->_element = new BlockParser_HtmlElement('hr');
912 class Block_heading extends BlockMarkup
916 function _match (&$input, $m) {
917 $tag = "h" . (5 - strlen($m->match));
918 $text = TransformInline(trim($m->postmatch));
921 $this->_element = new BlockParser_HtmlElement($tag, false, $text);
927 class Block_p extends BlockMarkup
932 function _match (&$input, $m) {
933 $this->_text = $m->match;
938 function merge ($nextBlock, $followsSpace) {
939 $class = get_class($nextBlock);
940 if ($class == 'block_p' && !$followsSpace) {
941 $this->_text .= "\n" . $nextBlock->_text;
942 $this->_tight_bot = $nextBlock->_tight_bot;
949 $content = TransformInline(trim($this->_text));
950 $p = new TightenableParagraph($content);
951 $p->setTightness($this->_tight_top, $this->_tight_bot);
956 ////////////////////////////////////////////////////////////////
959 function TransformText ($text, $markup = 2.0) {
960 if (isa($text, 'WikiDB_PageRevision')) {
962 $text = $rev->getPackedContent();
963 $markup = $rev->get('markup');
966 if (empty($markup) || $markup < 2.0) {
967 //include_once("lib/transform.php");
968 //return do_transform($text);
969 $text = ConvertOldMarkup($text);
972 // Expand leading tabs.
973 $text = expand_tabs($text);
977 $output = new WikiText($text);
978 return new XmlContent($output->getContent());
981 // (c-file-style: "gnu")
986 // c-hanging-comment-ender-p: nil
987 // indent-tabs-mode: nil