/+junk/build-libffmpeg-for-chromium

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/%2Bjunk/build-libffmpeg-for-chromium

« back to all changes in this revision

Viewing changes to tex_table_verts.py

  • Committer: Gustav Hartvigsson
  • Date: 2021-06-07 17:31:30 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20210607173130-ltls48c5mw8ypt6q
initial coode

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python3
2
 
# left shebang empty for you to change. you do you
3
 
import re
4
 
import sys
5
 
import copy
6
 
 
7
 
########################################################################################
8
 
#                                                                                      #
9
 
# License: CC0 -- do whatever the hell you want with this.  I hope it works for you!   #
10
 
# Love, Sven.                                                                          #
11
 
#                                                                                      #
12
 
########################################################################################
13
 
#                                                                                      #
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.                                         #
17
 
#                                                                                      #
18
 
# Example usage (just copy til end of line and paste in terminal), assuming you called #
19
 
# this file tex_table_verts.py:                                                        #
20
 
#                                                                                      #
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
22
 
#                                                                                      #
23
 
# Basically, the design is to just take your pandoc output and pipe it to this script. #
24
 
# Something like                                                                       #
25
 
#                                                                                      #
26
 
#     pandoc -s -f markdown -t latex input.md | python tex_table_verts.py > output.tex #
27
 
#                                                                                      #
28
 
# Windows users?  No idea if you can benefit.  Sorry.                                  #
29
 
#                                                                                      #
30
 
########################################################################################
31
 
#                                                                                      #
32
 
# We want to turn something like this:                                                 #
33
 
#                                                                                      #
34
 
#     \begin{longtable}[c]{@{}ll@{}}                                                   #
35
 
#                                                                                      #
36
 
# into something like this:                                                            #
37
 
#                                                                                      #
38
 
#     \begin{longtable}[c]{@{}|l|l|@{}}                                                #
39
 
#                                                                                      #
40
 
# We make the assumption that the input format always has                              #
41
 
#                                                                                      #
42
 
#     \begin{longtable}[c]{@{}ll@{}}                                                   #
43
 
#     |----------------------|XX|--|                                                   #
44
 
#                                                                                      #
45
 
# That is, the longtable is being searched for, as well as {@{}XXXX@{}}                #
46
 
# where XXXX changes depending on the table you are writing.                           #
47
 
#                                                                                      #
48
 
########################################################################################
49
 
 
50
 
 
51
 
try:
52
 
    orig_input = sys.stdin.read()
53
 
 
54
 
    # Important group being saved: the r, c, or l's for the table columns.
55
 
    #                                                        vvvvvvvv
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:
59
 
    #
60
 
    # 1. \begin{longtable}[c]{@{}
61
 
    # 2. [rcl]+
62
 
    # 3. @{}}
63
 
    #
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.
66
 
    replacements = []
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
72
 
        # separated.
73
 
        vert_cols = "|{}|".format("|".join(cols))
74
 
        replacements.append("{}{}{}".format(table_start, vert_cols, table_end))
75
 
 
76
 
    # probably not necessary
77
 
    output = copy.deepcopy(orig_input)
78
 
 
79
 
    # if the above loop executed, the same regex will have the matches replaced
80
 
    # according to the order we found them above
81
 
    if replacements:
82
 
        output = vert_re.sub(lambda cols: replacements.pop(0), output)
83
 
 
84
 
    # Set this to True if pandoc is giving you trouble with no horizontal rules in
85
 
    # tables that have multiple rows
86
 
    if False:
87
 
        output = re.sub(r'(\\tabularnewline)(\s+)(\\begin{minipage})', r'\1\2\\midrule\2\3', output)
88
 
 
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.
95
 
    sys.stderr.write(
96
 
        "Critical error, printing original stdin to stdout:\n{}".format(e)
97
 
    )
98
 
    sys.stdout.write(orig_input)