]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/templatetags/syntax.py
tox: Add tox.ini file
[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*Nacked-by: .*$', 'nacked-by'),
45         ('^\s*Tested-by: .*$', 'tested-by'),
46         ('^\s*Reviewed-by: .*$', 'reviewed-by'),
47         ('^\s*From: .*$', 'from'),
48         ('^\s*&gt;.*$', 'quote'),
49         ])
50
51 _span = '<span class="%s">%s</span>'
52
53 @register.filter
54 def patchsyntax(patch):
55     content = escape(patch.content)
56
57     for (r,cls) in _patch_span_res:
58         content = r.sub(lambda x: _span % (cls, x.group(0)), content)
59
60     content = _patch_chunk_re.sub( \
61             lambda x: \
62                 _span % ('p_chunk', x.group(1)) + ' ' + \
63                 _span % ('p_context', x.group(2)), \
64             content)
65
66     return mark_safe(content)
67
68 @register.filter
69 def commentsyntax(comment):
70     content = escape(comment.content)
71
72     for (r,cls) in _comment_span_res:
73         content = r.sub(lambda x: _span % (cls, x.group(0)), content)
74
75     return mark_safe(content)