2
# left shebang empty for you to change. you do you
7
########################################################################################
9
# License: CC0 -- do whatever the hell you want with this. I hope it works for you! #
12
########################################################################################
14
# This is *NOT* a pandoc filter. This is to be used if you want to manipulate the #
15
# output of pandoc latex to put verticle rules in the tables. Should work with #
16
# python2 and python3. Your mileage may vary. #
18
# Example usage (just copy til end of line and paste in terminal), assuming you called #
19
# this file tex_table_verts.py: #
21
# echo -e '\\begin{longtable}[c]{@{}llrrllclrl@{}}\nthis line does not have table\n\\begin{longtable}[c]{@{}llllll@{}}\nno\ntable\nhere\n\\begin{longtable}[c]{@{}l@{}}' | python3 tex_table_verts.py
23
# Basically, the design is to just take your pandoc output and pipe it to this script. #
26
# pandoc -s -f markdown -t latex input.md | python tex_table_verts.py > output.tex #
28
# Windows users? No idea if you can benefit. Sorry. #
30
########################################################################################
32
# We want to turn something like this: #
34
# \begin{longtable}[c]{@{}ll@{}} #
36
# into something like this: #
38
# \begin{longtable}[c]{@{}|l|l|@{}} #
40
# We make the assumption that the input format always has #
42
# \begin{longtable}[c]{@{}ll@{}} #
43
# |----------------------|XX|--| #
45
# That is, the longtable is being searched for, as well as {@{}XXXX@{}} #
46
# where XXXX changes depending on the table you are writing. #
48
########################################################################################
52
orig_input = sys.stdin.read()
54
# Important group being saved: the r, c, or l's for the table columns.
56
vert_re = re.compile(r'(\\begin\{longtable\}\[.*\]\{@\{\})([rcl]+)(@\{\}\})', re.MULTILINE)
57
# ^ not sure if pandoc changes this ever?
58
# We have three groups captured above:
60
# 1. \begin{longtable}[c]{@{}
64
# The below takes these three, turns group 2 into vertically separated columns, and
65
# then appends this to `replacements` joined with 1 and 3 so we can use `sub` below.
67
for match in vert_re.finditer(orig_input):
68
table_start, cols, table_end = match.groups()
69
# Gives you say |r|c|l|
70
# If you forever wanted just r|c|l without the outer ones, set vert_cols to just
71
# be "|".join(cols). Get creative if you don't want every inner one vertically
73
vert_cols = "|{}|".format("|".join(cols))
74
replacements.append("{}{}{}".format(table_start, vert_cols, table_end))
76
# probably not necessary
77
output = copy.deepcopy(orig_input)
79
# if the above loop executed, the same regex will have the matches replaced
80
# according to the order we found them above
82
output = vert_re.sub(lambda cols: replacements.pop(0), output)
84
# Set this to True if pandoc is giving you trouble with no horizontal rules in
85
# tables that have multiple rows
87
output = re.sub(r'(\\tabularnewline)(\s+)(\\begin{minipage})', r'\1\2\\midrule\2\3', output)
89
# write the conversion to stdout
90
sys.stdout.write(output)
91
except Exception as e:
92
# you may want to change this to fail out -- if an error was caught you probably
93
# aren't going to actually get any valid output anyway? up to you, just figured
94
# i'd write something *kind of* intelligent.
96
"Critical error, printing original stdin to stdout:\n{}".format(e)
98
sys.stdout.write(orig_input)