-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunindent_auto_doc.py
More file actions
92 lines (75 loc) · 2.62 KB
/
Copy pathunindent_auto_doc.py
File metadata and controls
92 lines (75 loc) · 2.62 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
import argparse
import glob
import os
import textwrap
START_MARKER = "<!-- MARKDOWN-AUTO-DOCS:START"
END_MARKER = "<!-- MARKDOWN-AUTO-DOCS:END -->"
CODE_BLOCK_START_MARKERS = ["```py", "```ts", "```protobuf"]
CODE_BLOCK_END = "```"
def unindent_code_snippet(snippet):
"""Un-indents a code snippet."""
return textwrap.dedent(snippet)
def unindent_markdown_section(content):
"""Un-indents only the code snippets between the markers."""
lines = content.splitlines(keepends=True)
new_lines = []
inside_block = False
inside_code_block = False
code_block_content: list[str] = []
modified = False
for line in lines:
if START_MARKER in line:
modified = True
inside_block = True
new_lines.append(line)
elif END_MARKER in line:
inside_block = False
inside_code_block = False
new_lines.append(line)
elif inside_block and any(
marker in line for marker in CODE_BLOCK_START_MARKERS
):
inside_code_block = True
new_lines.append(line)
elif inside_code_block and CODE_BLOCK_END in line:
if code_block_content:
unindented_code = unindent_code_snippet(
"".join(code_block_content)
)
new_lines.append(unindented_code)
code_block_content = []
inside_code_block = False
new_lines.append(line)
elif inside_code_block:
code_block_content.append(line)
else:
new_lines.append(line)
return (modified, "".join(new_lines))
def process_file(file_path):
"""Read, un-indent, and rewrite the file if needed."""
with open(file_path, "r") as file:
content = file.read()
modified, updated_content = unindent_markdown_section(content)
if modified:
# Write the updated content back to the file
with open(file_path, "w") as file:
file.write(updated_content)
print(f"Updated file: {file_path}")
if __name__ == "__main__":
print("Un-indenting code snippets in Markdown files.")
parser = argparse.ArgumentParser(
description="Un-indent code snippets in Markdown files."
)
parser.add_argument(
'files',
type=str,
nargs='+',
help="files or glob patterns to un-indent code snippets in.",
)
args = parser.parse_args()
for pattern in args.files:
if os.path.isfile(pattern):
process_file(pattern)
else:
for file_path in glob.glob(pattern, recursive=True):
process_file(file_path)