summaryrefslogtreecommitdiff
path: root/panfry/page.py
diff options
context:
space:
mode:
Diffstat (limited to 'panfry/page.py')
-rwxr-xr-xpanfry/page.py59
1 files changed, 59 insertions, 0 deletions
diff --git a/panfry/page.py b/panfry/page.py
new file mode 100755
index 0000000..27b4cb1
--- /dev/null
+++ b/panfry/page.py
@@ -0,0 +1,59 @@
+#!/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'