build.lua 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. </br>
  35. ]], url, url)
  36. end
  37. function tailer()
  38. return [[
  39. </article>
  40. </body>
  41. </html>]]
  42. end
  43. function ads()
  44. return [[
  45. <script async type="text/javascript" src="//cdn.carbonads.com/carbon.js?serve=CE7I52QU&placement=xmakeio" id="_carbonads_js"></script>
  46. <style>
  47. #carbonads {
  48. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu,
  49. Cantarell, "Helvetica Neue", Helvetica, Arial, sans-serif;
  50. }
  51. #carbonads {
  52. display: flex;
  53. max-width: 330px;
  54. background-color: hsl(0, 0%, 98%);
  55. box-shadow: 0 1px 4px 1px hsla(0, 0%, 0%, .1);
  56. }
  57. #carbonads a {
  58. color: inherit;
  59. text-decoration: none;
  60. }
  61. #carbonads a:hover {
  62. color: inherit;
  63. }
  64. #carbonads span {
  65. position: relative;
  66. display: block;
  67. overflow: hidden;
  68. }
  69. #carbonads .carbon-wrap {
  70. display: flex;
  71. }
  72. .carbon-img {
  73. display: block;
  74. margin: 0;
  75. line-height: 1;
  76. }
  77. .carbon-img img {
  78. display: block;
  79. }
  80. .carbon-text {
  81. font-size: 13px;
  82. padding: 10px;
  83. line-height: 1.5;
  84. text-align: left;
  85. }
  86. .carbon-poweredby {
  87. display: block;
  88. padding: 8px 10px;
  89. 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);
  90. text-align: center;
  91. text-transform: uppercase;
  92. letter-spacing: .5px;
  93. font-weight: 600;
  94. font-size: 9px;
  95. line-height: 1;
  96. }
  97. </style>
  98. ]]
  99. end
  100. -- fix links
  101. function _fixlinks(htmldata)
  102. -- <a href="/manual/builtin_modules?id=osmv">os.mv</a>
  103. -- => <a href="/mirror/manual/builtin_modules.html#osmv">os.mv</a>
  104. htmldata = htmldata:gsub("(href=\"(.-)\")", function(_, href)
  105. if href:startswith("/") and not href:startswith("/#/") then
  106. local splitinfo = href:split('?', {plain = true})
  107. local url = splitinfo[1]
  108. href = "/mirror" .. url .. ".html"
  109. if splitinfo[2] then
  110. local anchor = splitinfo[2]:gsub("id=", "")
  111. href = href .. "#" .. anchor
  112. end
  113. print(" -> fix %s", href)
  114. end
  115. return "href=\"" .. href .. "\""
  116. end)
  117. -- <h4 id="os-rm">os.rm</h4>
  118. -- => <h4 id="osrm">os.rm</h4>
  119. htmldata = htmldata:gsub("(id=\"(.-)\")", function(_, id)
  120. id = id:gsub("%-", "")
  121. return "id=\"" .. id .. "\""
  122. end)
  123. return htmldata
  124. end
  125. -- generate mirror files and sitemap.xml
  126. -- we need install https://github.com/cwjohan/markdown-to-html first
  127. -- npm install markdown-to-html -g
  128. --
  129. -- Or use showdown-cli https://github.com/showdownjs/showdown
  130. --
  131. function main()
  132. local siteroot = "https://xmake.io"
  133. local mirrordir = "mirror"
  134. local sitemap = io.open("sitemap.xml", 'w')
  135. sitemap:print([[
  136. <?xml version="1.0" encoding="UTF-8"?>
  137. <urlset
  138. xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
  139. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  140. xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
  141. http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
  142. ]])
  143. sitemap:print([[
  144. <url>
  145. <loc>%s</loc>
  146. <lastmod>%s</lastmod>
  147. </url>
  148. ]], siteroot, mtime("index.html"))
  149. os.rm(mirrordir)
  150. for _, markdown in ipairs(os.files("**.md")) do
  151. local basename = path.basename(markdown)
  152. if not basename:startswith("_") then
  153. -- get the raw url
  154. if basename == "README" then
  155. basename = ""
  156. end
  157. local url = siteroot .. '/mirror'
  158. local rawurl = siteroot .. '/#'
  159. local dir = path.directory(markdown)
  160. if dir ~= '.' then
  161. rawurl = rawurl .. '/' .. dir
  162. url = url .. '/' .. dir
  163. end
  164. rawurl = rawurl .. '/' .. basename
  165. url = url .. '/' .. (basename == "" and "index.html" or (basename .. ".html"))
  166. -- generate html file
  167. local htmlfile = path.join(mirrordir, dir, basename == "" and "index.html" or (basename .. ".html"))
  168. local htmldata = os.iorunv("markdown", {markdown})
  169. local f = io.open(htmlfile, 'w')
  170. if f then
  171. f:write(header(rawurl))
  172. f:write(ads())
  173. htmldata = htmldata:gsub("&%a-;", function (w)
  174. local maps = {["&lt;"] = "<", ["&gt;"] = ">", ["&quot;"] = "\""}
  175. return maps[w]
  176. end)
  177. f:write(_fixlinks(htmldata))
  178. f:write(tailer())
  179. f:close()
  180. end
  181. --[[
  182. local tmpfile = os.tmpfile()
  183. os.mkdir(path.directory(tmpfile))
  184. os.execv("showdown", {"makehtml", "-i", markdown, "-o", tmpfile})
  185. local f = io.open(htmlfile, 'w')
  186. if f then
  187. f:write(header(rawurl))
  188. f:write(ads())
  189. f:write(_fixlinks(io.readfile(tmpfile)))
  190. f:write(tailer())
  191. f:close()
  192. end
  193. os.rm(tmpfile)]]
  194. print("build %s => %s, %s", markdown, htmlfile, mtime(htmlfile))
  195. print("url %s -> %s", url, rawurl)
  196. -- generate sitemap
  197. sitemap:print([[
  198. <url>
  199. <loc>%s</loc>
  200. <lastmod>%s</lastmod>
  201. </url>
  202. ]], url, mtime(htmlfile))
  203. end
  204. end
  205. sitemap:print("</urlset>")
  206. sitemap:close()
  207. end