Generator for my site.

tags.lua 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env lua
  2. local sitelib = require "sitelib"
  3. local fs = require "lfs"
  4. local tags = {}
  5. if not fs.attributes(sitelib.outpath.."/tag") then
  6. fs.mkdir(sitelib.outpath.."/tag")
  7. end
  8. print("Building list of markdown files")
  9. local mdfiles = {}
  10. local dqueue = {sitelib.srcpath}
  11. for k,path in pairs(dqueue) do
  12. for file in fs.dir(path) do
  13. if file:sub(1,1) ~= "." then
  14. local attr = fs.attributes(path.."/"..file)
  15. if attr.mode == "directory" then
  16. dqueue[#dqueue+1] = path.."/"..file
  17. elseif file:sub(-3) == ".md" then
  18. mdfiles[#mdfiles+1] = path.."/"..file
  19. end
  20. end
  21. end
  22. end
  23. print("Reading tags...")
  24. for k,infile in pairs(mdfiles) do
  25. local f = io.open(infile,"rb")
  26. local c = f:read("*a")
  27. f:close()
  28. local page = sitelib.parsepage(c)
  29. if page.tags then
  30. print((infile:sub(sitelib.srcpath:len()+1)):sub(1,-4)..".html")
  31. for w in page.tags:gmatch("%S+") do
  32. tags[w] = tags[w] or {}
  33. table.insert(tags[w],{(infile:sub(sitelib.srcpath:len()+1)):sub(1,-4)..".html",page.title,page.md:match("\n\n(.-)\n")})
  34. end
  35. end
  36. end
  37. print("Writing individual tag indexes...")
  38. for tag,v in pairs(tags) do
  39. local f = io.open(sitelib.outpath.."/tag/"..tag..".html","wb")
  40. f:write("<html><head><title>Tag: "..tag.."</title>\n")
  41. f:write(sitelib.header)
  42. f:write("<h1>Tag: "..tag.."</h1>\n")
  43. for l,m in pairs(v) do
  44. f:write("<h3><a href=\""..m[1].."\">"..m[2].."</a></h3>\n")
  45. local fline = m[3]:gsub("\"","\\\"")
  46. f:write(io.popen("echo \""..fline.."\" | markdown -"):read("*a"))
  47. end
  48. f:write(sitelib.footer)
  49. f:close()
  50. end