robin/maivi

a tiny cute lua web framework

git clone https://git.robinwobin.dev/robin/maivi

robindocs: meow1be4c9c

push-srxvuswpkqxx
2.0 KiB89 linesraw
1maivi.router:add("/", maivi.mod("pages.index"), {})
2
3---@diagnostic disable-next-line: global-in-non-module
4H = require("helpers")
5
6---@class Section
7---@field stub string
8---@field path string
9---@field url string
10---@field header boolean
11---@field section string
12---@field id string
13---@field content? string
14
15---@type Section[]
16maivi.data.sections = {}
17
18for file, data in pairs(this.data) do
19  if string.sub(file, 1, 1) == "/" then
20    file = string.sub(file, 2)
21  end
22
23  assert(string.sub(file, 1, 4) == "data")
24
25  -- file: data/path/to/00-file.ext
26  -- url:       <------------->
27  local url = H.stripend(string.sub(file, #"data/"), "%.")
28
29  -- url: /path/to/00-url
30  -- stub:         <---->
31  data.stub = H.subafter(url, "/")
32
33  data.path = file
34  data.url = url
35
36  data.header = false
37  -- stub: 00--url--
38  -- section:  <->
39  if string.sub(data.stub, 4, 4) == "-" then
40    data.header = true
41  end
42
43  -- stub: 00-url
44  -- section: <->
45  if not data.section then
46    if data.header then
47      data.section = string.sub(data.stub, 5, -3)
48    else
49      data.section = string.sub(data.stub, 4)
50    end
51    ---@diagnostic disable-next-line: cast-type-mismatch
52    ---@cast data.section string
53    data.section = string.gsub(data.section, "%-", " ")
54  end
55
56  data.id = string.gsub(string.sub(url, 2), "/", "-")
57
58  if string.sub(data.path, -4) ~= ".txt" then
59    ---@diagnostic disable-next-line: undefined-global
60    local contents = read(data.path)
61
62    if string.sub(contents, 0, 3) == "+++" then
63      contents = string.gsub(contents, "^+++.*+++", "")
64    end
65
66    if string.sub(data.path, -4) == ".lua" then
67      local f = loadstring(contents, data.path)
68      if not f then
69        return
70      end
71      _, contents = pcall(f)
72    end
73
74    data.content = contents
75  end
76
77  -- maivi.print(data)
78
79  data.pos = tonumber(string.sub(data.stub, 0, 2))
80  table.insert(maivi.data.sections, data)
81end
82
83table.sort(maivi.data.sections, function(a, b)
84  if not a.pos or not b.pos then
85    return a.pos ~= nil
86  end
87
88  return a.pos < b.pos
89end)