Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
soukicz committed Dec 2, 2016
0 parents commit 8053523
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
language: php
sudo: false
php:
- 5.6
- 7.0
- 7.1
- hhvm

cache:
directories:
- vendor
- $HOME/.composer/cache

before_script:
- composer install

script: vendor/bin/phpunit --coverage-clover=coverage.xml

after_script:
- bash -c 'if [ "$TRAVIS_PHP_VERSION" == "5.6" ] ; then php ocular.phar code-coverage:upload --format=php-clover coverage.xml; fi;'
29 changes: 29 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "soukicz/urltolink",
"description": "Convert URLs in HTML to links",
"minimum-stability": "stable",
"license": "MIT",
"keywords": [],
"authors": [
{
"name": "Petr Soukup",
"email": "soukup@simplia.cz"
}
],
"require": {
"php": ">=5.6.0"
},
"require-dev": {
"phpunit/phpunit": "~5.0"
},
"autoload": {
"psr-4": {
"Soukicz\\UrlToLink\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Soukicz\\TestUrlToLink\\": "tests/"
}
}
}
31 changes: 31 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="true"
backupStaticAttributes="false"
bootstrap="./tests/bootstrap.php"
colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="false"
convertWarningsToExceptions="true"
forceCoversAnnotation="false"

processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"

testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader"
verbose="false">
<testsuites>
<testsuite name="Client">
<directory>./tests/</directory>
</testsuite>
</testsuites>

<filter>
<blacklist>
<directory>vendor</directory>
</blacklist>
</filter>

</phpunit>
28 changes: 28 additions & 0 deletions src/Processor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
namespace Soukicz\UrlToLink;

class Processor {
/**
* @param string $html
* @return string
*/
public function convertUrlToLinksInHtml($html) {
$html = preg_replace_callback('~(<a .*?>.*?</a>)~i', function (array $match) {
return '<<<a<' . base64_encode($match[0]) . '>>>>';
}, $html);

$html = preg_replace_callback('~(<img .*?>)~i', function (array $match) {
return '<<<i<' . base64_encode($match[0]) . '>>>>';
}, $html);

$html = preg_replace_callback('~https?://[a-z0-9/.\?&=%#\[\]_+\-]+~i', function (array $match) {
return '<a href="' . $match[0] . '">' . $match[0] . '</a>';
}, $html);

$html = preg_replace_callback('~<<<[ai]<(.*?)>>>>~', function (array $match) {
return base64_decode($match[1]);
}, $html);

return $html;
}
}
16 changes: 16 additions & 0 deletions tests/ProcessorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace Soukicz\TestUrlToLink;


use Soukicz\UrlToLink\Processor;

class ProcessorTest extends \PHPUnit_Framework_TestCase {
function testDecodeSizes() {
$link = new Processor();

$this->assertEquals(
'abc <a href="https://www.jadi.cz/Damska-obuv/Kotnickova-obuv/Podzim-zima/?f[v][]=40&f[p][313][]=571&f[cena_do]=3170">https://www.jadi.cz/Damska-obuv/Kotnickova-obuv/Podzim-zima/?f[v][]=40&f[p][313][]=571&f[cena_do]=3170</a> def <a href="https://jadi.cz">https://jadi.cz</a>',
$link->convertUrlToLinksInHtml('abc https://www.jadi.cz/Damska-obuv/Kotnickova-obuv/Podzim-zima/?f[v][]=40&f[p][313][]=571&f[cena_do]=3170 def <a href="https://jadi.cz">https://jadi.cz</a>')
);
}
}
12 changes: 12 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
if(file_exists(__DIR__ . '/../vendor/autoload.php')) {
// dependencies were installed via composer - this is the main project
$classLoader = require __DIR__ . '/../vendor/autoload.php';
} elseif(file_exists(__DIR__ . '/../../../../../autoload.php')) {
// installed as a dependency in `vendor`
$classLoader = require __DIR__ . '/../../../../../autoload.php';
} else {
throw new Exception('Can\'t find autoload.php. Did you install dependencies via composer?');
}

0 comments on commit 8053523

Please sign in to comment.