forked from jensl/critic
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinkify.py
182 lines (148 loc) · 5.9 KB
/
linkify.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# -*- mode: python; encoding: utf-8 -*-
#
# Copyright 2012 Jens Lindström, Opera Software ASA
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
import re
ALL_LINKTYPES = []
class Context(object):
def __init__(self, db=None, request=None, repository=None, review=None, **kwargs):
self.db = db
self.request = request
self.repository = repository or (review.repository if review else None)
self.review = review
self.extra = kwargs
class LinkType(object):
"""
A link type object is responsible for providing a regexp fragment
that matches the words (or substrings) that the link type produces
hyper-links from, and for constructing actual URLs from such
words.
"""
def __init__(self, fragment):
"""
LinkType(regexp) -> link type object
Create a link type object and add it to the global list of
link type objects. The 'fragment' argument should be a string
containing a regexp fragment without captures suitable to
insert into the complete regexp
(?:^|\b)(wordA|wordB|...)(?:\b|$)
which is then used to split text into "words" which are
individually turned into links or left as-is.
"""
self.fragment = fragment
ALL_LINKTYPES.append(self)
def linkify(self, word):
"""
linkify(word) -> None or a string.
If the whole word matches what this link type handles,
constructs a URL to which this word should be made a link,
otherwise returns None. Implementations should expect to be
called with words that don't match what they handle.
Sub-classes must override this method.
"""
pass
class SimpleLinkType(LinkType):
"""
Base class for link type when the word contains the URL.
"""
def __init__(self, fragment, regexp=None):
super(SimpleLinkType, self).__init__(fragment)
self.regexp = re.compile(regexp or "(%s)$" % fragment)
def linkify(self, word, context):
match = self.regexp.match(word)
if match: return match.group(1)
class HTTP(SimpleLinkType):
"""
Link type "plain URL string".
"""
def __init__(self):
super(HTTP, self).__init__("https?://\\S+[^\\s.,:;!?)]")
class URL(SimpleLinkType):
"""
Link type <URL:...>.
"""
def __init__(self):
super(URL, self).__init__("<URL:[^>]+>", "<URL:([^>]+)>$")
class SHA1(LinkType):
"""
SHA-1 link type.
Converts SHA-1 sums in text (either full or abbreviated) into
links to the diff of the referenced commit. When processed in the
context of a repository, a matching commit in that repository is
preferred (assuming it exists.) When processed in the context of
a review, a 'review=<id>' parameter is appended to the URL, which
links to the diff of the referenced commit in the context of the
review (which includes comments and allows reviewing.)
"""
def __init__(self):
super(SHA1, self).__init__("[0-9A-Fa-f]{8,40}")
self.regexp = re.compile("[0-9A-Fa-f]{8,40}$")
def linkify(self, word, context):
if self.regexp.match(word):
sha1 = word
if context.repository \
and context.repository.iscommit(word):
sha1 = context.repository.revparse(sha1)
if context.review \
and context.review.containsCommit(context.db, sha1):
return "/%s/%s?review=%d" % (context.repository.name, sha1, context.review.id)
else:
return "/%s/%s" % (context.repository.name, sha1)
else:
return "/%s" % sha1
class Diff(LinkType):
"""
Diff link type.
Like the SHA-1 link type, but with two sums separated by '..', and
links to the diff between the two referenced commits.
"""
def __init__(self):
super(Diff, self).__init__("[0-9A-Fa-f]{8,40}\\.\\.[0-9A-Fa-f]{8,40}")
self.regexp = re.compile("([0-9A-Fa-f]{8,40})\\.\\.([0-9A-Fa-f]{8,40})$")
def linkify(self, word, context):
match = self.regexp.match(word)
if match:
from_sha1 = match.group(1)
to_sha1 = match.group(2)
if context.repository \
and context.repository.iscommit(from_sha1) \
and context.repository.iscommit(to_sha1):
from_sha1 = context.repository.revparse(from_sha1)
to_sha1 = context.repository.revparse(to_sha1)
if context.review \
and context.review.containsCommit(context.db, from_sha1) \
and context.review.containsCommit(context.db, to_sha1):
return "/%s/%s..%s?review=%d" % (context.repository.name, from_sha1, to_sha1, context.review.id)
else:
return "/%s/%s..%s" % (context.repository.name, from_sha1, to_sha1)
else:
return "/%s..%s" % (from_sha1, to_sha1)
class Review(LinkType):
"""
Review link type.
Converts 'r/<id>' in text into a link to the front-page of the
corresponding review.
"""
def __init__(self):
super(Review, self).__init__("r/\\d+")
self.regexp = re.compile("r/\\d+$")
def linkify(self, word, context):
if self.regexp.match(word): return "/" + word
HTTP()
URL()
SHA1()
Diff()
Review()
try: import customization.linktypes
except ImportError: pass