summaryrefslogtreecommitdiff
path: root/panfry/page.py
blob: 27b4cb19aef2950053d75209829f6be6f96c8cc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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'