Last active
April 4, 2022 13:15
-
-
Save LukasWoodtli/cbd9d14aab8194358d46000c728a2d2f to your computer and use it in GitHub Desktop.
Convert code snippets in Markdown to use fences
This file contains hidden or 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 fileinput | |
import os | |
import sys | |
# WARNING: This is Work-In-Progress!!! | |
def convert(inp): | |
code_snippet = False | |
for line in inp: | |
if code_snippet and line == "\n": | |
code_snippet = False | |
print("```\n") | |
elif line.startswith(" " * 4) and line.strip().startswith(":::"): | |
code_snippet = True | |
converted_line = line.replace(":", "").strip() | |
converted_line = "```" + converted_line | |
print(converted_line) | |
elif code_snippet: | |
print(line.replace(" " * 4, "", 1).rstrip()) | |
else: | |
print(line, end='') | |
if __name__ == '__main__': | |
for root, dirs, files in os.walk(sys.argv[1]): | |
for file in files: | |
md_file = os.path.join(root, file) | |
if md_file.endswith(".md"): | |
print(f"Processing {md_file}", file=sys.stderr) | |
with fileinput.input(files=(md_file), inplace=True) as f: | |
convert(f) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment