Skip to content

Instantly share code, notes, and snippets.

@e000
Created March 14, 2011 03:50
Show Gist options
  • Select an option

  • Save e000/868726 to your computer and use it in GitHub Desktop.

Select an option

Save e000/868726 to your computer and use it in GitHub Desktop.
from texttable import Texttable
import re
_ignParens = re.compile('\(.*?\)')
def countDelim(text, delim = ' | '):
text = _ignParens.sub('', text)
return text.count(delim)
def renderTable(input, sub = False):
if not input:
return []
garbage = []
if len(input) == 1 and sub:
return input
initial_row_count = countDelim(input[0])
if initial_row_count >= 5:
return input
t = Texttable(100)
while input:
line = input.pop(0)
rCount = countDelim(line)
if rCount == 0:
garbage.append(line)
else:
rows = line.split(' | ', initial_row_count)
if ' | ' in rows[-1]:
sub_row_count = countDelim(rows[-1])
if len(input) > 1 and countDelim(input[1]) == sub_row_count:
buf = [rows[-1]]
while input:
line = input.pop(0)
if countDelim(line) != sub_row_count:
input.insert(0, line)
break
buf.append(line)
rows[-1] = '\n'.join(renderTable(buf, True))
t.add_row([utf8(row) for row in rows])
draw = t.draw()
if not draw:
draw = []
else:
draw = draw.split('\n')
return draw + garbage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment