Pukiwiki 見出しタイトル化

2017/03/10 Programming::PukiWiki

Pukiwiki改造: 見出しタイトル化

最初に出現したレベル1見出しをページタイトル(titleとh1)に反映する改造です。

改造前のconvert_htmlにbodycache用の記述も含まれているので、使っていない場合は無視してください。

  • lib/convert_html.php
    • function convert_html
      • 変更前
        function convert_html($lines)
        {
        	global $vars, $digest;
        	global $bodycache_status;
        	static $contents_id = 0;
        
        	if ( $bodycache_status === 'cached' ) {
        		$contents_id += 90 ;
        	}
        
        	// Set digest
        	$digest = md5(join('', get_source($vars['page'])));
        
        	if (! is_array($lines)) $lines = explode("\n", $lines);
        
        	$body = new Body(++$contents_id);
        	$body->parse($lines);
        
        	return $body->toString();
        }
        
      • 変更後: 「global $title, $page;」と「if ( isset( $body->title ) )~」を追記。
        function convert_html($lines)
        {
        	global $vars, $digest;
        	global $title, $page;
        	global $bodycache_status;
        	static $contents_id = 0;
        	
        	if ( $bodycache_status === 'cached' ) {
        		$contents_id += 90 ;
        	}
        
        	// Set digest
        	$digest = md5(join('', get_source($vars['page'])));
        
        	if (! is_array($lines)) $lines = explode("\n", $lines);
        
        	$body = new Body(++$contents_id);
        	$body->parse($lines);
        	
        	if ( isset( $body->title ) ){
        		$page = $title = htmlsc(trim($body->title));
        	}
        
        	return $body->toString();
        }
        
    • Heading::function Heading
      • 変更前
        	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(Factory_Inline($text));
        		$this->level++; // h2,h3,h4
        	}
        
      • 変更後
        	function Heading(& $root, $text)
        	{
        		parent::Element();
        
        		$this->level = min(3, strspn($text, '*'));
        
        		if( $this->level === 1 && !isset( $root->title ) && $root->id === 1 ){
        			$root->title = preg_replace('|\s*\[#.*|', '', $text);
        			$root->title = preg_replace('|^\*\s*|', '', $root->title);
        			$root->title = preg_replace('|\[\[([^>\]]+)(>[^>\]]+)?\]\]|', '$1', $root->title);
        		}
        
        		list($text, $this->msg_top, $this->id) = $root->getAnchor($text, $this->level);
        		$this->insert(Factory_Inline($text));
        		$this->level++; // h2,h3,h4
        	}
        
OK キャンセル 確認 その他