Skip to content

Commit

Permalink
autoindex の処理を改善。id属性を見出し要素自体に与えるようになった。
Browse files Browse the repository at this point in the history
  • Loading branch information
tomk79 committed Apr 28, 2023
1 parent 210bb7e commit dd2112f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ $ chmod -R 777 ./caches
### Pickles Framework v2.1.16 (リリース日未定)

- `$conf->tagline` を追加した。
- `autoindex` の処理を改善。id属性を見出し要素自体に与えるようになった。

### Pickles Framework v2.1.15 (2023年4月22日)

Expand Down
43 changes: 33 additions & 10 deletions php/processors/autoindex/autoindex.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,42 @@ public function apply_autoindex( $content ){
$indexCounter[$tmp['anch']] = 1;

$tmp['headlevel'] = intval($matched[3]);
if( $tmp['headlevel'] ){ // 引っかかったのが見出しの場合
array_push( $index , $tmp );
}

$content .= $matched[1];
if( $tmp['headlevel'] ){ // 引っかかったのが見出しの場合
// $content .= $this->back2top();
$content .= '<span';
$content .= ' id="'.htmlspecialchars($tmp['anch'] ?? "").'"';
$content .= '></span>';
if( $tmp['headlevel'] ){
// 引っかかったのが見出しの場合
$hxSrc = $matched[2];
if( preg_match('/^\<h'.preg_quote($tmp['headlevel'],'/').'(.*?)\>(.*)\<\/h'.preg_quote($tmp['headlevel'],'/').'\>$/i', $hxSrc, $matched_hxSrc) ){
$hxSrc_attrs = $matched_hxSrc[1];
$hxSrc_innerHTML = $matched_hxSrc[2];
if( strlen($hxSrc_attrs) ){
if( preg_match('/\sid\=(\"|\')(.*?)\1/i', ' '.$hxSrc_attrs, $matched_hxSrc_id) ){
// id属性をもともと持っていたら
$tmp['anch'] = $matched_hxSrc_id[2];
}else{
$hxSrc_attrs = trim($hxSrc_attrs).' id="'.htmlspecialchars($tmp['anch']).'"';
}
}else{
$hxSrc_attrs = trim($hxSrc_attrs).' id="'.htmlspecialchars($tmp['anch']).'"';
}
$hxSrc_attrs = ' '.trim($hxSrc_attrs);
$content .= '<h'.$tmp['headlevel'].$hxSrc_attrs.'>'.$hxSrc_innerHTML.'</h'.$tmp['headlevel'].'>';
unset($hxSrc_attrs, $hxSrc_innerHTML, $matched_hxSrc, $matched_hxSrc_id);
}else{
$content .= '<span';
$content .= ' id="'.htmlspecialchars($tmp['anch'] ?? "").'"';
$content .= '></span>';
$content .= $hxSrc;
}
}else{
$content .= $matched[2];
}
$content .= $matched[2];
$tmp_cont = $matched[5];

if( $tmp['headlevel'] ){
// 引っかかったのが見出しの場合
array_push( $index , $tmp );
}
}
set_time_limit(30);

Expand Down Expand Up @@ -186,6 +209,6 @@ public function apply_autoindex( $content ){
$content = preg_replace( '/'.$this->func_data_memos.'/s' , $anchorlinks , $content );
return $content;

} // apply_autoindex();
}

}
20 changes: 10 additions & 10 deletions tests/autoindexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ public function testAutoIndex(){
'/standard/.px_execute.php',
'/sample_pages/page1/3.html'
);
// var_dump($output);
$this->assertTrue( $this->utils->common_error( $output ) );
$this->assertEquals( preg_match( '/'.preg_quote('<h2>markdown記法のテストページです。</h2>', '/').'/s', $output ), 1 );
$this->assertEquals( preg_match( '/'.preg_quote('<span id="hash_見出し文字"></span><h3>見出し文字</h3>', '/').'/s', $output ), 1 );
$this->assertEquals( preg_match( '/'.preg_quote('<span id="hash_0_見出し文字"></span><h3>見出し文字</h3>', '/').'/s', $output ), 0 );
$this->assertEquals( preg_match( '/'.preg_quote('<span id="hash_1_見出し文字"></span><h3>見出し文字</h3>', '/').'/s', $output ), 0 );
$this->assertEquals( preg_match( '/'.preg_quote('<span id="hash_2_見出し文字"></span><h3>見出し文字</h3>', '/').'/s', $output ), 1 );
$this->assertEquals( preg_match( '/'.preg_quote('<span id="hash_3_3_見出し文字"></span><h3>3_見出し文字</h3>', '/').'/s', $output ), 1 );
$this->assertEquals( preg_match( '/'.preg_quote('<span id="hash_4_2_見出し文字"></span><h3>2_見出し文字</h3>', '/').'/s', $output ), 1 );

$this->assertTrue( $this->utils->common_error( $output ) );
$this->assertEquals( preg_match( '/'.preg_quote('<h2 id="hash_markdown記法のテストページです。">markdown記法のテストページです。</h2>', '/').'/s', $output ), 1 );
$this->assertEquals( preg_match( '/'.preg_quote('<h3 id="hash_見出し文字">見出し文字</h3>', '/').'/s', $output ), 1 );
$this->assertEquals( preg_match( '/'.preg_quote('<h3 id="hash_0_見出し文字">見出し文字</h3>', '/').'/s', $output ), 0 );
$this->assertEquals( preg_match( '/'.preg_quote('<h3 id="hash_1_見出し文字">見出し文字</h3>', '/').'/s', $output ), 0 );
$this->assertEquals( preg_match( '/'.preg_quote('<h3 id="hash_2_見出し文字">見出し文字</h3>', '/').'/s', $output ), 1 );
$this->assertEquals( preg_match( '/'.preg_quote('<h3 id="hash_3_3_見出し文字">3_見出し文字</h3>', '/').'/s', $output ), 1 );
$this->assertEquals( preg_match( '/'.preg_quote('<h3 id="hash_4_2_見出し文字">2_見出し文字</h3>', '/').'/s', $output ), 1 );
$this->assertEquals( preg_match( '/'.preg_quote('<a href="#cont-already-set-id">もともとid属性を持っている</a>', '/').'/s', $output ), 1 );
$this->assertEquals( preg_match( '/'.preg_quote('<h2 id="cont-already-set-id">もともとid属性を持っている</h2>', '/').'/s', $output ), 1 );


// 後始末
// $this->assertTrue( false );
$output = $this->utils->px_execute( '/standard/.px_execute.php', '/?PX=clearcache' );
$this->assertTrue( $this->utils->common_error( $output ) );
$this->assertTrue( !is_dir( __DIR__.'/testData/standard/caches/p/' ) );
Expand Down
4 changes: 4 additions & 0 deletions tests/testData/standard/sample_pages/page1/3.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@
### 2_見出し文字

重複する見出しのテスト。

<h2 id="cont-already-set-id">もともとid属性を持っている</h2>

もともとid属性を持っているテスト。

0 comments on commit dd2112f

Please sign in to comment.