blob: 19f9545dfcc84aa59256b43c969c7ec19545d0cf [file] [log] [blame] [edit]
#!/usr/bin/env python3
import os.path
import sys
import tempfile
from collections import defaultdict
def count_chars(l):
o = defaultdict(lambda: 0)
for i in l:
o[i] += 1
if '\n' in o:
del o['\n']
return dict(o)
def main(argv):
assert len(argv) == 1, argv
fname = argv[0]
assert fname.endswith('.rst'), fname
assert os.path.exists(fname), fname
output = ['',]
with open(fname) as f:
for l in f:
output.append(l)
c = count_chars(l)
if len(c) != 1:
continue
header = list(c.keys())[0]
if header not in ['-', '=', '+', '~']:
print("Possible header?", repr(l))
continue
lastline = output[-2]
if len(lastline) <= 4:
continue
oheader = (header * (len(lastline)-1))+'\n'
output[-1] = oheader
with open(fname, 'w') as f:
f.write("".join(output[1:]))
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))