1 <?php rcs_id('$Id: transform.php,v 1.33 2002-01-22 03:17:47 dairiki Exp $');
2 require_once('lib/WikiPlugin.php');
3 require_once('lib/HtmlElement.php');
5 define('WT_SIMPLE_MARKUP', 0);
6 define('WT_TOKENIZER', 1);
7 define('WT_MODE_MARKUP', 2);
9 define("ZERO_LEVEL", 0);
10 define("NESTED_LEVEL", 1);
14 // public variables (only meaningful during do_transform)
15 var $linenumber; // current linenumber
16 var $replacements; // storage for tokenized strings of current line
17 var $user_data; // can be used by the transformer functions
18 // to store miscellaneous data.
21 var $content; // wiki markup, array of lines
22 var $mode_set; // stores if a HTML mode for this line has been set
23 var $trfrm_func; // array of registered functions
24 var $stack; // stack for SetHTMLMode (keeping track of open tags)
27 function WikiTransform()
29 $this->trfrm_func = array();
30 $this->stack = new Stack;
34 * Register transformation functions
36 * This should be done *before* calling do_transform
38 * @param $type enum <dl>
39 * <dt>WT_MODE_MARKUP</dt>
40 * <dd>If one WT_MODE_MARKUP really sets the html mode, then
41 * all successive WT_MODE_MARKUP functions are skipped.</dd>
42 * <dt>WT_TOKENIZER</dt>
43 * <dd> The transformer function is called once for each match
44 * of the $regexp in the line. The matched values are tokenized
45 * to protect them from further transformation.</dd>
47 * @param $function string Function name
48 * @param $regexp string Required for WT_TOKENIZER functions.
49 * Optional for others. If given, the transformer function will only be
50 * called if the line matches the $regexp.
52 function register($type, $function, $regexp = false)
54 $this->trfrm_func[] = array ($type, $function, $regexp);
58 * Sets current mode like list, preformatted text, plain text
60 * Takes care of closing (open) tags
62 * This is a helper function used to keep track of what HTML
63 * block-level element we are currently processing.
64 * Block-level elements are things like paragraphs "<p>",
65 * pre-formatted text "<pre>", and the various list elements:
66 * "<ul>", "<ol>" and "<dl>". Now, SetHTMLMode is also used to
67 * keep track of "<li>" and "<dd>" elements. Note that some of these elements
68 * can be nested, while others can not. (In particular, according to
69 * the HTML 4.01 specification, a paragraph "<p>" element is not
70 * allowed to contain any other block-level elements. Also <pre>,
71 * <li>, <dt>, <dd>, <h1> ... have this same restriction.)
73 * SetHTMLMode generates whatever HTML is necessary to get us into
74 * the requested element type at the requested nesting level.
76 * @param $tag string Type of HTML element to open.
78 * If $tag is an array, $tag[0] gives the element type,
79 * and $tag[1] should be a hash containing attribute-value
80 * pairs for the element.
82 * If $tag is the empty string, all open elements (down to the
83 * level requested by $level) are closed. Use
84 * SetHTMLMode('',0) to close all open block-level elements.
86 * @param $level string Rrequested nesting level for current element.
87 * The nesting level for top level block is one (which is
89 * Nesting is arbitrary limited to 20 levels.
91 * @return string Returns the HTML markup to open the specified element.
93 function SetHTMLMode($tag, $level = 1)
96 $el = new HtmlElement($tag[0], $tag[1]);
98 $el = new HtmlElement($tag);
100 $this->mode_set = 1; // in order to prevent other mode markup
105 // arbitrarily limit tag nesting
106 ExitWiki(gettext ("Lists nested too deep in SetHTMLOutputMode"));
109 if ($level <= $this->stack->cnt()) {
110 // $tag has fewer nestings (old: tabs) than stack,
111 // reduce stack to that tab count
112 while ($this->stack->cnt() > $level) {
113 $closetag = $this->stack->pop();
114 assert('$closetag != false');
115 $retvar .= "</$closetag>\n";
118 // if list type isn't the same,
119 // back up one more and push new tag
120 if ($tag && $tag != $this->stack->top()) {
121 $closetag = $this->stack->pop();
122 $retvar .= "</$closetag>" . $el->_startTag() . "\n";
123 $this->stack->push($tag);
127 else {// $level > $this->stack->cnt()
128 // Test for and close top level elements which are not allowed to contain
129 // other block-level elements.
130 if ($this->stack->cnt() == 1 and
131 preg_match('/^(p|pre|h\d)$/i', $this->stack->top()))
133 $closetag = $this->stack->pop();
134 $retvar .= "</$closetag>";
137 // we add the diff to the stack
138 // stack might be zero
139 if ($this->stack->cnt() < $level) {
140 while ($this->stack->cnt() < $level - 1) {
141 // This is a bit of a hack:
143 // We're not nested deep enough, and have to make up some kind of block
144 // element to nest within.
146 // Currently, this can only happen for nested list element
147 // (either <ul> <ol> or <dl>). What we used to do here is
148 // to open extra lists of whatever type was requested.
149 // This would result in invalid HTML, since and list is
150 // not allowed to contain another list without first containing
151 // a list item. ("<ul><ul><li>Item</ul></ul>" is invalid.)
153 // So now, when we need extra list elements, we use a <dl>, and
154 // open it with an empty <dd>.
155 $stuff = $this->stack->cnt() % 2 == 0 ? 'dl' : 'dd';
156 $retvar .= "<$stuff>";
157 $this->stack->push($stuff);
160 $retvar .= $el->_startTag() . "\n";
161 $this->stack->push($tag);
165 return $this->rawtoken($retvar);
169 * Start new list item element.
171 * This closes any currently open list items at the specified level or deeper,
172 * then opens a new list item element.
174 * @param $list_type string Type of list element to open. This should
175 * be one of 'dl', 'ol', or 'ul'.
177 * @param $level integer Nesting depth for list item. Should be a positive integer.
179 * @param $defn_term string Definition term. Specifies the contents for the
180 * <dt> element. Only used if $list_type is 'dl'.
182 * @return string HTML
184 function ListItem($list_type, $level, $defn_term = '')
186 $level = min($level, 10);
188 $retval = $this->SetHTMLMode($list_type, 2 * $level - 1);
189 if ($list_type == 'dl') {
190 $retval .= AsXML(HTML::dt($defn_term));
191 $retval .= $this->SetHTMLMode('dd', 2 * $level);
194 $retval .= $this->SetHTMLMode('li', 2 * $level);
200 /** Work horse and main loop.
202 * This function does the transform from wiki markup to HTML.
204 * Contains main-loop and calls transformer functions.
206 * @param $html string HTML header (if needed, otherwise '')
207 * (This string is prepended to the return value.)
209 * @param $content array Wiki markup as array of lines
211 * @return string HTML
213 function do_transform($html, $content)
215 global $FieldSeparator;
217 $this->content = $content;
218 $this->replacements = array();
219 $this->user_data = array();
221 // Loop over all lines of the page and apply transformation rules
222 $numlines = count($this->content);
223 for ($lnum = 0; $lnum < $numlines; $lnum++)
226 $this->linenumber = $lnum;
227 $line = $this->content[$lnum];
229 // blank lines clear the current mode (to force new paragraph)
230 if (!strlen($line) || $line == "\r") {
231 $html .= $this->SetHTMLMode('', 0);
237 // main loop applying all registered functions
238 // tokenizers, markup, html mode, ...
239 // functions are executed in order of registering
240 foreach ($this->trfrm_func as $trfrm) {
241 list($flags, $func, $regexp) = $trfrm;
243 // if HTMLmode is already set then skip all following
244 // WT_MODE_MARKUP functions
245 if ($this->mode_set && ($flags & WT_MODE_MARKUP) != 0)
248 if (!empty($regexp) && !preg_match("/$regexp/", $line))
251 // call registered function
252 if (($flags & WT_TOKENIZER) != 0)
253 $line = $this->tokenize($line, $regexp, $func);
255 $line = $func($line, $this);
258 $html .= $line . "\n";
261 $html .= $this->SetHTMLMode('', 0);
263 return new RawXml($this->untokenize($html));
265 // end do_transfrom()
267 // Register a new token.
268 function rawtoken($repl) {
269 global $FieldSeparator;
270 $tok = $FieldSeparator . sizeof($this->replacements) . $FieldSeparator;
271 $this->replacements[] = $repl;
275 // Register a new token.
276 function token($repl) {
277 return $this->rawtoken(AsXML($repl));
280 // helper function which does actual tokenizing
281 function tokenize($str, $pattern, $func) {
282 // Find any strings in $str that match $pattern and
283 // store them in $orig, replacing them with tokens
284 // starting at number $ntokens - returns tokenized string
286 while (preg_match("/^(.*?)($pattern)/", $str, $matches)) {
287 $str = substr($str, strlen($matches[0]));
288 $new .= $matches[1] . $this->token($func($matches[2], $this));
293 function untokenize($line) {
294 global $FieldSeparator;
296 $chunks = explode ($FieldSeparator, "$line ");
298 for ($i = 1; $i < count($chunks); $i += 2)
301 $line .= $this->replacements[$tok] . $chunks[$i + 1];
306 // end class WikiTransform
309 //////////////////////////////////////////////////////////
311 class WikiPageTransform
312 extends WikiTransform {
313 function WikiPageTransform() {
314 global $WikiNameRegexp, $AllowedProtocols, $InterWikiLinkRegexp;
316 $this->WikiTransform();
318 // register functions
319 // functions are applied in order of registering
321 $this->register(WT_SIMPLE_MARKUP, 'wtm_plugin_link');
322 $this->register(WT_MODE_MARKUP, 'wtm_plugin');
324 $this->register(WT_TOKENIZER, 'wtt_doublebrackets', '\[\[');
325 $this->register(WT_TOKENIZER, 'wtt_footnotes', '^\[\d+\]');
326 $this->register(WT_TOKENIZER, 'wtt_footnoterefs', '\[\d+\]');
327 $this->register(WT_TOKENIZER, 'wtt_bracketlinks', '\[.+?\]');
328 $this->register(WT_TOKENIZER, 'wtt_urls',
329 "!?\b($AllowedProtocols):[^\s<>\[\]\"'()]*[^\s<>\[\]\"'(),.?]");
331 if (function_exists('wtt_interwikilinks')) {
332 $this->register(WT_TOKENIZER, 'wtt_interwikilinks',
333 pcre_fix_posix_classes("!?(?<![[:alnum:]])") .
334 "$InterWikiLinkRegexp:[^\\s.,;?()]+");
336 $this->register(WT_TOKENIZER, 'wtt_bumpylinks', "!?$WikiNameRegexp");
338 if (function_exists('wtm_table')) {
339 $this->register(WT_MODE_MARKUP, 'wtm_table', '^\|');
341 $this->register(WT_SIMPLE_MARKUP, 'wtm_htmlchars');
342 $this->register(WT_SIMPLE_MARKUP, 'wtm_linebreak');
343 $this->register(WT_SIMPLE_MARKUP, 'wtm_bold_italics');
345 $this->register(WT_MODE_MARKUP, 'wtm_list_ul');
346 $this->register(WT_MODE_MARKUP, 'wtm_list_ol');
347 $this->register(WT_MODE_MARKUP, 'wtm_list_dl');
348 $this->register(WT_MODE_MARKUP, 'wtm_preformatted');
349 $this->register(WT_MODE_MARKUP, 'wtm_headings');
350 $this->register(WT_MODE_MARKUP, 'wtm_hr');
351 $this->register(WT_MODE_MARKUP, 'wtm_paragraph');
355 function do_transform ($lines, $class = 'WikiPageTransform') {
356 if (is_string($lines))
357 $lines = preg_split('/[ \t\r]*\n/', trim($lines));
360 return $trfm->do_transform('', $lines);
364 extends WikiTransform {
365 function LinkTransform() {
366 global $WikiNameRegexp, $AllowedProtocols, $InterWikiLinkRegexp;
368 $this->WikiTransform();
370 // register functions
371 // functions are applied in order of registering
373 $this->register(WT_TOKENIZER, 'wtt_doublebrackets', '\[\[');
374 $this->register(WT_TOKENIZER, 'wtt_quotetoken', '\[\d+\]');
375 $this->register(WT_TOKENIZER, 'wtt_bracketlinks', '\[.+?\]');
376 $this->register(WT_TOKENIZER, 'wtt_urls',
377 "!?\b($AllowedProtocols):[^\s<>\[\]\"'()]*[^\s<>\[\]\"'(),.?]");
379 if (function_exists('wtt_interwikilinks')) {
380 $this->register(WT_TOKENIZER, 'wtt_interwikilinks',
381 pcre_fix_posix_classes("!?(?<![[:alnum:]])") .
382 "$InterWikiLinkRegexp:[^\\s.,;?()]+");
384 $this->register(WT_TOKENIZER, 'wtt_bumpylinks', "!?$WikiNameRegexp");
385 $this->register(WT_SIMPLE_MARKUP, 'wtm_htmlchars');
390 Requirements for functions registered to WikiTransform:
392 Signature: function wtm_xxxx($line, &$transform)
394 $line ... current line containing wiki markup
395 (Note: it may already contain HTML from other transform functions)
396 &$transform ... WikiTransform object -- public variables of this
397 object and their use see above.
399 Functions have to return $line (doesn't matter if modified or not)
400 All conversion should take place inside $line.
402 Tokenizer functions should use $transform->replacements to store
403 the replacement strings. Also, they have to keep track of
404 $transform->tokencounter. See functions below. Back substitution
405 of tokenized strings is done by do_transform().
410 //////////////////////////////////////////////////////////
411 // Tokenizer functions
414 function wtt_doublebrackets($match, &$trfrm)
419 function wtt_footnotes($match, &$trfrm)
421 // FIXME: should this set HTML mode?
422 $ftnt = trim(substr($match,1,-1)) + 0;
424 $html[] = HTML::br();
426 $fnlist = $trfrm->user_data['footnotes'][$ftnt];
427 if (!is_array($fnlist)) {
431 $trfrm->user_data['footnotes'][$ftnt] = 'footnote_seen';
433 while (list($k, $anchor) = each($fnlist)) {
434 $html[] = HTML::a(array("name" => "footnote-$ftnt",
435 "href" => "#$anchor",
436 "class" => "footnote-rev"),
444 function wtt_footnoterefs($match, &$trfrm)
446 $ftnt = trim(substr($match,1,-1)) + 0;
448 $footnote_definition_seen = false;
450 if (empty($trfrm->user_data['footnotes']))
451 $trfrm->user_data['footnotes'] = array();
452 if (empty($trfrm->user_data['footnotes'][$ftnt]))
453 $trfrm->user_data['footnotes'][$ftnt] = array();
454 else if (!is_array($trfrm->user_data['footnotes'][$ftnt]))
455 $footnote_definition_seen = true;
458 $link = HTML::a(array('href' => "#footnote-$ftnt"), "[$ftnt]");
459 if (!$footnote_definition_seen) {
460 $name = "footrev-$ftnt-" . count($trfrm->user_data['footnotes'][$ftnt]);
461 $link->setAttr('name', $name);
462 $trfrm->user_data['footnotes'][$ftnt][] = $name;
464 return HTML::sup(array('class' => 'footnote'), $link);
467 function wtt_bracketlinks($match, &$trfrm)
469 if (preg_match('/^\[\s*\]$/', $match))
472 $link = LinkBracketLink($match);
473 if ($link->isInlineElement())
477 return new RawXml("</p>" . $link->asXML() . "<p>");
481 // replace all URL's with tokens, so we don't confuse them
482 // with Wiki words later. Wiki words in URL's break things.
483 // URLs preceeded by a '!' are not linked
484 function wtt_urls($match, &$trfrm) {
485 return $match[0] == "!"
490 // Link Wiki words (BumpyText)
491 // Wikiwords preceeded by a '!' are not linked
492 function wtt_bumpylinks($match, &$trfrm) {
493 return $match[0] == "!" ? substr($match,1) : LinkWikiWord($match);
497 // Just quote the token.
498 function wtt_quotetoken($match, &$trfrm) {
504 // end of tokenizer functions
505 //////////////////////////////////////////////////////////
508 //////////////////////////////////////////////////////////
509 // basic simple markup functions
511 // escape HTML metachars
512 function wtm_htmlchars($line, &$transformer) {
513 return XmlElement::_quote($line);
516 // %%% are linebreaks
517 function wtm_linebreak($line, &$transformer) {
518 return str_replace('%%%', '<br />', $line);
522 function wtm_bold_italics($line, &$transformer) {
523 $line = preg_replace('|(__)(.*?)(__)|', '<strong>\2</strong>', $line);
524 $line = preg_replace("|('')(.*?)('')|", '<em>\2</em>', $line);
530 //////////////////////////////////////////////////////////
531 // some tokens to be replaced by (dynamic) content
533 // FIXME: some plugins are in-line (maybe?) and some are block level.
534 // Here we treat them all as inline, which will probably
535 // generate some minorly invalid HTML in some cases.
537 function wtm_plugin_link($line, &$transformer) {
538 // FIXME: is this good syntax?
539 global $dbi, $request; // FIXME: make these non-global?
541 if (preg_match('/^(.*?)(<\?plugin-link\s+.*?\?>)(.*)$/', $line, $m)) {
542 list(, $prematch, $plugin_pi, $postmatch) = $m;
543 $loader = new WikiPluginLoader;
544 $html = $loader->expandPI($plugin_pi, $dbi, $request);
545 $line = $prematch . $transformer->token($html) . $postmatch;
550 function wtm_plugin($line, &$transformer) {
551 // FIXME: is this good syntax?
552 global $dbi, $request; // FIXME: make these non-global?
554 if (preg_match('/^<\?plugin(-form)?\s.*\?>\s*$/', $line)) {
555 $loader = new WikiPluginLoader;
556 $html = $loader->expandPI($line, $dbi, $request);
557 $line = $transformer->SetHTMLMode('', 0) . $transformer->token($html);
563 //////////////////////////////////////////////////////////
564 // mode markup functions
567 // tabless markup for unordered, ordered, and dictionary lists
568 // ul/ol list types can be mixed, so we only look at the last
569 // character. Changes e.g. from "**#*" to "###*" go unnoticed.
570 // and wouldn't make a difference to the HTML layout anyway.
572 // unordered lists <UL>: "*"
573 // has to be registereed before list OL
574 function wtm_list_ul($line, &$trfrm) {
575 if (preg_match("/^([#*;]*\*)[^#]/", $line, $matches)) {
576 $numtabs = strlen($matches[1]);
577 $line = preg_replace("/^([#*]*\*)/", '', $line);
578 $line = $trfrm->ListItem('ul', $numtabs) . $line;
583 // ordered lists <OL>: "#"
584 function wtm_list_ol($line, &$trfrm) {
585 if (preg_match("/^([#*;]*\#)/", $line, $matches)) {
586 $numtabs = strlen($matches[1]);
587 $line = preg_replace("/^([#*]*\#)/", "", $line);
588 $line = $trfrm->ListItem('ol', $numtabs) . $line;
594 // definition lists <DL>: ";text:text"
595 function wtm_list_dl($line, &$trfrm) {
596 if (preg_match("/^([#*;]*;)(.*?):(.*$)/", $line, $matches)) {
597 $numtabs = strlen($matches[1]);
598 $line = $trfrm->ListItem('dl', $numtabs, $matches[2]) . $matches[3];
603 // mode: preformatted text, i.e. <pre>
604 function wtm_preformatted($line, &$trfrm) {
605 if (preg_match("/^\s+/", $line)) {
606 $line = $trfrm->SetHTMLMode('pre') . $line;
611 // mode: headings, i.e. <h1>, <h2>, <h3>
612 // lines starting with !,!!,!!! are headings
613 // Patch from steph/tara <tellme@climbtothestars.org>:
614 // use <h2>, <h3>, <h4> since <h1> is page title.
615 function wtm_headings($line, &$trfrm) {
616 if (preg_match("/^(!{1,3})[^!]/", $line, $whichheading)) {
617 if($whichheading[1] == '!') $heading = 'h4';
618 elseif($whichheading[1] == '!!') $heading = 'h3';
619 elseif($whichheading[1] == '!!!') $heading = 'h2';
620 $line = preg_replace("/^!+/", '', $line);
621 $line = $trfrm->SetHTMLMode($heading) . $line;
627 function wtm_table($line, &$trfrm)
630 while (preg_match('/^(\|+)(v*)([<>^]?)([^|]*)/', $line, $m))
632 $line = substr($line, strlen($m[0]));
636 if (strlen($m[1]) > 1)
637 $td->setAttr('colspan', strlen($m[1]));
638 if (strlen($m[2]) > 0)
639 $td->setAttr('rowspan', strlen($m[2]) + 1);
642 $td->setAttr('align', 'center');
643 else if ($m[3] == '>')
644 $td->setAttr('align', 'right');
646 $td->setAttr('align', 'left');
648 // FIXME: this is a hack: can't tokenize whole <td></td> since we
649 // haven't marked up italics, etc... yet
650 $row .= $trfrm->rawtoken($td->_startTag() . " ");
652 $row .= $trfrm->rawtoken(" " . $td->_endTag());
654 assert(empty($line));
655 $row = $trfrm->rawtoken("<tr>") . $row . $trfrm->rawtoken("</tr>");
657 return $trfrm->SetHTMLMode(array('table',
658 array('cellpadding' => 1,
664 // four or more dashes to <hr>
665 // Note this is of type WT_MODE_MARKUP becuase <hr>'s aren't
666 // allowed within <p>'s. (e.g. "<p><hr></p>" is not valid HTML.)
667 function wtm_hr($line, &$trfrm) {
668 if (preg_match('/^-{4,}(.*)$/', $line, $m)) {
669 $line = $trfrm->SetHTMLMode('', 0) . '<hr />';
671 $line .= $trfrm->SetHTMLMode('p') . $m[1];
676 // default mode: simple text paragraph
677 function wtm_paragraph($line, &$trfrm) {
678 return $trfrm->SetHTMLMode('p') . $line;
681 // (c-file-style: "gnu")
686 // c-hanging-comment-ender-p: nil
687 // indent-tabs-mode: nil