PageRenderTime 20ms CodeModel.GetById 11ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/hyde/ext/plugins/textlinks.py

http://github.com/hyde/hyde
Python | 35 lines | 22 code | 3 blank | 10 comment | 0 complexity | 3049bcd3807dc7130b07ccc13cf5b84c MD5 | raw file
 1# -*- coding: utf-8 -*-
 2"""
 3Textlinks plugin
 4"""
 5import re
 6
 7from hyde.plugin import Plugin
 8
 9class TextlinksPlugin(Plugin):
10    """
11    The plugin class for syntax text replacement.
12    """
13    def __init__(self, site):
14        super(TextlinksPlugin, self).__init__(site)
15
16    def begin_text_resource(self, resource, text):
17        """
18        Replace content url pattern [[/abc/def]])
19        with
20        {{ content_url('/abc/def') }} or equivalent and
21        Replace media url pattern [[!!/abc/def]]
22        with
23        {{ media_url('/abc/def') }} or equivalent.
24        """
25        if not resource.uses_template:
26            return text
27        content_link = re.compile('\[\[([^\]^!][^\]]*)\]\]', re.UNICODE|re.MULTILINE)
28        media_link = re.compile('\[\[\!\!([^\]]*)\]\]', re.UNICODE|re.MULTILINE)
29        def replace_content(match):
30            return self.template.get_content_url_statement(match.groups(1)[0])
31        def replace_media(match):
32            return self.template.get_media_url_statement(match.groups(1)[0])
33        text = content_link.sub(replace_content, text)
34        text = media_link.sub(replace_media, text)
35        return text