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('