Skip to content

Instantly share code, notes, and snippets.

@dwcramer
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwcramer/3c997a6a6360a6369bf2 to your computer and use it in GitHub Desktop.
Save dwcramer/3c997a6a6360a6369bf2 to your computer and use it in GitHub Desktop.
#!/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