Permalink
Browse files

Add escape.py and break.py scripts

  • Loading branch information...
kynikos committed Apr 6, 2015
1 parent 903445a commit d479cc08b47c250ced1c6cf4b664b1e5c81d4bb1
Showing with 54 additions and 0 deletions.
  1. +31 −0 break.py
  2. +23 −0 escape.py
View
@@ -0,0 +1,31 @@
#!/usr/bin/env python
import sys
fname = sys.argv[1]
LEN = 79
TAB = 4
def split_line(line):
rlen = LEN - TAB
split = line.split(" ")
newline = split[0]
for word in split[1:]:
newnewline = " ".join((newline, word))
if TAB + len(newnewline) < LEN:
newline = newnewline
else:
yield " " * TAB + newline + "\n"
newline = word
else:
yield " " * TAB + newline + "\n"
with open(fname, "r") as stream:
output = ""
for line in stream.readlines():
for newline in split_line(line[:-1]):
output += newline
with open(fname, "w") as stream:
stream.write(output)
View
@@ -0,0 +1,23 @@
#!/usr/bin/env python
import sys
import html
fname = sys.argv[1]
with open(fname, "r") as stream:
escaped = stream.read()
escaped = html.escape(escaped)
escnoamp = html.entities.entitydefs
del escnoamp["amp"]
for transl in escnoamp:
escaped = escaped.replace(html.entities.entitydefs[transl],
"&{};".format(transl))
escaped = escaped.encode('ascii', 'xmlcharrefreplace')
with open(fname, "wb") as stream:
stream.write(escaped)

0 comments on commit d479cc0

Please sign in to comment.