source: tracwysiwygplugin/0.12/tracwysiwyg/__init__.py

Last change on this file was 13454, checked in by Jun Omae, 10 years ago

TracWysiwygPlugin: removed wysiwyg-load.js

File size: 2.8 KB
Line 
1# -*- coding: utf-8 -*-
2
3from fnmatch import fnmatch
4import re
5
6from trac.core import Component, implements
7from trac.config import ListOption
8from trac.ticket.web_ui import TicketModule
9from trac.web.api import IRequestFilter
10from trac.web.chrome import ITemplateProvider, add_link, add_stylesheet, add_script, add_script_data
11from trac.web.href import Href
12
13
14__all__ = ['WysiwygModule']
15
16
17class WysiwygModule(Component):
18    implements(ITemplateProvider, IRequestFilter)
19
20    wysiwyg_stylesheets = ListOption('tracwysiwyg', 'wysiwyg_stylesheets',
21            doc="""Add stylesheets to the WYSIWYG editor""")
22
23    templates = ListOption('tracwysiwyg', 'templates', doc="""\
24            List of template names that the plugin will show a WYSIWYG editor
25            on each TracWiki textarea. The plugin shows on all pages by
26            default.""")
27
28    # ITemplateProvider#get_htdocs_dirs
29    def get_htdocs_dirs(self):
30        from pkg_resources import resource_filename
31        return [('tracwysiwyg', resource_filename(__name__, 'htdocs'))]
32
33    # ITemplateProvider#get_templates_dirs
34    def get_templates_dirs(self):
35        return []
36
37    # IRequestFilter#pre_process_request
38    def pre_process_request(self, req, handler):
39        return handler
40
41    # IRequestFilter#post_process_request
42    def post_process_request(self, req, template, data, content_type):
43        if not _is_wysiwyg_enabled(template, self.templates):
44            return template, data, content_type
45
46        options = {}
47        if template == 'ticket.html':
48            options['escapeNewlines'] = TicketModule(self.env).must_preserve_newlines
49        add_script_data(req, {'_tracwysiwyg': options})
50        add_link(req, 'tracwysiwyg.base', req.href() or '/')
51        stylesheets = ['chrome/common/css/trac.css', 'chrome/tracwysiwyg/editor.css']
52        stylesheets += self.wysiwyg_stylesheets
53        for stylesheet in stylesheets:
54            add_link(req, 'tracwysiwyg.stylesheet', _expand_filename(req, stylesheet))
55        add_stylesheet(req, 'tracwysiwyg/wysiwyg.css')
56        add_script(req, 'tracwysiwyg/wysiwyg.js')
57
58        return template, data, content_type
59
60
61def _expand_filename(req, filename):
62    if filename.startswith('chrome/common/') and 'htdocs_location' in req.chrome:
63        href = Href(req.chrome['htdocs_location'])
64        return href(filename[14:])
65    if filename.startswith('/') or re.match(r'https?://', filename):
66        href = Href(filename)
67        return href()
68    return req.href(filename)
69
70
71def _is_wysiwyg_enabled(template, patterns):
72    if not patterns:
73        return True
74    for pattern in patterns:
75        positive = not pattern.startswith('!')
76        if not positive:
77            pattern = pattern[1:]
78        if fnmatch(template, pattern):
79            return positive
80    return False
Note: See TracBrowser for help on using the repository browser.