forked from lvzixun/rainbowCoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrss.lua
More file actions
124 lines (94 loc) · 2.68 KB
/
rss.lua
File metadata and controls
124 lines (94 loc) · 2.68 KB
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
local lfs = require "lfs"
-- local print_r = require "print_r"
local mt = {}
mt.__index = mt
local function create_stream()
return setmetatable({}, mt)
end
function mt:write(s, deep)
deep = deep or 0
local prefix = ""
for i=1,deep do
prefix = prefix.."\t"
end
self[#self+1] = prefix..s
end
function mt:dump()
return table.concat( self, "\n")
end
local function read_file(path)
local handle = io.open(path, "r")
local ret = handle:read("*a")
handle:close()
return ret
end
local function write_file(path, s)
local handle = io.open(path, "w")
handle:write(s)
handle:close()
end
local function base_name(string_, suffix)
local LUA_DIRSEP = string.sub(package.config,1,1)
string_ = string_ or ''
local basename = string.gsub (string_, '[^'.. LUA_DIRSEP ..']*'.. LUA_DIRSEP ..'', '')
if suffix then
basename = string.gsub (basename, suffix, '')
end
return basename
end
local function _gen_des(html_file)
local s = read_file(html_file)
return string.match(s, ".+<body>(.*)</body>.+")
end
local function _gen_post(path)
local ret = {}
for file in lfs.dir(path) do
if string.match(file, ".*%.md") and file ~= "index.md" then
local name = base_name(file, ".md")
local html_file = path.."/../html/"..name..".html"
ret[#ret+1] = {
link = name..".html",
title = name ,
des = _gen_des(html_file)
}
end
end
return ret
end
local function _gen_item(stream, deep, item)
local title = item.title
local link = item.link
local des = item.des
stream:write("<item>", deep)
stream:write("<title>"..title.."</title>", deep+1)
stream:write("<link>"..link.."</link>", deep+1)
stream:write("<description> <![CDATA["..des.."]]></description>", deep+1)
stream:write("</item>", deep)
end
local function _gen_rss(items)
local stream = create_stream()
-- write header
stream:write('<?xml version="1.0"?>')
stream:write('<rss version="2.0">')
-- write channel
stream:write("<channel>", 1)
stream:write("<language>zh-cn</language>", 2)
stream:write("<copyright>zixun</copyright>", 2)
stream:write("<generator>www.rainbowcoder.com</generator>", 2)
stream:write("<title> rainbowcoder </title>", 2)
stream:write("<link>http://rainbowcoder.com/</link>", 2)
stream:write("<description> zixun's blog </description>", 2)
-- write items
for i=1,#items do
_gen_item(stream, 2, items[i])
end
stream:write("</channel>", 1)
stream:write("</rss>")
return stream:dump()
end
local function main(path)
local items = _gen_post(path) -- 获取item
local s = _gen_rss(items)
write_file("html/rainbowcoder_rss.xml", s)
end
main("post")