#!/usr/bin/env python # -*- coding: utf-8 -*- from jinja2 import Environment, FileSystemLoader class Templater: ''' Pandoc Template generator. Creates a template suitible for passing to pandoc for html generation. The most prominent feature is adding the custom table of contents navigator that navigates accross muliple html pages. The generator references a jinja2 template file called html5.template located either in the directory 'templates' under the source path, or in the directory passed as an option. ''' def __init__(self, path): self.templates_path = path self.j2 = Environment(loader=FileSystemLoader(self.templates_path), line_statement_prefix='#', extensions=['jinja2.ext.loopcontrols'], ) def page_template(self, doc, page): ''' Returns a new page template for use by pandoc. Arguments: - doc: Panfry.Document. - page: Panfry.Page from Panfry.Document to generate template for. ''' options = dict(toc=doc.toc_links) options['page'] = unicode(page.htmlfile, "utf8") options['pdf'] = unicode(doc.pdf_filename) options['epub'] = unicode(doc.epub_filename) options['prev'] = u'' options['next'] = u'' prevpage = doc.prev_page(page) nextpage = doc.next_page(page) if prevpage: options['prev'] = unicode(prevpage.htmlfile, "utf8") if nextpage: options['next'] = unicode(nextpage.htmlfile, "utf8") template = self.j2.get_template('html5.template') return template.render(options=options).encode('utf-8')