Skip to content

Commit

Permalink
Add ignore level option.
Browse files Browse the repository at this point in the history
  • Loading branch information
fumikito committed May 26, 2020
1 parent db27725 commit 72c58e5
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
9 changes: 9 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
tests/ export-ignore
bin/ export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.travis.yml export-ignore
.wp-env.json
docker-compose.yml export-ignore
phpunit.xml.dist export-ignore
theme-customizer.php export-ignore
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor/
tests/index.html
composer.lock
/wp/
4 changes: 2 additions & 2 deletions bin/install-wp-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ install_test_suite() {
if [ ! -d $WP_TESTS_DIR ]; then
# set up testing suite
mkdir -p $WP_TESTS_DIR
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data
svn co --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes
svn co --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data
fi

if [ ! -f wp-tests-config.php ]; then
Expand Down
6 changes: 5 additions & 1 deletion src/Kunoichi/TocGenerator/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ public function parse_html( $html ) {
}
foreach( $dom_nodes as $hn ) {
/** @var \DOMNode $dom */
if ( ! preg_match( '/h[1-6]/u', strtolower( $hn->nodeName ) ) ) {
if ( ! preg_match( '/h([1-6])/u', strtolower( $hn->nodeName ), $matches ) ) {
continue;
}
list( $match, $level ) = $matches;
if ( $this->ignore_deeper && ( (int) $level > $this->ignore_deeper ) ) {
continue;
}
$items[] = new Item( $hn );
Expand Down
16 changes: 15 additions & 1 deletion tests/test-toc.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,22 @@ public function test_toc_generation() {
HTML;

$toc = $parser->get_toc( $parser->parse_html( $parser->add_link_html( $html ) ) );
$path = __DIR__ . '/index.html';
file_put_contents( $path, sprintf( "<!DOCTYPE html>\n<html>%s</html>" , $toc) );

file_put_contents( __DIR__ . '/index.html', sprintf( "<!DOCTYPE html>\n<html>%s</html>" , $toc) );
$this->assertTrue( file_exists( $path ) );
}

public function test_depth() {
$parser = new Parser( 3, 3 );
$html = <<<HTML
<h1>Title</h1>
<h2>Section</h2>
<h3>Section</h3>
<h4>This will be ignored.</h4>
HTML;
$toc = $parser->get_toc( $parser->parse_html( $parser->add_link_html( $html ) ) );
$this->assertTrue( false === strpos( $toc, 'This will be ignored.' ) );
}


Expand Down

0 comments on commit 72c58e5

Please sign in to comment.