Added linkify filter #64

Open
wants to merge 5 commits into
from

Projects

None yet

5 participants

@darkbluesun

I searched everywhere for a decent linkify filter - couldn't believe such a useful filter didn't exist already!
Filter takes text with URLS and puts tags around the links.
If it does exist already, feel free to discard this code and tell me where I can get it!
Criticism welcome as this is my first ever pull request

@sstok sstok commented on an outdated diff Aug 19, 2012
lib/Twig/Extensions/Extension/Text.php
@@ -49,6 +50,14 @@ function twig_nl2br_filter($value, $sep = '<br />')
return str_replace("\n", $sep."\n", $value);
}
+function twig_linkify_filter($text)
+{
+ $text= preg_replace("/(^|[\n ])([\w]*?)((ht|f)tp(s)?:\/\/[\w]+[^ \,\"\n\r\t<]*)/is", "$1$2<a href=\"$3\" >$3</a>", $text);
+ $text= preg_replace("/(^|[\n ])([\w]*?)((www|ftp)\.[^ \,\"\t\n\r<]*)/is", "$1$2<a href=\"http://$3\" >$3</a>", $text);
+ $text= preg_replace("/(^|[\n ])([a-z0-9&\-_\.]+?)@([\w\-]+\.([\w\-\.]+)+)/i", "$1<a href=\"mailto:$2@$3\">$2@$3</a>", $text);
+ return($text);
@sstok
sstok Aug 19, 2012

missing newline before return. and return must be without () as its does not contain a condition.

@stof
Contributor
stof commented Aug 19, 2012

I don't think this should be part of Twig itself. It seems better to add it in the fabpot/Twig-extensions repository instead

@darkbluesun

Stof: it is...
Sstok: thanks, will do

@stof
Contributor
stof commented Aug 20, 2012

oups, sorry. I looked at it too fast and thought it was on Twig

@stof
Contributor
stof commented Aug 20, 2012

btw, if you could add some tests, it would be great

@darkbluesun

@stof can you point me to an example of a test - the documentation at the twig website isn't so verbose
http://twig.sensiolabs.org/doc/advanced.html#tests

@stof
Contributor
stof commented Sep 3, 2012

@darkbluesun This page is not about testing Twig but about ading new tests for the is operator in templates. :)
Look at the tests folder of the repo, and at the phpunit documentation

@blaugueux
Contributor

+1

@thewilkybarkid

@darkbluesun A bit late I know, but if you're using Symfony you can use the misd-service-development/linkify-bundle (if not, you should still be able to pick out the filter reasonably easily).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment