Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
duyuefeng0708
GitHub Repository: duyuefeng0708/Cryptography-From-First-Principle
Path: blob/main/scripts/remove_dashes.py
483 views
unlisted
1
#!/usr/bin/env python3
2
"""Replace ' -- ' (double-hyphen dashes) in notebook markdown cells and code comments.
3
4
Rules:
5
- In markdown cells: replace ' -- ' with '. ' or ', '
6
- In code cells: replace ' -- ' only in comments (lines starting with #) and strings
7
- Skip table separator lines
8
- Skip code fences
9
"""
10
11
import json
12
import re
13
from pathlib import Path
14
15
16
def fix_dashes_in_text(text: str) -> str:
17
"""Replace ' -- ' dashes in a line of text."""
18
if re.match(r'^[\s|:-]+$', text):
19
return text
20
if text.strip().startswith('```'):
21
return text
22
23
def replacer(m):
24
after = text[m.end():m.end() + 1] if m.end() < len(text) else ''
25
if after and (after.isupper() or after == '"' or after == "'"):
26
return '. '
27
return ', '
28
29
return re.sub(r' -- ', replacer, text)
30
31
32
def fix_dashes_in_code_line(line: str) -> str:
33
"""Replace ' -- ' in code comments and print strings only."""
34
if ' -- ' not in line:
35
return line
36
37
# Comment lines (# ...)
38
if line.lstrip().startswith('#'):
39
return fix_dashes_in_text(line)
40
41
# Inside print/string: replace within quoted portions
42
# Match f-strings and regular strings containing ' -- '
43
def fix_string(m):
44
return fix_dashes_in_text(m.group(0))
45
46
# Fix inside single-quoted strings
47
line = re.sub(r"'[^']*? -- [^']*?'", fix_string, line)
48
# Fix inside double-quoted strings
49
line = re.sub(r'"[^"]*? -- [^"]*?"', fix_string, line)
50
51
return line
52
53
54
def process_notebook(path: Path) -> int:
55
with open(path) as f:
56
nb = json.load(f)
57
58
changes = 0
59
for cell in nb.get('cells', []):
60
cell_type = cell.get('cell_type', '')
61
new_source = []
62
for line in cell['source']:
63
if cell_type == 'markdown':
64
fixed = fix_dashes_in_text(line)
65
elif cell_type == 'code':
66
fixed = fix_dashes_in_code_line(line)
67
else:
68
fixed = line
69
if fixed != line:
70
changes += 1
71
new_source.append(fixed)
72
cell['source'] = new_source
73
74
if changes > 0:
75
with open(path, 'w') as f:
76
json.dump(nb, f, indent=1, ensure_ascii=False)
77
f.write('\n')
78
79
return changes
80
81
82
def main():
83
root = Path(__file__).resolve().parent.parent
84
total = 0
85
files_changed = 0
86
87
for nb_path in sorted(root.rglob('*.ipynb')):
88
if '.ipynb_checkpoints' in str(nb_path):
89
continue
90
n = process_notebook(nb_path)
91
if n > 0:
92
rel = nb_path.relative_to(root)
93
print(f" {rel}: {n} fixes")
94
total += n
95
files_changed += 1
96
97
print(f"\nTotal: {total} fixes across {files_changed} files")
98
99
100
if __name__ == '__main__':
101
main()
102
103