diff --git a/gengopher.lua b/gengopher.lua new file mode 100755 index 0000000..9e3ccc1 --- /dev/null +++ b/gengopher.lua @@ -0,0 +1,62 @@ +#!/usr/bin/env lua +local sitelib = require "sitelib" +local fs = require "lfs" + +print("Copying static content...") +--os.execute("cp -rv static/* out/") +for k,v in pairs({"jpg","jpeg","png","gif"}) do +os.execute("find static/ -name '*."..v.."' -exec echo '{}' \\;") +end + +print("Building list of markdown files") +local mdfiles = {} +local dqueue = {sitelib.srcpath} +for k,path in pairs(dqueue) do + for file in fs.dir(path) do + if file:sub(1,1) ~= "." then + local attr = fs.attributes(path.."/"..file) + if attr.mode == "directory" then + dqueue[#dqueue+1] = path.."/"..file + elseif file:sub(-3) == ".md" then + mdfiles[#mdfiles+1] = path.."/"..file + end + end + end +end + +if not fs.attributes(sitelib.gopherpath) then + fs.mkdir(sitelib.outpath) +end + +print("Creating directories") +for k,v in pairs(dqueue) do + local path = sitelib.outpath..v:sub(sitelib.srcpath:len()+1) + fs.mkdir(path) + print(path) +end + +print("Generating output files") +for k,infile in pairs(mdfiles) do + local outfile = (sitelib.gopherpath..infile:sub(sitelib.srcpath:len()+1)) + print(infile,outfile) + local f = io.open(infile,"rb") + local c = f:read("*a") + f:close() + local page = sitelib.parsepage(c) + local f = io.open(outfile,"wb") + if page.title then + f:write("# "..page.title.."\n") + end + if page.author then + f:write("By "..page.author.."\n") + end + if page.date then + f:write(page.date.."\n") + end + f:write(page.md) + if page.tags then + f:write("Tags: "..page.tags) + end + f:close() +end + diff --git a/gopher-home.lua b/gopher-home.lua new file mode 100755 index 0000000..80f7d4e --- /dev/null +++ b/gopher-home.lua @@ -0,0 +1,54 @@ +#!/usr/bin/env lua +local sitelib = require "sitelib" +local fs = require "lfs" +local articles = 4 + +print("Finding blog posts...") +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 = {} +for i = #ftab, #ftab-(articles-1), -1 do + local f = io.open(sitelib.srcpath.."/blog/"..ftab[i]) + local c = f:read("*a") + f:close() + local page = sitelib.parsepage(c) + pages[#pages+1] = {page.title,"/blog/"..tostring(i)..".md"} +end + +print("Reading template...") +local f = io.open(sitelib.includepath.."/gheader-home","rb") +local header = f:read("*a") +f:close() +local f = io.open(sitelib.includepath.."/gfooter-home","rb") +local footer = f:read("*a") +f:close() + +print("Writing index...") +local f = io.open(sitelib.gopherpath.."/.gopherdir","wb") +f:write(header) +while #pages > 0 do + local article = table.remove(pages,1) + f:write(string.format("0%s\t%s\t%s\t%d\n",article[1],article[2],sitelib.hostname,sitelib.gopherport)) +--[[ + local lp, rp = table.remove(pages,1), table.remove(pages,1) + f:write('
') + f:write('
') + f:write('

'..lp[1]..'

') + f:write('
') + f:write('
') + f:write('

'..rp[1]..'

') + f:write('
') + f:write('
') +]]-- +end +f:write(footer) +--f:write(sitelib.footer) +f:close() diff --git a/gopherblog.lua b/gopherblog.lua new file mode 100755 index 0000000..d0f0b15 --- /dev/null +++ b/gopherblog.lua @@ -0,0 +1,38 @@ +#!/usr/bin/env lua +local sitelib = require "sitelib" +local fs = require "lfs" + +print("Reading blog entries") +local mdfiles = {} +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 + mdfiles[index] = file + end + end +end + +if not fs.attributes(sitelib.gopherpath) then + fs.mkdir(sitelib.gopherpath) +end + +local index="" +print("Generating index") +for k,infile in ipairs(mdfiles) do + local fpath = "/blog/"..infile + local infile = sitelib.srcpath.."/blog/"..infile + print(infile,fpath) + local f = io.open(infile,"rb") + local c = f:read("*a") + f:close() + local page = sitelib.parsepage(c) + if page.date then page.title = string.format("%s (%s)",page.title,page.date) end + local fline = page.md:match("\n\n(.-)\n") + index = string.format("0%s\t%s\t%s\t%d\ni%s\n%s",page.title,fpath,sitelib.hostname,sitelib.gopherport,fline,index) +end +index=sitelib.gheader .. index .. sitelib.gfooter + +local f = io.open(sitelib.gopherpath.."/blog/.gopherdir","wb") +f:write(index) +f:close() diff --git a/sitelib.lua b/sitelib.lua index 297e78a..a9eb1a2 100644 --- a/sitelib.lua +++ b/sitelib.lua @@ -2,10 +2,14 @@ local sitelib = {} sitelib.srcpath = "src" sitelib.outpath = "out" +sitelib.gopherpath = "gopher" sitelib.includepath = "include" sitelib.staticpath = "static" +sitelib.hostname = "shadowkat.net" +sitelib.gopherport = 70 sitelib.header, sitelib.footer = "", "" +sitelib.gheader, sitelib.gfooter = "", "" local f = io.open("include/header.html") if f then sitelib.header = f:read("*a") @@ -16,6 +20,16 @@ if f then sitelib.footer = f:read("*a") f:close() end +local f = io.open("include/gheader") +if f then + sitelib.gheader = f:read("*a") + f:close() +end +local f = io.open("include/gfooter") +if f then + sitelib.gfooter = f:read("*a") + f:close() +end function sitelib.parsepage(s) local page={}