diff options
| author | Scott Bahling <sbahling@suse.de> | 2013-04-12 11:48:07 +0200 |
|---|---|---|
| committer | Scott Bahling <sbahling@suse.de> | 2013-04-12 11:48:07 +0200 |
| commit | 094530f92d678f9c2f2ad498a2c21653de399a99 (patch) | |
| tree | 6e6d2d9b20b1bf1f3407d1d042a0d12ef92d49c9 /panfry/document.py | |
| parent | 7eba8bd7180f538c1bfdd338abf6c858ba9dfb67 (diff) | |
| download | panfry-094530f92d678f9c2f2ad498a2c21653de399a99.tar.gz panfry-094530f92d678f9c2f2ad498a2c21653de399a99.tar.xz panfry-094530f92d678f9c2f2ad498a2c21653de399a99.zip | |
Implement json TOC
Diffstat (limited to 'panfry/document.py')
| -rwxr-xr-x | panfry/document.py | 68 |
1 files changed, 59 insertions, 9 deletions
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, @@ -152,6 +160,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 filename = re.sub(r'\s+', ' ', filename) @@ -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) |
