Generator for my site.

genpages.lua 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env lua
  2. local sitelib = require "sitelib"
  3. local fs = require "lfs"
  4. print("Copying static content...")
  5. os.execute("cp -rv static/* out/")
  6. print("Building list of markdown files")
  7. local mdfiles = {}
  8. local dqueue = {sitelib.srcpath}
  9. for k,path in pairs(dqueue) do
  10. for file in fs.dir(path) do
  11. if file:sub(1,1) ~= "." then
  12. local attr = fs.attributes(path.."/"..file)
  13. if attr.mode == "directory" then
  14. dqueue[#dqueue+1] = path.."/"..file
  15. elseif file:sub(-3) == ".md" then
  16. mdfiles[#mdfiles+1] = path.."/"..file
  17. end
  18. end
  19. end
  20. end
  21. if not fs.attributes(sitelib.outpath) then
  22. fs.mkdir(sitelib.outpath)
  23. end
  24. print("Creating directories")
  25. for k,v in pairs(dqueue) do
  26. local path = sitelib.outpath..v:sub(sitelib.srcpath:len()+1)
  27. fs.mkdir(path)
  28. print(path)
  29. end
  30. print("Generating output files")
  31. for k,infile in pairs(mdfiles) do
  32. local outfile = (sitelib.outpath..infile:sub(sitelib.srcpath:len()+1)):sub(1,-4)..".html"
  33. print(infile,outfile)
  34. local f = io.open(infile,"rb")
  35. local c = f:read("*a")
  36. f:close()
  37. local page = sitelib.parsepage(c)
  38. local f = io.open(outfile,"wb")
  39. f:write("<html><head><title>"..(page.title or "").."</title>\n")
  40. f:write(sitelib.header)
  41. f:write(page.html)
  42. if page.author or page.date then
  43. 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')
  44. end
  45. if page.tags then
  46. f:write("<h4>Tags: ")
  47. for w in page.tags:gmatch("%S+") do
  48. f:write('<a href="/tag/'..w..'.html">'..w.."</a> ")
  49. end
  50. f:write("</h4>")
  51. end
  52. f:write(sitelib.footer)
  53. f:close()
  54. end