summaryrefslogtreecommitdiff
path: root/src/mistune/directives/image.py
blob: 5d9d40af577d2573e5798e992cb04afa1c1d2299 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import re
from ._base import DirectivePlugin
from ..util import escape as escape_text, escape_url

__all__ = ['Image', 'Figure']

_num_re = re.compile(r'^\d+(?:\.\d*)?')
_allowed_aligns = ["top", "middle", "bottom", "left", "center", "right"]


def _parse_attrs(options):
    attrs = {}
    if 'alt' in options:
        attrs['alt'] = options['alt']

    # validate align
    align = options.get('align')
    if align and align in _allowed_aligns:
        attrs['align'] = align

    height = options.get('height')
    width = options.get('width')
    if height and _num_re.match(height):
        attrs['height'] = height
    if width and _num_re.match(width):
        attrs['width'] = width
    if 'target' in options:
        attrs['target'] = escape_url(options['target'])
    return attrs


class Image(DirectivePlugin):
    NAME = 'image'

    def parse(self, block, m, state):
        options = dict(self.parse_options(m))
        attrs = _parse_attrs(options)
        attrs['src'] = self.parse_title(m)
        return {'type': 'block_image', 'attrs': attrs}

    def __call__(self, directive, md):
        directive.register(self.NAME, self.parse)
        if md.renderer.NAME == 'html':
            md.renderer.register('block_image', render_block_image)


def render_block_image(self, src: str, alt=None, width=None, height=None, **attrs):
    img = '<img src="' + src + '"'
    style = ''
    if alt:
        img += ' alt="' + escape_text(alt) + '"'
    if width:
        if width.isdigit():
            img += ' width="' + width + '"'
        else:
            style += 'width:' + width + ';'
    if height:
        if height.isdigit():
            img += ' height="' + height + '"'
        else:
            style += 'height:' + height + ';'
    if style:
        img += ' style="' + escape_text(style) + '"'

    img += ' />'

    _cls = 'block-image'
    align = attrs.get('align')
    if align:
        _cls += ' align-' + align

    target = attrs.get('target')
    if target:
        href = escape_text(self.safe_url(target))
        outer = '<a class="' + _cls + '" href="' + href + '">'
        return outer + img + '</a>\n'
    else:
        return '<div class="' + _cls + '">' + img + '</div>\n'


class Figure(DirectivePlugin):
    NAME = 'figure'

    def parse_directive_content(self, block, m, state):
        content = self.parse_content(m)
        if not content:
            return

        tokens = self.parse_tokens(block, content, state)
        caption = tokens[0]
        if caption['type'] == 'paragraph':
            caption['type'] = 'figcaption'
            children = [caption]
            if len(tokens) > 1:
                children.append({
                    'type': 'legend',
                    'children': tokens[1:]
                })
            return children

    def parse(self, block, m, state):
        options = dict(self.parse_options(m))
        image_attrs = _parse_attrs(options)
        image_attrs['src'] = self.parse_title(m)

        align = image_attrs.pop('align', None)
        fig_attrs = {}
        if align:
            fig_attrs['align'] = align
        for k in ['figwidth', 'figclass']:
            if k in options:
                fig_attrs[k] = options[k]

        children = [{'type': 'block_image', 'attrs': image_attrs}]
        content = self.parse_directive_content(block, m, state)
        if content:
            children.extend(content)
        return {
            'type': 'figure',
            'attrs': fig_attrs,
            'children': children,
        }

    def __call__(self, directive, md):
        directive.register(self.NAME, self.parse)

        if md.renderer.NAME == 'html':
            md.renderer.register('figure', render_figure)
            md.renderer.register('block_image', render_block_image)
            md.renderer.register('figcaption', render_figcaption)
            md.renderer.register('legend', render_legend)


def render_figure(self, text, align=None, figwidth=None, figclass=None):
    _cls = 'figure'
    if align:
        _cls += ' align-' + align
    if figclass:
        _cls += ' ' + figclass

    html = '<figure class="' + _cls + '"'
    if figwidth:
        html += ' style="width:' + figwidth + '"'
    return html + '>\n' + text + '</figure>\n'


def render_figcaption(self, text):
    return '<figcaption>' + text + '</figcaption>\n'


def render_legend(self, text):
    return '<div class="legend">\n' + text + '</div>\n'