Created
January 7, 2011 13:51
-
-
Save sbp/769471 to your computer and use it in GitHub Desktop.
Convert plain text to simple hypertext
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 | |
'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('<', '<') | |
return text.replace('>', '>') | |
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