]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/templatetags/syntax.py
Inital commit
[patchwork] / apps / patchwork / templatetags / syntax.py
1 # Patchwork - automated patch tracking system
2 # Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
3 #
4 # This file is part of the Patchwork package.
5 #
6 # Patchwork is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # Patchwork is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Patchwork; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20 from django import template
21 from django.utils.html import escape
22 from django.utils.safestring import mark_safe
23 import re
24
25 register = template.Library()
26
27 def _compile(t):
28     (r, str) = t
29     return (re.compile(r, re.M | re.I), str)
30
31 _patch_span_res = map(_compile, [
32         ('^(Index:?|diff|\-\-\-|\+\+\+|\*\*\*) .*$', 'p_header'),
33         ('^\+.*$', 'p_add'),
34         ('^-.*$', 'p_del'),
35         ('^!.*$', 'p_mod'),
36         ])
37
38 _patch_chunk_re = \
39         re.compile('^(@@ \-\d+(?:,\d+)? \+\d+(?:,\d+)? @@)(.*)$', re.M | re.I)
40
41 _comment_span_res = map(_compile, [
42         ('^\s*Signed-off-by: .*$', 'signed-off-by'),
43         ('^\s*Acked-by: .*$', 'acked-by'),
44         ('^\s*From: .*$', 'from'),
45         ('^\s*&gt;.*$', 'quote'),
46         ])
47
48 _span = '<span class="%s">%s</span>'
49
50 @register.filter
51 def patchsyntax(patch):
52     content = escape(patch.content)
53
54     for (r,cls) in _patch_span_res:
55         content = r.sub(lambda x: _span % (cls, x.group(0)), content)
56
57     content = _patch_chunk_re.sub( \
58             lambda x: \
59                 _span % ('p_chunk', x.group(1)) + ' ' + \
60                 _span % ('p_context', x.group(2)), \
61             content)
62
63     return mark_safe(content)
64
65 @register.filter
66 def commentsyntax(comment):
67     content = escape(comment.content)
68
69     for (r,cls) in _comment_span_res:
70         content = r.sub(lambda x: _span % (cls, x.group(0)), content)
71
72     return mark_safe(content)