summaryrefslogtreecommitdiff
path: root/.zs/components/nav
blob: e2ff28359120820883e6aff811914a0f370efcf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/lua

package.path = ".zs/?.lua;" .. package.path
local html = require "components.html"

---@alias Set table<any, true>

---@param t any[]
---@return Set
local function set(t)
    local tmp = {}
    for _, value in ipairs(t) do
        tmp[value] = true
    end
    return tmp
end

---@class Link
---@field url string
---@field title string
---@field index boolean?
---@field dir string?
---@field inside Set?

---@type Link[]
local links = {
    {
        url = "/",
        title = "home",
        inside = set {
            "index"
        }
    },
    {
        url = "/projects.html",
        title = "projects",
        inside = set {
            "prismite"
        }
    },
    {
        url = "/posts.html",
        title = "ramblings",
        dir = "blog"
    },
    {
        url = "/buttons.html",
        title = "buttons"
    },
    {
        url = "/rss.xml",
        title = "rss"
    }
}

---@param path string
---@return string, integer
local function get_file_stem(path)
    return path
        :gsub(".[^.]*$", "") -- get rid of ext
        :gsub(".*/", "") -- get rid of prefix
end

local current_path = assert(os.getenv("ZS_FILE"))
local current_file = get_file_stem(current_path)

local acc = {}

for _, link in ipairs(links) do
    local title = link.title
    local class = "tab"
    if
        current_file == get_file_stem(link.url) or
        link.inside and link.inside[current_file] or
        link.dir and current_path:match(link.dir .. "/") ~= nil
    then
        class = class .. " active"
    end

    table.insert(acc, {
        tag = "li",
        class = class,
        {
            tag = "a",
            href = link.url,
            title,
        }
    })
end

print(html {
    tag = "nav",
    {
        tag = "ul",
        acc
    },
})