Jump to content

TextformatterMakeLinks


interrobang
 Share

Recommended Posts

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>.
Download

https://github.com/phlppschrr/TextformatterMakeLinks

http://modules.processwire.com/modules/textformatter-make-links/

  • Like 14
Link to comment
Share on other sites

  • 2 years later...
  • 1 year later...
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

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

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

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.

  • Like 1
Link to comment
Share on other sites

@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.

  • Like 2
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...