#!/usr/bin/env python # -*- coding: utf-8 -*- import re from panfry.utils import get_lines class Page: ''' Represents single source file. ''' def __init__(self, filename, source): self.filename = filename self.source = source @property def title(self): ''' Returns a tuple containing a string representing the page title and an integer representing the level in the document outline hierarchy. The level is preserved so that page titles can be properly positioned (indented) in the table of contents. If the page has a pandoc title block, the title is retreived from there and returned with a level of 1 If a title block is not found, the first heading is returned with the corrisponding heading level. Finally, if no title block or headings are found in the 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 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('_', ' ') return (title, 1) @property def htmlfile(self): return '.'.join(self.filename.split('.')[:-1]) + '.html'