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
|
local rules = {
{ key = "Fixes", pattern = "^(%x%x%x%x%x%x%x+)", url = "./?id=%s" },
{ key = "Reverts", pattern = "^(%x%x%x%x%x%x%x+)", url = "./?id=%s" },
{ key = "Closes", pattern = "^#?(%d+)$", url = "https://bugs.example.com/?bug=%s" },
{ key = "Bug", pattern = "^#?(%d+)$", url = "https://bugs.example.com/?bug=%s" },
}
local link_urls = true
local key = ""
local chunks = {}
local function url_encode(s)
return (string.gsub(s, "[^%w._~-]", function(c)
return string.format("%%%02X", string.byte(c))
end))
end
local function make_link(url_template, capture, display)
local encoded = url_encode(capture)
local href = string.gsub(url_template, "%%s", function()
return encoded
end)
return "<a href='" .. href .. "'>" .. display .. "</a>"
end
local function link_url(text)
if not link_urls or not string.find(text, "^https?://[^%s'\"]+$") then
return nil
end
return "<a href='" .. text .. "'>" .. text .. "</a>"
end
local function link_rule(text)
local wanted = string.lower(key)
for _, rule in ipairs(rules) do
if rule.key == nil or string.lower(rule.key) == wanted then
local ok, start, stop, capture = pcall(string.find, text, rule.pattern)
if ok and start then
if capture == nil then
capture = string.sub(text, start, stop)
end
return string.sub(text, 1, start - 1) ..
make_link(rule.url, capture, string.sub(text, start, stop)) ..
string.sub(text, stop + 1)
end
end
end
return nil
end
function filter_open(trailer_key, page)
key = trailer_key or ""
chunks = {}
end
function filter_write(str)
chunks[#chunks + 1] = str
end
function filter_close()
local text = table.concat(chunks)
html(link_url(text) or link_rule(text) or text)
return 0
end
|