Skip to content

Instantly share code, notes, and snippets.

@mniip
Created July 25, 2015 12:29
Show Gist options
  • Save mniip/e8e1e85ec1c3e5e9620f to your computer and use it in GitHub Desktop.
Save mniip/e8e1e85ec1c3e5e9620f to your computer and use it in GitHub Desktop.
#!/var/www/core/lci
local page = env.REQUEST_URI
if page then
page = page:gsub("^/pages", ""):gsub("^/", "")
else
page = ""
end
page = page:gsub("[^a-zA-Z0-9_/-]", "")
local title
local content = tag"div"()
local tags
local filename = page
local f = io.open(".pages/" .. filename .. "/", "r")
while f do
filename = filename .. "/.index"
f:close()
f = io.open(".pages/" .. filename .. "/", "r")
end
f = io.open(".pages/" .. filename, "r")
if f then
local data = f:read"*a"
if data then
data = data:gsub("\\([<>&])", {["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;"})
content:force_content(data)
end
f:close()
end
for l in io.lines".pages/.titles" do
local k, v = l:match"^([^:]*): (.*)"
if k == page then
title = v
break
end
end
if page == "" then
title = ""
for l in io.lines".pages/.titles" do
local k, v = l:match"^([^:]*): (.*)"
content(tag"a"[{href = "/pages/" .. k}](v), tag"br")
end
elseif page:match"^tag/" then
local t = page:match"^tag/(.*)$"
if t == "" then
title = "Tags"
content = tag"div"(
tag"h2"("List of All Tags:")
)
local key = {}
for l in io.lines".pages/.tags" do
local k, v = l:match"^([^:]*): (.*)"
for tag in v:gmatch"%S+" do
key[tag] = (key[tag] or 0) + 1
end
end
local list = {}
local n = 0
for k in pairs(key) do
n = n + 1
list[n] = k
end
table.sort(list)
for _, v in ipairs(list) do
content(tag"a"[{href = "/pages/tag/" .. v}]("#" .. v), ": " .. key[v], tag"br")
end
else
title = "#" .. t
content = tag"div"
local titles = {}
for l in io.lines".pages/.titles" do
local k, v = l:match"^([^:]*): (.*)"
titles[k] = v
end
for l in io.lines".pages/.tags" do
local k, v = l:match"^([^:]*): (.*)"
for tt in v:gmatch"%S+" do
if tt == t then
content(tag"a"[{href = "/pages/" .. k}](titles[k]), tag"br")
break
end
end
end
tags = {"meta", "tags"}
end
end
if not tags then
for l in io.lines".pages/.tags" do
local k, v = l:match"^([^:]*): (.*)"
if k == page then
tags = {}
local n = 0
for tag in v:gmatch"%S+" do
n = n + 1
tags[n] = tag
end
break
end
end
end
local tagstag = tag"span"
for _, t in ipairs(tags or {}) do
tagstag(" ", tag"a"[{href = "/pages/tag/" .. t}]("#" .. t))
end
return doctype()(
tag"html"(
tag"head"(
tag"link"[{rel="stylesheet", href="/core/pages.css"}],
tag"link"[{rel="stylesheet", href="/core/highlight/styles/vim.css"}],
tag"title"(title),
tag"script"[{src="/core/highlight/hl.js"}]()
),
tag"body"[{onload="hljs.initHighlighting()"}](
tag"div"[{class="bar"}](
tag"div"[{class="content"}](
tag"h1":force_content(title),
content
)
),
tag"div"[{class="footer"}](
"Tags:", tagstag,
tag"div"[{class="menu"}](
tag"a"[{href = "/pages/"}]("Index"),
tag"a"[{href = "/pages/tag/"}]("Tags"),
tag"a"[{href = "/"}]("Home")
)
)
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment