-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathsyntax.py
More file actions
78 lines (58 loc) · 1.75 KB
/
syntax.py
File metadata and controls
78 lines (58 loc) · 1.75 KB
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
# Patchwork - automated patch tracking system
# Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
#
# SPDX-License-Identifier: GPL-2.0-or-later
import re
from django import template
from django.utils.html import escape
from django.utils.safestring import mark_safe
register = template.Library()
def _compile(value):
regex, cls = value
return re.compile(regex, re.M | re.I), cls
_patch_span_res = [
_compile(x)
for x in [
(r'^(Index:?|diff|\-\-\-|\+\+\+|\*\*\*) .*$', 'p_header'),
(r'^\+.*$', 'p_add'),
(r'^-.*$', 'p_del'),
(r'^!.*$', 'p_mod'),
]
]
_patch_chunk_re = re.compile(
r'^(@@ \-\d+(?:,\d+)? \+\d+(?:,\d+)? @@)(.*)$', re.M | re.I
)
_comment_span_res = [
_compile(x)
for x in [
(r'^\s*Signed-off-by: .*$', 'signed-off-by'),
(r'^\s*Acked-by: .*$', 'acked-by'),
(r'^\s*Nacked-by: .*$', 'nacked-by'),
(r'^\s*Tested-by: .*$', 'tested-by'),
(r'^\s*Reviewed-by: .*$', 'reviewed-by'),
(r'^\s*From: .*$', 'from'),
(r'^\s*>.*$', 'quote'),
]
]
_span = '<span class="%s">%s</span>'
@register.filter
def patchsyntax(patch):
diff = escape(patch.diff).replace('\r\n', '\n')
for regex, cls in _patch_span_res:
diff = regex.sub(lambda x: _span % (cls, x.group(0)), diff)
diff = _patch_chunk_re.sub(
lambda x: ' '.join(
[
_span % ('p_chunk', x.group(1)),
_span % ('p_context', x.group(2)),
],
),
diff,
)
return mark_safe(diff)
@register.filter
def commentsyntax(submission):
content = escape(submission.content)
for r, cls in _comment_span_res:
content = r.sub(lambda x: _span % (cls, x.group(0)), content)
return mark_safe(content)