Skip to content

Instantly share code, notes, and snippets.

@edunham
Created January 14, 2014 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edunham/8424784 to your computer and use it in GitHub Desktop.
Save edunham/8424784 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
import sys
import re
def get_doku_table():
if len(sys.argv) < 2:
print """Please pass the name of the file containing your dokuwiki
table as an argument to this script."""
exit()
path = sys.argv[1]
fd = open(path)
raw = fd.read()
fd.close()
return raw
def get_headers_lines_widths(r):
lines = r.split('\n')
lines[0] = re.sub('\|', '^', lines[0])
for l in range(1, len(lines)):
lines[l] = re.sub('\^', '|', lines[l])
# the slice compensates for leading and trailing marks
headers = [h.strip() for h in lines[0].split('^')[1:-1]]
# slice of lines is to ignore headers
lines = [[c.strip(' ^') for c in l.split('|')[1:-1]] for l in lines[1:]]
cols = len(headers)
col_widths = [0]*cols
for h in range(cols):
col_widths[h] = len(headers[h])
for l in lines:
if len(l) == cols:
for c in range(cols):
w = len(l[c])
if w > col_widths[c]:
col_widths[c] = w
else:
lines.remove(l)
return headers, lines, col_widths
def line(cols, arr):
out = '|'
for c in range(len(cols)):
pad = arr[c] - len(cols[c])
out = out + ' ' + cols[c] + ' ' * pad + ' |'
return out
def symbols(s, arr):
out = '+'
for c in range(len(arr)):
# the magic 2 is for leading and trailing spaces in lines
out = out + s * (arr[c] + 2) + '+'
return out
def main():
r = get_doku_table()
h, l, w = get_headers_lines_widths(r)
print symbols('-', w)
print line(h, w)
print symbols('=', w)
for n in l:
print line(n, w)
print symbols('-', w)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment