Browse Source

gopher export now

XeonSquared 3 months ago
parent
commit
47ef6350fd
4 changed files with 168 additions and 0 deletions
  1. 62
    0
      gengopher.lua
  2. 54
    0
      gopher-home.lua
  3. 38
    0
      gopherblog.lua
  4. 14
    0
      sitelib.lua

+ 62
- 0
gengopher.lua View File

@@ -0,0 +1,62 @@
1
+#!/usr/bin/env lua
2
+local sitelib = require "sitelib"
3
+local fs = require "lfs"
4
+
5
+print("Copying static content...")
6
+--os.execute("cp -rv static/* out/")
7
+for k,v in pairs({"jpg","jpeg","png","gif"}) do
8
+os.execute("find static/ -name '*."..v.."' -exec echo '{}' \\;")
9
+end
10
+
11
+print("Building list of markdown files")
12
+local mdfiles = {}
13
+local dqueue = {sitelib.srcpath}
14
+for k,path in pairs(dqueue) do
15
+ for file in fs.dir(path) do
16
+  if file:sub(1,1) ~= "." then
17
+   local attr = fs.attributes(path.."/"..file)
18
+   if attr.mode == "directory" then
19
+    dqueue[#dqueue+1] = path.."/"..file
20
+   elseif file:sub(-3) == ".md" then
21
+    mdfiles[#mdfiles+1] = path.."/"..file
22
+   end
23
+  end
24
+ end
25
+end
26
+
27
+if not fs.attributes(sitelib.gopherpath) then
28
+ fs.mkdir(sitelib.outpath)
29
+end
30
+
31
+print("Creating directories")
32
+for k,v in pairs(dqueue) do
33
+ local path = sitelib.outpath..v:sub(sitelib.srcpath:len()+1)
34
+ fs.mkdir(path)
35
+ print(path)
36
+end
37
+
38
+print("Generating output files")
39
+for k,infile in pairs(mdfiles) do
40
+ local outfile = (sitelib.gopherpath..infile:sub(sitelib.srcpath:len()+1))
41
+ print(infile,outfile)
42
+ local f = io.open(infile,"rb")
43
+ local c = f:read("*a")
44
+ f:close()
45
+ local page = sitelib.parsepage(c)
46
+ local f = io.open(outfile,"wb")
47
+ if page.title then
48
+  f:write("# "..page.title.."\n")
49
+ end
50
+ if page.author then
51
+  f:write("By "..page.author.."\n")
52
+ end
53
+ if page.date then
54
+  f:write(page.date.."\n")
55
+ end
56
+ f:write(page.md)
57
+ if page.tags then
58
+  f:write("Tags: "..page.tags)
59
+ end
60
+ f:close()
61
+end
62
+

+ 54
- 0
gopher-home.lua View File

@@ -0,0 +1,54 @@
1
+#!/usr/bin/env lua
2
+local sitelib = require "sitelib"
3
+local fs = require "lfs"
4
+local articles = 4
5
+
6
+print("Finding blog posts...")
7
+local ftab = {}
8
+for file in fs.dir(sitelib.srcpath.."/blog") do
9
+ if file:sub(1,1) ~= "." then
10
+  local index = tonumber(file:match("(%d+).md"))
11
+  if index then
12
+   ftab[index] = file
13
+  end
14
+ end
15
+end
16
+
17
+local pages = {}
18
+for i = #ftab, #ftab-(articles-1), -1 do
19
+ local f = io.open(sitelib.srcpath.."/blog/"..ftab[i])
20
+ local c = f:read("*a")
21
+ f:close()
22
+ local page = sitelib.parsepage(c)
23
+ pages[#pages+1] = {page.title,"/blog/"..tostring(i)..".md"}
24
+end
25
+
26
+print("Reading template...")
27
+local f = io.open(sitelib.includepath.."/gheader-home","rb")
28
+local header = f:read("*a")
29
+f:close()
30
+local f = io.open(sitelib.includepath.."/gfooter-home","rb")
31
+local footer = f:read("*a")
32
+f:close()
33
+
34
+print("Writing index...")
35
+local f = io.open(sitelib.gopherpath.."/.gopherdir","wb")
36
+f:write(header)
37
+while #pages > 0 do
38
+ local article = table.remove(pages,1)
39
+ f:write(string.format("0%s\t%s\t%s\t%d\n",article[1],article[2],sitelib.hostname,sitelib.gopherport))
40
+--[[
41
+ local lp, rp = table.remove(pages,1), table.remove(pages,1)
42
+ f:write('<div class="wrapper">')
43
+ f:write(' <div class="left-column">')
44
+ f:write('  <h2><a href="'..lp[2]..'">'..lp[1]..'</a></h2>')
45
+ f:write(' </div>')
46
+ f:write(' <div class="right-column">')
47
+ f:write('  <h2><a href="'..rp[2]..'">'..rp[1]..'</a></h2>')
48
+ f:write(' </div>')
49
+ f:write('</div>')
50
+]]--
51
+end
52
+f:write(footer)
53
+--f:write(sitelib.footer)
54
+f:close()

+ 38
- 0
gopherblog.lua View File

@@ -0,0 +1,38 @@
1
+#!/usr/bin/env lua
2
+local sitelib = require "sitelib"
3
+local fs = require "lfs"
4
+
5
+print("Reading blog entries")
6
+local mdfiles = {}
7
+for file in fs.dir(sitelib.srcpath.."/blog/") do
8
+ if file:sub(1,1) ~= "." then
9
+  local index = tonumber(file:match("(%d+).md"))
10
+  if index then
11
+   mdfiles[index] = file
12
+  end
13
+ end
14
+end
15
+
16
+if not fs.attributes(sitelib.gopherpath) then
17
+ fs.mkdir(sitelib.gopherpath)
18
+end
19
+
20
+local index=""
21
+print("Generating index")
22
+for k,infile in ipairs(mdfiles) do
23
+ local fpath = "/blog/"..infile
24
+ local infile = sitelib.srcpath.."/blog/"..infile
25
+ print(infile,fpath)
26
+ local f = io.open(infile,"rb")
27
+ local c = f:read("*a")
28
+ f:close()
29
+ local page = sitelib.parsepage(c)
30
+ if page.date then page.title = string.format("%s (%s)",page.title,page.date) end
31
+ local fline = page.md:match("\n\n(.-)\n")
32
+ index = string.format("0%s\t%s\t%s\t%d\ni%s\n%s",page.title,fpath,sitelib.hostname,sitelib.gopherport,fline,index)
33
+end
34
+index=sitelib.gheader .. index .. sitelib.gfooter
35
+
36
+local f = io.open(sitelib.gopherpath.."/blog/.gopherdir","wb")
37
+f:write(index)
38
+f:close()

+ 14
- 0
sitelib.lua View File

@@ -2,10 +2,14 @@ local sitelib = {}
2 2
 
3 3
 sitelib.srcpath = "src"
4 4
 sitelib.outpath = "out"
5
+sitelib.gopherpath = "gopher"
5 6
 sitelib.includepath = "include"
6 7
 sitelib.staticpath = "static"
8
+sitelib.hostname = "shadowkat.net"
9
+sitelib.gopherport = 70
7 10
 
8 11
 sitelib.header, sitelib.footer = "", ""
12
+sitelib.gheader, sitelib.gfooter = "", ""
9 13
 local f = io.open("include/header.html")
10 14
 if f then
11 15
  sitelib.header = f:read("*a")
@@ -16,6 +20,16 @@ if f then
16 20
  sitelib.footer = f:read("*a")
17 21
  f:close()
18 22
 end
23
+local f = io.open("include/gheader")
24
+if f then
25
+ sitelib.gheader = f:read("*a")
26
+ f:close()
27
+end
28
+local f = io.open("include/gfooter")
29
+if f then
30
+ sitelib.gfooter = f:read("*a")
31
+ f:close()
32
+end
19 33
 
20 34
 function sitelib.parsepage(s)
21 35
  local page={}

Loading…
Cancel
Save