forked from djangodeployment/django-deployment-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixlatex
executable file
·50 lines (39 loc) · 1.58 KB
/
fixlatex
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
#!/usr/bin/env python
r'''
Fix several issues in the Latex file.
Latex should be given as input. The following changes are made, and the
result is thrown in the standard output:
1. It removes \url{X} and converts it to X for all X that contain a
dollar sign. Assumes \url{} doesn't cross line boundaries, which
appears to be true. If this is not done, some strange code appears
inside the URLs (and it's also made a link, which doesn't make
sense).
2. It formats notes, hints and tips the same as warnings. This is
because warnings show in a box, which is nice, but the other
admonitions only show with a top and bottom line, which can look
confusing depending on where in the page it's shown.
'''
import re
import sys
regexp = re.compile(r'''\\url\{
(
[^}]* # Anything (but end of url),
\$ # then a dollar sign,
[^}]* # then anything (but end of url)
)
\} # then end of url
''', flags=re.VERBOSE)
def fix_urls(line):
return re.sub(regexp, r'\1', line)
def fix_admonitions(line):
line = line.replace(r'\begin{notice}{tip}',
r'\begin{notice}{warning}')
line = line.replace(r'\begin{notice}{note}',
r'\begin{notice}{warning}')
line = line.replace(r'\begin{notice}{hint}',
r'\begin{notice}{warning}')
return line
for line in sys.stdin:
line = fix_urls(line)
line = fix_admonitions(line)
sys.stdout.write(line)