OSDN Git Service

BugTrack/2420 AutoTicketLink - Improve regex and JSON encode
[pukiwiki/pukiwiki.git] / lib / convert_html.php
index eda75bf..05a88cc 100644 (file)
@@ -1,8 +1,8 @@
 <?php
 // PukiWiki - Yet another WikiWikiWeb clone
-// $Id: convert_html.php,v 1.21 2011/01/25 15:01:01 henoheno Exp $
-// Copyright (C)
-//   2002-2005 PukiWiki Developers Team
+// convert_html.php
+// Copyright
+//   2002-2016 PukiWiki Development Team
 //   2001-2002 Originally written by yu-ji
 // License: GPL v2 or (at your option) any later version
 //
@@ -19,7 +19,7 @@ function convert_html($lines)
 
        if (! is_array($lines)) $lines = explode("\n", $lines);
 
-       $body = new Body(++$contents_id);
+       $body = new Body(++$contents_id);
        $body->parse($lines);
 
        return $body->toString();
@@ -34,6 +34,10 @@ class Element
 
        function Element()
        {
+               $this->__construct();
+       }
+       function __construct()
+       {
                $this->elements = array();
                $this->last     = & $this;
        }
@@ -167,7 +171,11 @@ class Inline extends Element
 {
        function Inline($text)
        {
-               parent::Element();
+               $this->__construct($text);
+       }
+       function __construct($text)
+       {
+               parent::__construct();
                $this->elements[] = trim((substr($text, 0, 1) == "\n") ?
                        $text : make_link($text));
        }
@@ -191,7 +199,7 @@ class Inline extends Element
 
        function & toPara($class = '')
        {
-               $obj = new Paragraph('', $class);
+               $obj = new Paragraph('', $class);
                $obj->insert($this);
                return $obj;
        }
@@ -204,7 +212,11 @@ class Paragraph extends Element
 
        function Paragraph($text, $param = '')
        {
-               parent::Element();
+               $this->__construct($text, $param);
+       }
+       function __construct($text, $param = '')
+       {
+               parent::__construct();
                $this->param = $param;
                if ($text == '') return;
 
@@ -236,7 +248,11 @@ class Heading extends Element
 
        function Heading(& $root, $text)
        {
-               parent::Element();
+               $this->__construct($root, $text);
+       }
+       function __construct(& $root, $text)
+       {
+               parent::__construct();
 
                $this->level = min(3, strspn($text, '*'));
                list($text, $this->msg_top, $this->id) = $root->getAnchor($text, $this->level);
@@ -268,7 +284,11 @@ class HRule extends Element
 {
        function HRule(& $root, $text)
        {
-               parent::Element();
+               $this->__construct($root, $text);
+       }
+       function __construct(& $root, $text)
+       {
+               parent::__construct();
        }
 
        function canContain(& $obj)
@@ -290,19 +310,14 @@ class ListContainer extends Element
        var $tag2;
        var $level;
        var $style;
-       var $margin;
-       var $left_margin;
 
        function ListContainer($tag, $tag2, $head, $text)
        {
-               parent::Element();
-
-               $var_margin      = '_' . $tag . '_margin';
-               $var_left_margin = '_' . $tag . '_left_margin';
-               global $$var_margin, $$var_left_margin;
-
-               $this->margin      = $$var_margin;
-               $this->left_margin = $$var_left_margin;
+               $this->__construct($tag, $tag2, $head, $text);
+       }
+       function __construct($tag, $tag2, $head, $text)
+       {
+               parent::__construct();
 
                $this->tag   = $tag;
                $this->tag2  = $tag2;
@@ -322,19 +337,13 @@ class ListContainer extends Element
 
        function setParent(& $parent)
        {
-               global $_list_pad_str;
-
                parent::setParent($parent);
 
                $step = $this->level;
                if (isset($parent->parent) && is_a($parent->parent, 'ListContainer'))
                        $step -= $parent->parent->level;
 
-               $margin = $this->margin * $step;
-               if ($step == $this->level)
-                       $margin += $this->left_margin;
-
-               $this->style = sprintf($_list_pad_str, $this->level, $margin, $margin);
+               $this->style = sprintf(pkwk_list_attrs_template(), $this->level, $step);
        }
 
        function & insert(& $obj)
@@ -363,7 +372,11 @@ class ListElement extends Element
 {
        function ListElement($level, $head)
        {
-               parent::Element();
+               $this->__construct($level, $head);
+       }
+       function __construct($level, $head)
+       {
+               parent::__construct();
                $this->level = $level;
                $this->head  = $head;
        }
@@ -386,7 +399,11 @@ class UList extends ListContainer
 {
        function UList(& $root, $text)
        {
-               parent::ListContainer('ul', 'li', '-', $text);
+               $this->__construct($root, $text);
+       }
+       function __construct(& $root, $text)
+       {
+               parent::__construct('ul', 'li', '-', $text);
        }
 }
 
@@ -397,7 +414,11 @@ class OList extends ListContainer
 {
        function OList(& $root, $text)
        {
-               parent::ListContainer('ol', 'li', '+', $text);
+               $this->__construct($root, $text);
+       }
+       function __construct(& $root, $text)
+       {
+               parent::__construct('ol', 'li', '+', $text);
        }
 }
 
@@ -408,7 +429,11 @@ class DList extends ListContainer
 {
        function DList($out)
        {
-               parent::ListContainer('dl', 'dt', ':', $out[0]);
+               $this->__construct($out);
+       }
+       function __construct($out)
+       {
+               parent::__construct('dl', 'dt', ':', $out[0]);
                $this->last = & Element::insert(new ListElement($this->level, 'dd'));
                if ($out[1] != '')
                        $this->last = & $this->last->insert(Factory_Inline($out[1]));
@@ -423,7 +448,11 @@ class BQuote extends Element
 
        function BQuote(& $root, $text)
        {
-               parent::Element();
+               $this->__construct($root, $text);
+       }
+       function __construct(& $root, $text)
+       {
+               parent::__construct();
 
                $head = substr($text, 0, 1);
                $this->level = min(3, strspn($text, $head));
@@ -486,7 +515,11 @@ class TableCell extends Element
 
        function TableCell($text, $is_template = FALSE)
        {
-               parent::Element();
+               $this->__construct($text, $is_template);
+       }
+       function __construct($text, $is_template = FALSE)
+       {
+               parent::__construct();
                $this->style = $matches = array();
 
                while (preg_match('/^(?:(LEFT|CENTER|RIGHT)|(BG)?COLOR\(([#\w]+)\)|SIZE\((\d+)\)):(.*)$/',
@@ -563,7 +596,11 @@ class Table extends Element
 
        function Table($out)
        {
-               parent::Element();
+               $this->__construct($out);
+       }
+       function __construct($out)
+       {
+               parent::__construct();
 
                $cells       = explode('|', $out[1]);
                $this->col   = count($cells);
@@ -572,7 +609,7 @@ class Table extends Element
                $is_template = ($this->type == 'c');
                $row = array();
                foreach ($cells as $cell)
-                       $row[] = new TableCell($cell, $is_template);
+                       $row[] = new TableCell($cell, $is_template);
                $this->elements[] = $row;
        }
 
@@ -644,7 +681,7 @@ class Table extends Element
                                $row_string = '';
                                foreach (array_keys($row) as $ncol)
                                        $row_string .= $row[$ncol]->toString();
-                               $part_string .= $this->wrap($row_string, 'tr');
+                               $part_string .= $this->wrap($row_string, 'tr') . "\n";
                        }
                        $string .= $this->wrap($part_string, $part);
                }
@@ -654,46 +691,65 @@ class Table extends Element
        }
 }
 
-// , title1 , title2 , title3
-// , cell1  , cell2  , cell3
-// , cell4  , cell5  , cell6
+// , cell1  , cell2  ,  cell3 
+// , cell4  , cell5  ,  cell6 
+// , cell7  ,        right,==
+// ,left          ,==,  cell8
 class YTable extends Element
 {
-       var $col;
-
-       function YTable($_value)
-       {
-               parent::Element();
-
-               $align = $value = $matches = array();
-               foreach($_value as $val) {
-                       if (preg_match('/^(\s+)?(.+?)(\s+)?$/', $val, $matches)) {
-                               $align[] =($matches[1] != '') ?
-                                       ((isset($matches[3]) && $matches[3] != '') ?
-                                               ' style="text-align:center"' :
-                                               ' style="text-align:right"'
-                                       ) : '';
-                               $value[] = $matches[2];
+       var $col;       // Number of columns
+
+       function YTable($row = array('cell1 ', ' cell2 ', ' cell3'))
+       {
+               $this->__construct($row);
+       }
+       // TODO: Seems unable to show literal '==' without tricks.
+       //       But it will be imcompatible.
+       // TODO: Why toString() or toXHTML() here
+       function __construct($row = array('cell1 ', ' cell2 ', ' cell3'))
+       {
+               parent::__construct();
+
+               $str = array();
+               $col = count($row);
+
+               $matches = $_value = $_align = array();
+               foreach($row as $cell) {
+                       if (preg_match('/^(\s+)?(.+?)(\s+)?$/', $cell, $matches)) {
+                               if ($matches[2] == '==') {
+                                       // Colspan
+                                       $_value[] = FALSE;
+                                       $_align[] = FALSE;
+                               } else {
+                                       $_value[] = $matches[2];
+                                       if ($matches[1] == '') {
+                                               $_align[] = ''; // left
+                                       } else if (isset($matches[3])) {
+                                               $_align[] = 'center';
+                                       } else {
+                                               $_align[] = 'right';
+                                       }
+                               }
                        } else {
-                               $align[] = '';
-                               $value[] = $val;
+                               $_value[] = $cell;
+                               $_align[] = '';
                        }
                }
-               $this->col = count($value);
-               $colspan = array();
-               foreach ($value as $val)
-                       $colspan[] = ($val == '==') ? 0 : 1;
-               $str = '';
-               $count = count($value);
-               for ($i = 0; $i < $count; $i++) {
-                       if ($colspan[$i]) {
-                               while ($i + $colspan[$i] < $count && $value[$i + $colspan[$i]] == '==')
-                                       $colspan[$i]++;
-                               $colspan[$i] = ($colspan[$i] > 1) ? ' colspan="' . $colspan[$i] . '"' : '';
-                               $str .= '<td class="style_td"' . $align[$i] . $colspan[$i] . '>' . make_link($value[$i]) . '</td>';
-                       }
+
+               for ($i = 0; $i < $col; $i++) {
+                       if ($_value[$i] === FALSE) continue;
+                       $colspan = 1;
+                       while (isset($_value[$i + $colspan]) && $_value[$i + $colspan] === FALSE) ++$colspan;
+                       $colspan = ($colspan > 1) ? ' colspan="' . $colspan . '"' : '';
+                       $align = $_align[$i] ? ' style="text-align:' . $_align[$i] . '"' : '';
+                       $str[] = '<td class="style_td"' . $align . $colspan . '>';
+                       $str[] = make_link($_value[$i]);
+                       $str[] = '</td>';
+                       unset($_value[$i], $_align[$i]);
                }
-               $this->elements[] = $str;
+
+               $this->col        = $col;
+               $this->elements[] = implode('', $str);
        }
 
        function canContain(& $obj)
@@ -710,8 +766,9 @@ class YTable extends Element
        function toString()
        {
                $rows = '';
-               foreach ($this->elements as $str)
+               foreach ($this->elements as $str) {
                        $rows .= "\n" . '<tr class="style_tr">' . $str . '</tr>' . "\n";
+               }
                $rows = $this->wrap($rows, 'table', ' class="style_table" cellspacing="1" border="0"');
                return $this->wrap($rows, 'div', ' class="ie5"');
        }
@@ -724,8 +781,12 @@ class Pre extends Element
 {
        function Pre(& $root, $text)
        {
+               $this->__construct($root, $text);
+       }
+       function __construct(& $root, $text)
+       {
                global $preformat_ltrim;
-               parent::Element();
+               parent::__construct();
                $this->elements[] = htmlsc(
                        (! $preformat_ltrim || $text == '' || $text{0} != ' ') ? $text : substr($text, 1));
        }
@@ -755,7 +816,11 @@ class Div extends Element
 
        function Div($out)
        {
-               parent::Element();
+               $this->__construct($out);
+       }
+       function __construct($out)
+       {
+               parent::__construct();
                list(, $this->name, $this->param) = array_pad($out, 3, '');
        }
 
@@ -778,7 +843,11 @@ class Align extends Element
 
        function Align($align)
        {
-               parent::Element();
+               $this->__construct($align);
+       }
+       function __construct($align)
+       {
+               parent::__construct();
                $this->align = $align;
        }
 
@@ -813,10 +882,14 @@ class Body extends Element
 
        function Body($id)
        {
+               $this->__construct($id);
+       }
+       function __construct($id)
+       {
                $this->id            = $id;
-               $this->contents      = new Element();
+               $this->contents      = new Element();
                $this->contents_last = & $this->contents;
-               parent::Element();
+               parent::__construct();
        }
 
        function parse(& $lines)
@@ -965,27 +1038,26 @@ class Contents_UList extends ListContainer
 {
        function Contents_UList($text, $level, $id)
        {
+               $this->__construct($text, $level, $id);
+       }
+       function __construct($text, $level, $id)
+       {
                // Reformatting $text
                // A line started with "\n" means "preformatted" ... X(
                make_heading($text);
                $text = "\n" . '<a href="#' . $id . '">' . $text . '</a>' . "\n";
-               parent::ListContainer('ul', 'li', '-', str_repeat('-', $level));
+               parent::__construct('ul', 'li', '-', str_repeat('-', $level));
                $this->insert(Factory_Inline($text));
        }
 
        function setParent(& $parent)
        {
-               global $_list_pad_str;
-
                parent::setParent($parent);
                $step   = $this->level;
-               $margin = $this->left_margin;
                if (isset($parent->parent) && is_a($parent->parent, 'ListContainer')) {
                        $step  -= $parent->parent->level;
-                       $margin = 0;
                }
-               $margin += $this->margin * ($step == $this->level ? 1 : $step);
-               $this->style = sprintf($_list_pad_str, $this->level, $margin, $margin);
+               $indent_level = ($step == $this->level ? 1 : $step);
+               $this->style = sprintf(pkwk_list_attrs_template(), $this->level, $indent_level);
        }
 }
-?>