Last active
August 29, 2015 14:05
-
-
Save dwcramer/3c997a6a6360a6369bf2 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
#!/usr/bin/python | |
# Chunk a big markdown file into smaller files | |
# at the h2 level. Throw away anything above that. | |
# This assumes a 'well-formed' file. I.e. if you put an | |
# h1 below an h2, this happily sticks it in the file | |
# for it's parent h2. | |
# Name output files based on heading names, replacing | |
# spaces with - and adding ".html.md". No other bad characters | |
# are stripped from the heading. | |
import re | |
with open('concepts.html.markdown') as file: | |
data = file.readlines() | |
data.reverse() | |
headings = [] | |
for line in data: | |
m = re.match("^## ", line) | |
if m: | |
headings.append(line) | |
section = [] | |
for line in data: | |
section.append(line) | |
if len(headings) > 0 and line == headings[0]: | |
section.reverse() | |
filename = line.split("## ",1)[1].replace(" ","-").rstrip().lower() + ".html.md" | |
sectionFile = open(filename,'w') | |
for lline in section: | |
sectionFile.write('%s'% lline) | |
sectionFile.close() | |
section = [] | |
del headings[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment