Skip to content

Instantly share code, notes, and snippets.

@sbp
Created May 20, 2011 12:44
Show Gist options
  • Save sbp/982821 to your computer and use it in GitHub Desktop.
Save sbp/982821 to your computer and use it in GitHub Desktop.
Convert plain text files to hypertext
#!/usr/bin/env python
import sys
name = sys.argv[1]
print '<!DOCTYPE html>'
print '<title>%s</title>' % name
print '<meta charset="utf-8">'
print '<link rel="stylesheet" href="style.css">'
print
with open(name) as f:
for line in f:
line = line.rstrip('\r\n')
line = line.replace('&', '&amp;')
line = line.replace('<', '&lt;')
if line.startswith(' '):
a = len(line)
line = line.lstrip(' ')
line = ('&nbsp;' * (len(line) - a)) + line
print line + '<br>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment