-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdate_markdown_code_listings.py
More file actions
217 lines (199 loc) · 6.24 KB
/
Copy pathupdate_markdown_code_listings.py
File metadata and controls
217 lines (199 loc) · 6.24 KB
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
#: update_markdown_code_listings.py
import argparse
import difflib
import re
from dataclasses import dataclass, field
from pathlib import Path
from pprint import pformat
from typing import List
from rich.console import Console
width = 65
console = Console()
python_files = []
@dataclass
class MarkdownListing:
slugname: str
markdown_listing: str
source_file_path: Path | None
# Exclude field from constructor arguments:
source_file_contents: str = field(init=False)
changed: bool = field(init=False)
diffs: str = field(init=False)
def __post_init__(self):
if self.source_file_path is None:
console.print(
"[bold red] MarkdownListing: source_file_path is None"
f" for slugname: {self.slugname}[/bold red]"
)
console.print(pformat(python_files))
raise ValueError("source_file cannot be None")
self.source_file_contents = (
"```python\n"
+ self.source_file_path.read_text(
encoding="utf-8"
)
+ "```"
)
self.changed = (
self.markdown_listing
!= self.source_file_contents
)
if self.changed:
# Compute the differences between markdown_listing and source_file_contents
differ = difflib.Differ()
diff_lines = list(
differ.compare(
self.markdown_listing.splitlines(
keepends=True
),
self.source_file_contents.splitlines(
keepends=True
),
)
)
# Format the differences for display
self.diffs = "".join(diff_lines)
def __str__(self):
return f"""
Filename from slugline: {self.slugname}
Source File: {self.source_file_path.absolute() if self.source_file_path else ""}
{self.changed = }
{" Markdown Code Listing ".center(width, "-")}[chartreuse4]
{self.markdown_listing}[/chartreuse4]
{" Source File Code Listing ".center(width, "-")}[chartreuse4]
{self.source_file_contents}[/chartreuse4]
{" diffs ".center(width,"v")}[chartreuse4]
{self.diffs}[/chartreuse4]
{'=' * width}
"""
def find_python_files_and_listings(
markdown_content: str,
) -> List[MarkdownListing]:
"""
Find all #[code_location] paths in the markdown content and
return associated Python files and listings.
"""
global python_files
listings = []
code_location_pattern = re.compile(
r"#\[code_location\]\s*(.*)\s*-->"
)
for match in re.finditer(
code_location_pattern, markdown_content
):
code_location = Path(match.group(1))
if code_location.is_absolute():
python_files.extend(
list(code_location.glob("**/*.py"))
)
else: # Relative path:
python_files.extend(
list(
(Path.cwd() / code_location)
.resolve()
.glob("**/*.py")
)
)
console.print(
f"[orange3]{" Available Python Files ".center(width, "-")}[/orange3]"
)
for pyfile in [pf.name for pf in python_files]:
console.print(
f"\t[sea_green2]{pyfile}[/sea_green2]"
)
console.print(f"[orange3]{"-" * width}[/orange3]")
# If slug line doesn't exist group(1) returns None:
listing_pattern = re.compile(
r"```python\n(#\:(.*?)\n)?(.*?)```", re.DOTALL
)
for match in re.finditer(
listing_pattern, markdown_content
):
if match.group(1) is not None:
listing_content = match.group(
0
) # Include markdown tags
filename = (
match.group(2).strip()
if match.group(2)
else None
)
assert (
filename
), f"filename not found in {match}"
source_file = next(
(
file
for file in python_files
if file.name == filename
),
None,
)
listings.append(
MarkdownListing(
filename, listing_content, source_file
)
)
return listings
def update_markdown_listings(
markdown_content: str, listings: List[MarkdownListing]
) -> str:
updated_markdown = markdown_content
for listing in listings:
if not listing.changed:
console.print(
f"[bold green]{listing.slugname}[/bold green]"
)
if listing.changed:
console.print(
f"[bold red]{listing.slugname}[/bold red]"
)
console.print(
f"[bright_cyan]{listing}[/bright_cyan]"
)
updated_markdown = updated_markdown.replace(
listing.markdown_listing,
listing.source_file_contents,
)
return updated_markdown
def main():
parser = argparse.ArgumentParser(
description="Update Python slugline-marked source-code listings within a markdown file."
)
parser.add_argument(
"markdown_file",
help="Path to the markdown file to be updated.",
)
args = parser.parse_args()
markdown_file = Path(args.markdown_file)
markdown_content = markdown_file.read_text(
encoding="utf-8"
)
listings = find_python_files_and_listings(
markdown_content
)
changes = [
True for listing in listings if listing.changed
]
if any(changes):
updated_markdown = update_markdown_listings(
markdown_content, listings
)
markdown_file.write_text(
updated_markdown, encoding="utf-8"
)
change_count = f" {changes.count(True)} changes made to {markdown_file} ".center(
width, "-"
)
console.print(f"\n[orange3]{change_count}[/orange3]")
if any(changes):
for change in [
listing
for listing in listings
if listing.changed
]:
console.print(
f"[bright_cyan]{change.slugname}[/bright_cyan]"
)
if __name__ == "__main__":
main()