From 7c7caa7b6ef760b160ab7e8e63552d2312b04c69 Mon Sep 17 00:00:00 2001 From: Xuan Sang LE Date: Sun, 25 Feb 2018 01:57:42 +0100 Subject: [PATCH] change handler --- blog/Makefile | 2 +- blog/index.ls | 28 ---------------------------- blog/index.lua | 1 + blog/router.lua | 11 +++++++++-- blog/view.lua | 16 +++++++++++----- blog/view/detail.ls | 10 ++++++---- blog/view/entries.ls | 12 +++++++----- 7 files changed, 35 insertions(+), 45 deletions(-) delete mode 100644 blog/index.ls create mode 100644 blog/index.lua diff --git a/blog/Makefile b/blog/Makefile index 7249a7a..65a463c 100644 --- a/blog/Makefile +++ b/blog/Makefile @@ -1,6 +1,6 @@ BUILDDIR = ../build/blog -copyfiles = *.html assets *.lua *.ls view +copyfiles = *.html assets *.lua view main: - mkdir $(BUILDDIR) diff --git a/blog/index.ls b/blog/index.ls deleted file mode 100644 index 7e7b26b..0000000 --- a/blog/index.ls +++ /dev/null @@ -1,28 +0,0 @@ - \ No newline at end of file diff --git a/blog/index.lua b/blog/index.lua new file mode 100644 index 0000000..6241237 --- /dev/null +++ b/blog/index.lua @@ -0,0 +1 @@ +require("blog.router") \ No newline at end of file diff --git a/blog/router.lua b/blog/router.lua index 4ff5f31..ca29384 100644 --- a/blog/router.lua +++ b/blog/router.lua @@ -2,7 +2,6 @@ BLOG_ROOT = __ROOT__.."/blog" MAX_ENTRY = 10 local user = "mrsang" local handle = function(p) - std.html() local args = {} local sort = {} local i = 1 @@ -28,4 +27,12 @@ local handle = function(p) end end end -return handle \ No newline at end of file +std.html() +local action = REQUEST.query.action +if not action then action = "r:top:10" end +local r, s = action:find("^r:") +if r then + handle(action:sub(s+1)) +else + echo("Unknow action "..action) +end diff --git a/blog/view.lua b/blog/view.lua index 1ccecad..60c0ea6 100644 --- a/blog/view.lua +++ b/blog/view.lua @@ -2,10 +2,8 @@ local view = {} view.html = function(name) local path = BLOG_ROOT.."/view/"..name..".html" - local f = io.open(path, "rb") - if f then - echo(f:read("*all")) - f:close() + if unix.exists(path) then + std.f(path) else echo("Cannot find "..path) end @@ -17,12 +15,20 @@ view.render = function(action, data, sort) local fn = nil local e if action == "id" then + --echo(bytes.__tostring(std.b64decode(data[0].rendered)):gsub("%%","%%%%")) + --return true fn, e = loadscript(path.."/detail.ls") + --echo(data[0].rendered) + --fn = require("blog.view.compiledd") else + --fn = require("blog.view.compiledd") fn, e = loadscript(path.."/entries.ls") end if fn then - fn(data, sort) + local r,o = pcall(fn, data, sort) + if not r then + echo(o) + end else echo(e) end diff --git a/blog/view/detail.ls b/blog/view/detail.ls index 5c7d767..4390ffa 100644 --- a/blog/view/detail.ls +++ b/blog/view/detail.ls @@ -27,9 +27,11 @@ local i = 1 for tag in data.tags:gmatch(",*([^,]+)") do tag = std.trim(tag, " ") - local b64tag = std.b64encode(tag) - atags[i] = ''..tag.."" - i = i+ 1 + if tag ~= "" then + local b64tag = std.b64encode(tag) + atags[i] = ''..tag.."" + i = i+ 1 + end end echo(table.concat(atags, ", ")) ?> @@ -40,7 +42,7 @@
") if r then echo(content:sub(0,r-1)) diff --git a/blog/view/entries.ls b/blog/view/entries.ls index daaa915..484e002 100644 --- a/blog/view/entries.ls +++ b/blog/view/entries.ls @@ -27,9 +27,11 @@ local i = 1 for tag in data.tags:gmatch(",*([^,]+)") do tag = std.trim(tag, " ") - local b64tag = std.b64encode(tag) - atags[i] = ''..tag.."" - i = i+ 1 + if tag ~= "" then + local b64tag = std.b64encode(tag) + atags[i] = ''..tag.."" + i = i+ 1 + end end echo(table.concat(atags, ", ")) ?> @@ -39,7 +41,7 @@
)") if r then @@ -50,7 +52,7 @@
- '?> + '?>