-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathatom.lua
106 lines (91 loc) · 2.42 KB
/
atom.lua
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
-- Copyright (C) 2013 Tor Hveem (thveem)
-- Simple atom generator in lua
local setmetatable = setmetatable
local ipairs = ipairs
local pairs = pairs
local table = table
local tonumber = tonumber
local ngx = ngx
local os = os
local type = type
local tostring = tostring
module(...)
_VERSION = '0.01'
local mt = { __index = _M }
-- Helper to iterate a table by sorted keys
local function itersort (t, f)
local a = {}
-- Sort on timestamp key reverse
f = function(a,b) return tonumber(a)>tonumber(b) end
for n in pairs(t) do table.insert(a, n) end
table.sort(a, f)
local i = 0 -- iterator variable
local iter = function () -- iterator function
i = i + 1
if a[i] == nil then return nil
else return a[i], t[a[i]]
end
end
return iter
end
local function rfc3339(date)
return os.date('!%Y-%m-%dT%H:%M:%SZ', date)
end
local function htmlspecialchars(value)
local subst =
{
["&"] = "&";
['"'] = """;
["'"] = "'";
["<"] = "<";
[">"] = ">";
}
if type(value) == "number" then
return value
end
value = tostring(value)
return (value:gsub("[&\"'<>]", subst))
end
function generate_xml(title, link, description, author, feedurl, entries, entriescontent)
local entriesxml = ''
local updated
for date, ptitle in itersort(entries) do
if updated == nil then
updated = rfc3339(date)
end
local etitle = ptitle:gsub('-', ' ')
local htmlcontent = htmlspecialchars(entriescontent[ptitle])
local entryxml = [[
<entry>
<title>]]..etitle..[[</title>
<id>]]..link..ptitle..[[</id>
<content type="html">]]..htmlcontent..[[</content>
<link rel="alternate" href="]]..link..ptitle..[[" />
<updated>]]..rfc3339(date)..[[</updated>
</entry>
]]
entriesxml = entriesxml .. entryxml
end
local xml = [[
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>]] .. title .. [[</title>
<link href="]]..link..[["/>
<updated>]]..updated..[[</updated>
<author>
<name>]]..author..[[</name>
</author>
<link rel="self" href="]]..link..feedurl..[[" />
<id>]]..link..[[</id>
]]..entriesxml..[[
</feed>
]]
return xml
end
local class_mt = {
-- to prevent use of casual module global variables
__newindex = function (table, key, val)
error('attempt to write to undeclared variable "' .. key .. '"')
end
}
setmetatable(_M, class_mt)