gopher export now

This commit is contained in:
Izaya 2019-01-15 15:03:09 +11:00
parent f3a78ccf42
commit 47ef6350fd
4 changed files with 168 additions and 0 deletions

62
gengopher.lua Executable file
View File

@ -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

54
gopher-home.lua Executable file
View File

@ -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('<div class="wrapper">')
f:write(' <div class="left-column">')
f:write(' <h2><a href="'..lp[2]..'">'..lp[1]..'</a></h2>')
f:write(' </div>')
f:write(' <div class="right-column">')
f:write(' <h2><a href="'..rp[2]..'">'..rp[1]..'</a></h2>')
f:write(' </div>')
f:write('</div>')
]]--
end
f:write(footer)
--f:write(sitelib.footer)
f:close()

38
gopherblog.lua Executable file
View File

@ -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()

View File

@ -2,10 +2,14 @@ local sitelib = {}
sitelib.srcpath = "src" sitelib.srcpath = "src"
sitelib.outpath = "out" sitelib.outpath = "out"
sitelib.gopherpath = "gopher"
sitelib.includepath = "include" sitelib.includepath = "include"
sitelib.staticpath = "static" sitelib.staticpath = "static"
sitelib.hostname = "shadowkat.net"
sitelib.gopherport = 70
sitelib.header, sitelib.footer = "", "" sitelib.header, sitelib.footer = "", ""
sitelib.gheader, sitelib.gfooter = "", ""
local f = io.open("include/header.html") local f = io.open("include/header.html")
if f then if f then
sitelib.header = f:read("*a") sitelib.header = f:read("*a")
@ -16,6 +20,16 @@ if f then
sitelib.footer = f:read("*a") sitelib.footer = f:read("*a")
f:close() f:close()
end 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) function sitelib.parsepage(s)
local page={} local page={}