add blog.lua, for generating blog indexes and stuff

This commit is contained in:
Izaya 2018-10-05 06:14:46 +10:00
parent a1e35ece94
commit 2b6dd4ad69

87
blog.lua Executable file
View File

@ -0,0 +1,87 @@
#!/usr/bin/env lua
local sitelib = require "sitelib"
local fs = require "lfs"
print("Finding files...")
local ftab = {}
for file in fs.dir(sitelib.srcpath.."/blog") do
if file:sub(1,1) ~= "." then
local index = tonumber(file:match("(%d+).md"))
if index then
ftab[index] = file
end
end
end
local pages = {}
local cpage = 1
print("Generating pages...")
for i = #ftab, 1, -1 do
pages[cpage] = pages[cpage] or {}
local fname = ftab[i]
local outfile = sitelib.outpath.."/blog/"..fname:sub(1,-4)..".html"
local f=io.open(sitelib.srcpath.."/blog/"..fname,"rb")
local c = f:read("*a")
f:close()
local page = sitelib.parsepage(c)
local f = io.open(outfile,"wb")
f:write("<html><head><title>"..(page.title or "").."</title>\n")
f:write(sitelib.header)
f:write(page.html)
if ftab[i-1] or ftab[i+1] then
f:write("<div class=\"wrapper\">\n")
if ftab[i+1] then
f:write("<div class=\"right-column\"><h4><a href=\""..tostring(i+1)..".html\">Next</a></div>")
end
if ftab[i-1] then
f:write("<div class=\"left-column\"><h4><a href=\""..tostring(i-1)..".html\">Previous</a></div>")
end
f:write("</div>")
end
if page.tags then
f:write("<h4>Tags: ")
for w in page.tags:gmatch("%S+") do
f:write('<a href="/tag/'..w..'.html">'..w.."</a> ")
end
end
f:write("</h4>")
f:write(sitelib.footer)
f:close()
pages[cpage][#pages[cpage]+1] = {fname:sub(1,-4)..".html",page.title,page.md:match("\n\n(.-)\n")}
if #pages[cpage] == 10 then
cpage = cpage + 1
end
end
for k,v in pairs(pages) do
v.name = "index"..tostring(k)..".html"
if k == 1 then
v.name = "index.html"
end
end
print("Generating index pages...")
for i,page in pairs(pages) do
local f = io.open(sitelib.outpath.."/blog/"..page.name,"wb")
f:write("<html><head><title>"..(page.title or "").."</title>\n")
f:write(sitelib.header)
f:write("<h1>Blog</h1>\n<h2>Page "..tostring(i).."</h2>\n")
for n,entry in ipairs(page) do
f:write("<h3><a href=\""..entry[1].."\">"..entry[2].."</a></h3>\n")
local fline = entry[3]:gsub("\"","\\\"")
fline = io.popen("echo \""..fline.."\" | markdown -"):read("*a")
f:write(fline)
end
if pages[i-1] or pages[i+1] then
f:write("<div class=\"wrapper\">\n")
if pages[i+1] then
f:write("<div class=\"right-column\"><h4><a href=\""..pages[i+1].name.."\">Older</a></div>")
end
if pages[i-1] then
f:write("<div class=\"left-column\"><h4><a href=\""..pages[i-1].name.."\">Newer</a></div>")
end
f:write("</div>")
end
f:write(sitelib.footer)
f:close()
end