diff --git a/public/css/style.css b/public/style.css similarity index 100% rename from public/css/style.css rename to public/style.css diff --git a/src/quotesite.nim b/src/quotesite.nim index 8ac54ce..ede9187 100644 --- a/src/quotesite.nim +++ b/src/quotesite.nim @@ -4,6 +4,7 @@ import asyncdispatch, db_sqlite, jester, moustachu, os, const baseTemplate*: string = staticRead "./templates/layout.mustache" indexTemplate*: string = staticRead "./templates/index.mustache" + browseTemplate*: string = staticRead "./templates/browse.mustache" errorTemplate*: string = staticRead "./templates/error.mustache" quoteTemplate*: string = staticRead "./templates/quote.mustache" @@ -42,11 +43,29 @@ template fail*(): expr = var layoutCtx = moustachu.newContext() - layoutCtx["title"] = title - layoutCtx["body"] = render(templ, ctx) + layoutCtx["title"] = "fail" + layoutCtx["body"] = render(errorTemplate, ctx) halt render(baseTemplate, layoutCtx) +template contextQuote(ctx: Context, qid: int, quote: Row): expr = + ctx["listid"] = qid + ctx["id"] = quote[0].parseInt().encodeURLSimple() + ctx["channel"] = quote[1] + ctx["channelsafe"] = quote[1].replace("#", "hashtag-") + ctx["nick"] = quote[2] + ctx["adder"] = quote[3] + ctx["message"] = quote[4] + ctx["time"] = parseInt(split(quote[5], '.')[0]) + +template pagination(ctx: Context, qid: int, kind: string): expr = + ctx["paging"] = true + if qid != 0: + ctx["isnt1"] = true + ctx["prev"] = qid - 1 + ctx["next"] = qid + 1 + ctx["kind"] = kind + settings: port = 5000.Port bindAddr = "0.0.0.0" @@ -56,59 +75,61 @@ routes: renderMustache("test", testTemplate, newContext()) get "/": - let quotes = db.getAllRows(sql"SELECT * FROM quotes ORDER BY time desc LIMIT 20") + redirect "/browse/0" + get "/channel": + try: + var + channels = db.getAllRows(sql"select channel from quotes group by channel") + ctx: Context = newContext() + + renderMustache("channel list", testTemplate, ctx) + except: + fail() + + get "/@kind/@id": var - ctx = newContext() - quoteSeq = newSeq[Context]() - - for quote in items(quotes): - var c = newContext() - - c["listid"] = quote[0] - c["id"] = quote[0].parseInt().encodeURLSimple() - c["channel"] = quote[1] - c["channelsafe"] = quote[1].replace("#", "hashtag-") - c["nick"] = quote[2] - c["adder"] = quote[3] - c["message"] = quote[4] - c["time"] = parseInt(split(quote[5], '.')[0]) - - quoteSeq.add c - - ctx["quotes"] = quoteSeq - - renderMustache("most recent quotes", indexTemplate, ctx) - - get "/quotes/@id": - var qid: int = 0 + title: string = "" + ctx: Context + rows: seq[Row] + qid: int = 0 try: qid = (@"id").parseInt except: qid = (@"id").decodeURLSimple() - redirect "/quotes/" & $qid + redirect "/" & @"kind" & "/" & $qid - let quote = db.getRow(sql"SELECT * FROM quotes WHERE id = ?", qid) + case @"kind": + of "browse": + rows = db.getAllRows(sql"SELECT * FROM quotes ORDER BY time desc LIMIT 20 OFFSET ?", (qid * 20)) - if quote[1] == "": - halt Http404, "no such quote" + title = "page " & @"id" + of "quotes": + rows = db.getAllRows(sql"SELECT * FROM quotes WHERE id = ?", qid) - var - ctx = newContext() + title = "quote #" & $qid & " by " & rows[0][2] + else: + if (@"kind").startsWith "hashtag-": + let channel = (@"kind").replace("hashtag-", "#") - ctx["listid"] = qid - ctx["id"] = quote[0].parseInt().encodeURLSimple() - ctx["channel"] = quote[1] - ctx["channelsafe"] = quote[1].replace("#", "hashtag-") - ctx["nick"] = quote[2] - ctx["adder"] = quote[3] - ctx["message"] = quote[4] - ctx["time"] = parseInt(split(quote[5], '.')[0]) + rows = db.getAllRows(sql"SELECT * FROM quotes WHERE channel=? ORDER BY time desc LIMIT 20 OFFSET ?", channel, qid * 20) - ctx["last"] = qid - 1 - ctx["next"] = qid + 1 + title = channel & " page " & @"id" + else: + halt Http404, "not found" - renderMustache("quote #" & $qid & " by " & quote[2], quoteTemplate, ctx) + ctx = newContext() + var quoteSeq = newSeq[Context]() + + for row in items(rows): + var c = newContext() + c.contextQuote row[0].parseInt(), row + quoteSeq.add c + + ctx["quotes"] = quoteSeq + ctx.pagination(qid, @"kind") + + renderMustache(title, browseTemplate, ctx) runForever() diff --git a/src/templates/browse.mustache b/src/templates/browse.mustache new file mode 100644 index 0000000..5744505 --- /dev/null +++ b/src/templates/browse.mustache @@ -0,0 +1,16 @@ +{{# quotes }} +
+{{/ quotes }} + +({{nick}}) {{message}}
- Added by {{adder}}. Permalink. -