#!/usr/bin/env python # -*- coding: utf-8 -*- # Python Imports import os import re import cgi import sys def usage(): print """Usage: %s content.html [template.html] slides/ Content should be HTML containing

tags. A new page will be created for each

tag. """ % (os.path.basename(sys.argv[0])) class Slide: def __init__(self, title, atts): assert title self.lines=[] self.title=title self.atts=atts self.text=None def fill_template(tmpl, mydict): assert mydict.get('title') tmpl=re.sub(r'%([^(])', r'%%\1', tmpl) return tmpl % mydict def main(): args=list(sys.argv[1:]) if len(args)!=3: usage() sys.exit(1) htmlfile=args[0] templatefile=args[1] template=open(templatefile).read() outdir=args[2] if not os.path.isdir(outdir): raise ValueError("%s is not a directory." % outdir) fd=open(htmlfile) slides=[] def endslide(slides, slide): slide.text=''.join(slide.lines) slide.lines=None slide=None comments=0 linecount=0 for line in fd: linecount+=1 match=re.match(r'^(.*?)(.*?)

(.*)$', line) if match: if match.group(1): raise ValueError("Text before

: '%s'" % (match.group(1))) if match.group(4): raise ValueError("Text after

: '%s'" % (match.group(4))) attr=match.group(2) title=match.group(3) if slides: endslide(slides, slide) slide=Slide(title, attr) slides.append(slide) continue (line, n)=re.subn(r'\(#', '', line) if n and comments: raise ValueError("Comments (# ..#) mustnot be nested %s line %s" % ( htmlfile, linecount)) comments+=n (line, n)=re.subn(r'#\)', '', line) comments-=n if n and comments!=0: raise ValueError("Closing comment '#)' without opening comment. %s line %s" % ( htmlfile, linecount)) if not slides: continue slide.lines.append(line) if not comments==0: raise ValueError("Number of opening comments '(#' " "does not match number of closing comments '#)'") if not slide: raise ValueError('No line with "

...

" found: %s' % htmlfile) endslide(slides, slide) i=0 all_titles=[] all_slides=[] bottom="" css="" for slide in slides: i+=1 title=cgi.escape(slide.title) if i>1: prev='' % ( i-1) else: prev='' if i!=len(slides): next='' % ( i+1) else: next='' toc='%dv%d' % ( i, len(slides)) main=slide.text all_slides.append(main) outfile=os.path.join(outdir, "slide_%03d.html" % i) fd=open(outfile, "wt") html=fill_template(template, locals()) fd.write(html) fd.close() all_titles.append('%d%s' % ( i, i, title)) print "Created slides in %s" % outdir indexfile=os.path.join(outdir, "index.html") main=''' %s
''' % '\n'.join(all_titles) toc="" prev='' % len(slides) next='' title="Index" bottom='All' css="" index=fill_template(template, locals()) fd=open(indexfile, "wt") fd.write(index) fd.close() print "Created TOC %s" % indexfile # The page with all slides prev="" next="" title="" bottom="" css=''' ''' toc='Index' title='Index' main='
'.join(all_slides) main=main.replace('', '') allfile=os.path.join(outdir, "all.html") fd=open(allfile, "wt") fd.write(fill_template(template, locals())) fd.close() print "Created All Pages (incl. Comments): %s" % allfile if __name__=="__main__": main() template=''' %(css)s %(title)s
%(toc)s

%(title)s

%(main)s
%(prev)s %(bottom)s %(next)s
'''