-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_markdown.py
203 lines (164 loc) · 5.32 KB
/
run_markdown.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
#!/usr/bin/env python3
# Copy from DJRHails's gist
# More details on https://gist.github.com/DJRHails/fa1debd19bd6dbbe9f6789cebdc16b1d
from dataclasses import dataclass
from io import StringIO
from pathlib import Path
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
from typing import Optional
import contextlib
import re
import subprocess
import sys
import tempfile
import textwrap
import typer
app = typer.Typer()
def get_md_blocks_from_md(file_path: Path) -> list[str]:
"""
Get all code blocks from a markdown file.
Args:
file_path (Path): Path to the markdown file.
Returns:
list[str]: A list of all the code blocks in the markdown file.
"""
# Read the markdown file
with file_path.open('r') as f:
md_content = f.read()
# Use regex to extract code blocks from the parsed markdown
md_blocks = re.findall(r'(```.*?```)', md_content, re.DOTALL)
return md_blocks
@dataclass
class CodeBlock:
language: Optional[str]
code: str
def execute_python_code(code):
import ast
import builtins
code_ast = ast.parse(code, mode='exec')
global_scope = {
'__builtins__': builtins,
}
exec(compile(code_ast, '<string>', mode='exec'), global_scope)
# def execute_bash_code(code):
# with tempfile.TemporaryFile() as f:
# subprocess.run(code, shell=True, check=True, stdout=f, stderr=f)
# f.seek(0)
# sys.stdout.write(f.read().decode())
def execute_bash_code(code):
# Run the subprocess and capture the output
result = subprocess.run(code, shell=True, check=True, stdout=subprocess.PIPE, text=True)
# Print the stdout messages directly to the console
sys.stdout.write(result.stdout)
@contextlib.contextmanager
def capture_output():
"""
A context manager to capture stdout/stderr and redirect them to a StringIO object.
"""
old_stdout, old_stderr = sys.stdout, sys.stderr
try:
out = sys.stdout = sys.stderr = StringIO()
yield out
finally:
sys.stdout, sys.stderr = old_stdout, old_stderr
def execute_code_block(block):
"""
Execute a code block and display its output in a rich panel.
Args:
block (CodeBlock): The code block to execute.
"""
with capture_output() as output:
match block.language:
case "python":
execute_python_code(block.code)
case "bash":
execute_bash_code(block.code)
case _:
typer.echo(f"Language {block.language} not supported. Skipping...")
console = Console()
console.print(Panel(
output.getvalue().strip(),
title="Output",
border_style="green",
))
CODE_BLOCK_RE = r"""(?xm)
^```(?P<language>\w+?)\n
(?P<code>.*?)
^```
"""
def extract_code_block_from_md_block(md_block):
"""
Extract a code block from a markdown block.
Args:
md_block (str): The markdown block to extract the code block from.
Returns:
CodeBlock: The extracted code block.
"""
# Use regex to extract the language from the code block
matches = re.match(CODE_BLOCK_RE, md_block, re.DOTALL)
# Strip newlines and leading/trailing whitespace from the code
if matches:
code = matches.group("code").strip("\n").strip()
return CodeBlock(
language=matches.group("language"),
code=code
)
else:
return CodeBlock(
language="bash",
code="printf 'Cannot identify the language. Skipping...\n%s'" % md_block
)
def snippet_from_block(block):
"""
Shortens a code block's code to a snippet for display.
Args:
block (CodeBlock): The code block to shorten.
Returns:
str: The shortened code block.
"""
code = block.code
if len(code) > 50:
code = textwrap.shorten(code, width=50, placeholder="...")
return code
@app.command()
def execute(file_path: Path, run_all: bool = False):
"""
Execute code blocks from a markdown file.
Args:
file_path (Path): Path to the markdown file containing the code blocks.
"""
console = Console()
# Get all code blocks from the markdown file
md_blocks = get_md_blocks_from_md(file_path)
if not md_blocks:
console.print(Panel("No code blocks found in the markdown file", title="Error"))
return
# Display the code blocks to the user
code_blocks = [
extract_code_block_from_md_block(block)
for block in md_blocks
]
display_blocks = [
Panel(snippet_from_block(block), title=f"({i}) - {block.language}")
for i, block in enumerate(code_blocks)
]
grid = Table.grid(expand=True)
for i in range(0, len(display_blocks), 2):
grid.add_row(*display_blocks[i:i+2])
console.print(grid)
# If the user wants to run all code blocks, execute them and return
if run_all:
for block in code_blocks:
execute_code_block(block)
return
else:
selection = 0
if len(md_blocks) > 1:
# Offer the user to select a code block to execute
selection = typer.prompt("Choose a code block to execute", type=int)
# Execute the selected code block using the appropriate function
execute_code_block(code_blocks[selection])
if __name__ == "__main__":
app()