-
Notifications
You must be signed in to change notification settings - Fork 0
/
wclatex
executable file
·54 lines (44 loc) · 1.2 KB
/
wclatex
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
#!/usr/bin/python
import os.path
import re
import sys
if (len(sys.argv) == 3) and (sys.argv[1] == "-v"):
sys.argv[1] = sys.argv[2]
elif len(sys.argv) != 2:
print "Usage: wclatex [-v] <file.tex>"
sys.exit(1)
if not(os.path.isfile(sys.argv[1])):
print "Path does not exist or is not a file: " + sys.argv[1]
sys.exit(1)
comment = re.compile("\%.*")
markup = re.compile(r"\\(?:emph|textbf)\{(.*?)\}")
figure = re.compile(r"\\begin{figure}.*?\\end{figure}")
math = re.compile(r"\${1,2}.+?\${1,2}")
commands = re.compile(r"\\\w+((\*)?\{.*?\})*")
numbers = re.compile(r"\d+(.\d*)?")
nonwords = re.compile(r"\s\W+\s")
c = open(sys.argv[1]).read()
bstring = r"\begin{document}"
b = c.find(bstring)
e = c.find(r"\end{document}")
# TODO
# line.find("\appendix") || line.find("\bibliography"):
if (b == -1) or (e == -1):
print "Not a valid LaTeX file"
sys.exit(1)
c = c[b+len(bstring):e]
c = comment.sub("", c)
c = c.replace("\n", " ")
c = c.replace("/", " ");
c = c.replace("-", "") # or space!?
# TODO test
#c = sub(figure, "", c)
c = markup.sub(r"\1", c)
c = math.sub("", c)
c = commands.sub("", c)
c = numbers.sub("", c)
c = nonwords.sub(" ", c)
if len(sys.argv) == 3:
print c
wc = len(c.split())
print wc