自動で見出しを元にセクション分けして欲しい

メッセージ

セクション毎に字下げを行いたくて調べていたら、 次のような質問/解答を見つけました。

official:続・質問箱/68

私もちょうどこのような出力が欲しかったのですが、 解決していないようだったので自分で改造を施してみました。 せっかくですのでパッチをお送りします。(「$Id: convert_html.php,v 1.7 2005/01/21 13:17:16 henoheno Exp $」からのパッチ)

--- convert_html.php.original	2006-01-07 00:46:55.000000000 +0900
+++ convert_html.php	2006-01-07 16:54:15.691970424 +0900
@@ -206,7 +206,7 @@
 // *** Heading3
 class Heading extends Element
 {
-	var $level;
+	var $level; //'*'の数
 	var $id;
 	var $msg_top;
 
@@ -217,7 +217,6 @@
 		$this->level = min(3, strspn($text, '*'));
 		list($text, $this->msg_top, $this->id) = $root->getAnchor($text, $this->level);
 		$this->insert(Factory_Inline($text));
-		$this->level++; // h2,h3,h4
 	}
 
 	function & insert(& $obj)
@@ -234,7 +233,7 @@
 	function toString()
 	{
 		return $this->msg_top .  $this->wrap(parent::toString(),
-			'h' . $this->level, ' id="' . $this->id . '"');
+			'h' . ($this->level + 1), ' id="' . $this->id . '"');
 	}
 }
 
@@ -294,6 +293,8 @@
 
 	function canContain(& $obj)
 	{
+		if (is_a($obj, 'Heading')) return FALSE;
+		if (is_a($obj, 'HRule')) return FALSE;
 		return (! is_a($obj, 'ListContainer')
 			|| ($this->tag == $obj->tag && $this->level == $obj->level));
 	}
@@ -349,6 +350,8 @@
 
 	function canContain(& $obj)
 	{
+		if (is_a($obj, 'Heading')) return FALSE;
+		if (is_a($obj, 'HRule')) return FALSE;
 		return (! is_a($obj, 'ListContainer') || ($obj->level > $this->level));
 	}
 
@@ -421,6 +424,8 @@
 
 	function canContain(& $obj)
 	{
+		if (is_a($obj, 'Heading')) return FALSE;
+		if (is_a($obj, 'HRule')) return FALSE;
 		return (! is_a($obj, get_class($this)) || $obj->level >= $this->level);
 	}
 
@@ -529,6 +534,13 @@
 
 		return $this->wrap(parent::toString(), $this->tag, $param, FALSE);
 	}
+
+	function canContain(& $obj)
+	{
+		if (is_a($obj, 'Heading')) return FALSE;
+		if (is_a($obj, 'HRule')) return FALSE;
+		return TRUE;
+	}
 }
 
 // | title1 | title2 | title3 |
@@ -557,6 +569,8 @@
 
 	function canContain(& $obj)
 	{
+		if (is_a($obj, 'Heading')) return FALSE;
+		if (is_a($obj, 'HRule')) return FALSE;
 		return is_a($obj, 'Table') && ($obj->col == $this->col);
 	}
 
@@ -675,6 +689,8 @@
 
 	function canContain(& $obj)
 	{
+		if (is_a($obj, 'Heading')) return FALSE;
+		if (is_a($obj, 'HRule')) return FALSE;
 		return is_a($obj, 'YTable') && ($obj->col == $this->col);
 	}
 
@@ -817,13 +833,20 @@
 
 			// Empty
 			if ($line == '') {
-				$this->last = & $this;
+				while (! is_null($this->last)
+				    && ! is_a($this->last, 'Body')
+				    && ! is_a($this->last, 'Section')){
+					$this->last = & $this->last->parent;
+				}
+				if (is_null($this->last)){
+					$this->last = & $this;
+				}
 				continue;
 			}
 
 			// Horizontal Rule
 			if (substr($line, 0, 4) == '----') {
-				$this->insert(new HRule($this, $line));
+				$this->last = & $this->last->add(new HRule($this, $line));
 				continue;
 			}
 
@@ -832,7 +855,7 @@
 
 			// Heading
 			if ($head == '*') {
-				$this->insert(new Heading($this, $line));
+				$this->last = & $this->last->add(new Section(new Heading($this, $line)));
 				continue;
 			}
 
@@ -952,4 +975,41 @@
 		$this->style = sprintf($_list_pad_str, $this->level, $margin, $margin);
 	}
 }
+
+
+
+
+//Section
+class Section extends Element
+{
+	var $level;
+
+	function Section(& $heading)
+	{
+		parent::Element();
+		$this->level = $heading->level;
+		$this->insert($heading);
+	}
+
+	function canContain($obj)
+	{
+		//上又は同じ階層のSectionは入れられない。
+		if( is_a($obj, 'Section') && $obj->level <= $this->level ){
+			return FALSE;
+		}
+		return TRUE;
+	}
+
+	function & insert(& $obj)
+	{
+		if (is_a($obj, 'Inline')) $obj = & $obj->toPara();
+		return parent::insert($obj);
+	}
+
+	function toString()
+	{
+		return $this->wrap(parent::toString(), 'div', ' class="section"');
+	}
+}
+
 ?>

デフォルトでこのような出力を行う事は、何らかの問題があるでしょうか?




トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2006-01-07 (土) 17:16:16
Site admin: PukiWiki Development Team

PukiWiki 1.5.4+ © 2001-2022 PukiWiki Development Team. Powered by PHP 8.2.12. HTML convert time: 0.176 sec.

SourceForge