diff options
| author | Scott Bahling <sbahling@mudgum.net> | 2013-03-17 21:26:02 +0100 |
|---|---|---|
| committer | Scott Bahling <sbahling@mudgum.net> | 2013-03-17 21:26:02 +0100 |
| commit | bf0b3f2a096832a9063f7816dcc14a0607f67271 (patch) | |
| tree | efff29003b44d8c471587f751740b6f9eac48478 /panfry/document.py | |
| download | panfry-bf0b3f2a096832a9063f7816dcc14a0607f67271.tar.gz panfry-bf0b3f2a096832a9063f7816dcc14a0607f67271.tar.xz panfry-bf0b3f2a096832a9063f7816dcc14a0607f67271.zip | |
initial commit
Diffstat (limited to 'panfry/document.py')
| -rwxr-xr-x | panfry/document.py | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/panfry/document.py b/panfry/document.py new file mode 100755 index 0000000..42ef959 --- /dev/null +++ b/panfry/document.py @@ -0,0 +1,159 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +import os +import re +import pandoc +from tempfile import NamedTemporaryFile +from panfry.util import * + + +class Document: + + def __init__(self, path): + self.src_path = path + self.meta = self.get_meta(path) + self.pages = self.get_pages(path) + + css_file = 'css/style.css' + workdir = 'stdocs-work' + + def get_pages(self, path): + pages = [] + filelist = get_lines(self.meta['TOC']) + for filename in filelist: + source = read_file(os.path.join(path, filename)) + if source: + pages.append(Page(filename, source)) + else: + print("WARNING: Page: %s not found or is empty." % filename) + + return pages + + def get_meta(self, path): + metafiles = ['TOC', 'HEADER'] + meta = {} + for filename in metafiles: + source = read_file(os.path.join(path, filename)) + if source: + meta[filename] = source + else: + print("!E: %s not found or is empty. Aborting..." % filename) + exit(1) + + return meta + + def set_templater(self, templater): + self.templater = templater + + def next_page(self, page): + try: + idx = self.pages.index(page) + except: + return '' + if idx >= len(self.pages) - 1: + return '' + return self.pages[idx+1] + + def prev_page(self, page): + try: + idx = self.pages.index(page) + except: + return '' + if idx == 0: + return '' + return self.pages[idx-1] + + @property + def toc_links(self): + ''' + Returns a list of dictionaries. Each dictionary element contains + a page title and the html file name. + ''' + links = [] + for page in self.pages: + title, level = page.title + links.append(dict(link=unicode(page.htmlfile, "utf8"), + text=unicode(title, "utf8"), + level=level, + )) + + return links + + @property + def pdf_filename(self): + header = self.meta['HEADER'] + m = re.match('(^%)(.*\n.*)(%*)', header) + if m: + title = m.group(2).strip() + title = re.sub(r'\s+', ' ', title) + title = re.sub(r'[\n ]', '_', title) + title = re.sub(r'[:,]', '-', title) + title = re.sub(r'_+', '_', title) + title = re.sub(r'-_', '-', title) + title = re.sub(r'-+', '-', title) + else: + title = os.path.split(self.src_path)[1] + + return title + '.pdf' + + def publish_pdf(self, pub_path): + pdf_path = os.path.join(pub_path, self.pdf_filename) + src = self.meta['HEADER'] + for page in self.pages: + src += '\n%s' % page.source + + doc = pandoc.Document() + doc.markdown = src + + pandoc.set_cwd(os.path.abspath(self.src_path)) + doc.to_file(pdf_path) + pandoc.set_cwd(None) + + return self.pdf_filename + + def publish_css(self, pub_path): + src = os.path.join(self.src_path, 'css') + dst = os.path.join(pub_path, 'css') + if os.path.isdir(src): + copy(src, dst) + + def publish_images(self, pub_path): + src_path = self.src_path + if os.path.exists(os.path.join(src_path, 'images/html')): + src = os.path.join(src_path, 'images/html') + else: + src = os.path.join(src_path, 'images') + + dst = os.path.join(pub_path, 'images') + if os.path.exists(src): + copy(src, dst) + + def publish_html(self, pub_path): + pandoc.set_cwd(None) + for page in self.pages: + print("generating %s..." % page.htmlfile) + template_file = NamedTemporaryFile(mode='w', + suffix='pf.template', + delete=False) + template = self.templater.page_template(self, page) + template_file.write(template) + template_file.close() + doc = pandoc.Document() + doc.add_argument('toc') + doc.add_argument('template=%s' % template_file.name) + doc.add_argument('css=%s' % self.css_file) + doc.markdown = page.source + content = doc.html + write_file(os.path.join(pub_path, page.htmlfile), + unicode(content, 'utf-8')) + + # If there is not explicit index.html, then link 'index.html' + # to the toplevel page. + if not 'index.md' in self.pages: + src = os.path.join(self.pages[0].htmlfile) + ref = os.path.join(pub_path, 'index.html') + os.symlink(src, ref) + + ###### Copy any images to publish directory + self.publish_css(pub_path) + self.publish_images(pub_path) |
