OSDN Git Service

Cleanup.
authorhenoheno <henoheno>
Thu, 7 Oct 2004 14:15:27 +0000 (23:15 +0900)
committerhenoheno <henoheno>
Thu, 7 Oct 2004 14:15:27 +0000 (23:15 +0900)
* Added many spaces
* Shrink many brackets
* 'and' for boolean => '&&'
* 'or'  for boolean => '||'
* array_key_exists() => isset()

lib/convert_html.php

index 013ca1e..4c5eed7 100644 (file)
@@ -2,7 +2,7 @@
 /////////////////////////////////////////////////
 // PukiWiki - Yet another WikiWikiWeb clone.
 //
-// $Id: convert_html.php,v 1.2 2004/09/19 14:05:30 henoheno Exp $
+// $Id: convert_html.php,v 1.3 2004/10/07 14:15:27 henoheno Exp $
 //
 
 function convert_html($lines)
@@ -10,18 +10,15 @@ function convert_html($lines)
        global $vars, $digest;
        static $contents_id = 0;
 
-       if (!is_array($lines))
-       {
-               $lines = explode("\n", $lines);
-       }
-
+       // Set digest
        $digest = md5(join('', get_source($vars['page'])));
 
-       $body = &new Body(++$contents_id);
+       if (! is_array($lines)) $lines = explode("\n", $lines);
+
+       $body = & new Body(++$contents_id);
        $body->parse($lines);
-       $ret = $body->toString();
 
-       return $ret;
+       return $body->toString();
 }
 
 class Element
@@ -33,31 +30,31 @@ class Element
        function Element()
        {
                $this->elements = array();
-               $this->last = &$this;
+               $this->last     = & $this;
        }
 
-       function setParent(&$parent)
+       function setParent(& $parent)
        {
-               $this->parent = &$parent;
+               $this->parent = & $parent;
        }
 
