-
Notifications
You must be signed in to change notification settings - Fork 1
/
textabletidy
executable file
·57 lines (44 loc) · 1.78 KB
/
textabletidy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python
"""
textabletidy
Align the columns of TeX table, reading from STDIN or a named file and writing
to STDOUT.
This script identifies a row of a TeX table by looking for any line that has
an unescaped ampersand and the TeX newline command '\\'. This script does not
make any attempt to parse the preamble of the table to determine how many
columns there are, it doesn't handle table rows that are split across lines,
and it doesn't make any changes to lines that are not table rows.
Written by Daniel Grady <[email protected]> 2012-10-31
"""
# TODO Handle rows that don't have the right number of columns (like a row
# that uses multirow) by just ignoring them. Maybe any row that doesn't have
# the modal number of columns?
import re
import fileinput
rawlines = []
rows = []
postlines = []
# An unescaped ampersand is one that is not immediately preceded by backslash
unescamp = re.compile(r'(?<!\\)&')
newline = re.compile(r'\\\\')
# A line of text is a table row if it contains at least one unescaped
# ampersand and the newline command '\\'. It does not have to end with the
# newline command, because there may be for example '\hline' following that on
# the same line of text.
istablerow = lambda line: unescamp.search(line) and newline.search(line)
for line in fileinput.input():
rawlines.append(line)
if istablerow(line):
preline,postline = newline.split(line)
cells = [c.strip() for c in unescamp.split(preline)]
rows.append(cells)
postlines.append(postline.strip())
col_width = [max(len(x) for x in col) for col in zip(*rows)]
rows = [[c + ' '*(w-len(c)) for c,w in zip(row,col_width)] for row in rows]
for line in rawlines:
if istablerow(line):
row = rows.pop(0)
postline = postlines.pop(0)
print ' & '.join(row) + r' \\ ' + postline
else:
print line,