--- lib/convert_html.php 2010-02-11 05:40:38.000000000 +0900
+++ lib/convert_html.php 2010-02-11 06:02:50.000000000 +0900
@@ -2,25 +2,79 @@
/////////////////////////////////////////////////
// PukiWiki - Yet another WikiWikiWeb clone.
//
-// $Id: convert_html.php,v 1.1 2004/08/01 01:54:35 henoheno Exp $
+// $Id: convert_html.php,v 1.62 2004/03/18 09:52:52 arino Exp $
//
+ // PHP5 mod: $this cannot be reassigned
+function &Factory_Inline($text)
+{
+ if (substr($text,0,1) == '~') // 行頭~。パラグラフ開始
+ {
+ return new Paragraph(' '.substr($text,1));
+ }
+ return new Inline($text);
+}
+
+ // PHP5 mod: $this cannot be reassigned
+function &Factory_DList(&$root, $text)
+{
+ $out = explode('|', ltrim($text), 2);
+ if (count($out) < 2)
+ {
+ return Factory_Inline($text);
+ }
+
+ return new DList($out);
+}
+
+ // PHP5 mod: $this cannot be reassigned
+function &Factory_Table(&$root, $text)
+{
+ if (!preg_match("/^\|(.+)\|([hHfFcC]?)$/",$text,$out))
+ {
+ return Factory_Inline($text);
+ }
+ return new Table($out);
+}
+
+ // PHP5 mod: $this cannot be reassigned
+function &Factory_YTable(&$root,$text)
+{
+ $_value = csv_explode(',', substr($text,1));
+ if (count($_value) == 0)
+ {
+ return Factory_Inline($text);
+ }
+ return new YTable($_value);
+}
+
+ // PHP5 mod: $this cannot be reassigned
+function &Factory_Div(&$root,$text)
+{
+ if (!preg_match("/^\#([^\(]+)(?:\((.*)\))?/", $text, $out) or !exist_plugin_convert($out[1]))
+ {
+ return new Paragraph($text);
+ }
+ return new Div($out);
+}
+
+
function convert_html($lines)
{
- global $vars, $digest;
+ global $script, $vars, $digest;
static $contents_id = 0;
-
+
if (!is_array($lines))
{
$lines = explode("\n", $lines);
}
-
+
$digest = md5(join('', get_source($vars['page'])));
-
+
$body = &new Body(++$contents_id);
$body->parse($lines);
$ret = $body->toString();
-
+
return $ret;
}
@@ -29,45 +83,40 @@
var $parent; // 親要素
var $last; // 次に要素を挿入する先
var $elements; // 要素の配列
-
+
function Element()
{
$this->elements = array();
$this->last = &$this;
}
-
function setParent(&$parent)
{
$this->parent = &$parent;
}
-
function &add(&$obj)
{
if ($this->canContain($obj))
{
return $this->insert($obj);
}
-
+
return $this->parent->add($obj);
}
-
function &insert(&$obj)
{
$obj->setParent($this);
$this->elements[] = &$obj;
-
+
return $this->last = &$obj->last;
}
function canContain($obj)
{
return TRUE;
}
-
function wrap($string, $tag, $param = '', $canomit = TRUE)
{
return ($canomit and $string == '') ? '' : "<$tag$param>$string$tag>";
}
-
function toString()
{
$ret = array();
@@ -75,63 +124,49 @@
{
$ret[] = $this->elements[$key]->toString();
}
-
+
return join("\n",$ret);
}
-
function dump($indent = 0)
{
$ret = str_repeat(' ', $indent).get_class($this)."\n";
-
+
$indent += 2;
-
+
foreach (array_keys($this->elements) as $key)
{
$ret .= is_object($this->elements[$key]) ?
$this->elements[$key]->dump($indent) : '';
//str_repeat(' ',$indent).$this->elements[$key];
}
-
+
return $ret;
}
}
-
class Inline extends Element
{ // インライン要素
-
function Inline($text)
{
parent::Element();
-
- if (substr($text,0,1) == '~') // 行頭~。パラグラフ開始
- {
- $this = new Paragraph(' '.substr($text,1));
- $this->last = &$this;
-
- return;
- }
+
$this->elements[] = trim((substr($text, 0, 1) == "\n") ? $text : make_link($text));
}
-
function &insert(&$obj)
{
$this->elements[] = $obj->elements[0];
-
+
return $this;
}
-
function canContain($obj)
{
return is_a($obj,'Inline');
}
-
function toString()
{
global $line_break;
-
+
return join($line_break ? "
\n" : "\n",$this->elements);
}
-
function &toPara($class = '')
{
$obj = &new Paragraph('', $class);
@@ -139,15 +174,14 @@
return $obj;
}
}
-
class Paragraph extends Element
{ // 段落
var $param;
-
+
function Paragraph($text, $param = '')
{
parent::Element();
-
+
$this->param = $param;
if ($text == '')
{
@@ -157,14 +191,12 @@
{
$text = ' '.substr($text, 1);
}
- $this->insert(new Inline($text));
+ $this->insert(Factory_Inline($text));
}
-
function canContain($obj)
{
return is_a($obj,'Inline');
}
-
function toString()
{
return $this->wrap(parent::toString(), 'p', $this->param);
@@ -176,55 +208,47 @@
var $level;
var $id;
var $msg_top;
-
+
function Heading(&$root, $text)
{
parent::Element();
-
+
$this->level = min(3, strspn($text, '*'));
list($text, $this->msg_top, $this->id) = $root->getAnchor($text, $this->level);
- $this->insert(new Inline($text));
+ $this->insert(Factory_Inline($text));
$this->level++; // h2,h3,h4
}
-
function &insert(&$obj)
{
parent::insert($obj);
return $this->last = &$this;
}
-
function canContain(&$obj)
{
return FALSE;
}
-
function toString()
{
return $this->msg_top.$this->wrap(parent::toString(), 'h'.$this->level, " id=\"{$this->id}\"");
}
}
-
class HRule extends Element
{ // ----
-
function HRule(&$root, $text)
{
parent::Element();
}
-
function canContain(&$obj)
{
return FALSE;
}
-
function toString()
{
global $hr;
-
+
return $hr;
}
}
-
class ListContainer extends Element
{
var $tag;
@@ -233,43 +257,42 @@
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->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(new Inline($text));
+ $this->last = &$this->last->insert(Factory_Inline($text));
}
}
-
+
function canContain(&$obj)
{
return (!is_a($obj, 'ListContainer')
or ($this->tag == $obj->tag and $this->level == $obj->level));
}
-
function setParent(&$parent)
{
global $_list_pad_str;
-
+
parent::setParent($parent);
-
+
$step = $this->level;
if (isset($parent->parent) and is_a($parent->parent, 'ListContainer'))
{
@@ -282,7 +305,6 @@
}
$this->style = sprintf($_list_pad_str, $this->level, $margin, $margin);
}
-
function &insert(&$obj)
{
if (!is_a($obj, get_class($this)))
@@ -290,7 +312,7 @@
return $this->last = &$this->last->insert($obj);
}
// 行頭文字のみの指定時はUL/OLブロックを脱出
- // BugTrack/524
+ // BugTrack/524
if (count($obj->elements) == 1 && count($obj->elements[0]->elements) == 0)
{
return $this->last->parent; // up to ListElement.
@@ -300,16 +322,14 @@
{
parent::insert($obj->elements[$key]);
}
-
+
return $this->last;
}
-
function toString()
{
return $this->wrap(parent::toString(), $this->tag, $this->style);
}
}
-
class ListElement extends Element
{
function ListElement($level, $head)
@@ -318,18 +338,15 @@
$this->level = $level;
$this->head = $head;
}
-
function canContain(&$obj)
{
return (!is_a($obj, 'ListContainer') or ($obj->level > $this->level));
}
-
function toString()
{
return $this->wrap(parent::toString(), $this->head);
}
}
-
class UList extends ListContainer
{ // -
function UList(&$root, $text)
@@ -337,7 +354,6 @@
parent::ListContainer('ul', 'li', '-', $text);
}
}
-
class OList extends ListContainer
{ // +
function OList(&$root, $text)
@@ -345,41 +361,31 @@
parent::ListContainer('ol', 'li', '+', $text);
}
}
-
class DList extends ListContainer
{ // :
- function DList(&$root, $text)
+ function DList($out)
{
- $out = explode('|', $text, 2);
- if (count($out) < 2)
- {
- $this = new Inline($text);
- $this->last = &$this;
-
- return;
- }
parent::ListContainer('dl', 'dt', ':', $out[0]);
-
+
$this->last = &Element::insert(new ListElement($this->level, 'dd'));
if ($out[1] != '')
{
- $this->last = &$this->last->insert(new Inline($out[1]));
+ $this->last = &$this->last->insert(Factory_Inline($out[1]));
}
}
}
-
class BQuote extends Element
{ // >
var $level;
-
+
function BQuote(&$root, $text)
{
parent::Element();
-
+
$head = substr($text, 0, 1);
$this->level = min(3, strspn($text, $head));
$text = ltrim(substr($text, $this->level));
-
+
if ($head == '<') //blockquote close
{
$level = $this->level;
@@ -387,20 +393,18 @@
$this->last = &$this->end($root, $level);
if ($text != '')
{
- $this->last = &$this->last->insert(new Inline($text));
+ $this->last = &$this->last->insert(Factory_Inline($text));
}
}
else
{
- $this->insert(new Inline($text));
+ $this->insert(Factory_Inline($text));
}
}
-
function canContain(&$obj)
{
return (!is_a($obj, get_class($this)) or $obj->level >= $this->level);
}
-
function &insert(&$obj)
{
// BugTrack/521, BugTrack/545
@@ -417,16 +421,14 @@
}
return parent::insert($obj);
}
-
function toString()
{
return $this->wrap(parent::toString(), 'blockquote');
}
-
function &end(&$root, $level)
{
$parent = &$root->last;
-
+
while (is_object($parent))
{
if (is_a($parent,'BQuote') and $parent->level == $level)
@@ -438,19 +440,18 @@
return $this;
}
}
-
class TableCell extends Element
{
var $tag = 'td'; // {td|th}
var $colspan = 1;
var $rowspan = 1;
var $style; // is array('width'=>, 'align'=>...);
-
+
function TableCell($text, $is_template = FALSE)
{
parent::Element();
- $this->style = $matches = array();
-
+ $this->style = array();
+
while (preg_match('/^(?:(LEFT|CENTER|RIGHT)|(BG)?COLOR\(([#\w]+)\)|SIZE\((\d+)\)):(.*)$/',$text,$matches))
{
if ($matches[1])
@@ -490,7 +491,7 @@
if ($text != '' and $text{0} == '#')
{
// セル内容が'#'で始まるときはDivクラスを通してみる
- $obj = &new Div($this, $text);
+ $obj = &Factory_Div($this, $text);
if (is_a($obj, 'Paragraph'))
{
$obj = &$obj->elements[0];
@@ -498,11 +499,10 @@
}
else
{
- $obj = &new Inline($text);
+ $obj = &Factory_Inline($text);
}
$this->insert($obj);
}
-
function setStyle(&$style)
{
foreach ($style as $key=>$value)
@@ -513,7 +513,6 @@
}
}
}
-
function toString()
{
if ($this->rowspan == 0 or $this->colspan == 0)
@@ -534,29 +533,20 @@
{
$param .= ' style="'.join(' ', $this->style).'"';
}
-
+
return $this->wrap(parent::toString(), $this->tag, $param, FALSE);
}
}
-
class Table extends Element
{ // |
var $type;
var $types;
var $col; // number of column
-
- function Table(&$root, $text)
+
+ function Table($out)
{
parent::Element();
-
- $out = array();
- if (!preg_match("/^\|(.+)\|([hHfFcC]?)$/", $text, $out))
- {
- $this = new Inline($text);
- $this->last = &$this;
-
- return;
- }
+
$cells = explode('|', $out[1]);
$this->col = count($cells);
$this->type = strtolower($out[2]);
@@ -569,24 +559,21 @@
}
$this->elements[] = $row;
}
-
function canContain(&$obj)
{
return is_a($obj, 'Table') and ($obj->col == $this->col);
}
-
function &insert(&$obj)
{
$this->elements[] = $obj->elements[0];
$this->types[] = $obj->type;
-
+
return $this;
}
-
function toString()
{
static $parts = array('h'=>'thead', 'f'=>'tfoot', ''=>'tbody');
-
+
// rowspanを設定(下から上へ)
for ($ncol = 0; $ncol < $this->col; $ncol++)
{
@@ -661,24 +648,16 @@
return $this->wrap($string, 'div', ' class="ie5"');
}
}
-
class YTable extends Element
{ // ,
var $col;
-
- function YTable(&$root, $text)
+
+ function YTable($_value)
{
parent::Element();
-
- $_value = csv_explode(',', substr($text,1));
- if (count($_value) == 0)
- {
- $this = new Inline($text);
- $this->last = &$this;
-
- return;
- }
- $align = $value = $matches = array();
+
+ $align = array();
+ $value = array();
foreach($_value as $val)
{
if (preg_match('/^(\s+)?(.+?)(\s+)?$/', $val, $matches))
@@ -716,19 +695,16 @@
}
$this->elements[] = $str;
}
-
function canContain(&$obj)
{
return is_a($obj, 'YTable') and ($obj->col == $this->col);
}
-
function &insert(&$obj)
{
$this->elements[] = $obj->elements[0];
-
+
return $this;
}
-
function toString()
{
$rows = '';
@@ -740,89 +716,71 @@
return $this->wrap($rows, 'div', ' class="ie5"');
}
}
-
class Pre extends Element
{ // ' '
function Pre(&$root,$text)
{
global $preformat_ltrim;
-
+
parent::Element();
$this->elements[] = htmlspecialchars(
(!$preformat_ltrim or $text == '' or $text{0} != ' ') ? $text : substr($text, 1)
);
}
-
function canContain(&$obj)
{
return is_a($obj, 'Pre');
}
-
function &insert(&$obj)
{
$this->elements[] = $obj->elements[0];
-
+
return $this;
}
-
function toString()
{
return $this->wrap(join("\n", $this->elements), 'pre');
}
}
-
class Div extends Element
{ // #
var $name;
var $param;
-
- function Div(&$root, $text)
+
+ function Div($out)
{
parent::Element();
-
- if (!preg_match("/^\#([^\(]+)(?:\((.*)\))?/", $text, $out) or !exist_plugin_convert($out[1]))
- {
- $this = new Paragraph($text);
- $this->last = &$this;
-
- return;
- }
+
list(, $this->name, $this->param) = array_pad($out,3,'');
}
-
function canContain(&$obj)
{
return FALSE;
}
-
function toString()
{
return do_plugin_convert($this->name,$this->param);
}
}
-
class Align extends Element
{ // LEFT:/CENTER:/RIGHT:
var $align;
-
+
function Align($align)
{
parent::Element();
-
+
$this->align = $align;
}
-
function canContain(&$obj)
{
return is_a($obj, 'Inline');
}
-
function toString()
{
return $this->wrap(parent::toString(), 'div', ' style="text-align:'.$this->align.'"');
}
}
-
class Body extends Element
{ // Body
var $id;
@@ -832,14 +790,16 @@
var $classes = array(
'-' => 'UList',
'+' => 'OList',
+ '>' => 'BQuote',
+ '<' => 'BQuote'
+ );
+ var $factories = array(
':' => 'DList',
'|' => 'Table',
',' => 'YTable',
- '>' => 'BQuote',
- '<' => 'BQuote',
'#' => 'Div'
);
-
+
function Body($id)
{
$this->id = $id;
@@ -847,20 +807,20 @@
$this->contents_last = &$this->contents;
parent::Element();
}
-
function parse(&$lines)
{
$this->last = &$this;
-
+
while (count($lines))
{
$line = array_shift($lines);
-
+
if (substr($line,0,2) == '//') //コメントは処理しない
{
continue;
}
-
+
+ $align = '';
if (preg_match('/^(LEFT|CENTER|RIGHT):(.*)$/',$line,$matches))
{
$this->last = &$this->last->add(new Align(strtolower($matches[1]))); //
]*>) のときは行頭から、 ]*>) パスワードが違います。#contents<\/del>(\s*)(<\/p>)/', array(&$this,'replace_contents'),$text);
-
+
// 関連するページ
// のときは他の要素の子要素として存在
$text = preg_replace_callback('/(#related<\/del>(\s*)(<\/p>)/', array(&$this, 'replace_related'), $text);
$text = preg_replace('/#related<\/del>/',make_related($vars['page'],'del'),$text);
return "$text\n";
}
-
function replace_contents($arr)
{
$contents = "
EOD;
$name = "*$id";
-
+
return parent::setParam($page,$name,$body);
}
function toString()
@@ -375,7 +395,7 @@
class Link_mailto extends Link
{
var $is_image,$image;
-
+
function Link_mailto($start)
{
parent::Link($start);
@@ -412,7 +432,7 @@
var $url = '';
var $param = '';
var $anchor = '';
-
+
function Link_interwikiname($start)
{
parent::Link($start);
@@ -446,9 +466,9 @@
function set($arr,$page)
{
global $script;
-
+
list(,$alias,,$name,$this->param) = $this->splice($arr);
-
+
if (preg_match('/^([^#]+)(#[A-Za-z][\w-]*)$/',$this->param,$matches))
{
list(,$this->param,$this->anchor) = $matches;
@@ -457,7 +477,7 @@
$this->url = ($url === FALSE) ?
$script.'?'.rawurlencode('[['.$name.':'.$this->param.']]') :
htmlspecialchars($url);
-
+
return parent::setParam(
$page,
htmlspecialchars($name.':'.$this->param),
@@ -475,7 +495,7 @@
class Link_bracketname extends Link
{
var $anchor,$refer;
-
+
function Link_bracketname($start)
{
parent::Link($start);
@@ -483,7 +503,7 @@
function get_pattern()
{
global $WikiName,$BracketName;
-
+
$s2 = $this->start + 2;
return <<