Browse Source

added the tagging system

Izaya 2 months ago
parent
commit
494a5d133d
2 changed files with 104 additions and 0 deletions
  1. 50
    0
      graph.lua
  2. 54
    0
      tags.lua

+ 50
- 0
graph.lua View File

@@ -0,0 +1,50 @@
1
+#!/usr/bin/env lua
2
+local sitelib = require "sitelib"
3
+local fs = require "lfs"
4
+local tags = {}
5
+
6
+if not fs.attributes(sitelib.outpath.."/tag") then
7
+ fs.mkdir(sitelib.outpath.."/tag")
8
+end
9
+
10
+local mdfiles = {}
11
+local dqueue = {sitelib.srcpath}
12
+for k,path in pairs(dqueue) do
13
+ for file in fs.dir(path) do
14
+  if file:sub(1,1) ~= "." then
15
+   local attr = fs.attributes(path.."/"..file)
16
+   if attr.mode == "directory" then
17
+    dqueue[#dqueue+1] = path.."/"..file
18
+   elseif file:sub(-3) == ".md" then
19
+    mdfiles[#mdfiles+1] = path.."/"..file
20
+   end
21
+  end
22
+ end
23
+end
24
+
25
+for k,infile in pairs(mdfiles) do
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.tags then
31
+   print((infile:sub(sitelib.srcpath:len()+1)):sub(1,-4)..".html")
32
+  for w in page.tags:gmatch("%S+") do
33
+   tags[w] = tags[w] or {}
34
+   table.insert(tags[w],{(infile:sub(sitelib.srcpath:len()+1)):sub(1,-4)..".html",page.title})
35
+  end
36
+ end
37
+end
38
+
39
+local haslabel = {}
40
+print("graph blog {")
41
+for tag,v in pairs(tags) do
42
+ for l,m in pairs(v) do
43
+  if not haslabel[m[1]:match("(%d+).html")] then
44
+   print(m[1]:match("(%d+).html").." [label=\""..m[2]:gsub("\"","").."\"];")
45
+   haslabel[m[1]:match("(%d+).html")] = true
46
+  end
47
+  print(tag:gsub("-","").." -- "..m[1]:match("(%d+).html")..";")
48
+ end
49
+end
50
+print("}")

+ 54
- 0
tags.lua View File

@@ -0,0 +1,54 @@
1
+#!/usr/bin/env lua
2
+local sitelib = require "sitelib"
3
+local fs = require "lfs"
4
+local tags = {}
5
+
6
+if not fs.attributes(sitelib.outpath.."/tag") then
7
+ fs.mkdir(sitelib.outpath.."/tag")
8
+end
9
+
10
+print("Building list of markdown files")
11
+local mdfiles = {}
12
+local dqueue = {sitelib.srcpath}
13
+for k,path in pairs(dqueue) do
14
+ for file in fs.dir(path) do
15
+  if file:sub(1,1) ~= "." then
16
+   local attr = fs.attributes(path.."/"..file)
17
+   if attr.mode == "directory" then
18
+    dqueue[#dqueue+1] = path.."/"..file
19
+   elseif file:sub(-3) == ".md" then
20
+    mdfiles[#mdfiles+1] = path.."/"..file
21
+   end
22
+  end
23
+ end
24
+end
25
+
26
+print("Reading tags...")
27
+for k,infile in pairs(mdfiles) do
28
+ local f = io.open(infile,"rb")
29
+ local c = f:read("*a")
30
+ f:close()
31
+ local page = sitelib.parsepage(c)
32
+ if page.tags then
33
+   print((infile:sub(sitelib.srcpath:len()+1)):sub(1,-4)..".html")
34
+  for w in page.tags:gmatch("%S+") do
35
+   tags[w] = tags[w] or {}
36
+   table.insert(tags[w],{(infile:sub(sitelib.srcpath:len()+1)):sub(1,-4)..".html",page.title,page.md:match("\n\n(.-)\n")})
37
+  end
38
+ end
39
+end
40
+
41
+print("Writing individual tag indexes...")
42
+for tag,v in pairs(tags) do
43
+ local f = io.open(sitelib.outpath.."/tag/"..tag..".html","wb")
44
+ f:write("<html><head><title>Tag: "..tag.."</title>\n")
45
+ f:write(sitelib.header)
46
+ f:write("<h1>Tag: "..tag.."</h1>\n")
47
+ for l,m in pairs(v) do
48
+  f:write("<h3><a href=\""..m[1].."\">"..m[2].."</a></h3>\n")
49
+  local fline = m[3]:gsub("\"","\\\"")
50
+  f:write(io.popen("echo \""..fline.."\" | markdown -"):read("*a"))
51
+ end
52
+ f:write(sitelib.footer)
53
+ f:close()
54
+end