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/page.py | 29 ++++------------------------- 1 file changed, 4 insertions(+), 25 deletions(-) (limited to 'panfry/page.py') 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() -- cgit v1.2.3