#!/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)
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='[Prev]' % (
i-1)
else:
prev='[Index]'
if i!=len(slides):
next='[Next]' % (
i+1)
else:
next='[Index]'
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='''
''' % '\n'.join(all_titles)
toc=""
prev='[End]' % len(slides)
next='[Start]'
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('