1 <?php rcs_id('$Id: transform.php,v 1.38 2002-01-28 18:49:08 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
107 $request->finish(_("Lists nested too deep in SetHTMLOutputMode"));
110 if ($level <= $this->stack->cnt()) {
111 // $tag has fewer nestings (old: tabs) than stack,
112 // reduce stack to that tab count
113 while ($this->stack->cnt() > $level) {
114 $closetag = $this->stack->pop();
115 assert('$closetag != false');
116 $retvar .= "</$closetag>\n";
119 // if list type isn't the same,
120 // back up one more and push new tag
121 if ($tag && $tag != $this->stack->top()) {
122 $closetag = $this->stack->pop();
123 $retvar .= "</$closetag>" . $el->startTag() . "\n";
124 $this->stack->push($tag);
128 else {// $level > $this->stack->cnt()
129 // Test for and close top level elements which are not allowed to contain
130 // other block-level elements.
131 if ($this->stack->cnt() == 1 and
132 preg_match('/^(p|pre|h\d)$/i', $this->stack->top()))
134 $closetag = $this->stack->pop();
135 $retvar .= "</$closetag>";
138 // we add the diff to the stack
139 // stack might be zero
140 if ($this->stack->cnt() < $level) {
141 while ($this->stack->cnt() < $level - 1) {
142 // This is a bit of a hack:
144 // We're not nested deep enough, and have to make up some kind of block
145 // element to nest within.
147 // Currently, this can only happen for nested list element
148 // (either <ul> <ol> or <dl>). What we used to do here is
149 // to open extra lists of whatever type was requested.
150 // This would result in invalid HTML, since and list is
151 // not allowed to contain another list without first containing
152 // a list item. ("<ul><ul><li>Item</ul></ul>" is invalid.)
154 // So now, when we need extra list elements, we use a <dl>, and
155 // open it with an empty <dd>.
156 $stuff = $this->stack->cnt() % 2 == 0 ? 'dl' : 'dd';
157 $retvar .= "<$stuff>";
158 $this->stack->push($stuff);
161 $retvar .= $el->startTag() . "\n";
162 $this->stack->push($tag);
166 return $this->rawtoken($retvar);
170 * Start new list item element.
172 * This closes any currently open list items at the specified level or deeper,
173 * then opens a new list item element.
175 * @param $list_type string Type of list element to open. This should
176 * be one of 'dl', 'ol', or 'ul'.
178 * @param $level integer Nesting depth for list item. Should be a positive integer.
180 * @param $defn_term string Definition term. Specifies the contents for the
181 * <dt> element. Only used if $list_type is 'dl'.
183 * @return string HTML
185 function ListItem($list_type, $level, $defn_term = '')
187 $level = min($level, 10);
189 $retval = $this->SetHTMLMode($list_type, 2 * $level - 1);
190 if ($list_type == 'dl') {
191 $retval .= AsXML(HTML::dt(HTML::raw($defn_term)));
192 $retval .= $this->SetHTMLMode('dd', 2 * $level);
195 $retval .= $this->SetHTMLMode('li', 2 * $level);
201 /** Work horse and main loop.
203 * This function does the transform from wiki markup to HTML.
205 * Contains main-loop and calls transformer functions.
207 * @param $html string HTML header (if needed, otherwise '')
208 * (This string is prepended to the return value.)
210 * @param $content array Wiki markup as array of lines
212 * @return string HTML
214 function do_transform($html, $content)
216 global $FieldSeparator;
218 $this->content = $content;
219 $this->replacements = array();
220 $this->user_data = array();
222 // Loop over all lines of the page and apply transformation rules
223 $numlines = count($this->content);
224 for ($lnum = 0; $lnum < $numlines; $lnum++)
227 $this->linenumber = $lnum;
228 $line = $this->content[$lnum];
230 // blank lines clear the current mode (to force new paragraph)
231 if (!strlen($line) || $line == "\r") {
232 $html .= $this->SetHTMLMode('', 0);
238 // main loop applying all registered functions
239 // tokenizers, markup, html mode, ...
240 // functions are executed in order of registering
241 foreach ($this->trfrm_func as $trfrm) {
242 list($flags, $func, $regexp) = $trfrm;
244 // if HTMLmode is already set then skip all following
245 // WT_MODE_MARKUP functions
246 if ($this->mode_set && ($flags & WT_MODE_MARKUP) != 0)
249 if (!empty($regexp) && !preg_match("/$regexp/", $line))
252 // call registered function
253 if (($flags & WT_TOKENIZER) != 0)
254 $line = $this->tokenize($line, $regexp, $func);
256 $line = $func($line, $this);
259 $html .= $line . "\n";
262 $html .= $this->SetHTMLMode('', 0);
264 return new RawXml($this->untokenize($html));
266 // end do_transfrom()
268 // Register a new token.
269 function rawtoken($repl) {
270 global $FieldSeparator;
271 $tok = $FieldSeparator . sizeof($this->replacements) . $FieldSeparator;
272 $this->replacements[] = $repl;
276 // Register a new token.
277 function token($repl) {
278 return $this->rawtoken(AsXML($repl));
281 // helper function which does actual tokenizing
282 function tokenize($str, $pattern, $func) {
283 // Find any strings in $str that match $pattern and
284 // store them in $orig, replacing them with tokens
285 // starting at number $ntokens - returns tokenized string
287 while (preg_match("/^(.*?)($pattern)/", $str, $matches)) {
288 $str = substr($str, strlen($matches[0]));
289 $new .= $matches[1] . $this->token($func($matches[2], $this));
294 function untokenize($line) {
295 global $FieldSeparator;
297 $chunks = explode ($FieldSeparator, "$line ");
299 for ($i = 1; $i < count($chunks); $i += 2)
302 $line .= $this->replacements[$tok] . $chunks[$i + 1];
307 // end class WikiTransform
310 //////////////////////////////////////////////////////////
312 class WikiPageTransform
313 extends WikiTransform {
314 function WikiPageTransform() {
315 global $WikiNameRegexp, $AllowedProtocols, $InterWikiLinkRegexp;
317 $this->WikiTransform();
319 // register functions
320 // functions are applied in order of registering
322 $this->register(WT_SIMPLE_MARKUP, 'wtm_plugin_link');
323 $this->register(WT_MODE_MARKUP, 'wtm_plugin');
325 $this->register(WT_TOKENIZER, 'wtt_doublebrackets', '\[\[');
326 $this->register(WT_TOKENIZER, 'wtt_footnotes', '^\[\d+\]');
327 $this->register(WT_TOKENIZER, 'wtt_footnoterefs', '\[\d+\]');
328 $this->register(WT_TOKENIZER, 'wtt_bracketlinks', '\[.+?\]');
329 $this->register(WT_TOKENIZER, 'wtt_urls',
330 "!?\b($AllowedProtocols):[^\s<>\[\]\"'()]*[^\s<>\[\]\"'(),.?]");
332 if (function_exists('wtt_interwikilinks')) {
333 $this->register(WT_TOKENIZER, 'wtt_interwikilinks',
334 pcre_fix_posix_classes("!?(?<![[:alnum:]])") .
335 "$InterWikiLinkRegexp:[^\\s.,;?()]+");
337 $this->register(WT_TOKENIZER, 'wtt_bumpylinks', "!?$WikiNameRegexp");
339 if (function_exists('wtm_table')) {
340 $this->register(WT_MODE_MARKUP, 'wtm_table', '^\|');
342 $this->register(WT_SIMPLE_MARKUP, 'wtm_htmlchars');
343 $this->register(WT_SIMPLE_MARKUP, 'wtm_linebreak');
344 $this->register(WT_SIMPLE_MARKUP, 'wtm_bold_italics');
346 $this->register(WT_MODE_MARKUP, 'wtm_list_ul');
347 $this->register(WT_MODE_MARKUP, 'wtm_list_ol');
348 $this->register(WT_MODE_MARKUP, 'wtm_list_dl');
349 $this->register(WT_MODE_MARKUP, 'wtm_preformatted');
350 $this->register(WT_MODE_MARKUP, 'wtm_headings');
351 $this->register(WT_MODE_MARKUP, 'wtm_hr');
352 $this->register(WT_MODE_MARKUP, 'wtm_paragraph');
356 function do_transform ($lines, $class = 'WikiPageTransform') {
357 if (is_string($lines))
358 $lines = preg_split('/[ \t\r]*\n/', trim($lines));
361 return $trfm->do_transform('', $lines);
365 extends WikiTransform {
366 function LinkTransform() {
367 global $WikiNameRegexp, $AllowedProtocols, $InterWikiLinkRegexp;
369 $this->WikiTransform();
371 // register functions
372 // functions are applied in order of registering
374 $this->register(WT_TOKENIZER, 'wtt_doublebrackets', '\[\[');
375 $this->register(WT_TOKENIZER, 'wtt_quotetoken', '\[\d+\]');
376 $this->register(WT_TOKENIZER, 'wtt_bracketlinks', '\[.+?\]');
377 $this->register(WT_TOKENIZER, 'wtt_urls',
378 "!?\b($AllowedProtocols):[^\s<>\[\]\"'()]*[^\s<>\[\]\"'(),.?]");
380 if (function_exists('wtt_interwikilinks')) {
381 $this->register(WT_TOKENIZER, 'wtt_interwikilinks',
382 pcre_fix_posix_classes("!?(?<![[:alnum:]])") .
383 "$InterWikiLinkRegexp:[^\\s.,;?()]+");
385 $this->register(WT_TOKENIZER, 'wtt_bumpylinks', "!?$WikiNameRegexp");
386 $this->register(WT_SIMPLE_MARKUP, 'wtm_htmlchars');
391 Requirements for functions registered to WikiTransform:
393 Signature: function wtm_xxxx($line, &$transform)
395 $line ... current line containing wiki markup
396 (Note: it may already contain HTML from other transform functions)
397 &$transform ... WikiTransform object -- public variables of this
398 object and their use see above.
400 Functions have to return $line (doesn't matter if modified or not)
401 All conversion should take place inside $line.
403 Tokenizer functions should use $transform->replacements to store
404 the replacement strings. Also, they have to keep track of
405 $transform->tokencounter. See functions below. Back substitution
406 of tokenized strings is done by do_transform().
411 //////////////////////////////////////////////////////////
412 // Tokenizer functions
415 function wtt_doublebrackets($match, &$trfrm)
420 function wtt_footnotes($match, &$trfrm)
422 // FIXME: should this set HTML mode?
423 $ftnt = trim(substr($match,1,-1)) + 0;
425 $html = HTML(HTML::br());
427 $fnlist = $trfrm->user_data['footnotes'][$ftnt];
428 if (!is_array($fnlist)) {
429 $html->pushContent($fntext);
432 $trfrm->user_data['footnotes'][$ftnt] = 'footnote_seen';
434 while (list($k, $anchor) = each($fnlist)) {
435 $html->pushContent(HTML::a(array("name" => "footnote-$ftnt",
436 "href" => "#$anchor",
437 "class" => "footnote-rev"),
445 function wtt_footnoterefs($match, &$trfrm)
447 $ftnt = trim(substr($match,1,-1)) + 0;
449 $footnote_definition_seen = false;
451 if (empty($trfrm->user_data['footnotes']))
452 $trfrm->user_data['footnotes'] = array();
453 if (empty($trfrm->user_data['footnotes'][$ftnt]))
454 $trfrm->user_data['footnotes'][$ftnt] = array();
455 else if (!is_array($trfrm->user_data['footnotes'][$ftnt]))
456 $footnote_definition_seen = true;
459 $link = HTML::a(array('href' => "#footnote-$ftnt"), "[$ftnt]");
460 if (!$footnote_definition_seen) {
461 $name = "footrev-$ftnt-" . count($trfrm->user_data['footnotes'][$ftnt]);
462 $link->setAttr('name', $name);
463 $trfrm->user_data['footnotes'][$ftnt][] = $name;
465 return HTML::sup(array('class' => 'footnote'), $link);
468 function wtt_bracketlinks($match, &$trfrm)
471 if (preg_match('/^\[\s*\]$/', $match))
474 $link = LinkBracketLink($match);
476 if ($link->isInlineElement())
480 return new RawXml("</p>" . $link->asXML() . "<p>");
484 // replace all URL's with tokens, so we don't confuse them
485 // with Wiki words later. Wiki words in URL's break things.
486 // URLs preceeded by a '!' are not linked
487 function wtt_urls($match, &$trfrm) {
488 return $match[0] == "!"
493 // Link Wiki words (BumpyText)
494 // Wikiwords preceeded by a '!' are not linked
495 function wtt_bumpylinks($match, &$trfrm) {
496 return $match[0] == "!" ? substr($match,1) : LinkWikiWord($match);
500 // Just quote the token.
501 function wtt_quotetoken($match, &$trfrm) {
507 // end of tokenizer functions
508 //////////////////////////////////////////////////////////
511 //////////////////////////////////////////////////////////
512 // basic simple markup functions
514 // escape HTML metachars
515 function wtm_htmlchars($line, &$transformer) {
516 return XmlElement::_quote($line);
519 // %%% are linebreaks
520 function wtm_linebreak($line, &$transformer) {
521 return str_replace('%%%', '<br />', $line);
525 function wtm_bold_italics($line, &$transformer) {
526 $line = preg_replace('|(__)(.*?)(__)|', '<strong>\2</strong>', $line);
527 $line = preg_replace("|('')(.*?)('')|", '<em>\2</em>', $line);
533 //////////////////////////////////////////////////////////
534 // some tokens to be replaced by (dynamic) content
536 // FIXME: some plugins are in-line (maybe?) and some are block level.
537 // Here we treat them all as inline, which will probably
538 // generate some minorly invalid HTML in some cases.
540 function wtm_plugin_link($line, &$transformer) {
541 // FIXME: is this good syntax?
542 global $request; // FIXME: make these non-global?
544 if (preg_match('/^(.*?)(<\?plugin-link\s+.*?\?>)(.*)$/', $line, $m)) {
545 list(, $prematch, $plugin_pi, $postmatch) = $m;
546 $loader = new WikiPluginLoader;
547 $html = $loader->expandPI($plugin_pi, $request);
548 $line = $prematch . $transformer->token($html) . $postmatch;
553 function wtm_plugin($line, &$transformer) {
554 // FIXME: is this good syntax?
555 global $request; // FIXME: make these non-global?
557 if (preg_match('/^<\?plugin(-form)?\s.*\?>\s*$/', $line)) {
558 $loader = new WikiPluginLoader;
559 $html = $loader->expandPI($line, $request);
560 $line = $transformer->SetHTMLMode('', 0) . $transformer->token($html);
566 //////////////////////////////////////////////////////////
567 // mode markup functions
570 // tabless markup for unordered, ordered, and dictionary lists
571 // ul/ol list types can be mixed, so we only look at the last
572 // character. Changes e.g. from "**#*" to "###*" go unnoticed.
573 // and wouldn't make a difference to the HTML layout anyway.
575 // unordered lists <UL>: "*"
576 // has to be registereed before list OL
577 function wtm_list_ul($line, &$trfrm) {
578 if (preg_match("/^([#*;]*\*)[^#]/", $line, $matches)) {
579 $numtabs = strlen($matches[1]);
580 $line = preg_replace("/^([#*]*\*)/", '', $line);
581 $line = $trfrm->ListItem('ul', $numtabs) . $line;
586 // ordered lists <OL>: "#"
587 function wtm_list_ol($line, &$trfrm) {
588 if (preg_match("/^([#*;]*\#)/", $line, $matches)) {
589 $numtabs = strlen($matches[1]);
590 $line = preg_replace("/^([#*]*\#)/", "", $line);
591 $line = $trfrm->ListItem('ol', $numtabs) . $line;
597 // definition lists <DL>: ";text:text"
598 function wtm_list_dl($line, &$trfrm) {
599 if (preg_match("/^([#*;]*;)(.*?):(.*$)/", $line, $matches)) {
600 $numtabs = strlen($matches[1]);
601 $line = $trfrm->ListItem('dl', $numtabs, $matches[2]) . $matches[3];
606 // mode: preformatted text, i.e. <pre>
607 function wtm_preformatted($line, &$trfrm) {
608 if (preg_match("/^\s+/", $line)) {
609 $line = $trfrm->SetHTMLMode('pre') . $line;
614 // mode: headings, i.e. <h1>, <h2>, <h3>
615 // lines starting with !,!!,!!! are headings
616 // Patch from steph/tara <tellme@climbtothestars.org>:
617 // use <h2>, <h3>, <h4> since <h1> is page title.
618 function wtm_headings($line, &$trfrm) {
619 if (preg_match("/^(!{1,3})[^!]/", $line, $whichheading)) {
620 if($whichheading[1] == '!') $heading = 'h4';
621 elseif($whichheading[1] == '!!') $heading = 'h3';
622 elseif($whichheading[1] == '!!!') $heading = 'h2';
623 $line = preg_replace("/^!+/", '', $line);
624 $line = $trfrm->SetHTMLMode($heading) . $line;
630 function wtm_table($line, &$trfrm)
633 while (preg_match('/^(\|+)(v*)([<>^]?)([^|]*)/', $line, $m))
635 $line = substr($line, strlen($m[0]));
639 if (strlen($m[1]) > 1)
640 $td->setAttr('colspan', strlen($m[1]));
641 if (strlen($m[2]) > 0)
642 $td->setAttr('rowspan', strlen($m[2]) + 1);
645 $td->setAttr('align', 'center');
646 else if ($m[3] == '>')
647 $td->setAttr('align', 'right');
649 $td->setAttr('align', 'left');
651 // FIXME: this is a hack: can't tokenize whole <td></td> since we
652 // haven't marked up italics, etc... yet
653 $row .= $trfrm->rawtoken($td->startTag() . " ");
655 $row .= $trfrm->rawtoken(" " . $td->endTag());
657 assert(empty($line));
658 $row = $trfrm->rawtoken("<tr>") . $row . $trfrm->rawtoken("</tr>");
660 return $trfrm->SetHTMLMode(array('table',
661 array('cellpadding' => 1,
667 // four or more dashes to <hr>
668 // Note this is of type WT_MODE_MARKUP becuase <hr>'s aren't
669 // allowed within <p>'s. (e.g. "<p><hr></p>" is not valid HTML.)
670 function wtm_hr($line, &$trfrm) {
671 if (preg_match('/^-{4,}(.*)$/', $line, $m)) {
672 $line = $trfrm->SetHTMLMode('', 0) . '<hr />';
674 $line .= $trfrm->SetHTMLMode('p') . $m[1];
679 // default mode: simple text paragraph
680 function wtm_paragraph($line, &$trfrm) {
681 return $trfrm->SetHTMLMode('p') . $line;
684 // (c-file-style: "gnu")
689 // c-hanging-comment-ender-p: nil
690 // indent-tabs-mode: nil