# This software is released to the public domain by its sole author
# Moshe Zadka <moshez@twistedmatrix.com>
#
# Additional changes made by Itamar Shtull-Trauring
import os, string, cgi, types
class Lecture:
"""A series of slides."""
css = None
def __init__(self, name, author, publish_date, copyright, *slides):
self.name = name
self.slides = slides
self.copyright = copyright
self.author = author
self.publish_date = publish_date
def render(self, directory, basename, css=None):
"""Render all slides and their index."""
self.css = css
self._renderSlide(self.slides[0], 1, directory, basename, prev=0)
for i in range(1, len(self.slides)-1):
self._renderSlide(self.slides[i], i+1, directory, basename)
i = len(self.slides)-1
self._renderSlide(self.slides[i], i+1, directory, basename, next=0)
self._renderIndex(directory, basename)
def getHeader(self):
"""Return the header for HTML pages."""
result = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>%s</title>\n""" % cgi.escape(self.name)
if self.css:
result += '<link rel="stylesheet" type="text/css" href="%s">\n' % self.css
return result + '</head>\n<body>\n'
def getFooter(self):
"""Return the footer for HTML pages."""
return '<hr/><table class="footer" border="0" width="100%%"><tr><td align="left" width="33%%">%s</td><td align="center" align="left" width="33%%">%s</td><td align="right" align="left" width="33%%">%s</td></tr></table>\n</body></html>' % (self.author, self.publish_date, self.copyright)
def _renderIndex(self, directory, basename):
fp = open(os.path.join(directory, basename % 0), 'w')
fp.write(self.getHeader())
fp.write('<h1>%s</h1>\n' % self.name)
fp.write('<div class="slide">\n<ul>')
for i in range(len(self.slides)):
fp.write("<li><a href='%s'>%s</a>\n" %
(basename % (i+1), self.slides[i].title))
fp.write('</ul>\n</div>')
fp.write(self.getFooter())
fp.close()
def _renderSlide(self, slide, i, directory, basename, prev=1, next=1):
"""Render a single slide."""
html = '<div class="slide">' + slide.tohtml() + '</div>'
if prev:
prevName = basename % (i-1)
prev = '<a href="%s">Prev</a>' % prevName
else:
prev = 'Prev'
if next:
nextName = basename % (i+1)
next = '<a href="%s">Next</a>' % nextName
else:
next = 'Next'
index = '<a href="%s">Index</a>' % (basename % 0)
slidehead = '<h1>%s</h1>\n' % self.name
result = (self.getHeader() + '<div class="navi">' + " | ".join([prev, index, next]) + '</div>' +
slidehead + html + self.getFooter())
fp = open(os.path.join(directory, basename % i), 'w')
fp.write(result)
fp.close()
class Slide:
def __init__(self, title, *bullets):
self.title = title
self.bullets = bullets
def tohtml(self):
title = '<h2>%s</h2>' % cgi.escape(self.title)
bullets = []
for bullet in self.bullets:
bullets.append(bullet.tohtml())
return title+'<ul>'+string.join(bullets, '\n')+'</ul>'
class SubBullet:
def __init__(self, *bullets):
self.bullets = bullets
def tohtml(self):
bullets = []
for bullet in self.bullets:
bullets.append(toHTML(bullet))
return '<ul>'+string.join(bullets, '\n')+'</ul>'
class Bullet:
def __init__(self, *texts):
self.texts = texts
def tohtml(self):
return '<li>'+ "".join(map(toHTML, self.texts)) + '</li>\n'
class PRE:
def __init__(self, text):
self.text = text
def tohtml(self):
return '<pre>\n'+cgi.escape(self.text)+'</pre>'
def toHTML(text):
"""Convert string or object to HTML."""
if isinstance(text, types.StringType):
return cgi.escape(text)
else:
return text.tohtml()
if __name__ == '__main__':
print "This is just a module."