PukiWiki/1.4/ちょっと便利に

固定アンカーを#contentsの飛び先のアンカーとして使う -- reimy

変動する#contentsが参照する飛び先も固定アンカーを飛び先にしてしまおうという改造。

見出しに2つもアンカーが設定されるのはかっこわるいので(笑い)

また、固定アンカーに飛んだときに、見出しタイトルが複数行に表示されていると、タイトルの頭が隠れてしまうという問題も同時に解決させる。

aname.inc.phpにidを出力しないnoidオプションを追加。 aname.inc.phpについてはcvsに取り込まれた。

必要条件
  • 必ずpukiwiki.ini.phpで$fixed_heading_anchor = 1に設定しておくこと。
改造箇所
  • convert_html.php
  • ls2.inc.php
  • aname.inc.php
--- convert_html.php	Sun Apr 27 22:04:42 2003
+++ convert_html.php.2	Sun Apr 27 22:11:48 2003
@@ -763,17 +763,18 @@
		if (preg_match('/^(.*)\[#([A-Za-z][\w-]+)\](.*)$/',$text,$matches))
		{
			$text = $matches[1].$matches[3];
-			$anchor = ' &aname('.$matches[2].',super,full){†};';
+			$anchor = ' &aname('.$matches[2].',super,full,noid){†};';
+			$id = $matches[2];
+		} else {
+			$id = "content_{$this->id}_{$this->count}";
		}
-		$id = "content_{$this->id}_{$this->count}";
		$this->count++;
		$this->contents_last =& $this->contents_last->add(new Contents_UList($text,$this->id,$level,$id));
		return array($text.$anchor,$this->count > 1 ? $top : '',$id);
	}
	function getContents()
	{
-		$contents  = "<a id=\"contents_{$this->id}\"></a>";
-		$contents .= $this->contents->toString();
+		$contents  = $this->contents->toString();
		return "<div class=\"contents\">\n$contents</div>\n";
	}
	function &insert(&$obj)
--- ls2.inc.php	Sun Apr 27 22:07:18 2003
+++ ls2.inc.php.2	Sun Apr 27 22:14:12 2003
@@ -194,8 +194,10 @@
			if (preg_match('/^(.*)\[(#[A-Za-z][\w-]+)\](.*)$/',$title,$matches))
			{
				$title = $matches[1].$matches[3];
+				$id = $matches[2];
+			} else {
+				$id = LS2_CONTENT_HEAD.$anchor++;
			}
-			$id = LS2_CONTENT_HEAD.$anchor++;
			$s_title = strip_htmltag(inline2(inline($title,TRUE)));
			ls2_list_push($params,$level + strlen($level));
			array_push($params['result'], '<li>'.$s_title.

aname側にidを設定せず<h2>側のidを生かしたことで、タイトルが複数行に表示される場合でもきちんとタイトルの頭に飛ぶことができる。改造前のようにaname側にidがあると、タイトルが複数行に表示されている場合、タイトルの頭が見えなくなるので、この点でもこの改造のメリットがある。

固定アンカーの存在しないページ(従来のページ)では、従来通り、#content_1_1形式の変動アンカーを参照するので、いちおう互換性はとれてるはず。




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

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

SourceForge