#!/usr/bin/env python2 """The Power of Greypoint This is a tiny script that turns a ReST-formatted text into presentation slides. In the source text, slides are delimited by a line containing only a percent sign. From any slide you can use the "n", "b", and "i" accesskeys (Alt-[nbi] in Mozilla) to move one slide forward, one slide backward, or back to the beginning of the presentation. Presentation is handled by a 'default.css' file assumed to be in the same directory as the slide files. This script requires the docutils module.""" __author__ = "Leonard Richardson (leonardr@segfault.org)" __version__ = "1.0" __copyright__ = "Copyright (c) 2003 Leonard Richardson" __license__ = "Public domain" import os import re import string import sys try: from docutils.core import publish_string from docutils.writers import html4css1 except: raise "Unable to import the docutils module; please check your search path (%s)" % ':'.join(sys.path) class Presentation: def __init__(self, presentationFile): "Initializes a presentation from a source file." self.slides = re.split("%\n", presentationFile.read()) def write(self, destinationDir): "Writes a presentation into slide files in the specified directory." writer = html4css1.Writer() for i in range(0, len(self.slides)): slide = self.slides[i] + '\n.. raw:: html\n\n %s' % self.navigationText(i) output = open(os.path.join(destinationDir, self.getFilename(i)), 'w') output.write(publish_string(slide, writer=writer)) output.close() def navigationText(self, ordinal): "Returns navigation links for the given slide." return '%s %s %s' % (self.nextSlideLink(ordinal), self.indexLink(), self.previousSlideLink(ordinal)) def indexLink(self): "Returns a link to the main slide." return self.getLink(0, '', 'i') def nextSlideLink(self, ordinal): "Returns a link to the slide after the given ordinal, if any." link = '' if ordinal != len(self.slides) -1: link = self.getLink(ordinal+1, '', 'n') return link def previousSlideLink(self, ordinal): "Returns a link to the slide before the given ordinal, if any." link = '' if ordinal > 0: link = self.getLink(ordinal-1, '', 'b') return link def getLink(self, ordinal, text, accessKey, style="navigationLink"): "Returns a link to the slide with the given ordinal." return '%s' % \ (style, self.getFilename(ordinal), accessKey, text) def getFilename(self, ordinal): """Returns the filename to which a slide with the given ordinal should be written.""" if ordinal == 0: filename = 'index' else: filename = string.zfill(str(ordinal), 2) return filename + '.html' if __name__ == '__main__': if len(sys.argv) < 3: sys.exit("Syntax: %s [source file] [destination directory]" % sys.argv[0]) presentation = Presentation(open(sys.argv[1])) print "By the power of Greypoint..." presentation.write(sys.argv[2])