Generator for my site.

blog.lua 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env lua
  2. local sitelib = require "sitelib"
  3. local fs = require "lfs"
  4. print("Finding files...")
  5. local ftab = {}
  6. for file in fs.dir(sitelib.srcpath.."/blog") do
  7. if file:sub(1,1) ~= "." then
  8. local index = tonumber(file:match("(%d+).md"))
  9. if index then
  10. ftab[index] = file
  11. end
  12. end
  13. end
  14. local pages = {}
  15. local cpage = 1
  16. print("Generating pages...")
  17. for i = #ftab, 1, -1 do
  18. pages[cpage] = pages[cpage] or {}
  19. local fname = ftab[i]
  20. local outfile = sitelib.outpath.."/blog/"..fname:sub(1,-4)..".html"
  21. local f=io.open(sitelib.srcpath.."/blog/"..fname,"rb")
  22. local c = f:read("*a")
  23. f:close()
  24. local page = sitelib.parsepage(c)
  25. local f = io.open(outfile,"wb")
  26. f:write("<html><head><title>"..(page.title or "").."</title>\n")
  27. f:write(sitelib.header)
  28. f:write(page.html)
  29. if ftab[i-1] or ftab[i+1] then
  30. f:write("<div class=\"wrapper\">\n")
  31. if ftab[i+1] then
  32. f:write("<div class=\"right-column\"><h4><a href=\""..tostring(i+1)..".html\">Next</a></div>")
  33. end
  34. if ftab[i-1] then
  35. f:write("<div class=\"left-column\"><h4><a href=\""..tostring(i-1)..".html\">Previous</a></div>")
  36. end
  37. f:write("</div>")
  38. end
  39. if page.author or page.date then
  40. f:write('<h4><div class="wrapper"><div class="left-column">By '..(page.author or "")..'</div><div class="right-column">'..(page.date or "")..'</div></div></h4>\n')
  41. end
  42. if page.tags then
  43. f:write("<h4>Tags: ")
  44. for w in page.tags:gmatch("%S+") do
  45. f:write('<a href="/tag/'..w..'.html">'..w.."</a> ")
  46. end
  47. end
  48. f:write("</h4>")
  49. f:write(sitelib.footer)
  50. f:close()
  51. pages[cpage][#pages[cpage]+1] = {fname:sub(1,-4)..".html",page.title,page.md:match("\n\n(.-)\n")}
  52. if #pages[cpage] == 10 then
  53. cpage = cpage + 1
  54. end
  55. end
  56. for k,v in pairs(pages) do
  57. v.name = "index"..tostring(k)..".html"
  58. if k == 1 then
  59. v.name = "index.html"
  60. end
  61. end
  62. print("Generating index pages...")
  63. for i,page in pairs(pages) do
  64. local f = io.open(sitelib.outpath.."/blog/"..page.name,"wb")
  65. f:write("<html><head><title>"..(page.title or "").."</title>\n")
  66. f:write(sitelib.header)
  67. f:write("<h1>Blog</h1>\n<h2>Page "..tostring(i).."</h2>\n")
  68. for n,entry in ipairs(page) do
  69. f:write("<h3><a href=\""..entry[1].."\">"..entry[2].."</a></h3>\n")
  70. local fline = entry[3]:gsub("\"","\\\"")
  71. fline = io.popen("echo \""..fline.."\" | markdown -"):read("*a")
  72. f:write(fline)
  73. end
  74. if pages[i-1] or pages[i+1] then
  75. f:write("<div class=\"wrapper\">\n")
  76. if pages[i+1] then
  77. f:write("<div class=\"right-column\"><h4><a href=\""..pages[i+1].name.."\">Older</a></div>")
  78. end
  79. if pages[i-1] then
  80. f:write("<div class=\"left-column\"><h4><a href=\""..pages[i-1].name.."\">Newer</a></div>")
  81. end
  82. f:write("</div>")
  83. end
  84. f:write(sitelib.footer)
  85. f:close()
  86. end