-
Notifications
You must be signed in to change notification settings - Fork 23
/
latex.py
332 lines (280 loc) · 10 KB
/
latex.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import re
try:
import expand_to_regex_set
import expand_to_symbols
import utils
_ST3 = False
except:
from . import expand_to_regex_set
from . import expand_to_symbols
from . import utils
_ST3 = True
_BEGIN_END_REG = re.compile(
r"\\(?P<command>begin|end)"
r"(?:\[.*\])?"
r"\{(?P<name>[^\}]*)\}"
)
_EXCLUSIVE_BEGIN_END_REG = re.compile(
r"^"
r"\\(?P<command>begin|end)"
r"(?:\[.*\])?"
r"\{(?P<name>[^\}]*)\}"
r"$"
)
def chart_at(string, index):
"""returns the chart at the position or the empty string,
if the index is outside the string"""
return string[index:index+1]
def expand_to_tex_word(string, start, end):
"""Expand to a valid latex word."""
regex = re.compile(r"[a-zA-Z@]", re.UNICODE)
return expand_to_regex_set._expand_to_regex_rule(
string, start, end, regex, "tex_word")
def _get_closest_env_border(string, start_pos, end_pos, reverse=False):
open_command = "begin"
close_command = "end"
if _ST3:
iterator = _BEGIN_END_REG.finditer(string, pos=start_pos,
endpos=end_pos)
offset = 0
else:
s = string[start_pos:end_pos]
iterator = _BEGIN_END_REG.finditer(s)
offset = start_pos
if reverse:
iterator = reversed(list(iterator))
open_command, close_command = close_command, open_command
count = 0
for before in iterator:
line = utils.get_line(string, before.start(), before.end())
# ignore comment lines
if string[line["start"]:line["end"]].strip()[0] == "%":
continue
command = before.group("command")
if command == open_command:
count += 1
elif command == close_command and count > 0:
count -= 1
elif command == close_command:
# found begin before
return {
"start": offset + before.start(),
"end": offset + before.end(),
"name": before.group("name")
}
def expand_against_matching_env(string, start, end):
m = _EXCLUSIVE_BEGIN_END_REG.match(string[start:end])
if not m:
return None
if m.group("command") == "begin":
reverse = False # search downwards
search_start = end
search_end = len(string)
else: # == "end"
reverse = True # search upwards
search_start = 0
search_end = start
env_border = _get_closest_env_border(string, search_start, search_end,
reverse=reverse)
if not env_border:
return None
if not env_border["name"] == m.group("name"):
print("ExpandRegion, latex.py, Environments not matching '{}' and '{}'"
.format(env_border["name"], m.group("name")))
return None
if not reverse: # search from begin
start = start
end = env_border["end"]
else: # search from end
start = env_border["start"]
end = end
return utils.create_return_obj(start, end, string,
"latex_environment_matching")
def expand_against_env(string, start, end):
tex_begin = _get_closest_env_border(string, 0, start, reverse=True)
tex_end = _get_closest_env_border(string, end, len(string), reverse=False)
if tex_begin is None or tex_end is None:
return None
if tex_begin["name"] != tex_end["name"]:
print("ExpandRegion, latex.py, Environments not matching '{}' and '{}'"
.format(tex_begin["name"], tex_end["name"]))
return None
inner_env_selected = start == tex_begin["end"] and end == tex_end["start"]
if inner_env_selected:
start = tex_begin["start"]
end = tex_end["end"]
else:
start = tex_begin["end"]
end = tex_end["start"]
return utils.create_return_obj(start, end, string, "latex_environment")
def expand_agains_base_command(string, start, end):
start -= 1
if chart_at(string, start) == "\\":
if chart_at(string, end) == "*":
end += 1
result = utils.create_return_obj(start, end, string,
"latex_command_base")
return result
class NoSemanticUnit(Exception):
pass
def _stretch_over_previous_semantic_unit(string, start):
start -= 1
while chart_at(string, start) == " ":
start -= 1
if chart_at(string, start) in ["]", "}"]:
r = expand_to_symbols.expand_to_symbols(string, start, start)
if r is not None:
return r["start"] - 1
raise NoSemanticUnit()
def _stretch_over_next_semantic_unit(string, end):
while chart_at(string, end) == " ":
end += 1
if chart_at(string, end) in ["[", "{"]:
end += 1
r = expand_to_symbols.expand_to_symbols(string, end, end)
if r is not None:
end = r["end"]
# special case: '{}' (no content)
if end == r["start"] + 2:
return end
return end + 1
raise NoSemanticUnit()
def expand_against_command_args(string, start, end):
if not chart_at(string, start) == "\\":
return None
if chart_at(string, end) not in ["{", "["]:
return None
original_end = end
while True:
try:
end = _stretch_over_next_semantic_unit(string, end)
except NoSemanticUnit:
break
# if the end did not change: do nothing
if original_end == end:
return None
return utils.create_return_obj(start, end, string, "latex_command_arg")
def expand_against_surrounding_command(string, start, end):
if chart_at(string, start) in ["{", "["] and\
chart_at(string, end - 1) in ["}", "]"]:
# span backwards over [..] and {..}
while True:
try:
start = _stretch_over_previous_semantic_unit(string, start)
except NoSemanticUnit:
break
# span forwards over [..] and [..]
while True:
try:
end = _stretch_over_next_semantic_unit(string, end)
except NoSemanticUnit:
break
# span over the previous \command or \command*
if chart_at(string, start - 1) == "*":
start -= 1
result = expand_to_tex_word(string, start, start)
if result is None:
return None
start = result["start"] - 1
if chart_at(string, start) == "\\":
return utils.create_return_obj(start, end, string,
"latex_command_surround")
def expand_to_inline_math(string, start, end):
# don't expand if a dollar sign is inside the string
if re.search(r"(?:[^\\]|^)\$", string[start:end]):
return
line = utils.get_line(string, start, end)
escape = inside = False
open_pos = close_pos = None
# we only need to consider one position, because we have checked it does
# not contain any $-signs
pos = start - line["start"]
for i, char in enumerate(string[line["start"]:line["end"]]):
# break if we are behind
behind = pos < i
if not inside and behind:
return
if escape:
escape = False
elif char == "\\":
escape = True
continue
elif char == "$":
if not inside:
# the inner end of the $-sign
open_pos = i + 1
elif behind:
close_pos = i
break
inside = not inside
if open_pos is not None and close_pos is not None:
open_pos = line["start"] + open_pos
close_pos = line["start"] + close_pos
# expand to the outer end
if open_pos == start and close_pos == end:
open_pos -= 1
close_pos += 1
return utils.create_return_obj(
open_pos, close_pos, string, "latex_inline_math")
# TODO could be moved to utils?
def _closest_result(result1, result2):
if result1 is None:
return result2
if result2 is None:
return result1
if result1["start"] < result2["start"] and\
result2["end"] < result1["end"]:
return result2
else:
return result1
def expand(string, start, end):
expand_stack = []
expand_stack.append("tex_word")
result = expand_to_tex_word(string, start, end)
if result:
result["expand_stack"] = expand_stack
return result
expand_stack.append("latex_command_base")
result = expand_agains_base_command(string, start, end)
if result:
result["expand_stack"] = expand_stack
return result
expand_stack.append("tex_math_command")
# expand to math commands, e.g. \phi_x^2
regex = re.compile(r"[\w\\@^]", re.UNICODE)
result = expand_to_regex_set._expand_to_regex_rule(
string, start, end, regex, "tex_math_command")
if result:
result["expand_stack"] = expand_stack
return result
expand_stack = ["latex_command_arg"]
result = expand_against_command_args(string, start, end)
if result:
result["expand_stack"] = expand_stack
return result
expand_stack.append("latex_command_surround")
result = expand_against_surrounding_command(string, start, end)
if result:
result["expand_stack"] = expand_stack
return result
expand_stack.append("latex_inline_math")
result = expand_to_inline_math(string, start, end)
if result:
result["expand_stack"] = expand_stack
return result
expand_stack.append("latex_environment_matching")
result = expand_against_matching_env(string, start, end)
if result:
result["expand_stack"] = expand_stack
return result
env_result = expand_against_env(string, start, end)
# there might be a {} inside the environment
sym_result = expand_to_symbols.expand_to_symbols(string, start, end)
result = _closest_result(env_result, sym_result)
if result == env_result:
expand_stack.append("latex_environment")
else:
expand_stack.append("symbols")
if result:
result["expand_stack"] = expand_stack
return result