#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import print_function import os import shutil import shlex import SimpleHTTPServer import SocketServer import panfry.cli from panfry.templater import Templater from panfry.document import Document def get_env(): env = panfry.cli.init_argparser().parse_args() env.pub_path = os.path.abspath(env.pub_path) if env.cmd in ['gen']: if not os.path.isdir(env.doc_path): print("%s directory not found. Aborting..." % env.doc_path) exit(1) if not env.templates_path: env.templates_path = os.path.join(env.doc_path, 'templates') if not os.path.isdir(env.templates_path): print("No templates path found. Aborting...") exit(1) if env.pandoc_options: env.pandoc_options = list(shlex.split(env.pandoc_options)) return env def gen(env): document = Document(env.doc_path) document.set_templater(Templater(env.templates_path)) document.set_pandoc_options(env.pandoc_options) if env.simple_toc: document.set_simple_toc if env.clean and os.path.exists(env.pub_path): print("Cleanup, removing %s..." % env.pub_path) shutil.rmtree(env.pub_path) if not os.path.exists(env.pub_path): os.mkdir(env.pub_path) if env.assets: print("Publishing assets only...", end=' ') document.publish_assets(env.pub_path) print("done.") exit(0) print("\nUsing pandoc options:") for opt in env.pandoc_options: print(" --%s" % opt) print() ###### Create PDF pdffile = document.publish_pdf(env.pub_path) print("Wrote PDF: %s" % pdffile) ###### Create epub epubfile = document.publish_epub(env.pub_path) print("Wrote epub: %s" % epubfile) ###### Create HTML document.publish_html(env.pub_path) exit(0) def serve(env): os.chdir(env.pub_path) Handler = SimpleHTTPServer.SimpleHTTPRequestHandler httpd = SocketServer.TCPServer(("", int(env.port)), Handler) print("serving at port: %s" % int(env.port)) httpd.serve_forever() def main(): CMD = dict(gen=gen, serve=serve) env = get_env() CMD[env.cmd](env) if __name__ == "__main__": main()