-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
build.lua
163 lines (149 loc) · 4.84 KB
/
build.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
function mtime(file)
return os.date("%Y-%m-%dT%H:%M:%S+08:00", os.mtime(file))
end
function header(url)
return format([[
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>tbox</title>
<link rel="icon" href="/assets/img/favicon.ico">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="description" content="Description">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<link href="//cdn.jsdelivr.net/npm/github-markdown-css@4.0.0/github-markdown.min.css" rel="stylesheet">
<style>
.markdown-body {
box-sizing: border-box;
min-width: 200px;
max-width: 980px;
margin: 0 auto;
padding: 45px;
}
@media (max-width: 767px) {
.markdown-body {
padding: 15px;
}
}
</style>
</head>
<body>
<article class="markdown-body">
<h4>This is a mirror page, please see the original page: </h4><a href="%s">%s</a>
</br>
]], url, url)
end
function tailer()
return [[
</article>
</body>
</html>]]
end
-- fix links
function _fixlinks(htmldata)
-- <a href="/manual/builtin_modules?id=osmv">os.mv</a>
-- => <a href="/mirror/manual/builtin_modules.html#osmv">os.mv</a>
htmldata = htmldata:gsub("(href=\"(.-)\")", function(_, href)
if href:startswith("/") and not href:startswith("/#/") then
local splitinfo = href:split('?', {plain = true})
local url = splitinfo[1]
href = "/mirror" .. url .. ".html"
if splitinfo[2] then
local anchor = splitinfo[2]:gsub("id=", "")
href = href .. "#" .. anchor
end
print(" -> fix %s", href)
end
return "href=\"" .. href .. "\""
end)
-- <h4 id="os-rm">os.rm</h4>
-- => <h4 id="osrm">os.rm</h4>
htmldata = htmldata:gsub("(id=\"(.-)\")", function(_, id)
id = id:gsub("%-", "")
return "id=\"" .. id .. "\""
end)
return htmldata
end
-- generate mirror files and sitemap.xml
-- we need install https://github.com/cwjohan/markdown-to-html first
-- npm install markdown-to-html -g
--
-- Or use showdown-cli https://github.com/showdownjs/showdown
--
function main()
local siteroot = "https://tboox.io"
local mirrordir = "mirror"
local sitemap = io.open("sitemap.xml", 'w')
sitemap:print([[
<?xml version="1.0" encoding="UTF-8"?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
]])
sitemap:print([[
<url>
<loc>%s</loc>
<lastmod>%s</lastmod>
</url>
]], siteroot, mtime("index.html"))
os.rm(mirrordir)
for _, markdown in ipairs(os.files("**.md")) do
local basename = path.basename(markdown)
if not basename:startswith("_") then
-- get the raw url
if basename == "README" then
basename = ""
end
local url = siteroot .. '/mirror'
local rawurl = siteroot .. '/#'
local dir = path.directory(markdown)
if dir ~= '.' then
rawurl = rawurl .. '/' .. dir
url = url .. '/' .. dir
end
rawurl = rawurl .. '/' .. basename
url = url .. '/' .. (basename == "" and "index.html" or (basename .. ".html"))
-- generate html file
local htmlfile = path.join(mirrordir, dir, basename == "" and "index.html" or (basename .. ".html"))
local htmldata = os.iorunv("markdown", {markdown})
local f = io.open(htmlfile, 'w')
if f then
f:write(header(rawurl))
htmldata = htmldata:gsub("&%a-;", function (w)
local maps = {["<"] = "<", [">"] = ">", ["""] = "\""}
return maps[w]
end)
f:write(_fixlinks(htmldata))
f:write(tailer())
f:close()
end
--[[
local tmpfile = os.tmpfile()
os.mkdir(path.directory(tmpfile))
os.execv("showdown", {"makehtml", "-i", markdown, "-o", tmpfile})
local f = io.open(htmlfile, 'w')
if f then
f:write(header(rawurl))
f:write(ads())
f:write(_fixlinks(io.readfile(tmpfile)))
f:write(tailer())
f:close()
end
os.rm(tmpfile)]]
print("build %s => %s, %s", markdown, htmlfile, mtime(htmlfile))
print("url %s -> %s", url, rawurl)
-- generate sitemap
sitemap:print([[
<url>
<loc>%s</loc>
<lastmod>%s</lastmod>
</url>
]], url, mtime(htmlfile))
end
end
sitemap:print("</urlset>")
sitemap:close()
end