build.lua 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. function mtime(file)
  2. return os.date("%Y-%m-%dT%H:%M:%S+08:00", os.mtime(file))
  3. end
  4. function header(url)
  5. return format([[
  6. <!DOCTYPE html>
  7. <html lang="en">
  8. <head>
  9. <meta charset="UTF-8">
  10. <title>xmake</title>
  11. <link rel="icon" href="/assets/img/favicon.ico">
  12. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  13. <meta name="description" content="Description">
  14. <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  15. <link href="/assets/npm/github-markdown/github-markdown.min.css" rel="stylesheet">
  16. <style>
  17. .markdown-body {
  18. box-sizing: border-box;
  19. min-width: 200px;
  20. max-width: 980px;
  21. margin: 0 auto;
  22. padding: 45px;
  23. }
  24. @media (max-width: 767px) {
  25. .markdown-body {
  26. padding: 15px;
  27. }
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <article class="markdown-body">
  33. <h4>This is a mirror page, please see the original page: </h4><a href="%s">%s</a>
  34. <div id="wwads-panel" class="wwads-cn wwads-vertical wwads-sticky" data-id="239" style="max-width:180px;bottom:20px;right:20px;width:200px;height:260px;background:#fff;position:fixed"></div>
  35. </br>
  36. ]], url, url)
  37. end
  38. function tailer()
  39. return [[
  40. </article>
  41. </body>
  42. </html>]]
  43. end
  44. function ads()
  45. return [[
  46. <script type="text/javascript" charset="UTF-8" src="https://cdn.wwads.cn/js/makemoney.js" async></script>
  47. <script async type="text/javascript" src="//cdn.carbonads.com/carbon.js?serve=CE7I52QU&placement=xmakeio" id="_carbonads_js"></script>
  48. <style>
  49. #carbonads {
  50. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu,
  51. Cantarell, "Helvetica Neue", Helvetica, Arial, sans-serif;
  52. }
  53. #carbonads {
  54. display: flex;
  55. max-width: 330px;
  56. background-color: hsl(0, 0%, 98%);
  57. box-shadow: 0 1px 4px 1px hsla(0, 0%, 0%, .1);
  58. }
  59. #carbonads a {
  60. color: inherit;
  61. text-decoration: none;
  62. }
  63. #carbonads a:hover {
  64. color: inherit;
  65. }
  66. #carbonads span {
  67. position: relative;
  68. display: block;
  69. overflow: hidden;
  70. }
  71. #carbonads .carbon-wrap {
  72. display: flex;
  73. }
  74. .carbon-img {
  75. display: block;
  76. margin: 0;
  77. line-height: 1;
  78. }
  79. .carbon-img img {
  80. display: block;
  81. }
  82. .carbon-text {
  83. font-size: 13px;
  84. padding: 10px;
  85. line-height: 1.5;
  86. text-align: left;
  87. }
  88. .carbon-poweredby {
  89. display: block;
  90. padding: 8px 10px;
  91. 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);
  92. text-align: center;
  93. text-transform: uppercase;
  94. letter-spacing: .5px;
  95. font-weight: 600;
  96. font-size: 9px;
  97. line-height: 1;
  98. }
  99. </style>
  100. ]]
  101. end
  102. -- fix links
  103. function _fixlinks(htmldata)
  104. -- <a href="/manual/builtin_modules?id=osmv">os.mv</a>
  105. -- => <a href="/mirror/manual/builtin_modules.html#osmv">os.mv</a>
  106. htmldata = htmldata:gsub("(href=\"(.-)\")", function(_, href)
  107. if href:startswith("/") and not href:startswith("/#/") then
  108. local splitinfo = href:split('?', {plain = true})
  109. local url = splitinfo[1]
  110. href = "/mirror" .. url .. ".html"
  111. if splitinfo[2] then
  112. local anchor = splitinfo[2]:gsub("id=", "")
  113. href = href .. "#" .. anchor
  114. end
  115. print(" -> fix %s", href)
  116. end
  117. return "href=\"" .. href .. "\""
  118. end)
  119. -- <h4 id="os-rm">os.rm</h4>
  120. -- => <h4 id="osrm">os.rm</h4>
  121. htmldata = htmldata:gsub("(id=\"(.-)\")", function(_, id)
  122. id = id:gsub("%-", "")
  123. return "id=\"" .. id .. "\""
  124. end)
  125. return htmldata
  126. end
  127. -- generate mirror files and sitemap.xml
  128. -- we need install https://github.com/cwjohan/markdown-to-html first
  129. -- npm install markdown-to-html -g
  130. --
  131. -- Or use showdown-cli https://github.com/showdownjs/showdown
  132. --
  133. function main()
  134. local siteroot = "https://xmake.io"
  135. local mirrordir = "mirror"
  136. local sitemap = io.open("sitemap.xml", 'w')
  137. sitemap:print([[
  138. <?xml version="1.0" encoding="UTF-8"?>
  139. <urlset
  140. xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
  141. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  142. xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
  143. http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
  144. ]])
  145. sitemap:print([[
  146. <url>
  147. <loc>%s</loc>
  148. <lastmod>%s</lastmod>
  149. </url>
  150. ]], siteroot, mtime("index.html"))
  151. os.rm(mirrordir)
  152. for _, markdown in ipairs(os.files("**.md")) do
  153. local basename = path.basename(markdown)
  154. if not basename:startswith("_") then
  155. -- get the raw url
  156. if basename == "README" then
  157. basename = ""
  158. end
  159. local url = siteroot .. '/mirror'
  160. local rawurl = siteroot .. '/#'
  161. local dir = path.directory(markdown)
  162. if dir ~= '.' then
  163. rawurl = rawurl .. '/' .. dir
  164. url = url .. '/' .. dir
  165. end
  166. rawurl = rawurl .. '/' .. basename
  167. url = url .. '/' .. (basename == "" and "index.html" or (basename .. ".html"))
  168. -- generate html file
  169. local htmlfile = path.join(mirrordir, dir, basename == "" and "index.html" or (basename .. ".html"))
  170. local htmldata = os.iorunv("markdown", {markdown})
  171. local f = io.open(htmlfile, 'w')
  172. if f then
  173. f:write(header(rawurl))
  174. f:write(ads())
  175. htmldata = htmldata:gsub("&%a-;", function (w)
  176. local maps = {["&lt;"] = "<", ["&gt;"] = ">", ["&quot;"] = "\""}
  177. return maps[w]
  178. end)
  179. f:write(_fixlinks(htmldata))
  180. f:write(tailer())
  181. f:close()
  182. end
  183. --[[
  184. local tmpfile = os.tmpfile()
  185. os.mkdir(path.directory(tmpfile))
  186. os.execv("showdown", {"makehtml", "-i", markdown, "-o", tmpfile})
  187. local f = io.open(htmlfile, 'w')
  188. if f then
  189. f:write(header(rawurl))
  190. f:write(ads())
  191. f:write(_fixlinks(io.readfile(tmpfile)))
  192. f:write(tailer())
  193. f:close()
  194. end
  195. os.rm(tmpfile)]]
  196. print("build %s => %s, %s", markdown, htmlfile, mtime(htmlfile))
  197. print("url %s -> %s", url, rawurl)
  198. -- generate sitemap
  199. sitemap:print([[
  200. <url>
  201. <loc>%s</loc>
  202. <lastmod>%s</lastmod>
  203. </url>
  204. ]], url, mtime(htmlfile))
  205. end
  206. end
  207. sitemap:print("</urlset>")
  208. sitemap:close()
  209. end