Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ the following to your `composer.json` file:

{
"require": {
"nicl/silex-autolink": "1.0.*"
"nicl/silex-autolink": "2.0.*"
}
}

Expand All @@ -41,4 +41,4 @@ the silex-autolink root directory run:
phpunit

(You may need to adapt the phpunit command and paths depending on your
configuration.)
configuration.)
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
}
],
"require": {
"silex/silex": "1.0.*@dev",
"silex/silex": "2.0.*@dev",
"twig/twig": ">=1.8,<2.0-dev"
},
"autoload": {
"psr-0": { "Nicl": "src/" }
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
"dev-master": "2.0-dev"
}
}
}
}
53 changes: 2 additions & 51 deletions src/Nicl/Autolink.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,8 @@
*
* Identifying URLs within a string is actually quite difficult.
*/
class Autolink
class Autolink implements AutolinkInterface
{
/**
* Permissive match if begins with protocal
*/
const PROTOCAL = '((ftp|https?)://[-\p{L}\p{N}]+(\.[\p{L}\p{N}_][-\p{L}\p{N}_]*)+)';

/**
* Match when protocal missing
*
* Wikipedia helpfully lists available top-level domains.
* @link http://en.wikipedia.org/wiki/List_of_Internet_top-level_domains
*/
const NO_PROTOCAL = '(([\p{L}\p{N}]+ \. )+
( aero\b
| asia\b
| biz\b
| cat\b
| com\b
| coop\b
| edu\b
| gov\b
| info\b
| int\b
| jobs\b
| mil\b
| mobi\b
| museum\b
| name\b
| net\b
| org\b
| pro\b
| tel\b
| travel\b
| xxx\b
| [a-z][a-z]\b
))';

/**
* Definition of (optional) port
*/
const PORT = '(:\d+)?';

/**
* Pragmatic definition of path
*
* Note, the whitelist for ending characters which excludes several valid
* characters.
*/
const PATH = '(((/[\pL\pN-._~:/?#\[\]@!$&\'()*+,;=]*) *)?[\pL\pN_&=#\\/])?';

/**
* @var string
*
Expand Down Expand Up @@ -92,4 +43,4 @@ public function autolink($txt)

return preg_replace($pattern, $replacement, $txt);
}
}
}
67 changes: 67 additions & 0 deletions src/Nicl/AutolinkInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Nicl;

/**
* Interface for Autolink parsers
*/
interface AutolinkInterface
{
/**
* Permissive match if begins with protocal
*/
const PROTOCAL = '((ftp|https?)://[-\p{L}\p{N}]+(\.[\p{L}\p{N}_][-\p{L}\p{N}_]*)+)';

/**
* Match when protocal missing
*
* Wikipedia helpfully lists available top-level domains.
* @link http://en.wikipedia.org/wiki/List_of_Internet_top-level_domains
*/
const NO_PROTOCAL = '(([\p{L}\p{N}]+ \. )+
( aero\b
| asia\b
| biz\b
| cat\b
| com\b
| coop\b
| edu\b
| gov\b
| info\b
| int\b
| jobs\b
| mil\b
| mobi\b
| museum\b
| name\b
| net\b
| org\b
| pro\b
| tel\b
| travel\b
| xxx\b
| [a-z][a-z]\b
))';

/**
* Definition of (optional) port
*/
const PORT = '(:\d+)?';

/**
* Pragmatic definition of path
*
* Note, the whitelist for ending characters which excludes several valid
* characters.
*/
const PATH = '(((/[\pL\pN-._~:/?#\[\]@!$&\'()*+,;=]*) *)?[\pL\pN_&=#\\/])?';

/**
* Process string and convert all URLs to html links
*
* @param string $txt
*
* @return string
*/
public function autolink($txt);
}
25 changes: 11 additions & 14 deletions src/Nicl/Silex/AutolinkServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Nicl\Silex;

use Silex\Application;
use Silex\ServiceProviderInterface;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Nicl\Autolink;
use Nicl\Twig\Extension\AutolinkTwigExtension;

Expand All @@ -15,18 +15,15 @@ class AutolinkServiceProvider implements ServiceProviderInterface
/**
* {@inheritdoc}
*/
public function register(Application $app)
public function register(Container $app)
{
$app['twig'] = $app->share($app->extend('twig', function($twig, $app) {
$twig->addExtension(new AutolinkTwigExtension(new Autolink()));
return $twig;
}));
}
$app['autolink.parser'] = function () {
return new Autolink();
}

/**
* {@inheritdoc}
*/
public function boot(Application $app)
{
$app['twig'] = $app->extend('twig', function($twig, $app) {
$twig->addExtension(new AutolinkTwigExtension($app['autolink.parser']));
return $twig;
});
}
}
}
8 changes: 4 additions & 4 deletions src/Nicl/Twig/Extension/AutolinkTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Nicl\Twig\Extension;

use Nicl\Autolink;
use Nicl\AutolinkInterface;

/**
* Twig Autolink extension
Expand All @@ -14,11 +14,11 @@ class AutolinkTwigExtension extends \Twig_Extension
/**
* Public constructor
*
* @param Autolink $parser
* @param AutolinkInterface $parser
*
* @return AutolinkTwigExtension
*/
public function __construct(Autolink $parser)
public function __construct(AutolinkInterface $parser)
{
$this->parser = $parser;
}
Expand Down Expand Up @@ -53,4 +53,4 @@ public function getName()
{
return 'autolink';
}
}
}