Skip to content

Instantly share code, notes, and snippets.

@sbp
Created January 7, 2011 13:51
Show Gist options
  • Save sbp/769471 to your computer and use it in GitHub Desktop.
Save sbp/769471 to your computer and use it in GitHub Desktop.
Convert plain text to simple hypertext
#!/usr/bin/env python
'hyperion - Convert Text to Hypertext'
author = 'Sean B. Palmer, inamidst.com'
modified = '2010-12-10 22:59'
import sys, os.path
def encode(text):
text = text.replace('&', '&')
text = text.replace('<', '&lt;')
return text.replace('>', '&gt;')
def main():
arg = sys.argv[1]
if not arg.endswith('.txt'):
raise ValueError
outfn = arg[:-4] + '.html'
if os.path.exists(outfn):
raise IOError
o = open(outfn, 'w')
print >> o, '''\
<!DOCTYPE html>
<title>Text</title>
<style>
html { font: 12px/16px sans-serif }
body { margin: 3em; max-width: 70ex }
</style>
'''
f = open(arg)
for line in f:
print >> o, encode(line) + '<br>'
f.close()
o.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment