#!/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("
"..(page.title or "").."\n")
f:write(sitelib.header)
f:write(page.html)
if ftab[i-1] or ftab[i+1] then
f:write("\n")
if ftab[i+1] then
f:write("
")
end
if ftab[i-1] then
f:write("
")
end
f:write("
")
end
if page.author or page.date then
f:write('By '..(page.author or "")..'
'..(page.date or "")..'
\n')
end
if page.tags then
f:write("Tags: ")
for w in page.tags:gmatch("%S+") do
f:write(''..w.." ")
end
end
f:write("
")
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(""..(page.title or "").."\n")
f:write(sitelib.header)
f:write("Blog
\nPage "..tostring(i).."
\n")
for n,entry in ipairs(page) do
f:write("\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("\n")
if pages[i+1] then
f:write("
")
end
if pages[i-1] then
f:write("
")
end
f:write("
")
end
f:write(sitelib.footer)
f:close()
end