#! /usr/bin/python import os import shutil import optparse import string import markdown from codecs import open p = optparse.OptionParser() p.add_option('-t', '--template', dest='template', default='template.html', help='Location of HTML template') p.add_option('-b', '--base', dest='base', default='', help='Base URL for contest') p.add_option('-p', '--puzzles', dest='puzzles', default='puzzles', help='Directory containing puzzles') p.add_option('-w', '--htmldir', dest='htmldir', default='puzzler', help='Directory to write HTML puzzle tree') p.add_option('-k', '--keyfile', dest='keyfile', default='puzzler.keys', help='Where to write keys') opts, args = p.parse_args() keys = [] tmpl_f = open(opts.template, encoding='utf-8') template = string.Template(tmpl_f.read()) tmpl_f.close() js = ''' ''' for cat in os.listdir(opts.puzzles): dirname = os.path.join(opts.puzzles, cat) for points in os.listdir(dirname): pointsdir = os.path.join(dirname, points) if not os.path.isdir(pointsdir): continue outdir = os.path.join(opts.htmldir, cat, points) try: os.makedirs(outdir) except OSError: pass readme = '' files = [] for fn in os.listdir(pointsdir): path = os.path.join(pointsdir, fn) if fn == 'key': for key in open(path, encoding='utf-8'): key = key.rstrip() keys.append((cat, points, key)) elif fn == 'hint': pass elif fn == 'index.html': readme = open(path, encoding='utf-8').read() elif fn == 'index.mdwn': readme = open(path, encoding='utf-8').read() readme = markdown.markdown(readme) elif fn.endswith('~'): pass else: files.append((fn, path)) title = '%s for %s points' % (cat, points) body = [] if readme: body.append('