-
Notifications
You must be signed in to change notification settings - Fork 3
/
publish.py
177 lines (142 loc) · 6.37 KB
/
publish.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# !/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import pathlib
import re
def main():
if len(sys.argv) > 1:
print("Script does not take any arguments")
return
#extract_doc_from_dtx("highlightlatex.dtx", "highlightlatex_doc.tex")
generate_dtx("highlightlatex_doc.tex", "highlightlatex.sty", "publish/highlightlatex.dtx")
pass
def generate_dtx(docFile, styFile, path):
with open(path, "w") as outp:
with open("template_dtx.txt", "r") as templFile:
templ = iter(templFile)
for l in templ:
if "%% DOC-PREAMBLE" in l:
m = re.fullmatch(r"(?P<before>.*?)%% DOC-PREAMBLE(?P<after>.*)\n", l)
before = m.group("before")
after = m.group("after")
with open(docFile, "r") as doc:
for docline in doc:
if "\\begin{document}" in docline:
break
outp.write(before + docline + after)
elif "%% DOC-DOCUMENT" in l:
m = re.fullmatch(r"(?P<before>.*?)%% DOC-DOCUMENT(?P<after>.*)\n", l)
before = m.group("before")
after = m.group("after")
with open(docFile, "r") as doc:
dociter = iter(doc)
for docline in dociter:
if "\\begin{document}" in docline:
break
stripIndent = True
for docline in dociter:
if docline.startswith("\\end{document}"):
break
line = docline
if line.startswith("%"):
continue
line = line.replace("\t", " ")
if line == r"\def\defaultgobble{8}" + "\n":
line = f"\\def\\defaultgobble{{{8 + len(before)}}}\n"
line = doHighlightBlockLogic(line, before)
# highlBlockBegin = re.fullmatch(
# r"(?P<argsBefore>(?P<indent>\s*)" +
# r"\\begin\{highlightblock\})\[(?P<args>.*)\]" +
# r"(?P<argsAfter>\n?)", line)
# if highlBlockBegin != None:
# highlighBlockIndent = highlBlockBegin.group("indent")
# newgobble = len(highlighBlockIndent) + 4 + len(before)
# gobbleMatch = re.fullmatch(r"(?P<before>.*,\s*)?gobble=\d+(?P<after>\s*,.*)?", highlBlockBegin.group("args"))
# if gobbleMatch != None:
# line = highlBlockBegin.group("argsBefore") \
# + f"[{gobbleMatch.group('before') or ''}gobble={newgobble}" \
# + f"{gobbleMatch.group('after') or ''}]" \
# + highlBlockBegin.group("argsAfter")
# if stripIndent:
# if line.startswith("\t"):
# line = line[1:]
# elif line.startswith(" "):
# line = line[4:]
# else:
# stripIndent = False
outp.write(before + line + after)
elif "%% STY" in l:
m = re.fullmatch(r"(?P<before>.*?)%% STY(?P<after>.*)\n", l)
before = m.group("before")
after = m.group("after")
with open(styFile, "r") as doc:
for docline in doc:
if docline.startswith("%%%"):
continue
docline = docline.replace("\t", " ")
outp.write(before + docline + after)
else:
outp.write(l)
highlightBlockIndent = None
def doHighlightBlockLogic(line, before):
global highlightBlockIndent
if highlightBlockIndent != None:
m = re.fullmatch(
r"(?P<argsBefore>(?P<indent>\s*)" +
r"\\end\{highlightblock\})(?P<argsAfter>\n?)", line)
if m != None:
highlightBlockIndent = None
return line
m = re.fullmatch(
r"(?P<argsBefore>(?P<indent>\s*)" +
r"\\begin\{highlightblock\})(\[(?P<args>.*)\])?" +
r"(?P<argsAfter>\n?)", line)
if m == None:
return line
highlightBlockIndent = m.group("indent")
newgobble = len(highlightBlockIndent) + 4 + len(before)
gobbleMatch = None
if m.group("args") != None:
gobbleMatch = re.fullmatch(r"(?P<before>.*,\s*)?gobble=\d+(?P<after>\s*,.*)?",
m.group("args"))
if gobbleMatch == None:
return line
return m.group("argsBefore") \
+ f"[{gobbleMatch.group('before') or ''}gobble={newgobble}" \
+ f"{gobbleMatch.group('after') or ''}]" \
+ m.group("argsAfter")
def extract_doc_from_dtx(dtxFile, texFile):
with open(texFile, "w") as outp:
with open(dtxFile, "r") as inpFile:
inp = iter(inpFile)
for l in inp:
if l.startswith("%"):
continue
if "\\DocInput" in l:
extract_doc_docinput(dtxFile, outp, indent="\t")
elif "\\endinput" in l:
break
else:
outp.write(l)
def extract_doc_docinput(dtxFile, outp, indent=""):
with open(dtxFile, "r") as inpFile:
inp = iter(inpFile)
while True:
l = next(inp, None)
if l == None:
break
if l.startswith("% \\iffalse"):
if "\\fi" in l:
continue
while l != None and not l.startswith("% \\fi"):
l = next(inp, None)
continue
if "\\Finale" in l:
break
if l.startswith("% "):
l = l[2:]
elif l.startswith("%"):
l = l[1:]
outp.write(indent + l)
if __name__ == "__main__":
main()