forked from Etiene/lua.space
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_rss.lua
85 lines (73 loc) · 1.63 KB
/
generate_rss.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
local markdown = require "markdown"
local lfs = require "lfs"
local AMOUNT = 10
local date = require "date"
local function read_md(path)
local f = io.open (path..".md", "rb")
if f then
local src = f:read("*all")
f:close()
return markdown(src)
end
return false
end
local function get_posts()
local posts = require "posts.posts_meta"
if #posts < AMOUNT then AMOUNT = #posts end
local rss_posts = {}
for i=1,AMOUNT do
posts[i].date = date(posts[i].date):fmt("%a, %d %b %Y %T GMT")
local category = posts[i].category or 'general'
posts[i].body = read_md('posts/'..category..'/'..posts[i].short_url)
posts[i].url = "http://lua.space/"..category..'/'..posts[i].short_url
rss_posts[#rss_posts+1] = posts[i]
end
return rss_posts
end
local function build_rss()
local rss = {
[[<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Lua.Space</title>
<link>http://http://lua.space/</link>
<description>The unofficial Lua blog</description>]]
}
local item_template = {
[[
<item>
<title>]],
"title",
[[</title>
<link>]],
"url",
[[</link>
<guid>]],
"url again",
[[</guid>
<pubDate>]],
"date",
[[</pubDate>
<description><![CDATA[]],
"content",
[=[
]]></description>
</item>]=]
}
for _,p in ipairs(get_posts()) do
item_template[2] = p.page_title
item_template[4] = p.url
item_template[6] = p.url
item_template[8] = p.date
item_template[10] = p.body
rss[#rss+1] = table.concat(item_template)
end
rss[#rss+1] = [[
</channel>
</rss>]]
-- print(table.concat(rss))
local file = io.output ("rss.xml")
file:write(table.concat(rss))
file:close()
end
build_rss()