summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott Bahling <sbahling@suse.de>2013-04-12 11:48:07 +0200
committerScott Bahling <sbahling@suse.de>2013-04-12 11:48:07 +0200
commit094530f92d678f9c2f2ad498a2c21653de399a99 (patch)
tree6e6d2d9b20b1bf1f3407d1d042a0d12ef92d49c9
parent7eba8bd7180f538c1bfdd338abf6c858ba9dfb67 (diff)
downloadpanfry-094530f92d678f9c2f2ad498a2c21653de399a99.tar.gz
panfry-094530f92d678f9c2f2ad498a2c21653de399a99.tar.xz
panfry-094530f92d678f9c2f2ad498a2c21653de399a99.zip
Implement json TOC
-rwxr-xr-xpanfry/cli.py9
-rwxr-xr-xpanfry/document.py68
-rwxr-xr-xpanfry/main.py3
-rwxr-xr-xpanfry/page.py29
4 files changed, 75 insertions, 34 deletions
diff --git a/panfry/cli.py b/panfry/cli.py
index 6949474..650b724 100755
--- a/panfry/cli.py
+++ b/panfry/cli.py
@@ -107,6 +107,14 @@ Args:
Example --pandoc-options="smart base-header-level=2"
+ json_toc:
+ flags: [--json-toc]
+ action: store_true
+ default: False
+ help: |
+ Generate toc.json file instead of toc integrated in
+ the html
+
port:
flags: [-P, --port]
default: 8080
@@ -129,6 +137,7 @@ Subparsers:
- pub_path
- templates_path
- simple_toc
+ - json_toc
- assets
- clean
- css
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)
diff --git a/panfry/main.py b/panfry/main.py
index 40c2b15..9572243 100755
--- a/panfry/main.py
+++ b/panfry/main.py
@@ -62,6 +62,9 @@ def gen(env):
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)
diff --git a/panfry/page.py b/panfry/page.py
index 722241c..21b6323 100755
--- a/panfry/page.py
+++ b/panfry/page.py
@@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
import re
import pandoc
+from panfry.toc import TOCItem
class TextFile:
@@ -25,28 +26,6 @@ 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 header_id(self):
- # - Remove all formatting, links, etc.
- # - Remove all punctuation, except underscores, hyphens, and periods.
- # - Replace all spaces and newlines with hyphens.
- # - Convert all alphabetic characters to lowercase.
- # - Remove everything up to the first letter (identifiers may not begin
- # with a number or punctuation mark).
-
- remove = re.compile('^[^a-zA-Z]|[+~!@#$%^&*\(\){}\[\];:"\',<>?/\`]')
- header_id = self.heading.lower().replace('\n', ' ').replace(' ', '-')
- return remove.sub('', header_id)
-
-
class Page(TextFile):
'''
Represents single page source file.
@@ -106,14 +85,14 @@ class Page(TextFile):
for line in self.markdown.split('\n'):
line = line.strip()
if heading and re.match('[=]{2}', line):
- toc.append(TOCItem(heading, 1))
+ toc.append(TOCItem(heading, 1, self.htmlfile))
continue
if heading and re.match('[-]{2}', line):
- toc.append(TOCItem(heading, 2))
+ toc.append(TOCItem(heading, 2, self.htmlfile))
continue
if re.match('#+.+[A-z|0-9]', line):
level = len(re.match('#+', line).group())
- toc.append(TOCItem(line.split(' ', 1)[1].strip(), level))
+ toc.append(TOCItem(line.split(' ', 1)[1].strip(), level, self.htmlfile))
continue
heading = line.strip()