-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmacros.py
More file actions
30 lines (23 loc) · 749 Bytes
/
macros.py
File metadata and controls
30 lines (23 loc) · 749 Bytes
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
import re
def load_macros(filename):
macros = {}
with open(filename, "r") as f:
for line in f:
line = line.strip()
if not line or line.startswith("#"):
continue
if "=" not in line:
raise SyntaxError(f"Invalid macro line: {line}")
name, expr = line.split("=", 1)
macros[name.strip()] = expr.strip()
return macros
def inline_macros(text, macros):
if not macros:
return text
names = list(macros.keys())
pattern = re.compile(r"\b(" + "|".join(names) + r")\b")
prev_text = None
while prev_text != text:
prev_text = text
text = pattern.sub(lambda m: macros[m.group(0)], text)
return text