function mtime(file) return os.date("%Y-%m-%dT%H:%M:%S+08:00", os.mtime(file)) end function header(url) return format([[ xmake

This is a mirror page, please see the original page:

%s

]], url, url) end function tailer() return [[
]] end function ads() return [[ ]] end -- fix links function _fixlinks(htmldata) -- os.mv -- => os.mv 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) --

os.rm

-- =>

os.rm

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([[ ]]) sitemap:print([[ %s %s ]], 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([[ %s %s ]], url, mtime(htmlfile)) end end sitemap:print("") sitemap:close() end