Fix MarkupFixer, del files, mod headers

Этот коммит содержится в:
Igor V Belousov 2015-02-04 23:42:05 +03:00
родитель e8d9b87ced
Коммит c00cb51d29
16 изменённых файлов: 156 добавлений и 343 удалений

4
.gitignore поставляемый
Просмотреть файл

@ -1,6 +1,4 @@
# files # files
vendor/ vendor/
phpunit.xml phpunit.xml
composer.lock
# IDE stuff
.idea/workspace.xml

Просмотреть файл

@ -1,6 +1,13 @@
# ChangeLog - PHP TableOfContents (TOC) # ChangeLog - PHP TableOfContents (TOC)
All notable changes to this project are documented in this file. 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 <http://www.w3.org/TR/html4/types.html#type-name>
## Original 1.0 - 2014-12-30
### Added ### Added
- Initial Version - Initial Version

Просмотреть файл

@ -1,6 +1,7 @@
The MIT License (MIT) The MIT License (MIT)
Copyright (c) 2013-2014 Casey McLaughlin 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 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 this software and associated documentation files (the "Software"), to deal in

Просмотреть файл

@ -30,7 +30,7 @@ Install via [Composer](http://getcomposer.org/) by including the following in yo
{ {
"require": { "require": {
"caseyamcl/toc": "~1.0", "igorvbelousov/toc": "~1.0",
} }
} }

Просмотреть файл

@ -10,6 +10,7 @@
{ {
"name": "Igor V Belousov", "name": "Igor V Belousov",
"email": "igor@belousovv.ru", "email": "igor@belousovv.ru",
"homepage": "http://belousovv.ru",
"role": "Developer" "role": "Developer"
} }

245
composer.lock сгенерированный
Просмотреть файл

@ -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": []
}

Просмотреть файл

@ -1,20 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./tests/bootstrap.php" colors="true">
<testsuites>
<testsuite name="PHP TOC Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>

Просмотреть файл

@ -4,10 +4,11 @@
* PHP TableOfContents Library * PHP TableOfContents Library
* *
* @license http://opensource.org/licenses/MIT * @license http://opensource.org/licenses/MIT
* @link https://github.com/caseyamcl/toc * @link https://github.com/igorvbelousov/toc
* @version 1.0 * @version 1.0
* @package caseyamcl/toc * @package igorvbelousov/toc
* @author Casey McLaughlin <caseyamcl@gmail.com> * @author Casey McLaughlin <caseyamcl@gmail.com>
* @author Igor V Belousov <igor@belousovv.ru>
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.

Просмотреть файл

