Skip to content

Instantly share code, notes, and snippets.

@cameron
Created October 14, 2014 18:52
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 cameron/039be433fa3504d738ba to your computer and use it in GitHub Desktop.
Save cameron/039be433fa3504d738ba to your computer and use it in GitHub Desktop.
python script to split the curriculum readme file into chunks by section
import re
import os
import shutil
sprints_dir = 'sprints'
section_prefix = '# '
day_prefix = '## '
half_day_prefix = '### '
sections = {}
class Section(object):
def __init__(self, heading):
self.filename = self._heading_to_filename(heading)
self.body = ''
self._day_counter = 0
def _heading_to_filename(self, heading):
fname = re.sub(section_prefix + '|\n|,','', heading)
return fname.replace(' ', '-').replace('&', 'and').lower() + '.md'
def _next_day_heading(self):
self._day_counter += 1
return 'Day %s' % self._day_counter
def append(self, line):
if re.match('.*Week.*Day.*', line):
line = day_prefix + self._next_day_heading()
if re.match('.*(Morning|Afternoon|Evening).*', line):
line = half_day_prefix + line.replace(' ===', '').replace('#', '')
if re.match('^#####', line):
line = line[1:]
self.body += line
def save(self, dirname):
f = open(os.path.join(dirname, self.filename), 'w')
f.write(self.body)
f.close()
def split_curriculum(verbose=False):
if os.path.isdir(sprints_dir):
shutil.rmtree(sprints_dir)
os.makedirs(sprints_dir)
current_section = None
for line in open('README.md'):
if line.startswith(section_prefix):
if current_section:
if debug:
print current_section.filename
print current_section.body
print
current_section.save(sprints_dir)
current_section = Section(line)
sections[current_section.filename] = current_section
current_section and current_section.append(line)
if __name__ == '__main__':
split_curriculum(verbose=len(sys.argv) > 1 and sys.argv[1] == '-v')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment