/////////////////////////////////////////////////
// PukiWiki - Yet another WikiWikiWeb clone.
//
-// $Id: config.php,v 1.1 2003/03/07 06:45:26 panda Exp $
+// $Id: config.php,v 1.2 2003/03/10 11:32:23 panda Exp $
//
/*
* ¥×¥é¥°¥¤¥ó¤ÎÀßÄê¤òPukiWiki¤Î¥Ú¡¼¥¸¤Ëµ½Ò¤¹¤ë
function read()
{
$this->objs = array();
- $title = '';
- $obj = &$this->get_object($title);
+ $obj = &new ConfigTable();
foreach (get_source($this->page) as $line)
{
- if ($line != '' and $line{0} == '|' and preg_match('/^\|(.+)\|\s*$/',$line,$matches))
+ if ($line == '')
{
- $obj->add_value(explode('|',$matches[1]));
+ continue;
+ }
+
+ $head = $line{0};
+ $level = strspn($line,$head);
+
+ if ($level > 3)
+ {
+ $obj->add_line($line);
+ continue;
+ }
+
+ if ($head == '*')
+ {
+ if ($level == 1)
+ {
+ $this->objs[$obj->title] = &$obj;
+ $obj = &new ConfigTable();
+ $obj->add_line($line);
+ }
+ else
+ {
+ if (!is_a($obj,'ConfigTable_Direct'))
+ {
+ $obj = &new ConfigTable_Direct($obj->after);
+ }
+ $obj->set_key($line);
+ }
}
- else if ($line != '' and $line{0} == '*')
+ else if ($head == '-' and $level > 1)
{
- $level = strspn($line,'*');
- $title = trim(substr($line,$level));
- $obj = &$this->get_object($title,$level);
+ if (!is_a($obj,'ConfigTable_Direct'))
+ {
+ $obj = &new ConfigTable_Direct($obj->after);
+ }
+ $obj->add_value($line);
+ }
+ else if ($head == '|' and preg_match('/^\|(.+)\|\s*$/',$line,$matches))
+ {
+ if (!is_a($obj,'ConfigTable_Sequential'))
+ {
+ $obj = &new ConfigTable_Sequential($obj->after);
+ }
+ $obj->add_value(explode('|',$matches[1]));
}
else
{
$obj->add_line($line);
}
}
- $this->objs[$title] = &$obj;
+ $this->objs[$obj->title] = &$obj;
}
// ÇÛÎó¤ò¼èÆÀ¤¹¤ë
function &get($title)
$obj->values[] = $value;
}
// ¥ª¥Ö¥¸¥§¥¯¥È¤ò¼èÆÀ¤¹¤ë(¤Ê¤¤¤È¤¤Ïºî¤ë)
- function &get_object($title,$level=1)
+ function &get_object($title)
{
if (!array_key_exists($title,$this->objs))
{
- $this->objs[$title] = &new ConfigTable(str_repeat('*',$level).$title."\n");
+ $this->objs[$title] = &new ConfigTable(array('*'.trim($title)."\n"));
}
return $this->objs[$title];
}
//ÇÛÎóÃͤòÊÝ»ý¤¹¤ë¥¯¥é¥¹
class ConfigTable
{
+ // ¥Æ¡¼¥Ö¥ë¤Î̾Á°
+ var $title = '';
+ // ¥Ú¡¼¥¸¤ÎÆâÍÆ(¥Æ¡¼¥Ö¥ë°Ê³°¤ÎÉôʬ)
+ var $before = array();
// ¼èÆÀ¤·¤¿ÃͤÎÇÛÎó
var $values = array();
// ¥Ú¡¼¥¸¤ÎÆâÍÆ(¥Æ¡¼¥Ö¥ë°Ê³°¤ÎÉôʬ)
- var $line;
+ var $after = array();
- function ConfigTable($title)
+ function ConfigTable($lines=NULL)
{
- $this->line = $title;
+ if ($lines !== NULL)
+ {
+ $this->title = trim(substr($lines[0],strspn($lines[0],'*')));
+ $this->before = $lines;
+ }
}
+ // ÀâÌÀ¤ÎÄɲÃ
+ function add_line($line)
+ {
+ $this->after[] = $line;
+ }
+ function toString()
+ {
+ return join('',$this->before).join('',$this->after);
+ }
+}
+class ConfigTable_Sequential extends ConfigTable
+{
// ¹Ô¤ÎÄɲÃ
function add_value($value)
{
$this->values[] = (count($value) == 1) ? $value[0] : $value;
}
- // ÀâÌÀ¤ÎÄɲÃ
- function add_line($line)
- {
- $this->line .= $line;
- }
// ½ñ¼°²½
function toString()
{
- $retval = $this->line;
+ $retval = join('',$this->before);
if (is_array($this->values))
{
foreach ($this->values as $value)
$retval .= "|$value|\n";
}
}
- $retval .= "\n"; // ¶õ¹Ô :)
-
- return $retval;
+ return $retval.join('',$this->after);
+ }
+}
+class ConfigTable_Direct extends ConfigTable
+{
+ // ¼èÆÀ¤·¤¿¥¡¼¤ÎÇÛÎó¡£½é´ü²½»þ¤Ë»ÈÍѤ¹¤ë¡£
+ var $_keys = array();
+
+ // ¥¡¼¤ÎÀßÄê
+ function set_key($line)
+ {
+ $level = strspn($line,'*');
+ $this->_keys[$level] = trim(substr($line,$level));
+ }
+ // ¹Ô¤ÎÄɲÃ
+ function add_value($line)
+ {
+ $level = strspn($line,'-');
+ $arr = &$this->values;
+ for ($n = 2; $n <= $level; $n++)
+ {
+ $arr = &$arr[$this->_keys[$n]];
+ }
+ $arr[] = trim(substr($line,$level));
+ }
+ // ½ñ¼°²½
+ function toString($values = NULL,$level = 2)
+ {
+ $retval = join('',$this->before);
+ if ($values == NULL)
+ {
+ $retval = parent::toString();
+ $values = &$this->values;
+ }
+ foreach ($values as $key=>$value)
+ {
+ if (is_array($value))
+ {
+ $retval .= str_repeat('*',$level).$key."\n";
+ $retval .= $this->toString($value,$level + 1);
+ }
+ else
+ {
+ $retval .= str_repeat('-',$level).$value."\n";
+ }
+ }
+ return $retval.join('',$this->after);
}
}
?>