OSDN Git Service

BugTrack/2561 Simpify English words - Page list / Page file list
[pukiwiki/pukiwiki.git] / lib / convert_html.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone
3 // convert_html.php
4 // Copyright
5 //   2002-2022 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{1,20})\)|SIZE\((\d{1,2})\)|(BOLD)):(.*)$/',
526                     $text, $matches)) {
527                         if ($matches[1]) {
528                                 $this->style['align'] = 'text-align:' . strtolower($matches[1]) . ';';
529                                 $text = $matches[6];
530                         } else if ($matches[3]) {
531                                 $name = $matches[2] ? 'background-color' : 'color';
532                                 $this->style[$name] = $name . ':' . htmlsc($matches[3]) . ';';
533                                 $text = $matches[6];
534                         } else if (is_numeric($matches[4])) {
535                                 $this->style['size'] = 'font-size:' . htmlsc($matches[4]) . 'px;';
536                                 $text = $matches[6];
537                         } else if ($matches[5]) {
538                                 $this->style['bold'] = 'font-weight:bold;';
539                                 $text = $matches[6];
540                         }
541                 }
542                 if ($is_template && is_numeric($text))
543                         $this->style['width'] = 'width:' . $text . 'px;';
544
545                 if ($text == '>') {
546                         $this->colspan = 0;
547                 } else if ($text == '~') {
548                         $this->rowspan = 0;
549                 } else if (substr($text, 0, 1) == '~') {
550                         $this->tag = 'th';
551                         $text      = substr($text, 1);
552                 }
553
554                 if ($text != '' && $text[0] == '#') {
555                         // Try using Div class for this $text
556                         $obj = & Factory_Div($this, $text);
557                         if (is_a($obj, 'Paragraph'))
558                                 $obj = & $obj->elements[0];
559                 } else {
560                         $obj = & Factory_Inline($text);
561                 }
562
563                 $this->insert($obj);
564         }
565
566         function setStyle(& $style)
567         {
568                 foreach ($style as $key=>$value)
569                         if (! isset($this->style[$key]))
570                                 $this->style[$key] = $value;
571         }
572
573         function toString()
574         {
575                 if ($this->rowspan == 0 || $this->colspan == 0) return '';
576
577                 $param = ' class="style_' . $this->tag . '"';
578                 if ($this->rowspan > 1)
579                         $param .= ' rowspan="' . $this->rowspan . '"';
580                 if ($this->colspan > 1) {
581                         $param .= ' colspan="' . $this->colspan . '"';
582                         unset($this->style['width']);
583                 }
584                 if (! empty($this->style))
585                         $param .= ' style="' . join(' ', $this->style) . '"';
586
587                 return $this->wrap(parent::toString(), $this->tag, $param, FALSE);
588         }
589 }
590
591 // | title1 | title2 | title3 |
592 // | cell1  | cell2  | cell3  |
593 // | cell4  | cell5  | cell6  |
594 class Table extends Element
595 {
596         var $type;
597         var $types;
598         var $col; // number of column
599
600         function Table($out)
601         {
602                 $this->__construct($out);
603         }
604         function __construct($out)
605         {
606                 parent::__construct();
607
608                 $cells       = explode('|', $out[1]);
609                 $this->col   = count($cells);
610                 $this->type  = strtolower($out[2]);
611                 $this->types = array($this->type);
612                 $is_template = ($this->type == 'c');
613                 $row = array();
614                 foreach ($cells as $cell)
615                         $row[] = new TableCell($cell, $is_template);
616                 $this->elements[] = $row;
617         }
618
619         function canContain(& $obj)
620         {
621                 return is_a($obj, 'Table') && ($obj->col == $this->col);
622         }
623
624         function & insert(& $obj)
625         {
626                 $this->elements[] = $obj->elements[0];
627                 $this->types[]    = $obj->type;
628                 return $this;
629         }
630
631         function toString()
632         {
633                 static $parts = array('h'=>'thead', 'f'=>'tfoot', ''=>'tbody');
634
635                 // Set rowspan (from bottom, to top)
636                 for ($ncol = 0; $ncol < $this->col; $ncol++) {
637                         $rowspan = 1;
638                         foreach (array_reverse(array_keys($this->elements)) as $nrow) {
639                                 $row = & $this->elements[$nrow];
640                                 if ($row[$ncol]->rowspan == 0) {
641                                         ++$rowspan;
642                                         continue;
643                                 }
644                                 $row[$ncol]->rowspan = $rowspan;
645                                 // Inherits row type
646                                 while (--$rowspan)
647                                         $this->types[$nrow + $rowspan] = $this->types[$nrow];
648                                 $rowspan = 1;
649                         }
650                 }
651
652                 // Set colspan and style
653                 $stylerow = NULL;
654                 foreach (array_keys($this->elements) as $nrow) {
655                         $row = & $this->elements[$nrow];
656                         if ($this->types[$nrow] == 'c')
657                                 $stylerow = & $row;
658                         $colspan = 1;
659                         foreach (array_keys($row) as $ncol) {
660                                 if ($row[$ncol]->colspan == 0) {
661                                         ++$colspan;
662                                         continue;
663                                 }
664                                 $row[$ncol]->colspan = $colspan;
665                                 if ($stylerow !== NULL) {
666                                         $row[$ncol]->setStyle($stylerow[$ncol]->style);
667                                         // Inherits column style
668                                         while (--$colspan)
669                                                 $row[$ncol - $colspan]->setStyle($stylerow[$ncol]->style);
670                                 }
671                                 $colspan = 1;
672                         }
673                 }
674
675                 // toString
676                 $string = '';
677                 foreach ($parts as $type => $part)
678                 {
679                         $part_string = '';
680                         foreach (array_keys($this->elements) as $nrow) {
681                                 if ($this->types[$nrow] != $type)
682                                         continue;
683                                 $row        = & $this->elements[$nrow];
684                                 $row_string = '';
685                                 foreach (array_keys($row) as $ncol)
686                                         $row_string .= $row[$ncol]->toString();
687                                 $part_string .= $this->wrap($row_string, 'tr') . "\n";
688                         }
689                         $string .= $this->wrap($part_string, $part);
690                 }
691                 $string = $this->wrap($string, 'table', ' class="style_table" cellspacing="1" border="0"');
692
693                 return $this->wrap($string, 'div', ' class="ie5"');
694         }
695 }
696
697 // , cell1  , cell2  ,  cell3 
698 // , cell4  , cell5  ,  cell6 
699 // , cell7  ,        right,==
700 // ,left          ,==,  cell8
701 class YTable extends Element
702 {
703         var $col;       // Number of columns
704
705         function YTable($row = array('cell1 ', ' cell2 ', ' cell3'))
706         {
707                 $this->__construct($row);
708         }
709         // TODO: Seems unable to show literal '==' without tricks.
710         //       But it will be imcompatible.
711         // TODO: Why toString() or toXHTML() here
712         function __construct($row = array('cell1 ', ' cell2 ', ' cell3'))
713         {
714                 parent::__construct();
715
716                 $str = array();
717                 $col = count($row);
718
719                 $matches = $_value = $_align = array();
720                 foreach($row as $cell) {
721                         if (preg_match('/^(\s+)?(.+?)(\s+)?$/', $cell, $matches)) {
722                                 if ($matches[2] == '==') {
723                                         // Colspan
724                                         $_value[] = FALSE;
725                                         $_align[] = FALSE;
726                                 } else {
727                                         $_value[] = $matches[2];
728                                         if ($matches[1] == '') {
729                                                 $_align[] = ''; // left
730                                         } else if (isset($matches[3])) {
731                                                 $_align[] = 'center';
732                                         } else {
733                                                 $_align[] = 'right';
734                                         }
735                                 }
736                         } else {
737                                 $_value[] = $cell;
738                                 $_align[] = '';
739                         }
740                 }
741
742                 for ($i = 0; $i < $col; $i++) {
743                         if ($_value[$i] === FALSE) continue;
744                         $colspan = 1;
745                         while (isset($_value[$i + $colspan]) && $_value[$i + $colspan] === FALSE) ++$colspan;
746                         $colspan = ($colspan > 1) ? ' colspan="' . $colspan . '"' : '';
747                         $align = $_align[$i] ? ' style="text-align:' . $_align[$i] . '"' : '';
748                         $str[] = '<td class="style_td"' . $align . $colspan . '>';
749                         $str[] = make_link($_value[$i]);
750                         $str[] = '</td>';
751                         unset($_value[$i], $_align[$i]);
752                 }
753
754                 $this->col        = $col;
755                 $this->elements[] = implode('', $str);
756         }
757
758         function canContain(& $obj)
759         {
760                 return is_a($obj, 'YTable') && ($obj->col == $this->col);
761         }
762
763         function & insert(& $obj)
764         {
765                 $this->elements[] = $obj->elements[0];
766                 return $this;
767         }
768
769         function toString()
770         {
771                 $rows = '';
772                 foreach ($this->elements as $str) {
773                         $rows .= "\n" . '<tr class="style_tr">' . $str . '</tr>' . "\n";
774                 }
775                 $rows = $this->wrap($rows, 'table', ' class="style_table" cellspacing="1" border="0"');
776                 return $this->wrap($rows, 'div', ' class="ie5"');
777         }
778 }
779
780 // ' 'Space-beginning sentence
781 // ' 'Space-beginning sentence
782 // ' 'Space-beginning sentence
783 class Pre extends Element
784 {
785         function Pre(& $root, $text)
786         {
787                 $this->__construct($root, $text);
788         }
789         function __construct(& $root, $text)
790         {
791                 global $preformat_ltrim;
792                 parent::__construct();
793                 $this->elements[] = htmlsc(
794                         (! $preformat_ltrim || $text == '' || $text[0] != ' ') ? $text : substr($text, 1));
795         }
796
797         function canContain(& $obj)
798         {
799                 return is_a($obj, 'Pre');
800         }
801
802         function & insert(& $obj)
803         {
804                 $this->elements[] = $obj->elements[0];
805                 return $this;
806         }
807
808         function toString()
809         {
810                 return $this->wrap(join("\n", $this->elements), 'pre');
811         }
812 }
813
814 // Block plugin: #something (started with '#')
815 class Div extends Element
816 {
817         var $name;
818         var $param;
819
820         function Div($out)
821         {
822                 $this->__construct($out);
823         }
824         function __construct($out)
825         {
826                 parent::__construct();
827                 list(, $this->name, $this->param) = array_pad($out, 3, '');
828         }
829
830         function canContain(& $obj)
831         {
832                 return FALSE;
833         }
834
835         function toString()
836         {
837                 // Call #plugin
838                 return do_plugin_convert($this->name, $this->param);
839         }
840 }
841
842 // LEFT:/CENTER:/RIGHT:
843 class Align extends Element
844 {
845         var $align;
846
847         function Align($align)
848         {
849                 $this->__construct($align);
850         }
851         function __construct($align)
852         {
853                 parent::__construct();
854                 $this->align = $align;
855         }
856
857         function canContain(& $obj)
858         {
859                 return is_a($obj, 'Inline');
860         }
861
862         function toString()
863         {
864                 return $this->wrap(parent::toString(), 'div', ' style="text-align:' . $this->align . '"');
865         }
866 }
867
868 // Body
869 class Body extends Element
870 {
871         var $id;
872         var $count = 0;
873         var $contents;
874         var $contents_last;
875         var $classes = array(
876                 '-' => 'UList',
877                 '+' => 'OList',
878                 '>' => 'BQuote',
879                 '<' => 'BQuote');
880         var $factories = array(
881                 ':' => 'DList',
882                 '|' => 'Table',
883                 ',' => 'YTable',
884                 '#' => 'Div');
885
886         function Body($id)
887         {
888                 $this->__construct($id);
889         }
890         function __construct($id)
891         {
892                 $this->id            = $id;
893                 $this->contents      = new Element();
894                 $this->contents_last = & $this->contents;
895                 parent::__construct();
896         }
897
898         function parse(& $lines)
899         {
900                 $this->last = & $this;
901                 $matches = array();
902
903                 while (! empty($lines)) {
904                         $line = array_shift($lines);
905
906                         // Escape comments
907                         if (substr($line, 0, 2) == '//') continue;
908
909                         if (preg_match('/^(LEFT|CENTER|RIGHT):(.*)$/', $line, $matches)) {
910                                 // <div style="text-align:...">
911                                 $this->last = & $this->last->add(new Align(strtolower($matches[1])));
912                                 if ($matches[2] == '') continue;
913                                 $line = $matches[2];
914                         }
915
916                         $line = rtrim($line, "\r\n");
917
918                         // Empty
919                         if ($line == '') {
920                                 $this->last = & $this;
921                                 continue;
922                         }
923
924                         // Horizontal Rule
925                         if (substr($line, 0, 4) == '----') {
926                                 $this->insert(new HRule($this, $line));
927                                 continue;
928                         }
929
930                         // Multiline-enabled block plugin
931                         if (! PKWKEXP_DISABLE_MULTILINE_PLUGIN_HACK &&
932                             preg_match('/^#[^{]+(\{\{+)\s*$/', $line, $matches)) {
933                                 $len = strlen($matches[1]);
934                                 $line .= "\r"; // Delimiter
935                                 while (! empty($lines)) {
936                                         $next_line = preg_replace("/[\r\n]*$/", '', array_shift($lines));
937                                         if (preg_match('/\}{' . $len . '}/', $next_line)) {
938                                                 $line .= $next_line;
939                                                 break;
940                                         } else {
941                                                 $line .= $next_line .= "\r"; // Delimiter
942                                         }
943                                 }
944                         }
945
946                         // The first character
947                         $head = $line[0];
948
949                         // Heading
950                         if ($head == '*') {
951                                 $this->insert(new Heading($this, $line));
952                                 continue;
953                         }
954
955                         // Pre
956                         if ($head == ' ' || $head == "\t") {
957                                 $this->last = & $this->last->add(new Pre($this, $line));
958                                 continue;
959                         }
960
961                         // Line Break
962                         if (substr($line, -1) == '~')
963                                 $line = substr($line, 0, -1) . "\r";
964                         
965                         // Other Character
966                         if (isset($this->classes[$head])) {
967                                 $classname  = $this->classes[$head];
968                                 $this->last = & $this->last->add(new $classname($this, $line));
969                                 continue;
970                         }
971
972                         // Other Character
973                         if (isset($this->factories[$head])) {
974                                 $factoryname = 'Factory_' . $this->factories[$head];
975                                 $this->last  = & $this->last->add($factoryname($this, $line));
976                                 continue;
977                         }
978
979                         // Default
980                         $this->last = & $this->last->add(Factory_Inline($line));
981                 }
982         }
983
984         function getAnchor($text, $level)
985         {
986                 global $top, $_symbol_anchor;
987
988                 // Heading id (auto-generated)
989                 $autoid = 'content_' . $this->id . '_' . $this->count;
990                 $this->count++;
991
992                 // Heading id (specified by users)
993                 $id = make_heading($text, FALSE); // Cut fixed-anchor from $text
994                 if ($id == '') {
995                         // Not specified
996                         $id     = & $autoid;
997                         $anchor = '';
998                 } else {
999                         $anchor = '&aname(' . $id . ',super,full,nouserselect){' . $_symbol_anchor . '};';
1000                 }
1001                 $text = trim($text);
1002                 // Add 'page contents' link to its heading
1003                 $this->contents_last = & $this->contents_last->add(new Contents_UList($text, $level, $id));
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 }