forked from pittlerf/ckurs2017
-
Notifications
You must be signed in to change notification settings - Fork 1
/
format-all-code.py
66 lines (45 loc) · 1.32 KB
/
format-all-code.py
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
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright © 2017 Martin Ueding <dev@martin-ueding.de>
import argparse
import glob
import subprocess
def main():
options = _parse_args()
for filename in glob.glob('*.tex'):
format_file(filename)
def format_file(filename):
with open(filename) as f:
lines = list(f)
result = []
code = []
within_code = False
for line in lines:
if line.strip().startswith(r'\end{lstlisting}'):
within_code = False
with open('.formatter.cpp', 'w') as f:
f.write(''.join(code))
subprocess.call(['clang-format', '-i', '.formatter.cpp'])
with open('.formatter.cpp') as f:
code = list(f)
result += code
code = []
if within_code:
code.append(line)
else:
result.append(line)
if line.strip().startswith(r'\begin{lstlisting}'):
within_code = True
with open(filename, 'w') as f:
f.write(''.join(result))
def _parse_args():
'''
Parses the command line arguments.
:return: Namespace with arguments.
:rtype: Namespace
'''
parser = argparse.ArgumentParser(description='')
options = parser.parse_args()
return options
if __name__ == '__main__':
main()