diff options
Diffstat (limited to 'panfry')
| -rwxr-xr-x | panfry/document.py | 21 | ||||
| -rwxr-xr-x | panfry/page.py | 71 |
2 files changed, 63 insertions, 29 deletions
diff --git a/panfry/document.py b/panfry/document.py index 7bb06b4..0b450da 100755 --- a/panfry/document.py +++ b/panfry/document.py @@ -5,6 +5,7 @@ import re import pandoc from tempfile import NamedTemporaryFile from panfry.util import * +from panfry.page import TextFile, Page class Document: @@ -19,16 +20,20 @@ class Document: @property def title(self): - header = self.meta['HEADER'].strip() - if header.startswith('%'): - return header.split('%')[1].strip() + if self.header.firstline.startswith('%'): + return self.header.firstline.split('%')[1].strip() else: return pages[0].title def get_pages(self, path): pages = [] - filelist = get_lines(self.meta['TOC']) + filelist = self.toc.lines + print filelist for filename in filelist: + print("Reading in %s..." % filename) + if not filename: # blank line in toc + print("WARNING: %s contains blank lines" % self.toc.filename) + continue source = read_file(os.path.join(path, filename)) if source: pages.append(Page(filename, source)) @@ -43,7 +48,8 @@ class Document: for filename in metafiles: source = read_file(os.path.join(path, filename)) if source: - meta[filename] = source + metafile = TextFile(filename, source) + setattr(self, filename.lower(), metafile) else: print("!E: %s not found or is empty. Aborting..." % filename) exit(1) @@ -90,7 +96,6 @@ class Document: @property def pdf_filename(self): filename = self.title - filename = self.meta['HEADER'].split('%')[1].strip() filename = re.sub(r'\s+', ' ', filename) filename = re.sub(r'[\n ]', '_', filename) filename = re.sub(r'[:,]', '-', filename) @@ -102,7 +107,7 @@ class Document: def publish_pdf(self, pub_path): pdf_path = os.path.join(pub_path, self.pdf_filename) - src = self.meta['HEADER'] + src = self.header.source for page in self.pages: src += '\n%s' % page.source @@ -146,7 +151,7 @@ class Document: doc.add_argument('toc') doc.add_argument('template=%s' % template_file.name) doc.add_argument('css=%s' % self.css_file) - doc.markdown = '%s\n%s' % (self.meta['HEADER'], page.source) + doc.markdown = '%s\n%s' % (self.header.source, page.source) content = doc.html write_file(os.path.join(pub_path, page.htmlfile), unicode(content, 'utf-8')) diff --git a/panfry/page.py b/panfry/page.py index 27b4cb1..2c82e04 100755 --- a/panfry/page.py +++ b/panfry/page.py @@ -1,18 +1,35 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- import re -from panfry.utils import get_lines -class Page: +class TextFile: ''' - Represents single source file. + Basic text file class ''' def __init__(self, filename, source): self.filename = filename self.source = source @property + def lines(self): + ''' + Returns list of lines from source. + Leading and trailing blank lines are removed. + ''' + return self.source.strip().split('\n') + + @property + def firstline(self): + return self.lines[0] + + +class Page(TextFile): + ''' + Represents single page source file. + ''' + + @property def title(self): ''' Returns a tuple containing a string representing the page title @@ -32,27 +49,39 @@ class Page: the filename is returned as the title with underscores changed to spaces. ''' - title = '' - for line in get_lines(self.source): - if line.startswith('% '): - title = line.split(' ', 1)[1].strip() - if '(' and ')' in line: - num = line.split('(')[1][0] - return 'man(%s) %s' % (num, title.split('(')[0].strip()) - else: - return (title, 1) - if re.match('[=]{2}', line): - return (title, 1) - if re.match('[-]{2}', line): - return (title, 2) + if self.lines[0].startswith('%'): + title = (self.lines[0].split(' ', 1)[1].strip(), 1) + elif self.toc: + title = self.toc[0] + else: + title = (self.filename.replace('_', ' '), 1) + + return title + + @property + def toc(self): + ''' + Returns a list of tuples representing the headings/sub-heading + structure of the page. + The first element of each tuple is the heading text. + The second element of each tuple is the heading level. + ''' + toc = [] + heading = '' + for line in self.lines: + if heading and re.match('[=]{2}', line): + toc.append((heading, 1)) + continue + if heading and re.match('[-]{2}', line): + toc.append((heading, 2)) + continue if re.match('#+.+[A-z|0-9]', line): level = len(re.match('#+', line).group()) - return (line.split(' ', 1)[1].strip(), level) - title = line.strip() - - title = self.filename.replace('_', ' ') + toc.append((line.split(' ', 1)[1].strip(), level)) + continue + heading = line.strip() - return (title, 1) + return toc @property def htmlfile(self): |