-       function &add(&$obj)
+       function & add(& $obj)
        {
-               if ($this->canContain($obj))
-               {
+               if ($this->canContain($obj)) {
                        return $this->insert($obj);
+               } else {
+                       return $this->parent->add($obj);
                }
-
-               return $this->parent->add($obj);
        }
 
-       function &insert(&$obj)
+       function & insert(& $obj)
        {
                $obj->setParent($this);
-               $this->elements[] = &$obj;
+               $this->elements[] = & $obj;
 
-               return $this->last = &$obj->last;
+               return $this->last = & $obj->last;
        }
+
        function canContain($obj)
        {
                return TRUE;
@@ -65,28 +62,26 @@ class Element
 
        function wrap($string, $tag, $param = '', $canomit = TRUE)
        {
-               return ($canomit and $string == '') ? '' : "<$tag$param>$string</$tag>";
+               return ($canomit && $string == '') ? '' : "<$tag$param>$string</$tag>";
        }
 
        function toString()
        {
                $ret = array();
-               foreach (array_keys($this->elements) as $key)
-               {
+               foreach (array_keys($this->elements) as $key) {
                        $ret[] = $this->elements[$key]->toString();
                }
 
-               return join("\n",$ret);
+               return join("\n", $ret);
        }
 
        function dump($indent = 0)
        {
-               $ret = str_repeat(' ', $indent).get_class($this)."\n";
+               $ret = str_repeat(' ', $indent) . get_class($this) . "\n";
 
                $indent += 2;
 
-               foreach (array_keys($this->elements) as $key)
-               {
+               foreach (array_keys($this->elements) as $key) {
                        $ret .= is_object($this->elements[$key]) ?
                                $this->elements[$key]->dump($indent) : '';
                                //str_repeat(' ',$indent).$this->elements[$key];
@@ -96,86 +91,83 @@ class Element
        }
 }
 
-  // PHP5 mod: $this cannot be reassigned
-function &Factory_Inline($text)
+function & Factory_Inline($text)
 {
-       if (substr($text,0,1) == '~') { // ¹ÔƬ~¡£¥Ñ¥é¥°¥é¥Õ³«»Ï
-               return new Paragraph(' '.substr($text,1));
+       if (substr($text, 0, 1) == '~') {
+               // ¹ÔƬ '~' ¡£¥Ñ¥é¥°¥é¥Õ³«»Ï
+               return new Paragraph(' ' . substr($text, 1));
+       } else {
+               return new Inline($text);
        }
-       return new Inline($text);
 }
 
-  // PHP5 mod: $this cannot be reassigned
-function &Factory_DList(&$root, $text)
+function & Factory_DList(& $root, $text)
 {
        $out = explode('|', ltrim($text), 2);
        if (count($out) < 2) {
                return Factory_Inline($text);
+       } else {
+               return new DList($out);
        }
-
-       return new DList($out);
 }
 
-  // PHP5 mod: $this cannot be reassigned
-function &Factory_Table(&$root, $text)
+function & Factory_Table(& $root, $text)
 {
-       if (!preg_match("/^\|(.+)\|([hHfFcC]?)$/",$text,$out)) {
+       if (! preg_match("/^\|(.+)\|([hHfFcC]?)$/", $text, $out)) {
                return Factory_Inline($text);
+       } else {
+               return new Table($out);
        }
-       return new Table($out);
 }
 
-  // PHP5 mod: $this cannot be reassigned
-function &Factory_YTable(&$root,$text)
+function & Factory_YTable(& $root, $text)
 {
-       $_value = csv_explode(',', substr($text,1));
+       $_value = csv_explode(',', substr($text, 1));
        if (count($_value) == 0) {
                return Factory_Inline($text);
+       } else {
+               return new YTable($_value);
        }
-       return new YTable($_value);
 }
 
-  // PHP5 mod: $this cannot be reassigned
-function &Factory_Div(&$root,$text)
+function & Factory_Div(& $root, $text)
 {
-       if (!preg_match("/^\#([^\(]+)(?:\((.*)\))?/", $text, $out) or !exist_plugin_convert($out[1])) {
+       if (! preg_match("/^\#([^\(]+)(?:\((.*)\))?/", $text, $out) || ! exist_plugin_convert($out[1])) {
                return new Paragraph($text);
+       } else {
+               return new Div($out);
        }
-       return new Div($out);
 }
 
+// ¥¤¥ó¥é¥¤¥óÍ×ÁÇ
 class Inline extends Element
-{ // ¥¤¥ó¥é¥¤¥óÍ×ÁÇ
-
+{
        function Inline($text)
        {
                parent::Element();
-
                $this->elements[] = trim((substr($text, 0, 1) == "\n") ? $text : make_link($text));
        }
 
-       function &insert(&$obj)
+       function & insert(& $obj)
        {
                $this->elements[] = $obj->elements[0];
-
                return $this;
        }
 
        function canContain($obj)
        {
-               return is_a($obj,'Inline');
+               return is_a($obj, 'Inline');
        }
 
        function toString()
        {
                global $line_break;
-
-               return join($line_break ? "<br />\n" : "\n",$this->elements);
+               return join($line_break ? "<br />\n" : "\n", $this->elements);
        }
 
-       function &toPara($class = '')
+       function & toPara($class = '')
        {
-               $obj = &new Paragraph('', $class);
+               $obj = & new Paragraph('', $class);
                $obj->insert($this);
                return $obj;
        }
@@ -190,20 +182,18 @@ class Paragraph extends Element
                parent::Element();
 
                $this->param = $param;
-               if ($text == '')
-               {
-                       return;
-               }
+
+               if ($text == '') return;
+
                if (substr($text,0,1) == '~')
-               {
-                       $text = ' '.substr($text, 1);
-               }
+                       $text = ' ' . substr($text, 1);
+
                $this->insert(Factory_Inline($text));
        }
 
        function canContain($obj)
        {
-               return is_a($obj,'Inline');
+               return is_a($obj, 'Inline');
        }
 
        function toString()
@@ -212,13 +202,16 @@ class Paragraph extends Element
        }
 }
 
+// * Heading1
+// ** Heading2
+// *** Heading3
 class Heading extends Element
-{ // *
+{
        var $level;
        var $id;
        var $msg_top;
 
-       function Heading(&$root, $text)
+       function Heading(& $root, $text)
        {
                parent::Element();
 
@@ -228,32 +221,32 @@ class Heading extends Element
                $this->level++; // h2,h3,h4
        }
 
-       function &insert(&$obj)
+       function & insert(& $obj)
        {
                parent::insert($obj);
-               return $this->last = &$this;
+               return $this->last = & $this;
        }
 
-       function canContain(&$obj)
+       function canContain(& $obj)
        {
                return FALSE;
        }
 
        function toString()
        {
-               return $this->msg_top.$this->wrap(parent::toString(), 'h'.$this->level, " id=\"{$this->id}\"");
+               return $this->msg_top . $this->wrap(parent::toString(), 'h' . $this->level, " id=\"{$this->id}\"");
        }
 }
 
+// ----
 class HRule extends Element
-{ // ----
-
-       function HRule(&$root, $text)
+{
+       function HRule(& $root, $text)
        {
                parent::Element();
        }
 
-       function canContain(&$obj)
+       function canContain(& $obj)
        {
                return FALSE;
        }
@@ -261,7 +254,6 @@ class HRule extends Element
        function toString()
        {
                global $hr;
-
                return $hr;
        }
 }
@@ -280,67 +272,61 @@ class ListContainer extends Element
                parent::Element();
 
                //¥Þ¡¼¥¸¥ó¤ò¼èÆÀ
-               $var_margin = "_{$tag}_margin";
+               $var_margin      = "_{$tag}_margin";
                $var_left_margin = "_{$tag}_left_margin";
+
                global $$var_margin, $$var_left_margin;
-               $this->margin = $$var_margin;
+
+               $this->margin      = $$var_margin;
                $this->left_margin = $$var_left_margin;
 
                //½é´ü²½
-               $this->tag = $tag;
-               $this->tag2 = $tag2;
+               $this->tag   = $tag;
+               $this->tag2  = $tag2;
                $this->level = min(3, strspn($text, $head));
                $text = ltrim(substr($text, $this->level));
 
                parent::insert(new ListElement($this->level, $tag2));
                if ($text != '')
-               {
-                       $this->last = &$this->last->insert(Factory_Inline($text));
-               }
+                       $this->last = & $this->last->insert(Factory_Inline($text));
        }
 
-       function canContain(&$obj)
+       function canContain(& $obj)
        {
-               return (!is_a($obj, 'ListContainer')
-                       or ($this->tag == $obj->tag and $this->level == $obj->level));
+               return (! is_a($obj, 'ListContainer')
+                       || ($this->tag == $obj->tag && $this->level == $obj->level));
        }
 
-       function setParent(&$parent)
+       function setParent(& $parent)
        {
                global $_list_pad_str;
 
                parent::setParent($parent);
 
                $step = $this->level;
-               if (isset($parent->parent) and is_a($parent->parent, 'ListContainer'))
-               {
+               if (isset($parent->parent) && is_a($parent->parent, 'ListContainer')) {
                        $step -= $parent->parent->level;
                }
                $margin = $this->margin * $step;
-               if ($step == $this->level)
-               {
+               if ($step == $this->level) {
                        $margin += $this->left_margin;
                }
                $this->style = sprintf($_list_pad_str, $this->level, $margin, $margin);
        }
 
-       function &insert(&$obj)
+       function & insert(& $obj)
        {
-               if (!is_a($obj, get_class($this)))
-               {
-                       return $this->last = &$this->last->insert($obj);
-               }
-        // ¹ÔƬʸ»ú¤Î¤ß¤Î»ØÄê»þ¤ÏUL/OL¥Ö¥í¥Ã¥¯¤òæ½Ð
-        // BugTrack/524
-               if (count($obj->elements) == 1 && count($obj->elements[0]->elements) == 0)
-               {
+               if (! is_a($obj, get_class($this)))
+                       return $this->last = & $this->last->insert($obj);
+
+               // ¹ÔƬʸ»ú¤Î¤ß¤Î»ØÄê»þ¤ÏUL/OL¥Ö¥í¥Ã¥¯¤òæ½Ð
+               // BugTrack/524
+               if (count($obj->elements) == 1 && empty($obj->elements[0]->elements))
                        return $this->last->parent; // up to ListElement.
-               }
+
                // move elements.
                foreach(array_keys($obj->elements) as $key)
-               {
                        parent::insert($obj->elements[$key]);
-               }
 
                return $this->last;
        }
@@ -360,9 +346,9 @@ class ListElement extends Element
                $this->head = $head;
        }
 
-       function canContain(&$obj)
+       function canContain(& $obj)
        {
-               return (!is_a($obj, 'ListContainer') or ($obj->level > $this->level));
+               return (! is_a($obj, 'ListContainer') || ($obj->level > $this->level));
        }
 
        function toString()
@@ -371,41 +357,52 @@ class ListElement extends Element
        }
 }
 
+// - One
+// - Two
+// - Three
 class UList extends ListContainer
-{ // -
-       function UList(&$root, $text)
+{
+       function UList(& $root, $text)
        {
                parent::ListContainer('ul', 'li', '-', $text);
        }
 }
 
+// + One
+// + Two
+// + Three
 class OList extends ListContainer
-{ // +
-       function OList(&$root, $text)
+{
+       function OList(& $root, $text)
        {
                parent::ListContainer('ol', 'li', '+', $text);
        }
 }
 
+// : definition1 | description1
+// : definition2 | description2
+// : definition3 | description3
 class DList extends ListContainer
-{ // :
+{
        function DList($out)
        {
                parent::ListContainer('dl', 'dt', ':', $out[0]);
 
-               $this->last = &Element::insert(new ListElement($this->level, 'dd'));
+               $this->last = & Element::insert(new ListElement($this->level, 'dd'));
                if ($out[1] != '')
                {
-                       $this->last = &$this->last->insert(Factory_Inline($out[1]));
+                       $this->last = & $this->last->insert(Factory_Inline($out[1]));
                }
        }
 }
 
+// > Someting cited
+// > like E-mail text
 class BQuote extends Element
-{ // >
+{
        var $level;
 
-       function BQuote(&$root, $text)
+       function BQuote(& $root, $text)
        {
                parent::Element();
 
@@ -413,40 +410,32 @@ class BQuote extends Element
                $this->level = min(3, strspn($text, $head));
                $text = ltrim(substr($text, $this->level));
 
-               if ($head == '<') //blockquote close
-               {
-                       $level = $this->level;
+               if ($head == '<') { // Blockquote close
+                       $level       = $this->level;
                        $this->level = 0;
-                       $this->last = &$this->end($root, $level);
+                       $this->last  = & $this->end($root, $level);
                        if ($text != '')
-                       {
-                               $this->last = &$this->last->insert(Factory_Inline($text));
-                       }
-               }
-               else
-               {
+                               $this->last = & $this->last->insert(Factory_Inline($text));
+               } else {
                        $this->insert(Factory_Inline($text));
                }
        }
 
-       function canContain(&$obj)
+       function canContain(& $obj)
        {
-               return (!is_a($obj, get_class($this)) or $obj->level >= $this->level);
+               return (! is_a($obj, get_class($this)) || $obj->level >= $this->level);
        }
 
-       function &insert(&$obj)
+       function & insert(& $obj)
        {
-        // BugTrack/521, BugTrack/545
-               if (is_a($obj, 'inline')) {
-               return parent::insert($obj->toPara(' class="quotation"'));
-        }
-               if (is_a($obj, 'BQuote') and $obj->level == $this->level and count($obj->elements))
-               {
-                       $obj = &$obj->elements[0];
-                       if (is_a($this->last,'Paragraph') and count($obj->elements))
-                       {
-                               $obj = &$obj->elements[0];
-                       }
+               // BugTrack/521, BugTrack/545
+               if (is_a($obj, 'inline'))
+                       return parent::insert($obj->toPara(' class="quotation"'));
+
+               if (is_a($obj, 'BQuote') && $obj->level == $this->level && count($obj->elements)) {
+                       $obj = & $obj->elements[0];
+                       if (is_a($this->last, 'Paragraph') && count($obj->elements))
+                               $obj = & $obj->elements[0];
                }
                return parent::insert($obj);
        }
@@ -456,17 +445,14 @@ class BQuote extends Element
                return $this->wrap(parent::toString(), 'blockquote');
        }
 
-       function &end(&$root, $level)
+       function & end(& $root, $level)
        {
-               $parent = &$root->last;
+               $parent = & $root->last;
 
-               while (is_object($parent))
-               {
-                       if (is_a($parent,'BQuote') and $parent->level == $level)
-                       {
+               while (is_object($parent)) {
+                       if (is_a($parent, 'BQuote') && $parent->level == $level)
                                return $parent->parent;
-                       }
-                       $parent = &$parent->parent;
+                       $parent = & $parent->parent;
                }
                return $this;
        }
@@ -503,77 +489,65 @@ class TableCell extends Element
                                $text = $matches[5];
                        }
                }
-               if ($is_template and is_numeric($text))
-               {
+               if ($is_template && is_numeric($text)) {
                        $this->style['width'] = "width:{$text}px;";
                }
-               if ($text == '>')
-               {
+
+               if ($text == '>') {
                        $this->colspan = 0;
-               }
-               else if ($text == '~')
-               {
+               } else if ($text == '~') {
                        $this->rowspan = 0;
-               }
-               else if (substr($text, 0, 1) == '~')
-               {
+               } else if (substr($text, 0, 1) == '~') {
                        $this->tag = 'th';
-                       $text = substr($text, 1);
+                       $text      = substr($text, 1);
                }
-               if ($text != '' and $text{0} == '#')
-               {
+
+               if ($text != '' && $text{0} == '#') {
                        // ¥»¥ëÆâÍƤ¬'#'¤Ç»Ï¤Þ¤ë¤È¤­¤ÏDiv¥¯¥é¥¹¤òÄ̤·¤Æ¤ß¤ë
-                       $obj = &Factory_Div($this, $text);
-                       if (is_a($obj, 'Paragraph'))
-                       {
-                               $obj = &$obj->elements[0];
+                       $obj = & Factory_Div($this, $text);
+                       if (is_a($obj, 'Paragraph')) {
+                               $obj = & $obj->elements[0];
                        }
+               } else {
+                       $obj = & Factory_Inline($text);
                }
-               else
-               {
-                       $obj = &Factory_Inline($text);
-               }
+
                $this->insert($obj);
        }
 
-       function setStyle(&$style)
+       function setStyle(& $style)
        {
-               foreach ($style as $key=>$value)
-               {
-                       if (!array_key_exists($key, $this->style))
-                       {
+               foreach ($style as $key=>$value) {
+                       if (! isset($this->style[$key]))
                                $this->style[$key] = $value;
-                       }
                }
        }
 
        function toString()
        {
-               if ($this->rowspan == 0 or $this->colspan == 0)
-               {
-                       return '';
-               }
+               if ($this->rowspan == 0 || $this->colspan == 0) return '';
+
                $param = " class=\"style_{$this->tag}\"";
-               if ($this->rowspan > 1)
-               {
+               if ($this->rowspan > 1) {
                        $param .= " rowspan=\"{$this->rowspan}\"";
                }
-               if ($this->colspan > 1)
-               {
+               if ($this->colspan > 1) {
                        $param .= " colspan=\"{$this->colspan}\"";
                        unset($this->style['width']);
                }
-               if (count($this->style))
-               {
-                       $param .= ' style="'.join(' ', $this->style).'"';
+               if (! empty($this->style)) {
+                       $param .= ' style="' . join(' ', $this->style) . '"';
                }
 
                return $this->wrap(parent::toString(), $this->tag, $param, FALSE);
        }
 }
 
+// | title1 | title2 | title3 |
+// | cell1  | cell2  | cell3  |
+// | cell4  | cell5  | cell6  |
 class Table extends Element
-{ // |
+{
        var $type;
        var $types;
        var $col; // number of column
@@ -582,29 +556,27 @@ class Table extends Element
        {
                parent::Element();
 
-               $cells = explode('|', $out[1]);
-               $this->col = count($cells);
-               $this->type = strtolower($out[2]);
+               $cells       = explode('|', $out[1]);
+               $this->col   = count($cells);
+               $this->type  = strtolower($out[2]);
                $this->types = array($this->type);
                $is_template = ($this->type == 'c');
                $row = array();
-               foreach ($cells as $cell)
-               {
-                       $row[] = &new TableCell($cell, $is_template);
+               foreach ($cells as $cell) {
+                       $row[] = & new TableCell($cell, $is_template);
                }
                $this->elements[] = $row;
        }
 
-       function canContain(&$obj)
+       function canContain(& $obj)
        {
-               return is_a($obj, 'Table') and ($obj->col == $this->col);
+               return is_a($obj, 'Table') && ($obj->col == $this->col);
        }
 
-       function &insert(&$obj)
+       function & insert(& $obj)
        {
                $this->elements[] = $obj->elements[0];
-               $this->types[] = $obj->type;
-
+               $this->types[]    = $obj->type;
                return $this;
        }
 
@@ -613,69 +585,57 @@ class Table extends Element
                static $parts = array('h'=>'thead', 'f'=>'tfoot', ''=>'tbody');
 
                // rowspan¤òÀßÄê(²¼¤«¤é¾å¤Ø)
-               for ($ncol = 0; $ncol < $this->col; $ncol++)
-               {
+               for ($ncol = 0; $ncol < $this->col; $ncol++) {
                        $rowspan = 1;
-                       foreach (array_reverse(array_keys($this->elements)) as $nrow)
-                       {
-                               $row = &$this->elements[$nrow];
-                               if ($row[$ncol]->rowspan == 0)
-                               {
-                                       $rowspan++;
+                       foreach (array_reverse(array_keys($this->elements)) as $nrow) {
+                               $row = & $this->elements[$nrow];
+                               if ($row[$ncol]->rowspan == 0) {
+                                       ++$rowspan;
                                        continue;
                                }
                                $row[$ncol]->rowspan = $rowspan;
-                               while (--$rowspan) // ¹Ô¼ïÊ̤ò·Ñ¾µ¤¹¤ë
-                               {
+                               while (--$rowspan) { // ¹Ô¼ïÊ̤ò·Ñ¾µ¤¹¤ë
                                        $this->types[$nrow + $rowspan] = $this->types[$nrow];
                                }
                                $rowspan = 1;
                        }
                }
+
                // colspan,style¤òÀßÄê
                $stylerow = NULL;
-               foreach (array_keys($this->elements) as $nrow)
-               {
-                       $row = &$this->elements[$nrow];
-                       if ($this->types[$nrow] == 'c')
-                       {
-                               $stylerow = &$row;
+               foreach (array_keys($this->elements) as $nrow) {
+                       $row = & $this->elements[$nrow];
+                       if ($this->types[$nrow] == 'c') {
+                               $stylerow = & $row;
                        }
                        $colspan = 1;
-                       foreach (array_keys($row) as $ncol)
-                       {
-                               if ($row[$ncol]->colspan == 0)
-                               {
-                                       $colspan++;
+                       foreach (array_keys($row) as $ncol) {
+                               if ($row[$ncol]->colspan == 0) {
+                                       ++$colspan;
                                        continue;
                                }
                                $row[$ncol]->colspan = $colspan;
-                               if ($stylerow !== NULL)
-                               {
+                               if ($stylerow !== NULL) {
                                        $row[$ncol]->setStyle($stylerow[$ncol]->style);
-                                       while (--$colspan) // Îó¥¹¥¿¥¤¥ë¤ò·Ñ¾µ¤¹¤ë
-                                       {
+                                       while (--$colspan) { // Îó¥¹¥¿¥¤¥ë¤ò·Ñ¾µ¤¹¤ë
                                                $row[$ncol - $colspan]->setStyle($stylerow[$ncol]->style);
                                        }
                                }
                                $colspan = 1;
                        }
                }
+
                // ¥Æ¥­¥¹¥È²½
                $string = '';
                foreach ($parts as $type => $part)
                {
                        $part_string = '';
-                       foreach (array_keys($this->elements) as $nrow)
-                       {
+                       foreach (array_keys($this->elements) as $nrow) {
                                if ($this->types[$nrow] != $type)
-                               {
                                        continue;
-                               }
-                               $row = &$this->elements[$nrow];
+                               $row        = & $this->elements[$nrow];
                                $row_string = '';
-                               foreach (array_keys($row) as $ncol)
-                               {
+                               foreach (array_keys($row) as $ncol) {
                                        $row_string .= $row[$ncol]->toString();
                                }
                                $part_string .= $this->wrap($row_string, 'tr');
@@ -683,12 +643,16 @@ class Table extends Element
                        $string .= $this->wrap($part_string, $part);
                }
                $string = $this->wrap($string, 'table', ' class="style_table" cellspacing="1" border="0"');
+
                return $this->wrap($string, 'div', ' class="ie5"');
        }
 }
 
+// , title1 , title2 , title3
+// , cell1  , cell2  , cell3
+// , cell4  , cell5  , cell6
 class YTable extends Element
-{ // ,
+{
        var $col;
 
        function YTable($_value)
@@ -696,35 +660,28 @@ class YTable extends Element
                parent::Element();
 
                $align = $value = $matches = array();
-               foreach($_value as $val)
-               {
-                       if (preg_match('/^(\s+)?(.+?)(\s+)?$/', $val, $matches))
-                       {
+               foreach($_value as $val) {
+                       if (preg_match('/^(\s+)?(.+?)(\s+)?$/', $val, $matches)) {
                                $align[] =($matches[1] != '') ?
-                                       ((array_key_exists(3,$matches) and $matches[3] != '') ?
-                                               ' style="text-align:center"' : ' style="text-align:right"'
+                                       ((isset($matches[3]) && $matches[3] != '') ?
+                                               ' style="text-align:center"' :
+                                               ' style="text-align:right"'
                                        ) : '';
                                $value[] = $matches[2];
-                       }
-                       else
-                       {
+                       } else {
                                $align[] = '';
                                $value[] = $val;
                        }
                }
                $this->col = count($value);
                $colspan = array();
-               foreach ($value as $val)
-               {
+               foreach ($value as $val) {
                        $colspan[] = ($val == '==') ? 0 : 1;
                }
                $str = '';
-               for ($i = 0; $i < count($value); $i++)
-               {
-                       if ($colspan[$i])
-                       {
-                               while ($i + $colspan[$i] < count($value) and $value[$i + $colspan[$i]] == '==')
-                               {
+               for ($i = 0; $i < count($value); $i++) {
+                       if ($colspan[$i]) {
+                               while ($i + $colspan[$i] < count($value) && $value[$i + $colspan[$i]] == '==') {
                                        $colspan[$i]++;
                                }
                                $colspan[$i] = ($colspan[$i] > 1) ? " colspan=\"{$colspan[$i]}\"" : '';
@@ -734,23 +691,21 @@ class YTable extends Element
                $this->elements[] = $str;
        }
 
-       function canContain(&$obj)
+       function canContain(& $obj)
        {
-               return is_a($obj, 'YTable') and ($obj->col == $this->col);
+               return is_a($obj, 'YTable') && ($obj->col == $this->col);
        }
 
-       function &insert(&$obj)
+       function & insert(& $obj)
        {
                $this->elements[] = $obj->elements[0];
-
                return $this;
        }
 
        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"');
@@ -758,27 +713,29 @@ class YTable extends Element
        }
 }
 
+// ' 'Space-beginning sentence
+// ' 'Space-beginning sentence
+// ' 'Space-beginning sentence
 class Pre extends Element
-{ // ' '
-       function Pre(&$root,$text)
+{
+       function Pre(& $root, $text)
        {
                global $preformat_ltrim;
 
                parent::Element();
                $this->elements[] = htmlspecialchars(
-                       (!$preformat_ltrim or $text == '' or $text{0} != ' ') ? $text : substr($text, 1)
+                       (! $preformat_ltrim || $text == '' || $text{0} != ' ') ? $text : substr($text, 1)
                );
        }
 
-       function canContain(&$obj)
+       function canContain(& $obj)
        {
                return is_a($obj, 'Pre');
        }
 
-       function &insert(&$obj)
+       function & insert(& $obj)
        {
                $this->elements[] = $obj->elements[0];
-
                return $this;
        }
 
@@ -788,53 +745,54 @@ class Pre extends Element
        }
 }
 
+// #someting(started with '#')
 class Div extends Element
-{ // #
+{
        var $name;
        var $param;
 
        function Div($out)
        {
                parent::Element();
-
-               list(, $this->name, $this->param) = array_pad($out,3,'');
+               list(, $this->name, $this->param) = array_pad($out, 3, '');
        }
 
-       function canContain(&$obj)
+       function canContain(& $obj)
        {
                return FALSE;
        }
 
        function toString()
        {
-               return do_plugin_convert($this->name,$this->param);
+               return do_plugin_convert($this->name, $this->param);
        }
 }
 
+// LEFT:/CENTER:/RIGHT:
 class Align extends Element
-{ // LEFT:/CENTER:/RIGHT:
+{
        var $align;
 
        function Align($align)
        {
                parent::Element();
-
                $this->align = $align;
        }
 
-       function canContain(&$obj)
+       function canContain(& $obj)
        {
                return is_a($obj, 'Inline');
        }
 
        function toString()
        {
-               return $this->wrap(parent::toString(), 'div', ' style="text-align:'.$this->align.'"');
+               return $this->wrap(parent::toString(), 'div', ' style="text-align:' . $this->align . '"');
        }
 }
 
+// Body
 class Body extends Element
-{ // Body
+{
        var $id;
        var $count = 0;
        var $contents;
@@ -854,109 +812,102 @@ class Body extends Element
 
        function Body($id)
        {
-               $this->id = $id;
-               $this->contents = &new Element();
-               $this->contents_last = &$this->contents;
+               $this->id            = $id;
+               $this->contents      = & new Element();
+               $this->contents_last = & $this->contents;
                parent::Element();
        }
 
-       function parse(&$lines)
+       function parse(& $lines)
        {
-               $this->last = &$this;
+               $this->last = & $this;
+               $matches = array();
 
-               while (count($lines))
-               {
+               while (! empty($lines)) {
                        $line = array_shift($lines);
 
-                       if (substr($line,0,2) == '//') //¥³¥á¥ó¥È¤Ï½èÍý¤·¤Ê¤¤
-                       {
-                               continue;
-                       }
+                       // Escape comments
+                       if (substr($line, 0, 2) == '//') continue;
+
+                       if (preg_match('/^(LEFT|CENTER|RIGHT):(.*)$/', $line, $matches)) {
+                               // <div style="text-align:...">
+                               $this->last = & $this->last->add(new Align(strtolower($matches[1])));
+
+                               if ($matches[2] == '') continue;
 
-                       if (preg_match('/^(LEFT|CENTER|RIGHT):(.*)$/',$line,$matches))
-                       {
-                               $this->last = &$this->last->add(new Align(strtolower($matches[1]))); // <div style="text-align:...">
-                               if ($matches[2] == '')
-                               {
-                                       continue;
-                               }
                                $line = $matches[2];
                        }
 
-                       $line = preg_replace("/[\r\n]*$/",'',$line);
+                       $line = preg_replace("/[\r\n]*$/", '', $line);
 
                        // Empty
-                       if ($line == '')
-                       {
-                               $this->last = &$this;
+                       if ($line == '') {
+                               $this->last = & $this;
                                continue;
                        }
+
                        // Horizontal Rule
-                       if (substr($line,0,4) == '----')
-                       {
-                               $this->insert(new HRule($this,$line));
+                       if (substr($line, 0, 4) == '----') {
+                               $this->insert(new HRule($this, $line));
                                continue;
                        }
-                       // ¹ÔƬʸ»ú
+
+                       // The first character
                        $head = $line{0};
 
                        // Heading
-                       if ($head == '*')
-                       {
-                               $this->insert(new Heading($this,$line));
+                       if ($head == '*') {
+                               $this->insert(new Heading($this, $line));
                                continue;
                        }
+
                        // Pre
-                       if ($head == ' ' or $head == "\t")
-                       {
-                               $this->last = &$this->last->add(new Pre($this,$line));
+                       if ($head == ' ' || $head == "\t") {
+                               $this->last = & $this->last->add(new Pre($this, $line));
                                continue;
                        }
+
                        // Line Break
-                       if (substr($line,-1) == '~')
-                       {
+                       if (substr($line, -1) == '~') {
                                $line = substr($line,0,-1)."\r";
                        }
+                       
                        // Other Character
-                       if (array_key_exists($head, $this->classes))
-                       {
-                               $classname = $this->classes[$head];
-                               $this->last = &$this->last->add(new $classname($this,$line));
+                       if (isset($this->classes[$head])) {
+                               $classname  = $this->classes[$head];
+                               $this->last = & $this->last->add(new $classname($this, $line));
                                continue;
                        }
+
                        // Other Character
-                       if (array_key_exists($head, $this->factories))
-                       {
-                               $factoryname = 'Factory_'. $this->factories[$head];
-                               $this->last = &$this->last->add($factoryname($this,$line));
+                       if (isset($this->factories[$head])) {
+                               $factoryname = 'Factory_' . $this->factories[$head];
+                               $this->last  = & $this->last->add($factoryname($this, $line));
                                continue;
                        }
 
                        // Default
-                       $this->last = &$this->last->add(Factory_Inline($line));
+                       $this->last = & $this->last->add(Factory_Inline($line));
                }
        }
 
-       function getAnchor($text,$level)
+       function getAnchor($text, $level)
        {
-               global $top,$_symbol_anchor;
+               global $top, $_symbol_anchor;
 
-               $anchor = (($id = make_heading($text,FALSE)) == '') ?
+               $anchor = (($id = make_heading($text, FALSE)) == '') ?
                        '' : " &aname($id,super,full)\{$_symbol_anchor};";
-               $text = ' '.$text;
+               $text = ' ' . $text;
                $id = "content_{$this->id}_{$this->count}";
                $this->count++;
-               $this->contents_last = &$this->contents_last->add(new Contents_UList($text,$level,$id));
+               $this->contents_last = & $this->contents_last->add(new Contents_UList($text, $level, $id));
 
                return array($text. $anchor, $this->count > 1 ? "\n$top" : '', $id);
        }
 
-       function &insert(&$obj)
+       function & insert(& $obj)
        {
-               if (is_a($obj, 'Inline'))
-               {
-                       $obj = &$obj->toPara();
-               }
+               if (is_a($obj, 'Inline')) $obj = & $obj->toPara();
                return parent::insert($obj);
        }
 
@@ -967,12 +918,12 @@ class Body extends Element
                $text = parent::toString();
 
                // #contents
-               $text = preg_replace_callback('/(<p[^>]*>)<del>#contents<\/del>(\s*)(<\/p>)/', array(&$this,'replace_contents'),$text);
+               $text = preg_replace_callback('/(<p[^>]*>)<del>#contents<\/del>(\s*)(<\/p>)/', array(& $this, 'replace_contents'), $text);
 
                // ´ØÏ¢¤¹¤ë¥Ú¡¼¥¸
                // <p>¤Î¤È¤­¤Ï¹ÔƬ¤«¤é¡¢<del>¤Î¤È¤­¤Ï¾¤ÎÍ×ÁǤλÒÍ×ÁǤȤ·¤Æ¸ºß
-               $text = preg_replace_callback('/(<p[^>]*>)<del>#related<\/del>(\s*)(<\/p>)/', array(&$this, 'replace_related'), $text);
-               $text = preg_replace('/<del>#related<\/del>/',make_related($vars['page'],'del'),$text);
+               $text = preg_replace_callback('/(<p[^>]*>)<del>#related<\/del>(\s*)(<\/p>)/', array(& $this, 'replace_related'), $text);
+               $text = preg_replace('/<del>#related<\/del>/', make_related($vars['page'], 'del'), $text);
                return "$text\n";
        }
 
@@ -984,7 +935,7 @@ class Body extends Element
                $contents .= "</div>\n";
                array_shift($arr);
 
-               return ($arr[1] != '') ? $contents.join('',$arr) : $contents;
+               return ($arr[1] != '') ? $contents . join('', $arr) : $contents;
        }
 
        function replace_related($arr)
@@ -993,41 +944,39 @@ class Body extends Element
                static $related = NULL;
 
                if (is_null($related))
-               {
                        $related = make_related($vars['page'],'p');
-               }
+
                array_shift($arr);
 
-               return ($arr[1] != '') ? $related.join('',$arr) : $related;
+               return ($arr[1] != '') ? $related . join('', $arr) : $related;
        }
 }
 
 class Contents_UList extends ListContainer
 {
-       function Contents_UList($text,$level,$id)
+       function Contents_UList($text, $level, $id)
        {
                // ¥Æ¥­¥¹¥È¤Î¥ê¥Õ¥©¡¼¥à
                // ¹ÔƬ\n¤ÇÀ°·ÁºÑ¤ß¤òɽ¤¹ ... X(
                make_heading($text);
                $text = "\n<a href=\"#$id\">$text</a>\n";
-               parent::ListContainer('ul', 'li', '-', str_repeat('-',$level));
+               parent::ListContainer('ul', 'li', '-', str_repeat('-', $level));
                $this->insert(Factory_Inline($text));
        }
 
-       function setParent(&$parent)
+       function setParent(& $parent)
        {
                global $_list_pad_str;
 
                parent::setParent($parent);
-               $step = $this->level;
+               $step   = $this->level;
                $margin = $this->left_margin;
-               if (isset($parent->parent) and is_a($parent->parent,'ListContainer'))
-               {
-                       $step -= $parent->parent->level;
+               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);
+               $this->style = sprintf($_list_pad_str, $this->level, $margin, $margin);
        }
 }
 ?>