OSDN Git Service

BugTrack/2198 Check FrontPage readable when plugin return empty body
[pukiwiki/pukiwiki.git] / lib / convert_html.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone
3 // convert_html.php
4 // Copyright
5 //   2002-2016 PukiWiki Development Team
6 //   2001-2002 Originally written by yu-ji
7 // License: GPL v2 or (at your option) any later version
8 //
9 // function 'convert_html()', wiki text parser
10 // and related classes-and-functions
11
12 function convert_html($lines)
13 {
14         global $vars, $digest;
15         static $contents_id = 0;
16
17         // Set digest
18         $digest = md5(join('', get_source($vars['page'])));
19
20         if (! is_array($lines)) $lines = explode("\n", $lines);
21
22         $body = new Body(++$contents_id);
23         $body->parse($lines);
24
25         return $body->toString();
26 }
27
28 // Block elements
29 class Element
30 {
31         var $parent;
32         var $elements; // References of childs
33         var $last;     // Insert new one at the back of the $last
34
35         function Element()
36         {
37                 $this->__construct();
38         }
39         function __construct()
40         {
41                 $this->elements = array();
42                 $this->last     = & $this;
43         }
44
45         function setParent(& $parent)
46         {
47                 $this->parent = & $parent;
48         }
49
50         function & add(& $obj)
51         {
52                 if ($this->canContain($obj)) {
53                         return $this->insert($obj);
54                 } else {
55                         return $this->parent->add($obj);
56                 }
57         }
58
59         function & insert(& $obj)
60         {
61                 $obj->setParent($this);
62                 $this->elements[] = & $obj;
63
64                 return $this->last = & $obj->last;
65         }
66
67         function canContain($obj)
68         {
69                 return TRUE;
70         }
71
72         function wrap($string, $tag, $param = '', $canomit = TRUE)
73         {
74                 return ($canomit && $string == '') ? '' :
75                         '<' . $tag . $param . '>' . $string . '</' . $tag . '>';
76         }
77
78         function toString()
79         {
80                 $ret = array();
81                 foreach (array_keys($this->elements) as $key)
82                         $ret[] = $this->elements[$key]->toString();
83                 return join("\n", $ret);
84         }
85
86         function dump($indent = 0)
87         {
88                 $ret = str_repeat(' ', $indent) . get_class($this) . "\n";
89                 $indent += 2;
90                 foreach (array_keys($this->elements) as $key) {
91                         $ret .= is_object($this->elements[$key]) ?
92                                 $this->elements[$key]->dump($indent) : '';
93                                 //str_repeat(' ', $indent) . $this->elements[$key];
94                 }
95                 return $ret;
96         }
97 }
98
99 // Returns inline-related object
100 function & Factory_Inline($text)
101 {
102         // Check the first letter of the line
103         if (substr($text, 0, 1) == '~') {
104                 return new Paragraph(' ' . substr($text, 1));
105         } else {
106                 return new Inline($text);
107         }
108 }
109
110 function & Factory_DList(& $root, $text)
111 {
112         $out = explode('|', ltrim($text), 2);
113         if (count($out) < 2) {
114                 return Factory_Inline($text);
115         } else {
116                 return new DList($out);
117         }
118 }
119
120 // '|'-separated table
121 function & Factory_Table(& $root, $text)
122 {
123         if (! preg_match('/^\|(.+)\|([hHfFcC]?)$/', $text, $out)) {
124                 return Factory_Inline($text);
125         } else {
126                 return new Table($out);
127         }
128 }
129
130 // Comma-separated table
131 function & Factory_YTable(& $root, $text)
132 {
133         if ($text == ',') {
134                 return Factory_Inline($text);
135         } else {
136                 return new YTable(csv_explode(',', substr($text, 1)));
137         }
138 }
139
140 function & Factory_Div(& $root, $text)
141 {
142         $matches = array();
143
144         // Seems block plugin?
145         if (PKWKEXP_DISABLE_MULTILINE_PLUGIN_HACK) {
146                 // Usual code
147                 if (preg_match('/^\#([^\(]+)(?:\((.*)\))?/', $text, $matches) &&
148                     exist_plugin_convert($matches[1])) {
149                         return new Div($matches);
150                 }
151         } else {
152                 // Hack code
153                 if(preg_match('/^#([^\(\{]+)(?:\(([^\r]*)\))?(\{*)/', $text, $matches) &&
154                    exist_plugin_convert($matches[1])) {
155                         $len  = strlen($matches[3]);
156                         $body = array();
157                         if ($len == 0) {
158                                 return new Div($matches); // Seems legacy block plugin
159                         } else if (preg_match('/\{{' . $len . '}\s*\r(.*)\r\}{' . $len . '}/', $text, $body)) { 
160                                 $matches[2] .= "\r" . $body[1] . "\r";
161                                 return new Div($matches); // Seems multiline-enabled block plugin
162                         }
163                 }
164         }
165
166         return new Paragraph($text);
167 }
168
169 // Inline elements
170 class Inline extends Element
171 {
172         function Inline($text)
173         {
174                 $this->__construct($text);
175         }
176         function __construct($text)
177         {
178                 parent::__construct();
179                 $this->elements[] = trim((substr($text, 0, 1) == "\n") ?
180                         $text : make_link($text));
181         }
182
183         function & insert(& $obj)
184         {
185                 $this->elements[] = $obj->elements[0];
186                 return $this;
187         }
188
189         function canContain($obj)
190         {
191                 return is_a($obj, 'Inline');
192         }
193
194         function toString()
195         {
196                 global $line_break;
197                 return join(($line_break ? '<br />' . "\n" : "\n"), $this->elements);
198         }
199
200         function & toPara($class = '')
201         {
202                 $obj = new Paragraph('', $class);
203                 $obj->insert($this);
204                 return $obj;
205         }
206 }
207
208 // Paragraph: blank-line-separated sentences
209 class Paragraph extends Element
210 {
211         var $param;
212
213         function Paragraph($text, $param = '')
214         {
215                 $this->__construct($text, $param);
216         }
217         function __construct($text, $param = '')
218         {
219                 parent::__construct();
220                 $this->param = $param;
221                 if ($text == '') return;
222
223                 if (substr($text, 0, 1) == '~')
224                         $text = ' ' . substr($text, 1);
225
226                 $this->insert(Factory_Inline($text));
227         }
228
229         function canContain($obj)
230         {
231                 return is_a($obj, 'Inline');
232         }
233
234         function toString()
235         {
236                 return $this->wrap(parent::toString(), 'p', $this->param);
237         }
238 }
239
240 // * Heading1
241 // ** Heading2
242 // *** Heading3
243 class Heading extends Element
244 {
245         var $level;
246         var $id;
247         var $msg_top;
248
249         function Heading(& $root, $text)
250         {
251                 $this->__construct($root, $text);
252         }
253         function __construct(& $root, $text)
254         {
255                 parent::__construct();
256
257                 $this->level = min(3, strspn($text, '*'));
258                 list($text, $this->msg_top, $this->id) = $root->getAnchor($text, $this->level);
259                 $this->insert(Factory_Inline($text));
260                 $this->level++; // h2,h3,h4
261         }
262
263         function & insert(& $obj)
264         {
265                 parent::insert($obj);
266                 return $this->last = & $this;
267         }
268
269         function canContain(& $obj)
270         {
271                 return FALSE;
272         }
273
274         function toString()
275         {
276                 return $this->msg_top .  $this->wrap(parent::toString(),
277                         'h' . $this->level, ' id="' . $this->id . '"');
278         }
279 }
280
281 // ----
282 // Horizontal Rule
283 class HRule extends Element
284 {
285         function HRule(& $root, $text)
286         {
287                 $this->__construct($root, $text);
288         }
289         function __construct(& $root, $text)
290         {
291                 parent::__construct();
292         }
293
294         function canContain(& $obj)
295         {
296                 return FALSE;
297         }
298
299         function toString()
300         {
301                 global $hr;
302                 return $hr;
303         }
304 }
305
306 // Lists (UL, OL, DL)
307 class ListContainer extends Element
308 {
309         var $tag;
310         var $tag2;
311         var $level;
312         var $style;
313
314         function ListContainer($tag, $tag2, $head, $text)
315         {
316                 $this->__construct($tag, $tag2, $head, $text);
317         }
318         function __construct($tag, $tag2, $head, $text)
319         {
320                 parent::__construct();
321
322                 $this->tag   = $tag;
323                 $this->tag2  = $tag2;
324                 $this->level = min(3, strspn($text, $head));
325                 $text = ltrim(substr($text, $this->level));
326
327                 parent::insert(new ListElement($this->level, $tag2));
328                 if ($text != '')
329                         $this->last = & $this->last->insert(Factory_Inline($text));
330         }
331
332         function canContain(& $obj)
333         {
334                 return (! is_a($obj, 'ListContainer')
335                         || ($this->tag == $obj->tag && $this->level == $obj->level));
336         }
337
338         function setParent(& $parent)
339         {
340                 parent::setParent($parent);
341
342                 $step = $this->level;
343                 if (isset($parent->parent) && is_a($parent->parent, 'ListContainer'))
344                         $step -= $parent->parent->level;
345
346                 $this->style = sprintf(pkwk_list_attrs_template(), $this->level, $step);
347         }
348
349         function & insert(& $obj)
350         {
351                 if (! is_a($obj, get_class($this)))
352                         return $this->last = & $this->last->insert($obj);
353
354                 // Break if no elements found (BugTrack/524)
355                 if (count($obj->elements) == 1 && empty($obj->elements[0]->elements))
356                         return $this->last->parent; // up to ListElement
357
358                 // Move elements
359                 foreach(array_keys($obj->elements) as $key)
360                         parent::insert($obj->elements[$key]);
361
362                 return $this->last;
363         }
364
365         function toString()
366         {
367                 return $this->wrap(parent::toString(), $this->tag, $this->style);
368         }
369 }
370
371 class ListElement extends Element
372 {
373         function ListElement($level, $head)
374         {
375                 $this->__construct($level, $head);
376         }
377         function __construct($level, $head)
378         {
379                 parent::__construct();
380                 $this->level = $level;
381                 $this->head  = $head;
382         }
383
384         function canContain(& $obj)
385         {
386                 return (! is_a($obj, 'ListContainer') || ($obj->level > $this->level));
387         }
388
389         function toString()
390         {
391                 return $this->wrap(parent::toString(), $this->head);
392         }
393 }
394
395 // - One
396 // - Two
397 // - Three
398 class UList extends ListContainer
399 {
400         function UList(& $root, $text)
401         {
402                 $this->__construct($root, $text);
403         }
404         function __construct(& $root, $text)
405         {
406                 parent::__construct('ul', 'li', '-', $text);
407         }
408 }
409
410 // + One
411 // + Two
412 // + Three
413 class OList extends ListContainer
414 {
415         function OList(& $root, $text)
416         {
417                 $this->__construct($root, $text);
418         }
419         function __construct(& $root, $text)
420         {
421                 parent::__construct('ol', 'li', '+', $text);
422         }
423 }
424
425 // : definition1 | description1
426 // : definition2 | description2
427 // : definition3 | description3
428 class DList extends ListContainer
429 {
430         function DList($out)
431         {
432                 $this->__construct($out);
433         }
434         function __construct($out)
435         {
436                 parent::__construct('dl', 'dt', ':', $out[0]);
437                 $this->last = & Element::insert(new ListElement($this->level, 'dd'));
438                 if ($out[1] != '')
439                         $this->last = & $this->last->insert(Factory_Inline($out[1]));
440         }
441 }
442
443 // > Someting cited
444 // > like E-mail text
445 class BQuote extends Element
446 {
447         var $level;
448
449         function BQuote(& $root, $text)
450         {
451                 $this->__construct($root, $text);
452         }
453         function __construct(& $root, $text)
454         {
455                 parent::__construct();
456
457                 $head = substr($text, 0, 1);
458                 $this->level = min(3, strspn($text, $head));
459                 $text = ltrim(substr($text, $this->level));
460
461                 if ($head == '<') { // Blockquote close
462                         $level       = $this->level;
463                         $this->level = 0;
464                         $this->last  = & $this->end($root, $level);
465                         if ($text != '')
466                                 $this->last = & $this->last->insert(Factory_Inline($text));
467                 } else {
468                         $this->insert(Factory_Inline($text));
469                 }
470         }
471
472         function canContain(& $obj)
473         {
474                 return (! is_a($obj, get_class($this)) || $obj->level >= $this->level);
475         }
476
477         function & insert(& $obj)
478         {
479                 // BugTrack/521, BugTrack/545
480                 if (is_a($obj, 'inline'))
481                         return parent::insert($obj->toPara(' class="quotation"'));
482
483                 if (is_a($obj, 'BQuote') && $obj->level == $this->level && count($obj->elements)) {
484                         $obj = & $obj->elements[0];
485                         if (is_a($this->last, 'Paragraph') && count($obj->elements))
486                                 $obj = & $obj->elements[0];
487                 }
488                 return parent::insert($obj);
489         }
490
491         function toString()
492         {
493                 return $this->wrap(parent::toString(), 'blockquote');
494         }
495
496         function & end(& $root, $level)
497         {
498                 $parent = & $root->last;
499
500                 while (is_object($parent)) {
501                         if (is_a($parent, 'BQuote') && $parent->level == $level)
502                                 return $parent->parent;
503                         $parent = & $parent->parent;
504                 }
505                 return $this;
506         }
507 }
508
509 class TableCell extends Element
510 {
511         var $tag = 'td'; // {td|th}
512         var $colspan = 1;
513         var $rowspan = 1;
514         var $style; // is array('width'=>, 'align'=>...);
515
516         function TableCell($text, $is_template = FALSE)
517         {
518                 $this->__construct($text, $is_template);
519         }
520         function __construct($text, $is_template = FALSE)
521         {
522                 parent::__construct();
523                 $this->style = $matches = array();
524
525                 while (preg_match('/^(?:(LEFT|CENTER|RIGHT)|(BG)?COLOR\(([#\w]+)\)|SIZE\((\d+)\)):(.*)$/',
526                     $text, $matches)) {
527                         if ($matches[1]) {
528                                 $this->style['align'] = 'text-align:' . strtolower($matches[1]) . ';';
529                                 $text = $matches[5];
530                         } else if ($matches[3]) {
531                                 $name = $matches[2] ? 'background-color' : 'color';
532                                 $this->style[$name] = $name . ':' . htmlsc($matches[3]) . ';';
533                                 $text = $matches[5];
534                         } else if ($matches[4]) {
535                                 $this->style['size'] = 'font-size:' . htmlsc($matches[4]) . 'px;';
536                                 $text = $matches[5];
537                         }
538                 }
539                 if ($is_template && is_numeric($text))
540                         $this->style['width'] = 'width:' . $text . 'px;';
541
542                 if ($text == '>') {
543                         $this->colspan = 0;
544                 } else if ($text == '~') {
545                         $this->rowspan = 0;
546                 } else if (substr($text, 0, 1) == '~') {
547                         $this->tag = 'th';
548                         $text      = substr($text, 1);
549                 }
550
551                 if ($text != '' && $text{0} == '#') {
552                         // Try using Div class for this $text
553                         $obj = & Factory_Div($this, $text);
554                         if (is_a($obj, 'Paragraph'))
555                                 $obj = & $obj->elements[0];
556                 } else {
557                         $obj = & Factory_Inline($text);
558                 }
559
560                 $this->insert($obj);
561         }
562
563         function setStyle(& $style)
564         {
565                 foreach ($style as $key=>$value)
566                         if (! isset($this->style[$key]))
567                                 $this->style[$key] = $value;
568         }
569
570         function toString()
571         {
572                 if ($this->rowspan == 0 || $this->colspan == 0) return '';
573
574                 $param = ' class="style_' . $this->tag . '"';
575                 if ($this->rowspan > 1)
576                         $param .= ' rowspan="' . $this->rowspan . '"';
577                 if ($this->colspan > 1) {
578                         $param .= ' colspan="' . $this->colspan . '"';
579                         unset($this->style['width']);
580                 }
581                 if (! empty($this->style))
582                         $param .= ' style="' . join(' ', $this->style) . '"';
583
584                 return $this->wrap(parent::toString(), $this->tag, $param, FALSE);
585         }
586 }
587
588 // | title1 | title2 | title3 |
589 // | cell1  | cell2  | cell3  |
590 // | cell4  | cell5  | cell6  |
591 class Table extends Element
592 {
593         var $type;
594         var $types;
595         var $col; // number of column
596
597         function Table($out)
598         {
599                 $this->__construct($out);
600         }
601         function __construct($out)
602         {
603                 parent::__construct();
604
605                 $cells       = explode('|', $out[1]);
606                 $this->col   = count($cells);
607                 $this->type  = strtolower($out[2]);
608                 $this->types = array($this->type);
609                 $is_template = ($this->type == 'c');
610                 $row = array();
611                 foreach ($cells as $cell)
612                         $row[] = new TableCell($cell, $is_template);
613                 $this->elements[] = $row;
614         }
615
616         function canContain(& $obj)
617         {
618                 return is_a($obj, 'Table') && ($obj->col == $this->col);
619         }
620
621         function & insert(& $obj)
622         {
623                 $this->elements[] = $obj->elements[0];
624                 $this->types[]    = $obj->type;
625                 return $this;
626         }
627
628         function toString()
629         {
630                 static $parts = array('h'=>'thead', 'f'=>'tfoot', ''=>'tbody');
631
632                 // Set rowspan (from bottom, to top)
633                 for ($ncol = 0; $ncol < $this->col; $ncol++) {
634                         $rowspan = 1;
635                         foreach (array_reverse(array_keys($this->elements)) as $nrow) {
636                                 $row = & $this->elements[$nrow];
637                                 if ($row[$ncol]->rowspan == 0) {
638                                         ++$rowspan;
639                                         continue;
640                                 }
641                                 $row[$ncol]->rowspan = $rowspan;
642                                 // Inherits row type
643                                 while (--$rowspan)
644                                         $this->types[$nrow + $rowspan] = $this->types[$nrow];
645                                 $rowspan = 1;
646                         }
647                 }
648
649                 // Set colspan and style
650                 $stylerow = NULL;
651                 foreach (array_keys($this->elements) as $nrow) {
652                         $row = & $this->elements[$nrow];
653                         if ($this->types[$nrow] == 'c')
654                                 $stylerow = & $row;
655                         $colspan = 1;
656                         foreach (array_keys($row) as $ncol) {
657                                 if ($row[$ncol]->colspan == 0) {
658                                         ++$colspan;
659                                         continue;
660                                 }
661                                 $row[$ncol]->colspan = $colspan;
662                                 if ($stylerow !== NULL) {
663                                         $row[$ncol]->setStyle($stylerow[$ncol]->style);
664                                         // Inherits column style
665                                         while (--$colspan)
666                                                 $row[$ncol - $colspan]->setStyle($stylerow[$ncol]->style);
667                                 }
668                                 $colspan = 1;
669                         }
670                 }
671
672                 // toString
673                 $string = '';
674                 foreach ($parts as $type => $part)
675                 {
676                         $part_string = '';
677                         foreach (array_keys($this->elements) as $nrow) {
678                                 if ($this->types[$nrow] != $type)
679                                         continue;
680                                 $row        = & $this->elements[$nrow];
681                                 $row_string = '';
682                                 foreach (array_keys($row) as $ncol)
683                                         $row_string .= $row[$ncol]->toString();
684                                 $part_string .= $this->wrap($row_string, 'tr') . "\n";
685                         }
686                         $string .= $this->wrap($part_string, $part);
687                 }
688                 $string = $this->wrap($string, 'table', ' class="style_table" cellspacing="1" border="0"');
689
690                 return $this->wrap($string, 'div', ' class="ie5"');
691         }
692 }
693
694 // , cell1  , cell2  ,  cell3 
695 // , cell4  , cell5  ,  cell6 
696 // , cell7  ,        right,==
697 // ,left          ,==,  cell8
698 class YTable extends Element
699 {
700         var $col;       // Number of columns
701
702         function YTable($row = array('cell1 ', ' cell2 ', ' cell3'))
703         {
704                 $this->__construct($row);
705         }
706         // TODO: Seems unable to show literal '==' without tricks.
707         //       But it will be imcompatible.
708         // TODO: Why toString() or toXHTML() here
709         function __construct($row = array('cell1 ', ' cell2 ', ' cell3'))
710         {
711                 parent::__construct();
712
713                 $str = array();
714                 $col = count($row);
715
716                 $matches = $_value = $_align = array();
717                 foreach($row as $cell) {
718                         if (preg_match('/^(\s+)?(.+?)(\s+)?$/', $cell, $matches)) {
719                                 if ($matches[2] == '==') {
720                                         // Colspan
721                                         $_value[] = FALSE;
722                                         $_align[] = FALSE;
723                                 } else {
724                                         $_value[] = $matches[2];
725                                         if ($matches[1] == '') {
726                                                 $_align[] = ''; // left
727                                         } else if (isset($matches[3])) {
728                                                 $_align[] = 'center';
729                                         } else {
730                                                 $_align[] = 'right';
731                                         }
732                                 }
733                         } else {
734                                 $_value[] = $cell;
735                                 $_align[] = '';
736                         }
737                 }
738
739                 for ($i = 0; $i < $col; $i++) {
740                         if ($_value[$i] === FALSE) continue;
741                         $colspan = 1;
742                         while (isset($_value[$i + $colspan]) && $_value[$i + $colspan] === FALSE) ++$colspan;
743                         $colspan = ($colspan > 1) ? ' colspan="' . $colspan . '"' : '';
744                         $align = $_align[$i] ? ' style="text-align:' . $_align[$i] . '"' : '';
745                         $str[] = '<td class="style_td"' . $align . $colspan . '>';
746                         $str[] = make_link($_value[$i]);
747                         $str[] = '</td>';
748                         unset($_value[$i], $_align[$i]);
749                 }
750
751                 $this->col        = $col;
752                 $this->elements[] = implode('', $str);
753         }
754
755         function canContain(& $obj)
756         {
757                 return is_a($obj, 'YTable') && ($obj->col == $this->col);
758         }
759
760         function & insert(& $obj)
761         {
762                 $this->elements[] = $obj->elements[0];
763                 return $this;
764         }
765
766         function toString()
767         {
768                 $rows = '';
769                 foreach ($this->elements as $str) {
770                         $rows .= "\n" . '<tr class="style_tr">' . $str . '</tr>' . "\n";
771                 }
772                 $rows = $this->wrap($rows, 'table', ' class="style_table" cellspacing="1" border="0"');
773                 return $this->wrap($rows, 'div', ' class="ie5"');
774         }
775 }
776
777 // ' 'Space-beginning sentence
778 // ' 'Space-beginning sentence
779 // ' 'Space-beginning sentence
780 class Pre extends Element
781 {
782         function Pre(& $root, $text)
783         {
784                 $this->__construct($root, $text);
785         }
786         function __construct(& $root, $text)
787         {
788                 global $preformat_ltrim;
789                 parent::__construct();
790                 $this->elements[] = htmlsc(
791                         (! $preformat_ltrim || $text == '' || $text{0} != ' ') ? $text : substr($text, 1));
792         }
793
794         function canContain(& $obj)
795         {
796                 return is_a($obj, 'Pre');
797         }
798
799         function & insert(& $obj)
800         {
801                 $this->elements[] = $obj->elements[0];
802                 return $this;
803         }
804
805         function toString()
806         {
807                 return $this->wrap(join("\n", $this->elements), 'pre');
808         }
809 }
810
811 // Block plugin: #something (started with '#')
812 class Div extends Element
813 {
814         var $name;
815         var $param;
816
817         function Div($out)
818         {
819                 $this->__construct($out);
820         }
821         function __construct($out)
822         {
823                 parent::__construct();
824                 list(, $this->name, $this->param) = array_pad($out, 3, '');
825         }
826
827         function canContain(& $obj)
828         {
829                 return FALSE;
830         }
831
832         function toString()
833         {
834                 // Call #plugin
835                 return do_plugin_convert($this->name, $this->param);
836         }
837 }
838
839 // LEFT:/CENTER:/RIGHT:
840 class Align extends Element
841 {
842         var $align;
843
844         function Align($align)
845         {
846                 $this->__construct($align);
847         }
848         function __construct($align)
849         {
850                 parent::__construct();
851                 $this->align = $align;
852         }
853
854         function canContain(& $obj)
855         {
856                 return is_a($obj, 'Inline');
857         }
858
859         function toString()
860         {
861                 return $this->wrap(parent::toString(), 'div', ' style="text-align:' . $this->align . '"');
862         }
863 }
864
865 // Body
866 class Body extends Element
867 {
868         var $id;
869         var $count = 0;
870         var $contents;
871         var $contents_last;
872         var $classes = array(
873                 '-' => 'UList',
874                 '+' => 'OList',
875                 '>' => 'BQuote',
876                 '<' => 'BQuote');
877         var $factories = array(
878                 ':' => 'DList',
879                 '|' => 'Table',
880                 ',' => 'YTable',
881                 '#' => 'Div');
882
883         function Body($id)
884         {
885                 $this->__construct($id);
886         }
887         function __construct($id)
888         {
889                 $this->id            = $id;
890                 $this->contents      = new Element();
891                 $this->contents_last = & $this->contents;
892                 parent::__construct();
893         }
894
895         function parse(& $lines)
896         {
897                 $this->last = & $this;
898                 $matches = array();
899
900                 while (! empty($lines)) {
901                         $line = array_shift($lines);
902
903                         // Escape comments
904                         if (substr($line, 0, 2) == '//') continue;
905
906                         if (preg_match('/^(LEFT|CENTER|RIGHT):(.*)$/', $line, $matches)) {
907                                 // <div style="text-align:...">
908                                 $this->last = & $this->last->add(new Align(strtolower($matches[1])));
909                                 if ($matches[2] == '') continue;
910                                 $line = $matches[2];
911                         }
912
913                         $line = rtrim($line, "\r\n");
914
915                         // Empty
916                         if ($line == '') {
917                                 $this->last = & $this;
918                                 continue;
919                         }
920
921                         // Horizontal Rule
922                         if (substr($line, 0, 4) == '----') {
923                                 $this->insert(new HRule($this, $line));
924                                 continue;
925                         }
926
927                         // Multiline-enabled block plugin
928                         if (! PKWKEXP_DISABLE_MULTILINE_PLUGIN_HACK &&
929                             preg_match('/^#[^{]+(\{\{+)\s*$/', $line, $matches)) {
930                                 $len = strlen($matches[1]);
931                                 $line .= "\r"; // Delimiter
932                                 while (! empty($lines)) {
933                                         $next_line = preg_replace("/[\r\n]*$/", '', array_shift($lines));
934                                         if (preg_match('/\}{' . $len . '}/', $next_line)) {
935                                                 $line .= $next_line;
936                                                 break;
937                                         } else {
938                                                 $line .= $next_line .= "\r"; // Delimiter
939                                         }
940                                 }
941                         }
942
943                         // The first character
944                         $head = $line{0};
945
946                         // Heading
947                         if ($head == '*') {
948                                 $this->insert(new Heading($this, $line));
949                                 continue;
950                         }
951
952                         // Pre
953                         if ($head == ' ' || $head == "\t") {
954                                 $this->last = & $this->last->add(new Pre($this, $line));
955                                 continue;
956                         }
957
958                         // Line Break
959                         if (substr($line, -1) == '~')
960                                 $line = substr($line, 0, -1) . "\r";
961                         
962                         // Other Character
963                         if (isset($this->classes[$head])) {
964                                 $classname  = $this->classes[$head];
965                                 $this->last = & $this->last->add(new $classname($this, $line));
966                                 continue;
967                         }
968
969                         // Other Character
970                         if (isset($this->factories[$head])) {
971                                 $factoryname = 'Factory_' . $this->factories[$head];
972                                 $this->last  = & $this->last->add($factoryname($this, $line));
973                                 continue;
974                         }
975
976                         // Default
977                         $this->last = & $this->last->add(Factory_Inline($line));
978                 }
979         }
980
981         function getAnchor($text, $level)
982         {
983                 global $top, $_symbol_anchor;
984
985                 // Heading id (auto-generated)
986                 $autoid = 'content_' . $this->id . '_' . $this->count;
987                 $this->count++;
988
989                 // Heading id (specified by users)
990                 $id = make_heading($text, FALSE); // Cut fixed-anchor from $text
991                 if ($id == '') {
992                         // Not specified
993                         $id     = & $autoid;
994                         $anchor = '';
995                 } else {
996                         $anchor = ' &aname(' . $id . ',super,full){' . $_symbol_anchor . '};';
997                 }
998
999                 $text = ' ' . $text;
1000
1001                 // Add 'page contents' link to its heading
1002                 $this->contents_last = & $this->contents_last->add(new Contents_UList($text, $level, $id));
1003
1004                 // Add heding
1005                 return array($text . $anchor, $this->count > 1 ? "\n" . $top : '', $autoid);
1006         }
1007
1008         function & insert(& $obj)
1009         {
1010                 if (is_a($obj, 'Inline')) $obj = & $obj->toPara();
1011                 return parent::insert($obj);
1012         }
1013
1014         function toString()
1015         {
1016                 global $vars;
1017
1018                 $text = parent::toString();
1019
1020                 // #contents
1021                 $text = preg_replace_callback('/<#_contents_>/',
1022                         array(& $this, 'replace_contents'), $text);
1023
1024                 return $text . "\n";
1025         }
1026
1027         function replace_contents($arr)
1028         {
1029                 $contents  = '<div class="contents">' . "\n" .
1030                                 '<a id="contents_' . $this->id . '"></a>' . "\n" .
1031                                 $this->contents->toString() . "\n" .
1032                                 '</div>' . "\n";
1033                 return $contents;
1034         }
1035 }
1036
1037 class Contents_UList extends ListContainer
1038 {
1039         function Contents_UList($text, $level, $id)
1040         {
1041                 $this->__construct($text, $level, $id);
1042         }
1043         function __construct($text, $level, $id)
1044         {
1045                 // Reformatting $text
1046                 // A line started with "\n" means "preformatted" ... X(
1047                 make_heading($text);
1048                 $text = "\n" . '<a href="#' . $id . '">' . $text . '</a>' . "\n";
1049                 parent::__construct('ul', 'li', '-', str_repeat('-', $level));
1050                 $this->insert(Factory_Inline($text));
1051         }
1052
1053         function setParent(& $parent)
1054         {
1055                 parent::setParent($parent);
1056                 $step   = $this->level;
1057                 if (isset($parent->parent) && is_a($parent->parent, 'ListContainer')) {
1058                         $step  -= $parent->parent->level;
1059                 }
1060                 $indent_level = ($step == $this->level ? 1 : $step);
1061                 $this->style = sprintf(pkwk_list_attrs_template(), $this->level, $indent_level);
1062         }
1063 }