Generator for my site.

home.lua 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env lua
  2. local sitelib = require "sitelib"
  3. local fs = require "lfs"
  4. local articles = 4
  5. print("Finding blog posts...")
  6. local ftab = {}
  7. for file in fs.dir(sitelib.srcpath.."/blog") do
  8. if file:sub(1,1) ~= "." then
  9. local index = tonumber(file:match("(%d+).md"))
  10. if index then
  11. ftab[index] = file
  12. end
  13. end
  14. end
  15. local pages = {}
  16. for i = #ftab, #ftab-(articles-1), -1 do
  17. local f = io.open(sitelib.srcpath.."/blog/"..ftab[i])
  18. local c = f:read("*a")
  19. f:close()
  20. local page = sitelib.parsepage(c)
  21. pages[#pages+1] = {page.title,"/blog/"..tostring(i)..".html"}
  22. end
  23. print("Reading template...")
  24. local f = io.open(sitelib.includepath.."/home-top.html","rb")
  25. local header = f:read("*a")
  26. f:close()
  27. local f = io.open(sitelib.includepath.."/home-end.html","rb")
  28. local footer = f:read("*a")
  29. f:close()
  30. print("Writing index...")
  31. local f = io.open(sitelib.outpath.."/index.html","wb")
  32. f:write(header)
  33. while #pages > 0 do
  34. local lp, rp = table.remove(pages,1), table.remove(pages,1)
  35. f:write('<div class="wrapper">')
  36. f:write(' <div class="left-column">')
  37. f:write(' <h2><a href="'..lp[2]..'">'..lp[1]..'</a></h2>')
  38. f:write(' </div>')
  39. f:write(' <div class="right-column">')
  40. f:write(' <h2><a href="'..rp[2]..'">'..rp[1]..'</a></h2>')
  41. f:write(' </div>')
  42. f:write('</div>')
  43. end
  44. f:write(footer)
  45. f:write(sitelib.footer)
  46. f:close()