diff options
Diffstat (limited to 'panfry/page.py')
| -rwxr-xr-x | panfry/page.py | 71 |
1 files changed, 50 insertions, 21 deletions
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): |
