Skip to content

Instantly share code, notes, and snippets.

@ralphbean
Forked from lmacken/linkify.py
Created February 21, 2012 19:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ralphbean/1878233 to your computer and use it in GitHub Desktop.
Save ralphbean/1878233 to your computer and use it in GitHub Desktop.
A script to linkify URLs in text
#!/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 <lmacken@redhat.com>
# 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