Created
May 16, 2016 15:56
-
-
Save madjar/d0a7e8a1548187718732b3102c33e7c9 to your computer and use it in GitHub Desktop.
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
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