From c00cb51d29d6071bc9bd79979192fe3be12910f0 Mon Sep 17 00:00:00 2001 From: Igor V Belousov Date: Wed, 4 Feb 2015 23:42:05 +0300 Subject: [PATCH] Fix MarkupFixer, del files, mod headers --- .gitignore | 4 +- CHANGELOG.md | 9 +- LICENSE | 1 + README.md | 2 +- composer.json | 1 + composer.lock | 245 ----------------------------------- phpunit.xml.dist | 20 --- src/HeaderTagInterpreter.php | 5 +- src/MarkupFixer.php | 139 ++++++++++++-------- src/TocGenerator.php | 5 +- src/TocTwigExtension.php | 5 +- src/UniqueSluggifier.php | 5 +- tests/MarkupFixerTest.php | 39 +++++- tests/TocGeneratorTest.php | 5 +- tests/Util/TOCTestUtils.php | 9 +- tests/bootstrap.php | 5 + 16 files changed, 156 insertions(+), 343 deletions(-) delete mode 100644 composer.lock delete mode 100644 phpunit.xml.dist diff --git a/.gitignore b/.gitignore index 249d009..7a43728 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,4 @@ # files vendor/ phpunit.xml - -# IDE stuff -.idea/workspace.xml \ No newline at end of file +composer.lock diff --git a/CHANGELOG.md b/CHANGELOG.md index f9addbb..e37362c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ # ChangeLog - PHP TableOfContents (TOC) All notable changes to this project are documented in this file. -## 1.0 - 2014-12-30 +## Forked 1.0 - 2015-02-04 +### Fixed + +- MarkupFixer - Don't remove EOL-symbol +- MarkupFixer - Id naming + +## Original 1.0 - 2014-12-30 ### Added + - Initial Version diff --git a/LICENSE b/LICENSE index 1c141b4..d7932dc 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,7 @@ The MIT License (MIT) Copyright (c) 2013-2014 Casey McLaughlin +Copyright (c) 2015 Igor V Belousov Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/README.md b/README.md index 2ea4426..90908cd 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Install via [Composer](http://getcomposer.org/) by including the following in yo { "require": { - "caseyamcl/toc": "~1.0", + "igorvbelousov/toc": "~1.0", } } diff --git a/composer.json b/composer.json index b09ba67..75d91bb 100644 --- a/composer.json +++ b/composer.json @@ -10,6 +10,7 @@ { "name": "Igor V Belousov", "email": "igor@belousovv.ru", + "homepage": "http://belousovv.ru", "role": "Developer" } diff --git a/composer.lock b/composer.lock deleted file mode 100644 index d571520..0000000 --- a/composer.lock +++ /dev/null @@ -1,245 +0,0 @@ -{ - "_readme": [ - "This file locks the dependencies of your project to a known state", - "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", - "This file is @generated automatically" - ], - "hash": "01914fa61e59dbe4c7c3d1772643be14", - "packages": [ - { - "name": "cocur/slugify", - "version": "v1.0", - "source": { - "type": "git", - "url": "https://github.com/cocur/slugify.git", - "reference": "2c929c56997e663747be9bb635e58db918a0134f" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/cocur/slugify/zipball/2c929c56997e663747be9bb635e58db918a0134f", - "reference": "2c929c56997e663747be9bb635e58db918a0134f", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "require-dev": { - "codeclimate/php-test-reporter": "dev-master", - "laravel/framework": "~4.1", - "mockery/mockery": "~0.9", - "phpunit/phpunit": "~3.7", - "sami/sami": "~1.3", - "satooshi/php-coveralls": "0.6.*", - "silex/silex": "~1.2", - "symfony/dependency-injection": "~2.4", - "symfony/http-kernel": "~2.4", - "twig/twig": "~1", - "zendframework/zend-modulemanager": "~2.2", - "zendframework/zend-servicemanager": "~2.2", - "zendframework/zend-view": "~2.2" - }, - "type": "library", - "autoload": { - "psr-4": { - "Cocur\\Slugify\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Ivo Bathke", - "email": "ivo.bathke@gmail.com" - }, - { - "name": "Florian Eckerstorfer", - "email": "florian@eckerstorfer.co", - "homepage": "https://florian.ec" - } - ], - "description": "Converts a string into a slug.", - "keywords": [ - "slug", - "slugify" - ], - "time": "2014-11-26 22:45:14" - }, - { - "name": "knplabs/knp-menu", - "version": "v2.0.1", - "source": { - "type": "git", - "url": "https://github.com/KnpLabs/KnpMenu.git", - "reference": "5758d0026d7ed00c8dd4727e413918cf2dc74c1a" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/KnpLabs/KnpMenu/zipball/5758d0026d7ed00c8dd4727e413918cf2dc74c1a", - "reference": "5758d0026d7ed00c8dd4727e413918cf2dc74c1a", - "shasum": "" - }, - "require": { - "php": ">=5.3.0" - }, - "require-dev": { - "pimple/pimple": "1.0.*", - "silex/silex": "1.0.*", - "twig/twig": ">=1.2,<2.0-dev" - }, - "suggest": { - "pimple/pimple": "for the built-in implementations of the menu provider and renderer provider", - "silex/silex": "for the integration with your silex application", - "twig/twig": "for the TwigRenderer and the integration with your templates" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "2.0.x-dev" - } - }, - "autoload": { - "psr-0": { - "Knp\\Menu\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Christophe Coevoet", - "email": "stof@notk.org" - }, - { - "name": "KnpLabs", - "homepage": "http://knplabs.com" - }, - { - "name": "Symfony Community", - "homepage": "https://github.com/KnpLabs/KnpMenu/contributors" - } - ], - "description": "An object oriented menu library", - "homepage": "http://knplabs.com", - "keywords": [ - "menu", - "tree" - ], - "time": "2014-08-01 09:50:16" - }, - { - "name": "sunra/php-simple-html-dom-parser", - "version": "v1.5.0", - "source": { - "type": "git", - "url": "https://github.com/sunra/php-simple-html-dom-parser.git", - "reference": "a0b80ace086c7e09085669205e1b3c2c9c7a453c" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sunra/php-simple-html-dom-parser/zipball/a0b80ace086c7e09085669205e1b3c2c9c7a453c", - "reference": "a0b80ace086c7e09085669205e1b3c2c9c7a453c", - "shasum": "" - }, - "require": { - "php": ">=5.3.2" - }, - "type": "library", - "autoload": { - "psr-0": { - "Sunra\\PhpSimple\\HtmlDomParser": "Src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Sunra", - "email": "sunra@yandex.ru", - "homepage": "http://github.com/sunra" - } - ], - "description": "Composer adaptation of: A HTML DOM parser written in PHP5+ let you manipulate HTML in a very easy way! Require PHP 5+. Supports invalid HTML. Find tags on an HTML page with selectors just like jQuery. Extract contents from HTML in a single line.", - "homepage": "https://github.com/sunra/php-simple-html-dom-parser", - "keywords": [ - "dom", - "html", - "parser" - ], - "time": "2013-05-04 14:32:03" - } - ], - "packages-dev": [ - { - "name": "twig/twig", - "version": "v1.17.0", - "source": { - "type": "git", - "url": "https://github.com/twigphp/Twig.git", - "reference": "2493970fa4d587eca73f77e6d8bd48a8bdd4c608" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/twigphp/Twig/zipball/2493970fa4d587eca73f77e6d8bd48a8bdd4c608", - "reference": "2493970fa4d587eca73f77e6d8bd48a8bdd4c608", - "shasum": "" - }, - "require": { - "php": ">=5.2.4" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.17-dev" - } - }, - "autoload": { - "psr-0": { - "Twig_": "lib/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Fabien Potencier", - "email": "fabien@symfony.com", - "homepage": "http://fabien.potencier.org", - "role": "Lead Developer" - }, - { - "name": "Armin Ronacher", - "email": "armin.ronacher@active-4.com", - "role": "Project Founder" - }, - { - "name": "Twig Team", - "homepage": "http://twig.sensiolabs.org/contributors", - "role": "Contributors" - } - ], - "description": "Twig, the flexible, fast, and secure template language for PHP", - "homepage": "http://twig.sensiolabs.org", - "keywords": [ - "templating" - ], - "time": "2015-01-14 10:15:49" - } - ], - "aliases": [], - "minimum-stability": "stable", - "stability-flags": [], - "prefer-stable": false, - "prefer-lowest": false, - "platform": { - "php": ">=5.4" - }, - "platform-dev": [] -} diff --git a/phpunit.xml.dist b/phpunit.xml.dist deleted file mode 100644 index 605c384..0000000 --- a/phpunit.xml.dist +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - ./tests - - - - - - ./ - - ./tests - ./vendor - - - - \ No newline at end of file diff --git a/src/HeaderTagInterpreter.php b/src/HeaderTagInterpreter.php index 868b400..f937051 100644 --- a/src/HeaderTagInterpreter.php +++ b/src/HeaderTagInterpreter.php @@ -4,10 +4,11 @@ * PHP TableOfContents Library * * @license http://opensource.org/licenses/MIT - * @link https://github.com/caseyamcl/toc + * @link https://github.com/igorvbelousov/toc * @version 1.0 - * @package caseyamcl/toc + * @package igorvbelousov/toc * @author Casey McLaughlin + * @author Igor V Belousov * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/MarkupFixer.php b/src/MarkupFixer.php index c0477b1..e11e6b7 100644 --- a/src/MarkupFixer.php +++ b/src/MarkupFixer.php @@ -4,11 +4,11 @@ * PHP TableOfContents Library * * @license http://opensource.org/licenses/MIT - * @link https://github.com/caseyamcl/toc + * @link https://github.com/igorvbelousov/toc * @version 1.0 - * @package caseyamcl/toc + * @package igorvbelousov/toc * @author Casey McLaughlin - * @author Igor V Belouosv + * @author Igor V Belousov * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -19,75 +19,104 @@ namespace TOC; use RuntimeException; -use Sunra\PhpSimple\HtmlDomParser; /** * TOC Markup Fixer adds `id` attributes to all H1...H6 tags where they do not * already exist * * @author Casey McLaughlin + * @author Igor V Belousov */ class MarkupFixer { use HeaderTagInterpreter; + private $ids = array(); - // --------------------------------------------------------------- + /** + * Fix markup + * + * @param string $markup + * @param int $topLevel + * @param int $depth + * @return string Markup with added IDs + */ + public function fix($markup, $topLevel = 1, $depth = 6) + { - /** - * @var HtmlDomParser - */ - private $domParser; + $tags = $this->determineHeaderTags($topLevel, $depth); + preg_replace_callback( + '/<.*?id[\t,\ ]*?=[\t,\ ]*?[\'|"](.*?)[\'|"].*?>/ui', + function ($m) + { + array_push($this->ids, $m[1]); + }, + $markup); + $markup = preg_replace_callback( + '/(]*?)?)>(.*?)<\/h/ui', + 'self::fix_callback', + $markup + ); + $this->ids = array(); - // --------------------------------------------------------------- + return $markup; + } - /** - * Constructor - * - * @param HtmlDomParser $domParser - */ - public function __construct(HtmlDomParser $domParser = null) + /** + * Replace callback for fix function + * + * @param array $value + * @return string Markup with added IDs + */ + private function fix_callback($value) + { + $sluggifier = new UniqueSluggifier(); + if (preg_match('/id/ui',$value[1])==false) { - $this->domParser = $domParser ?: new HtmlDomParser(); - } + if (preg_match('/title/ui',$value[1])) { + $title = preg_replace_callback( + '/.*title[\t,\ ]*?=[\t,\ ]*?[\'|"](.*?)[\'|"]/ui', + function ($m){return $m[1];}, + $value[1]); + return $value[1].' id="'. + preg_replace( + '/^[-0-9._:]/', + 'N', + $sluggifier->slugify($title) + ) + .'">'.$value[3].''.$value[3].'determineHeaderTags($topLevel, $depth); - $parsed = $this->domParser->str_get_html($markup); - - // Runtime exception for bad code - if ( ! $parsed) { - throw new RuntimeException("Could not parse HTML"); - } - - // Extract items - foreach ($parsed->find(implode(', ', $tags)) as $tag) { - - // Ignore tags that already have IDs - if ($tag->id) { - continue; - } - - $tag->id = preg_replace('/^[-0-9._:]+/', '', - $sluggifier->slugify($tag->title ?: $tag->plaintext) - ); - } - - return (string) $parsed; - } + /** + * Check and return unique id + * + * @param string $id Check id + * @param integer $number parametr for recursive check + * + * @return string unique id + */ + private function CheckUniqId($id,$number=0){ + $tmpid = ($number>0)?$id.'-'.$number:$id; + if (array_search($tmpid, $this->ids)===false) + { + array_push($this->ids,$tmpid); + return $tmpid; + } + $number++; + return $this->CheckUniqId($id,$number); + } } /* EOF: TocMarkupFixer.php */ diff --git a/src/TocGenerator.php b/src/TocGenerator.php index a98ab39..0d60c36 100644 --- a/src/TocGenerator.php +++ b/src/TocGenerator.php @@ -4,10 +4,11 @@ * PHP TableOfContents Library * * @license http://opensource.org/licenses/MIT - * @link https://github.com/caseyamcl/toc + * @link https://github.com/igorvbelousov/toc * @version 1.0 - * @package caseyamcl/toc + * @package igorvbelousov/toc * @author Casey McLaughlin + * @author Igor V Belousov * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/TocTwigExtension.php b/src/TocTwigExtension.php index 193a612..5a75f83 100644 --- a/src/TocTwigExtension.php +++ b/src/TocTwigExtension.php @@ -4,10 +4,11 @@ * PHP TableOfContents Library * * @license http://opensource.org/licenses/MIT - * @link https://github.com/caseyamcl/toc + * @link https://github.com/igorvbelousov/toc * @version 1.0 - * @package caseyamcl/toc + * @package igorvbelousov/toc * @author Casey McLaughlin + * @author Igor V Belousov * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/UniqueSluggifier.php b/src/UniqueSluggifier.php index 195d358..2d1d47f 100644 --- a/src/UniqueSluggifier.php +++ b/src/UniqueSluggifier.php @@ -4,10 +4,11 @@ * PHP TableOfContents Library * * @license http://opensource.org/licenses/MIT - * @link https://github.com/caseyamcl/toc + * @link https://github.com/igorvbelousov/toc * @version 1.0 - * @package caseyamcl/toc + * @package igorvbelousov/toc * @author Casey McLaughlin + * @author Igor V Belousov * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/tests/MarkupFixerTest.php b/tests/MarkupFixerTest.php index f2053ae..ed7e6e1 100644 --- a/tests/MarkupFixerTest.php +++ b/tests/MarkupFixerTest.php @@ -4,10 +4,11 @@ * PHP TableOfContents Library * * @license http://opensource.org/licenses/MIT - * @link https://github.com/caseyamcl/toc + * @link https://github.com/igorvbelousov/toc * @version 1.0 - * @package caseyamcl/toc + * @package igorvbelousov/toc * @author Casey McLaughlin + * @author Igor V Belousov * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -42,14 +43,30 @@ class MarkupFixerTest extends \PHPUnit_Framework_TestCase // --------------------------------------------------------------- + public function testFixAddsIdsW3C() + { + $obj = new MarkupFixer(); + + $html = "

1 Number and ID

: 123

"; + + $this->assertEquals( + '

1 Number and ID

: 123

', + $obj->fix($html, 1, 2) + ); + } + + // --------------------------------------------------------------- + public function testFixDoesNotDuplicateIdsWhenFixing() { $obj = new MarkupFixer(); - $html = "

FooBar

FooBar

FooBar

"; + $html = "

FooBar

FooBar

FooBar

+
"; $this->assertEquals( - '

FooBar

FooBar

FooBar

', + '

FooBar

FooBar

FooBar

+
', $obj->fix($html, 1, 3) ); } @@ -60,6 +77,20 @@ class MarkupFixerTest extends \PHPUnit_Framework_TestCase { $obj = new MarkupFixer(); + $html = "

No ID

Existing ID

Ignored

"; + + $this->assertEquals( + '

No ID

Existing ID

Ignored

', + $obj->fix($html, 1, 2) + ); + } + + // --------------------------------------------------------------- + + public function testFixUsesTitleAttributeWhenAvailableiWithOutTabs() + { + $obj = new MarkupFixer(); + $html = "

No ID

Existing ID

Ignored

"; $this->assertEquals( diff --git a/tests/TocGeneratorTest.php b/tests/TocGeneratorTest.php index ceaa53d..26403b9 100644 --- a/tests/TocGeneratorTest.php +++ b/tests/TocGeneratorTest.php @@ -4,10 +4,11 @@ * PHP TableOfContents Library * * @license http://opensource.org/licenses/MIT - * @link https://github.com/caseyamcl/toc + * @link https://github.com/igorvbelousov/toc * @version 1.0 - * @package caseyamcl/toc + * @package igorvbelousov/toc * @author Casey McLaughlin + * @author Igor V Belousov * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/tests/Util/TOCTestUtils.php b/tests/Util/TOCTestUtils.php index e003f84..bac9d19 100644 --- a/tests/Util/TOCTestUtils.php +++ b/tests/Util/TOCTestUtils.php @@ -2,11 +2,12 @@ /** * toc * - * @license ${LICENSE_LINK} - * @link ${PROJECT_URL_LINK} - * @version ${VERSION} - * @package ${PACKAGE_NAME} + * @license http://opensource.org/licenses/MIT + * @link https://github.com/igorvbelousov/toc + * @version 1.0 + * @package igorvbelousov/toc * @author Casey McLaughlin + * @author Igor V Belousov * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 3272123..a6e2a93 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -6,7 +6,12 @@ /** * PHP TOC Unit Tests Bootstrap File * + * @license http://opensource.org/licenses/MIT + * @link https://github.com/igorvbelousov/toc + * @version 1.0 + * @package igorvbelousov/toc * @author Casey McLaughlin + * @author Igor V Belousov */ //Files to ensure exist