forked from xmake-io/xmake-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.lua
231 lines (207 loc) · 6.12 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
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>xmake</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/[email protected]/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
function ads()
return [[
<script async type="text/javascript" src="//cdn.carbonads.com/carbon.js?serve=CE7I52QU&placement=xmakeio" id="_carbonads_js"></script>
<style>
#carbonads {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu,
Cantarell, "Helvetica Neue", Helvetica, Arial, sans-serif;
}
#carbonads {
display: flex;
max-width: 330px;
background-color: hsl(0, 0%, 98%);
box-shadow: 0 1px 4px 1px hsla(0, 0%, 0%, .1);
}
#carbonads a {
color: inherit;
text-decoration: none;
}
#carbonads a:hover {
color: inherit;
}
#carbonads span {
position: relative;
display: block;
overflow: hidden;
}
#carbonads .carbon-wrap {
display: flex;
}
.carbon-img {
display: block;
margin: 0;
line-height: 1;
}
.carbon-img img {
display: block;
}
.carbon-text {
font-size: 13px;
padding: 10px;
line-height: 1.5;
text-align: left;
}
.carbon-poweredby {
display: block;
padding: 8px 10px;
background: repeating-linear-gradient(-45deg, transparent, transparent 5px, hsla(0, 0%, 0%, .025) 5px, hsla(0, 0%, 0%, .025) 10px) hsla(203, 11%, 95%, .4);
text-align: center;
text-transform: uppercase;
letter-spacing: .5px;
font-weight: 600;
font-size: 9px;
line-height: 1;
}
</style>
]]
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://xmake.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))
f:write(ads())
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