From ae99cfbd9a0372b577a23636f0677f266a559c5f Mon Sep 17 00:00:00 2001 From: Scott Bahling Date: Tue, 19 Mar 2013 18:25:18 +0100 Subject: Implement full table of contents generation By default panfry now generates a full table of contents navigation for split page documents. This means the TOC navitation has links to all pages and page sections. Simple, page only level table of contents can be switched on by passing the --simple-toc option. --- panfry/cli.py | 7 +++++++ panfry/document.py | 34 ++++++++++++++++++++++++---------- panfry/main.py | 3 +++ panfry/page.py | 19 ++++++++++++++++--- 4 files changed, 50 insertions(+), 13 deletions(-) (limited to 'panfry') diff --git a/panfry/cli.py b/panfry/cli.py index 363bbcc..9beae43 100755 --- a/panfry/cli.py +++ b/panfry/cli.py @@ -79,6 +79,12 @@ Args: css file for html pages. Includes full path relative to html directory. + simple_toc: + flags: [--simple-toc] + action: store_true + default: False + help: 'Do not generate full level table of contents' + Parser: help: @@ -94,6 +100,7 @@ Subparsers: - src_path - pub_path - templates_path + - simple_toc - css """ diff --git a/panfry/document.py b/panfry/document.py index 0b450da..aa29561 100755 --- a/panfry/document.py +++ b/panfry/document.py @@ -14,9 +14,20 @@ class Document: self.src_path = path self.meta = self.get_meta(path) self.pages = self.get_pages(path) + self.full_toc = True - css_file = 'css/style.css' - workdir = 'stdocs-work' + self.css_file = 'css/style.css' + + @property + def set_simple_toc(self): + self.full_toc = False + + @property + def set_full_toc(self): + self.full_toc = True + + def set_templater(self, templater): + self.templater = templater @property def title(self): @@ -56,9 +67,6 @@ class Document: return meta - def set_templater(self, templater): - self.templater = templater - def next_page(self, page): try: idx = self.pages.index(page) @@ -85,11 +93,17 @@ class Document: ''' links = [] for page in self.pages: - title, level = page.title - links.append(dict(link=unicode(page.htmlfile, "utf8"), - text=unicode(title, "utf8"), - level=level, - )) + if self.full_toc: + toc = page.toc + else: + toc = [page.toc[0]] + + for tocitem in toc: + link = '%s#%s' % (page.htmlfile, tocitem.section_id) + links.append(dict(link=unicode(link, "utf8"), + text=unicode(tocitem.heading, "utf8"), + level=tocitem.level, + )) return links diff --git a/panfry/main.py b/panfry/main.py index 1a7acf7..776fd52 100755 --- a/panfry/main.py +++ b/panfry/main.py @@ -24,6 +24,7 @@ def get_env(): return env + def main(): env = get_env() if os.path.exists(env.pub_path): @@ -32,6 +33,8 @@ def main(): document = Document(env.src_path) document.set_templater(Templater(env.templates_path)) + if env.simple_toc: + document.set_simple_toc ###### Create PDF pdffile = document.publish_pdf(env.pub_path) diff --git a/panfry/page.py b/panfry/page.py index 2c82e04..76620bc 100755 --- a/panfry/page.py +++ b/panfry/page.py @@ -24,6 +24,19 @@ class TextFile: return self.lines[0] +class TOCItem: + ''' + Table of Contents Line Item + ''' + def __init__(self, heading, level): + self.heading = heading + self.level = level + + @property + def section_id(self): + return self.heading.lower().replace(' ', '-') + + class Page(TextFile): ''' Represents single page source file. @@ -70,14 +83,14 @@ class Page(TextFile): heading = '' for line in self.lines: if heading and re.match('[=]{2}', line): - toc.append((heading, 1)) + toc.append(TOCItem(heading, 1)) continue if heading and re.match('[-]{2}', line): - toc.append((heading, 2)) + toc.append(TOCItem(heading, 2)) continue if re.match('#+.+[A-z|0-9]', line): level = len(re.match('#+', line).group()) - toc.append((line.split(' ', 1)[1].strip(), level)) + toc.append(TOCItem(line.split(' ', 1)[1].strip(), level)) continue heading = line.strip() -- cgit v1.2.3