-
-
Save ralphbean/1878233 to your computer and use it in GitHub Desktop.
A script to linkify URLs in text
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 python | |
# A simple script that can be used to turn URLs into links | |
# | |
# Using within a pipeline: | |
# | |
# $ echo 'http://lewk.org' | linkify | |
# <a href="http://lewk.org">http://lewk.org</a> | |
# | |
# Using on a file: | |
# | |
# $ linkify filename | |
# <a href="http://lewk.org">http://lewk.org</a> | |
# | |
# Author: Luke Macken <[email protected]> | |
# License: GPLv3 | |
import re, sys, fileinput | |
regex = re.compile(r'.*(https?:\/\/\S+).*') | |
def main(): | |
for line in fileinput.input(): | |
match = regex.match(line) | |
if match: | |
match = match.groups(0)[0] | |
line = line.replace(match, '<a href="%s">%s</a>' % (match, match)) | |
print line, | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment