interrobang Posted October 27, 2014 Share Posted October 27, 2014 TextformatterMakeLinks This Textformatter module is just a wrapper around the method fHTML::makeLinks from flourishlib (http://flourishlib.com/api/fHTML#makeLinks) The following description is basically just slightly modified copy from the official flourishlib documetation (http://flourishlib.com/docs/fHTML): The Textformatter will parse through a string and create HTML links out of anything that resembles a URL or email address, as long as it is not already part of an tag. Here is an example of it in action: If you put this text into a textarea inputfield which uses this textformatter Example 1: www.example.com. Example 2: https://example.com.'>https://example.com. Example 3: john@example.com. Example 4: ftp://john:password@example.com.'>ftp://john:password@example.com. Example 5: www.example.co.uk. Example 6: john@example.co.uk. Example 7: <a href="http://example.com">http://example.com</a>. The output would be: Example 1: <a href="http://www.example.com">www.example.com</a>. Example 2: <a href="https://example.com">https://example.com</a>. Example 3: <a href="mailto:john@example.com">john@example.com</a>. Example 4: <a href="ftp://john:password@example.com">ftp://john:password@example.com</a>. Example 5: <a href="http://www.example.co.uk">www.example.co.uk</a>. Example 6: <a href="mailto:john@example.co.uk">john@example.co.uk</a>. Example 7: <a href="http://example.com">http://example.com</a>. Downloadhttps://github.com/phlppschrr/TextformatterMakeLinks http://modules.processwire.com/modules/textformatter-make-links/ 14 Link to comment Share on other sites More sharing options...
Mike Rockett Posted October 31, 2014 Share Posted October 31, 2014 Looks cool to me! One thing: perhaps auto email obfuscation is in order? Link to comment Share on other sites More sharing options...
Philipp Posted October 31, 2014 Share Posted October 31, 2014 Couldn't this be done with this Textformatter afterwards? http://modules.processwire.com/modules/email-obfuscation/ 2 Link to comment Share on other sites More sharing options...
Mike Rockett Posted October 31, 2014 Share Posted October 31, 2014 @Phillip - True, but I just feel that it would be handy to have it in one module... That's just my opinion. Link to comment Share on other sites More sharing options...
interrobang Posted October 31, 2014 Author Share Posted October 31, 2014 Thanks for giving my module a try. I see that it would be handy in your case, but I think it's better to keep these Textformatters separate. Not everybody wants obfuscation, and having separate Formatters is way more flexible. 2 Link to comment Share on other sites More sharing options...
ICF Church Posted October 9, 2017 Share Posted October 9, 2017 Works with 3.0 Link to comment Share on other sites More sharing options...
gmclelland Posted May 1, 2019 Share Posted May 1, 2019 Love the module, but is there any way you could fix the trailing slash(/) on urls not being recognized? Link to comment Share on other sites More sharing options...
interrobang Posted May 2, 2019 Author Share Posted May 2, 2019 16 hours ago, gmclelland said: Love the module, but is there any way you could fix the trailing slash(/) on urls not being recognized? Unfortunately, I'm not very good with regex, but I still tried to solve the problem. Can you please give it a try if the result is better if you remove the \b in lines 52 and 53? Has anyone here with more regex experience an idea what problems could arise from this change? Link to comment Share on other sites More sharing options...
gmclelland Posted May 2, 2019 Share Posted May 2, 2019 Thanks interrobang. I didn't know much about regex either, but I'm learning now! That worked with detecting urls with/without an ending slash, but then I noticed that email addresses were no longer linked. test@something.com gets converted into a link "something.com." I even tried removing the other \b, but it didn't make a difference. Edit: Actually, after playing around some more... If I remove the \b in lines 52 only, everything seems to work correctly. Urls with/without trailing slashes and email addresses are now linked correctly. Can you test that on your end? Do you want me to submit a pull request for this change? Link to comment Share on other sites More sharing options...
interrobang Posted May 3, 2019 Author Share Posted May 3, 2019 So far I haven't found a fully working regex. My test case is this. You you or anybody else finds a solution a PR is more than welcome. $test = "<h2>Fully URLs</h2>"; $test .= "http://domain.com <br>"; $test .= "http://domain.com/ <br>"; $test .= "http://domain.com/dir <br>"; $test .= "(http://domain.com/dir) <br>"; $test .= "http://domain.com/dir/ <br>"; $test .= "http://domain.com/dir/?a=1 <br>"; $test .= "<h2>www. domains</h2>"; $test .= "www.domain.com <br>"; $test .= "www.domain.com/ <br>"; $test .= "www.domain.com/dir <br>"; $test .= "www.domain.com/dir/ <br>"; $test .= "www.domain.com/dir/?a=1 <br>"; $test .= "<h2>flourishlib Examples</h2>"; $test .= "Example 1: www.example.com.<br>"; $test .= "Example 2: https://example.com.<br>"; $test .= "Example 3: john@example.com.<br>"; $test .= "Example 4: ftp://john:password@example.com.<br>"; $test .= "Example 5: www.example.co.uk.<br>"; $test .= "Example 6: john@example.co.uk.<br>"; $test .= 'Example 7: <a href="http://example.com">http://example.com</a>.'; wireModules("TextformatterMakeLinks")->format($test); echo $test; Link to comment Share on other sites More sharing options...
gmclelland Posted May 3, 2019 Share Posted May 3, 2019 Hmm... Thanks for the test data. I see the problem as well with the www. urls with a trailing slash. I'll see if I can come up with a alternative regex that works. Link to comment Share on other sites More sharing options...
gmclelland Posted May 3, 2019 Share Posted May 3, 2019 I just remembered that Drupal has a URL filter built into core. Maybe we can somehow use it's code since it's been battle tested? ...with credit of course. https://api.drupal.org/api/drupal/modules!filter!filter.module/function/_filter_url/7.x - For Drupal 7 In the comments of the Drupal module, it interestingly states "Each type must be processed separately, as there is no one regular expression that could possibly match all of the cases in one pass." There is also https://github.com/thephpleague/uri-parser that looks like it may help, but that would require composer to install the library? Also, I've been testing different regex patterns with https://regex101.com. It's the best regex tester I've found so far. 1 Link to comment Share on other sites More sharing options...
gmclelland Posted May 6, 2019 Share Posted May 6, 2019 @interrobang - can you try this regex? '~ \b([a-z]{3,}://[a-z0-9%\$\-_.+!*;/?:@=&\'\#,]+[a-z0-9\$\-_+!*;/?:@=&\'\#,])\b\/? | # Fully URLs \b(www\.(?:[a-z0-9\-]+\.)+[a-z]{2,}(?:/[a-z0-9%\$\-_.+!*;/?:@=&\'\#,]+[a-z0-9\$\-_+!*;/?:@=&\'\#,])?)\b\/? | # www. domains \b([a-z0-9\\.+\'_\\-]+@(?:[a-z0-9\\-]+\.)+[a-z]{2,})\b # email addresses ~ix' It looks like it works. All I did was add the "\/?" to the end of the first two lines. If it works, I'll make a pull request. 2 Link to comment Share on other sites More sharing options...
interrobang Posted May 7, 2019 Author Share Posted May 7, 2019 Thank you! This looks good to me! If you make a PR I will merge and release a new version. Link to comment Share on other sites More sharing options...
gmclelland Posted May 7, 2019 Share Posted May 7, 2019 Here you go https://github.com/phlppschrr/TextformatterMakeLinks/pull/3 1 Link to comment Share on other sites More sharing options...
interrobang Posted May 7, 2019 Author Share Posted May 7, 2019 Perfect! Thank you! I have merged your PR. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now