From 66120bb19e24096f2482df5e479e7e4b9ca59fed Mon Sep 17 00:00:00 2001 From: Scott Bahling Date: Thu, 18 Jul 2013 12:43:07 +0200 Subject: Implement multi-level documentation handling --- panfry/document.py | 11 +++++--- panfry/main.py | 82 ++++++++++++++++++++++++++++++------------------------ panfry/page.py | 9 +++++- 3 files changed, 60 insertions(+), 42 deletions(-) diff --git a/panfry/document.py b/panfry/document.py index 035bf56..e6e35c8 100755 --- a/panfry/document.py +++ b/panfry/document.py @@ -1,18 +1,19 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +from __future__ import print_function import os import re import pandoc from tempfile import NamedTemporaryFile from panfry.util import * -from panfry.page import TextFile, Page, TOCItem +from panfry.page import TextFile, Page from panfry.toc import TOCItem class Document: - def __init__(self, path): - self.src_path = os.path.join(path, 'src') + 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 @@ -289,9 +290,11 @@ class Document: # If there is not explicit index.html, then link 'index.html' # to the toplevel page. - if self.standalone and not 'index.md' in self.pages: + htmlfiles = [page.htmlfile for page in self.pages] + if self.standalone and not 'index.html' in htmlfiles: src = os.path.join(self.pages[0].htmlfile) ref = os.path.join(pub_path, 'index.html') + print("Linking %s to %s", (ref, src)) if os.path.exists(ref): os.remove(ref) os.symlink(src, ref) diff --git a/panfry/main.py b/panfry/main.py index 9572243..1b90181 100755 --- a/panfry/main.py +++ b/panfry/main.py @@ -36,8 +36,10 @@ class StoppableHttpServer(BaseHTTPServer.HTTPServer): def get_env(): env = panfry.cli.init_argparser().parse_args() env.pub_path = os.path.abspath(env.pub_path) + env.index = [''] if env.cmd in ['gen']: + INDEX = os.path.join(env.doc_path, 'src', 'INDEX') if not os.path.isdir(env.doc_path): print("%s directory not found. Aborting..." % env.doc_path) exit(1) @@ -52,47 +54,53 @@ def get_env(): if env.pandoc_options: env.pandoc_options = list(shlex.split(env.pandoc_options)) + if os.path.isfile(INDEX): + env.index += open(INDEX, 'r').readlines() + 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.json_toc: - document.set_json_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) + for item in env.index: + item = item.strip() + 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) + if env.simple_toc: + document.set_simple_toc + + if env.json_toc: + document.set_json_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(docpubpath): + os.makedirs(docpubpath) + + 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(docpubpath) + print("Wrote PDF: %s" % pdffile) + + ###### Create epub + epubfile = document.publish_epub(docpubpath) + print("Wrote epub: %s" % epubfile) + + ###### Create HTML + document.publish_html(docpubpath) exit(0) diff --git a/panfry/page.py b/panfry/page.py index 21b6323..efd3561 100755 --- a/panfry/page.py +++ b/panfry/page.py @@ -38,6 +38,9 @@ class Page(TextFile): self.format = format self._markdown = '' + def __repr__(self): + return self.markdown + @property def markdown(self): if self.format == 'markdown': @@ -92,7 +95,11 @@ class Page(TextFile): continue if re.match('#+.+[A-z|0-9]', line): level = len(re.match('#+', line).group()) - toc.append(TOCItem(line.split(' ', 1)[1].strip(), level, self.htmlfile)) + toc.append(TOCItem(line.split(' ', 1)[1].strip(), + level, + self.htmlfile, + ) + ) continue heading = line.strip() -- cgit v1.2.3