Created
April 6, 2019 22:41
-
-
Save Alhadis/20ccc2da4f3dce698e50395a563574ce to your computer and use it in GitHub Desktop.
Match URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# Adopted from https://github.com/atom/language-hyperlink | |
use strict; | |
use warnings; | |
use v5.14; | |
my $matchURL = qr~ \b | |
# Protocol | |
( https? | |
| s?ftp | |
| ftps | |
| file | |
| smb | |
| git | |
| ssh | |
| rsync | |
| afp | |
| nfs | |
| (?:x-)?man(?:-page)? | |
| gopher | |
| txmt | |
| issue | |
| atom | |
) :// | |
# Path specifier | |
(?: | |
(?! \#\w*\#) | |
(?: [-:\@\w.,\~\%+_/?=\&\#;|!]) | |
)+ | |
# Don't include trailing punctuation | |
(?<![-.,?:\#;]) | |
~aix; | |
my $matchEmail = qr~ \b | |
mailto: (?: | |
(?! \#\w*\#) | |
(?: [-:\@\w.,\~\%+_/?=\&\#;|!]) | |
)+ | |
(?<![-.,?:\#;]) | |
~aix; | |
$matchURL = qr~$matchURL|$matchEmail~; | |
# Usage: | |
my $input = <<EOF; | |
Do this and go to https://some.site/file.html?okay=1. | |
http://deno.land? | |
mailto:this\@guy.com. | |
EOF | |
-t 1 | |
? $input =~ s/$matchURL/\e[4m$&\e[0m/g | |
: $input =~ s/$matchURL/<a href="$&">$&<\/a>/g; | |
say $input; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment