-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.lua
More file actions
52 lines (46 loc) · 963 Bytes
/
common.lua
File metadata and controls
52 lines (46 loc) · 963 Bytes
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
local C = {}
function C.normalize(str)
return str:gsub("^md%-src/", "")
:gsub("%s+", "-")
:gsub("%.md$", "")
:gsub("_", "-")
:gsub("[^%w%-]", "")
:lower()
end
function C.exists(name)
local f = io.open(name, "r")
return f ~= nil and io.close(f)
end
function C.readBacklinks()
local backlinks = {}
local f = io.open("backlinks.csv", "r")
if not f then
return backlinks
end
for line in f:lines() do
local dst, count = line:match("^(%S+), (%d+)$")
if dst and count then
backlinks[dst] = tonumber(count)
end
end
f:close()
return backlinks
end
function C.writeBacklinks(backlinks)
local f = io.open("backlinks.csv", "w")
for dst, count in pairs(backlinks) do
f:write(string.format("%s, %d\n", dst, count))
end
f:close()
end
function C.getAllPages()
local pages = {}
local ls = io.popen("ls -1 md-src/")
if ls then
for name in ls:lines() do
table.insert(pages, C.normalize(name))
end
end
return pages
end
return C