diff options
| author | Scott Bahling <sbahling@suse.de> | 2013-09-23 15:37:14 +0200 |
|---|---|---|
| committer | Scott Bahling <sbahling@suse.de> | 2013-09-23 15:37:14 +0200 |
| commit | 047df954a7ad23c54ffba0f191a05419ffd1bcf5 (patch) | |
| tree | d1a5539ddeea32ca4989e5cb260910a39edefe16 | |
| parent | ef2744a43f22563d50af678777ae1e3d8120c9a7 (diff) | |
| download | panfry-047df954a7ad23c54ffba0f191a05419ffd1bcf5.tar.gz panfry-047df954a7ad23c54ffba0f191a05419ffd1bcf5.tar.xz panfry-047df954a7ad23c54ffba0f191a05419ffd1bcf5.zip | |
A bit of refactoring
- Add NOPDF and NOEPUB features
| -rwxr-xr-x | panfry/document.py | 64 | ||||
| -rwxr-xr-x | panfry/main.py | 15 |
2 files changed, 54 insertions, 25 deletions
diff --git a/panfry/document.py b/panfry/document.py index 01f8f83..eec8aea 100755 --- a/panfry/document.py +++ b/panfry/document.py @@ -10,38 +10,54 @@ from panfry.page import TextFile, Page from panfry.toc import TOCItem -class Document: +class Document(object): + 'A panfry documment' + + valid_toc_types = ('simple', + 'full', + 'json', + ) + + standalone_toc_types = ('full') def __init__(self, path, subdoc=''): self.src_path = os.path.join(path, 'src', subdoc) self.meta = self.get_meta(self.src_path) self.pages = self.get_pages(self.src_path) - self.full_toc = True + self._toc_type = 'full' self.standalone = True - self.json_toc = False + self.template_path = os.path.join(path, 'templates') self.css_file = 'css/style.css' - self.assets_dir = 'assets' + self.assets_dir = os.path.join('assets', subdoc) self.pandoc_options = [] - @property - def set_simple_toc(self): - self.full_toc = False @property - def set_json_toc(self): - self.json_toc = True - self.standalone = False + def no_pdf(self): + if 'NOPDF' in os.listdir(self.src_path): + return True + return False - def set_pandoc_options(self, options): - self.pandoc_options = options + @property + def no_epub(self): + if 'NOEPUB' in os.listdir(self.src_path): + return True + return False @property - def set_full_toc(self): - self.full_toc = True + def toc_type(self): + return self._toc_type + + @toc_type.setter + def toc_type(self, toc_type): + if toc_type in self.valid_toc_types: + self._toc_type = toc_type + self.standalone = toc_type in self.standalone_toc_types + else: + return '' - def set_templater(self, templater): - self.templater = templater + return toc_type def expand_int_links(self, source): ''' @@ -146,7 +162,7 @@ class Document: ''' links = [] for page in self.pages: - if self.full_toc: + if self.toc_type == 'full': toc = page.toc else: toc = [page.toc[0]] @@ -212,6 +228,8 @@ class Document: return self.filename_base + '.epub' def publish_pdf(self, pub_path): + if self.no_pdf: + return '' pdf_path = os.path.join(pub_path, self.pdf_filename) src = self.title_block.source for page in self.pages: @@ -221,7 +239,11 @@ class Document: doc.markdown = src pandoc.set_cwd(os.path.abspath(self.src_path)) - doc.add_argument('latex-engine=xelatex') + template = os.path.abspath(os.path.join(self.template_path, 'tex.template')) + if os.path.isfile(template): + print(template) + doc.add_argument('template=%s' % template) + doc.add_argument('latex-engine=lualatex') for option in self.pandoc_options: doc.add_argument(option) doc.to_file(pdf_path) @@ -230,6 +252,8 @@ class Document: return self.pdf_filename def publish_epub(self, pub_path): + if self.no_epub: + return '' epub_path = os.path.join(pub_path, self.epub_filename) src = self.title_block.source for page in self.pages: @@ -255,6 +279,8 @@ class Document: dst = os.path.join(pub_path, asset) if os.path.isdir(src): copy(src, dst) + else: + copy(src, dst) def publish_html(self, pub_path): pandoc.set_cwd(None) @@ -299,7 +325,7 @@ class Document: os.remove(ref) os.symlink(src, ref) - if self.json_toc: + if self.toc_type == 'json': write_file(os.path.join(pub_path, 'toc.json'), unicode(self.toc_json, 'utf-8')) diff --git a/panfry/main.py b/panfry/main.py index 917ebc0..5d05d20 100755 --- a/panfry/main.py +++ b/panfry/main.py @@ -70,15 +70,16 @@ def get_env(): def gen(env): for item in env.index: item = item.strip() + print("Processing: %s..." % item) docpubpath = os.path.join(env.pub_path, item) document = Document(env.doc_path, item) - document.set_templater(Templater(env.templates_path)) - document.set_pandoc_options(env.pandoc_options) + document.templater = Templater(env.templates_path) + document.pandoc_options = env.pandoc_options if env.simple_toc: - document.set_simple_toc + document.toc_type = 'simple' if env.json_toc: - document.set_json_toc + document.toc_type = 'json' if env.clean and os.path.exists(env.pub_path): print("Cleanup, removing %s..." % env.pub_path) @@ -100,11 +101,13 @@ def gen(env): ###### Create PDF pdffile = document.publish_pdf(docpubpath) - print("Wrote PDF: %s" % pdffile) + if pdffile: + print("Wrote PDF: %s" % pdffile) ###### Create epub epubfile = document.publish_epub(docpubpath) - print("Wrote epub: %s" % epubfile) + if epubfile: + print("Wrote epub: %s" % epubfile) ###### Create HTML document.publish_html(docpubpath) |
