summaryrefslogtreecommitdiff
path: root/src/mistune/directives/include.py
blob: d2180bab04af4cb7612c7b93d59e94feadf3eec3 (plain) (blame)
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
60
61
62
63
64
65
import os
from ._base import DirectivePlugin


class Include(DirectivePlugin):
    def parse(self, block, m, state):
        source_file = state.env.get('__file__')
        if not source_file:
            return {'type': 'block_error', 'raw': 'Missing source file'}

        encoding = 'utf-8'
        options = self.parse_options(m)
        if options:
            attrs = dict(options)
            if 'encoding' in attrs:
                encoding = attrs['encoding']
        else:
            attrs = {}

        relpath = self.parse_title(m)
        dest = os.path.join(os.path.dirname(source_file), relpath)
        dest = os.path.normpath(dest)

        if dest == source_file:
            return {
                'type': 'block_error',
                'raw': 'Could not include self: ' + relpath,
            }

        if not os.path.isfile(dest):
            return {
                'type': 'block_error',
                'raw': 'Could not find file: ' + relpath,
            }

        with open(dest, 'rb') as f:
            content = f.read()
            content = content.decode(encoding)

        ext = os.path.splitext(relpath)[1]
        if ext in {'.md', '.markdown', '.mkd'}:
            new_state = block.state_cls()
            new_state.env['__file__'] = dest
            new_state.process(content)
            block.parse(new_state)
            return new_state.tokens

        elif ext in {'.html', '.xhtml', '.htm'}:
            return {'type': 'block_html', 'raw': content}

        attrs['filepath'] = dest
        return {
            'type': 'include',
            'raw': content,
            'attrs': attrs,
        }

    def __call__(self, directive, md):
        directive.register('include', self.parse)
        if md.renderer and md.renderer.NAME == 'html':
            md.renderer.register('include', render_html_include)


def render_html_include(renderer, text, **attrs):
    return '<pre class="directive-include">\n' + text + '</pre>\n'