From 7eba8bd7180f538c1bfdd338abf6c858ba9dfb67 Mon Sep 17 00:00:00 2001 From: Scott Bahling Date: Mon, 8 Apr 2013 20:21:44 +0200 Subject: Rename HEADER to TITLE_BLOCK --- panfry/document.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'panfry/document.py') diff --git a/panfry/document.py b/panfry/document.py index 2b1cf01..0ea619d 100755 --- a/panfry/document.py +++ b/panfry/document.py @@ -71,9 +71,9 @@ class Document: @property def title(self): - if self.header.firstline.startswith('%'): - title = self.header.firstline.split('%')[1].strip() - for line in self.header.lines[1:]: + if self.title_block.firstline.startswith('%'): + title = self.title_block.firstline.split('%')[1].strip() + for line in self.title_block.lines[1:]: if line.startswith('%'): break title = '%s %s' % (title, line.strip()) @@ -98,7 +98,7 @@ class Document: return pages def get_meta(self, path): - metafiles = ['TOC', 'HEADER'] + metafiles = ['TOC', 'TITLE_BLOCK'] meta = {} for filename in metafiles: source = read_file(os.path.join(path, filename)) @@ -173,7 +173,7 @@ class Document: def publish_pdf(self, pub_path): pdf_path = os.path.join(pub_path, self.pdf_filename) - src = self.header.source + src = self.title_block.source for page in self.pages: src += '\n%s' % page.source @@ -191,7 +191,7 @@ class Document: def publish_epub(self, pub_path): epub_path = os.path.join(pub_path, self.epub_filename) - src = self.header.source + src = self.title_block.source for page in self.pages: src += '\n%s' % page.source -- cgit v1.2.3 From 094530f92d678f9c2f2ad498a2c21653de399a99 Mon Sep 17 00:00:00 2001 From: Scott Bahling Date: Fri, 12 Apr 2013 11:48:07 +0200 Subject: Implement json TOC --- panfry/document.py | 68 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 59 insertions(+), 9 deletions(-) (limited to 'panfry/document.py') diff --git a/panfry/document.py b/panfry/document.py index 0ea619d..035bf56 100755 --- a/panfry/document.py +++ b/panfry/document.py @@ -5,7 +5,8 @@ import re import pandoc from tempfile import NamedTemporaryFile from panfry.util import * -from panfry.page import TextFile, Page +from panfry.page import TextFile, Page, TOCItem +from panfry.toc import TOCItem class Document: @@ -15,6 +16,8 @@ class Document: self.meta = self.get_meta(self.src_path) self.pages = self.get_pages(self.src_path) self.full_toc = True + self.standalone = True + self.json_toc = False self.css_file = 'css/style.css' self.assets_dir = 'assets' @@ -24,6 +27,11 @@ class Document: def set_simple_toc(self): self.full_toc = False + @property + def set_json_toc(self): + self.json_toc = True + self.standalone = False + def set_pandoc_options(self, options): self.pandoc_options = options @@ -62,10 +70,10 @@ class Document: for page in self.pages: for tocitem in page.toc: if tocitem.heading == heading.replace('\n', ' '): - return '[%s](%s#%s)' % (heading, - page.htmlfile, - tocitem.header_id, - ) + return '[%s](%s%s)' % (heading, + page.htmlfile, + tocitem.header_id, + ) return '[%s]' % ref_txt @@ -143,7 +151,7 @@ class Document: toc = [page.toc[0]] for tocitem in toc: - link = '%s#%s' % (page.htmlfile, tocitem.header_id) + link = '%s%s' % (page.htmlfile, tocitem.header_id) links.append(dict(link=unicode(link, "utf8"), text=unicode(tocitem.heading, "utf8"), level=tocitem.level, @@ -151,6 +159,37 @@ class Document: return links + @property + def toc_json(self): + lastchild = top = toc = TOCItem(self.title, 1, 'title_block.html') + + curlevel = 0 + for page in self.pages: + heading = '' + for line in page.markdown.split('\n'): + line = line.strip() + if heading and re.match('[=]{2}', line): + level = 1 + elif heading and re.match('[-]{2}', line): + level = 2 + elif re.match('#+.+[A-z|0-9]', line): + level = len(re.match('#+', line).group()) + heading = line.split(' ', 1)[1].strip() + else: + heading = line.strip() + continue + + if curlevel < level: + toc = lastchild + curlevel = toc.level + while toc.parent and curlevel >= level: + toc = toc.parent + curlevel = toc.level + + lastchild = toc.add_child(heading, level, page.htmlfile) + + return top.json + @property def filename_base(self): filename = self.title @@ -231,24 +270,35 @@ class Document: doc.add_argument('toc') doc.add_argument('template=%s' % template_file.name) doc.add_argument('css=%s' % self.css_file) + for option in self.pandoc_options: if option == 'number-sections': continue doc.add_argument(option) - doc.markdown = '%s\n%s' % (self.header.source, - self.expand_int_links(page.markdown)) + + if self.standalone: + markdown = '%s\n' % self.title_block.source + else: + markdown = '' + + markdown += self.expand_int_links(page.markdown) + doc.markdown = markdown content = doc.html5 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: + if self.standalone and not 'index.md' in self.pages: src = os.path.join(self.pages[0].htmlfile) ref = os.path.join(pub_path, 'index.html') if os.path.exists(ref): os.remove(ref) os.symlink(src, ref) + if self.json_toc: + write_file(os.path.join(pub_path, 'toc.json'), + unicode(self.toc_json, 'utf-8')) + ###### Copy any assets to publish directory self.publish_assets(pub_path) -- cgit v1.2.3 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 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'panfry/document.py') 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) -- cgit v1.2.3 From ab77b7fcc1416c1acb8e77572db7b30c72cc5292 Mon Sep 17 00:00:00 2001 From: Scott Bahling Date: Thu, 18 Jul 2013 12:45:01 +0200 Subject: fix print statement --- panfry/document.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'panfry/document.py') diff --git a/panfry/document.py b/panfry/document.py index e6e35c8..e03aca1 100755 --- a/panfry/document.py +++ b/panfry/document.py @@ -294,7 +294,7 @@ class Document: 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)) + print("Linking %s to %s" % (ref, src)) if os.path.exists(ref): os.remove(ref) os.symlink(src, ref) -- cgit v1.2.3