Generator for my site.

graph.lua 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. local mdfiles = {}
  9. local dqueue = {sitelib.srcpath}
  10. for k,path in pairs(dqueue) do
  11. for file in fs.dir(path) do
  12. if file:sub(1,1) ~= "." then
  13. local attr = fs.attributes(path.."/"..file)
  14. if attr.mode == "directory" then
  15. dqueue[#dqueue+1] = path.."/"..file
  16. elseif file:sub(-3) == ".md" then
  17. mdfiles[#mdfiles+1] = path.."/"..file
  18. end
  19. end
  20. end
  21. end
  22. for k,infile in pairs(mdfiles) do
  23. local f = io.open(infile,"rb")
  24. local c = f:read("*a")
  25. f:close()
  26. local page = sitelib.parsepage(c)
  27. if page.tags then
  28. print((infile:sub(sitelib.srcpath:len()+1)):sub(1,-4)..".html")
  29. for w in page.tags:gmatch("%S+") do
  30. tags[w] = tags[w] or {}
  31. table.insert(tags[w],{(infile:sub(sitelib.srcpath:len()+1)):sub(1,-4)..".html",page.title})
  32. end
  33. end
  34. end
  35. local haslabel = {}
  36. print("graph blog {")
  37. for tag,v in pairs(tags) do
  38. for l,m in pairs(v) do
  39. if not haslabel[m[1]:match("(%d+).html")] then
  40. print(m[1]:match("(%d+).html").." [label=\""..m[2]:gsub("\"","").."\"];")
  41. haslabel[m[1]:match("(%d+).html")] = true
  42. end
  43. print(tag:gsub("-","").." -- "..m[1]:match("(%d+).html")..";")
  44. end
  45. end
  46. print("}")