@ -4,11 +4,11 @@
* PHP TableOfContents Library * PHP TableOfContents Library
* *
* @license http://opensource.org/licenses/MIT * @license http://opensource.org/licenses/MIT
* @link https://github.com/caseyamcl/toc * @link https://github.com/igorvbelousov/toc
* @version 1.0 * @version 1.0
* @package caseyamcl/toc * @package igorvbelousov/toc
* @author Casey McLaughlin <caseyamcl@gmail.com> * @author Casey McLaughlin <caseyamcl@gmail.com>
* @author Igor V Belouosv <igor@belousovv.ru> * @author Igor V Belousov <igor@belousovv.ru>
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
@ -19,38 +19,18 @@
namespace TOC; namespace TOC;
use RuntimeException; use RuntimeException;
use Sunra\PhpSimple\HtmlDomParser;
/** /**
* TOC Markup Fixer adds `id` attributes to all H1...H6 tags where they do not * TOC Markup Fixer adds `id` attributes to all H1...H6 tags where they do not
* already exist * already exist
* *
* @author Casey McLaughlin <caseyamcl@gmail.com> * @author Casey McLaughlin <caseyamcl@gmail.com>
* @author Igor V Belousov <igor@belousovv.ru>
*/ */
class MarkupFixer class MarkupFixer
{ {
use HeaderTagInterpreter; use HeaderTagInterpreter;
private $ids = array();
// ---------------------------------------------------------------
/**
* @var HtmlDomParser
*/
private $domParser;
// ---------------------------------------------------------------
/**
* Constructor
*
* @param HtmlDomParser $domParser
*/
public function __construct(HtmlDomParser $domParser = null)
{
$this->domParser = $domParser ?: new HtmlDomParser();
}
// ---------------------------------------------------------------
/** /**
* Fix markup * Fix markup
@ -59,34 +39,83 @@ class MarkupFixer
* @param int $topLevel * @param int $topLevel
* @param int $depth * @param int $depth
* @return string Markup with added IDs * @return string Markup with added IDs
* @throws RuntimeException
*/ */
public function fix($markup, $topLevel = 1, $depth = 6) public function fix($markup, $topLevel = 1, $depth = 6)
{ {
$sluggifier = new UniqueSluggifier();
$tags = $this->determineHeaderTags($topLevel, $depth); $tags = $this->determineHeaderTags($topLevel, $depth);
$parsed = $this->domParser->str_get_html($markup); preg_replace_callback(
'/<.*?id[\t,\ ]*?=[\t,\ ]*?[\'|"](.*?)[\'|"].*?>/ui',
// Runtime exception for bad code function ($m)
if ( ! $parsed) { {
throw new RuntimeException("Could not parse HTML"); array_push($this->ids, $m[1]);
} },
$markup);
// Extract items $markup = preg_replace_callback(
foreach ($parsed->find(implode(', ', $tags)) as $tag) { '/(<h['.
implode('|', $tags)
// Ignore tags that already have IDs .'](\s+[^>]*?)?)>(.*?)<\/h/ui',
if ($tag->id) { 'self::fix_callback',
continue; $markup
}
$tag->id = preg_replace('/^[-0-9._:]+/', '',
$sluggifier->slugify($tag->title ?: $tag->plaintext)
); );
$this->ids = array();
return $markup;
} }
return (string) $parsed; /**
* 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)
{
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].'</h';
}
return $value[1].' id="'.
preg_replace(
'/^[-0-9._:]/',
'N',
$this->CheckUniqId($sluggifier->slugify($value[3]))
)
.'">'.$value[3].'</h';
}
return $value[0];
}
/**
* 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);
} }
} }

Просмотреть файл

@ -4,10 +4,11 @@
* PHP TableOfContents Library * PHP TableOfContents Library
* *
* @license http://opensource.org/licenses/MIT * @license http://opensource.org/licenses/MIT
* @link https://github.com/caseyamcl/toc * @link https://github.com/igorvbelousov/toc
* @version 1.0 * @version 1.0
* @package caseyamcl/toc * @package igorvbelousov/toc
* @author Casey McLaughlin <caseyamcl@gmail.com> * @author Casey McLaughlin <caseyamcl@gmail.com>
* @author Igor V Belousov <igor@belousovv.ru>
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.

Просмотреть файл

@ -4,10 +4,11 @@
* PHP TableOfContents Library * PHP TableOfContents Library
* *
* @license http://opensource.org/licenses/MIT * @license http://opensource.org/licenses/MIT
* @link https://github.com/caseyamcl/toc * @link https://github.com/igorvbelousov/toc
* @version 1.0 * @version 1.0
* @package caseyamcl/toc * @package igorvbelousov/toc
* @author Casey McLaughlin <caseyamcl@gmail.com> * @author Casey McLaughlin <caseyamcl@gmail.com>
* @author Igor V Belousov <igor@belousovv.ru>
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.

Просмотреть файл

@ -4,10 +4,11 @@
* PHP TableOfContents Library * PHP TableOfContents Library
* *
* @license http://opensource.org/licenses/MIT * @license http://opensource.org/licenses/MIT
* @link https://github.com/caseyamcl/toc * @link https://github.com/igorvbelousov/toc
* @version 1.0 * @version 1.0
* @package caseyamcl/toc * @package igorvbelousov/toc
* @author Casey McLaughlin <caseyamcl@gmail.com> * @author Casey McLaughlin <caseyamcl@gmail.com>
* @author Igor V Belousov <igor@belousovv.ru>
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.

Просмотреть файл

@ -4,10 +4,11 @@
* PHP TableOfContents Library * PHP TableOfContents Library
* *
* @license http://opensource.org/licenses/MIT * @license http://opensource.org/licenses/MIT
* @link https://github.com/caseyamcl/toc * @link https://github.com/igorvbelousov/toc
* @version 1.0 * @version 1.0
* @package caseyamcl/toc * @package igorvbelousov/toc
* @author Casey McLaughlin <caseyamcl@gmail.com> * @author Casey McLaughlin <caseyamcl@gmail.com>
* @author Igor V Belousov <igor@belousovv.ru>
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * 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 = "<h1>1 Number and ID</h1><h2>: 123</h2>";
$this->assertEquals(
'<h1 id="N-number-and-id">1 Number and ID</h1><h2 id="N23">: 123</h2>',
$obj->fix($html, 1, 2)
);
}
// ---------------------------------------------------------------
public function testFixDoesNotDuplicateIdsWhenFixing() public function testFixDoesNotDuplicateIdsWhenFixing()
{ {
$obj = new MarkupFixer(); $obj = new MarkupFixer();
$html = "<h1>FooBar</h1><h2>FooBar</h2><h3>FooBar</h3>"; $html = "<h1>FooBar</h1><h2>FooBar</h2><h3>FooBar</h3><div id='foobar'>
</div>";
$this->assertEquals( $this->assertEquals(
'<h1 id="foobar">FooBar</h1><h2 id="foobar-1">FooBar</h2><h3 id="foobar-2">FooBar</h3>', '<h1 id="foobar-1">FooBar</h1><h2 id="foobar-2">FooBar</h2><h3 id="foobar-3">FooBar</h3><div id=\'foobar\'>
</div>',
$obj->fix($html, 1, 3) $obj->fix($html, 1, 3)
); );
} }
@ -60,6 +77,20 @@ class MarkupFixerTest extends \PHPUnit_Framework_TestCase
{ {
$obj = new MarkupFixer(); $obj = new MarkupFixer();
$html = "<h1>No ID</h1><h2 title = 'b'>Existing ID</h2><h3>Ignored</h3>";
$this->assertEquals(
'<h1 id="no-id">No ID</h1><h2 title = \'b\' id="b">Existing ID</h2><h3>Ignored</h3>',
$obj->fix($html, 1, 2)
);
}
// ---------------------------------------------------------------
public function testFixUsesTitleAttributeWhenAvailableiWithOutTabs()
{
$obj = new MarkupFixer();
$html = "<h1>No ID</h1><h2 title='b'>Existing ID</h2><h3>Ignored</h3>"; $html = "<h1>No ID</h1><h2 title='b'>Existing ID</h2><h3>Ignored</h3>";
$this->assertEquals( $this->assertEquals(

Просмотреть файл

@ -4,10 +4,11 @@
* PHP TableOfContents Library * PHP TableOfContents Library
* *
* @license http://opensource.org/licenses/MIT * @license http://opensource.org/licenses/MIT
* @link https://github.com/caseyamcl/toc * @link https://github.com/igorvbelousov/toc
* @version 1.0 * @version 1.0
* @package caseyamcl/toc * @package igorvbelousov/toc
* @author Casey McLaughlin <caseyamcl@gmail.com> * @author Casey McLaughlin <caseyamcl@gmail.com>
* @author Igor V Belousov <igor@belousovv.ru>
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.

Просмотреть файл

@ -2,11 +2,12 @@
/** /**
* toc * toc
* *
* @license ${LICENSE_LINK} * @license http://opensource.org/licenses/MIT
* @link ${PROJECT_URL_LINK} * @link https://github.com/igorvbelousov/toc
* @version ${VERSION} * @version 1.0
* @package ${PACKAGE_NAME} * @package igorvbelousov/toc
* @author Casey McLaughlin <caseyamcl@gmail.com> * @author Casey McLaughlin <caseyamcl@gmail.com>
* @author Igor V Belousov <igor@belousovv.ru>
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.

Просмотреть файл

@ -6,7 +6,12 @@
/** /**
* PHP TOC Unit Tests Bootstrap File * 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 <caseyamcl@gmail.com> * @author Casey McLaughlin <caseyamcl@gmail.com>
* @author Igor V Belousov <igor@belousovv.ru>
*/ */
//Files to ensure exist //Files to ensure exist