変動する#contentsが参照する飛び先も固定アンカーを飛び先にしてしまおうという改造。
見出しに2つもアンカーが設定されるのはかっこわるいので(笑い)
また、固定アンカーに飛んだときに、見出しタイトルが複数行に表示されていると、タイトルの頭が隠れてしまうという問題も同時に解決させる。
aname.inc.phpにidを出力しないnoidオプションを追加。 aname.inc.phpについてはcvsに取り込まれた。
--- 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形式の変動アンカーを参照するので、いちおう互換性はとれてるはず。
if ($params['title'] and preg_match('/^(\*{1,3})/',$line,$matches)) { if (($id = make_heading($line)) == '') { $id = LS2_CONTENT_HEAD.$anchor++; } $level = strlen($matches[1]); ls2_list_push($params,$level + strlen($level)); array_push($params['result'], '<li><a href="'.$href.'#'.$id.'">'.$line.'</a>'); } else if ($params['include'] and preg_match('/^#include\((.+)\)/',$line,$matches) and is_page($matches[1])) { ls2_get_headings($matches[1],$params,$level + 1,TRUE); }
function getAnchor($text,$level) { global $top,$_symbol_anchor; if (($id = make_heading($text,FALSE)) == '') { $anchor = ''; $id = "content_{$this->id}_{$this->count}"; } else { $anchor = " &aname($id,super,full,noid)\{$_symbol_anchor}; &edit(,$id);"; } $this->count++; $this->contents_last = &$this->contents_last->add(new Contents_UList($text,$level,$id)); return array($text. $anchor, $this->count > 1 ? $top : '', $id); }
function replace_contents($arr) { $contents = "<div class=\"contents\">\n"; // $contents .= "<a id=\"contents_{$this->id}\"></a>"; $contents .= $this->contents->toString(); $contents .= "</div>\n"; array_shift($arr); return ($arr[1] != '') ? $contents.join('',$arr) : $contents; }
foreach (get_source($page) as $line) { if ($params['title'] && preg_match('/^(\*{1,3})/', $line, $matches)) { $id = make_heading($line); $level = strlen($matches[1]); if ($id == '') { $id = PLUGIN_LS2_ANCHOR_PREFIX . $anchor; } $anchor++; plugin_ls2_list_push($params, $level + strlen($level)); array_push($params['result'], '<li><a href="' . $href . $id . '">' . $line . '</a>'); } else if ($params['include'] && preg_match('/^#include\((.+)\)/', $line, $matches) && is_page($matches[1])) {