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
|
local digest = require("openssl.digest")
local avatar_size = 13
local default_image = "retro"
local base_url = "https://www.gravatar.com/avatar/"
local alt_text = "Gravatar"
local buffer = ""
local avatar_hash = nil
local function hash_hex(input)
local raw = digest.new("md5"):final(input)
local hex = ""
for i = 1, #raw do
hex = hex .. string.format("%.2x", string.byte(raw, i))
end
return hex
end
local function normalize_email(email)
if email == nil then
return nil
end
local inner = email:match("<(.*)>")
if inner ~= nil then
email = inner
end
email = (email:gsub("^%s*(.-)%s*$", "%1")):lower()
if email == "" then
return nil
end
return email
end
function filter_open(email, page)
buffer = ""
local address = normalize_email(email)
if address == nil then
avatar_hash = nil
else
avatar_hash = hash_hex(address)
end
end
function filter_write(text)
buffer = buffer .. text
end
function filter_close()
if avatar_hash == nil then
html(buffer)
else
html("<img src='" .. base_url .. avatar_hash ..
"?s=" .. avatar_size ..
"&d=" .. default_image ..
"' width='" .. avatar_size ..
"' height='" .. avatar_size ..
"' alt='" .. alt_text .. "' /> " .. buffer)
end
return 0
end
|