-
-
Save uschille/9f9f2f5ce7d199a3cc64ceb20182cad9 to your computer and use it in GitHub Desktop.
pulls code out of carpentries episodes - not the most reliable!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import os | |
import re | |
path = os.path.dirname(sys.argv[1]) | |
path = "." if path == "" else path | |
episode = os.path.splitext(os.path.basename(sys.argv[1]))[0] | |
with open(f"{path}/{episode}.md") as f: | |
content = f.read() | |
content = re.sub(r"^(>\s)*", "", content, flags=re.MULTILINE) # strip all "> " | |
content = re.sub("(\%matplotlib widget)", "", content) # strip notebook magics | |
matches = re.findall( | |
"(\~\~\~\n)(?!{)([\w\W]*?)(\~\~\~\n)(.*)", | |
content, | |
) # find all code blocks | |
code = "" | |
for m in matches: | |
if "python" in m[3]: # python code blocks only | |
code += m[1] | |
print(code) | |
with open(f"{path}/{episode}.py", "w") as f: | |
f.write(code) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment