Skip to content

Instantly share code, notes, and snippets.

@madjar
Created May 16, 2016 15:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madjar/d0a7e8a1548187718732b3102c33e7c9 to your computer and use it in GitHub Desktop.
Save madjar/d0a7e8a1548187718732b3102c33e7c9 to your computer and use it in GitHub Desktop.
from pathlib import Path
import sys
import itertools
source = Path(sys.argv[1])
target = Path(sys.argv[2])
if target.exists():
print("%s already exists, aborting." % target)
sys.exit()
for source_file in source.glob("**/*.md"):
fixed_file = Path(target, source_file)
fixed_file.parent.mkdir(parents=True, exist_ok=True)
with source_file.open() as in_, fixed_file.open('w') as out:
for is_block, lines in itertools.groupby(in_, lambda l: l.startswith(' ')):
if is_block:
first_line = next(lines)[4:]
if first_line.strip() in {'python', 'html', 'css', 'shell'}:
out.write("```" + first_line)
else:
out.write("```\n")
out.write(first_line)
out.writelines(l[4:] for l in lines if l.strip())
out.write("```\n")
else:
out.writelines(lines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment