more work went into this
This commit is contained in:
parent
e5432053ff
commit
f4165c96dc
|
@ -4,6 +4,7 @@ import asyncdispatch, db_sqlite, jester, moustachu, os,
|
||||||
const
|
const
|
||||||
baseTemplate*: string = staticRead "./templates/layout.mustache"
|
baseTemplate*: string = staticRead "./templates/layout.mustache"
|
||||||
indexTemplate*: string = staticRead "./templates/index.mustache"
|
indexTemplate*: string = staticRead "./templates/index.mustache"
|
||||||
|
browseTemplate*: string = staticRead "./templates/browse.mustache"
|
||||||
errorTemplate*: string = staticRead "./templates/error.mustache"
|
errorTemplate*: string = staticRead "./templates/error.mustache"
|
||||||
quoteTemplate*: string = staticRead "./templates/quote.mustache"
|
quoteTemplate*: string = staticRead "./templates/quote.mustache"
|
||||||
|
|
||||||
|
@ -42,11 +43,29 @@ template fail*(): expr =
|
||||||
var
|
var
|
||||||
layoutCtx = moustachu.newContext()
|
layoutCtx = moustachu.newContext()
|
||||||
|
|
||||||
layoutCtx["title"] = title
|
layoutCtx["title"] = "fail"
|
||||||
layoutCtx["body"] = render(templ, ctx)
|
layoutCtx["body"] = render(errorTemplate, ctx)
|
||||||
|
|
||||||
halt render(baseTemplate, layoutCtx)
|
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:
|
settings:
|
||||||
port = 5000.Port
|
port = 5000.Port
|
||||||
bindAddr = "0.0.0.0"
|
bindAddr = "0.0.0.0"
|
||||||
|
@ -56,59 +75,61 @@ routes:
|
||||||
renderMustache("test", testTemplate, newContext())
|
renderMustache("test", testTemplate, newContext())
|
||||||
|
|
||||||
get "/":
|
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
|
var
|
||||||
ctx = newContext()
|
title: string = ""
|
||||||
quoteSeq = newSeq[Context]()
|
ctx: Context
|
||||||
|
rows: seq[Row]
|
||||||
for quote in items(quotes):
|
qid: int = 0
|
||||||
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
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
qid = (@"id").parseInt
|
qid = (@"id").parseInt
|
||||||
except:
|
except:
|
||||||
qid = (@"id").decodeURLSimple()
|
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] == "":
|
title = "page " & @"id"
|
||||||
halt Http404, "no such quote"
|
of "quotes":
|
||||||
|
rows = db.getAllRows(sql"SELECT * FROM quotes WHERE id = ?", qid)
|
||||||
|
|
||||||
var
|
title = "quote #" & $qid & " by " & rows[0][2]
|
||||||
ctx = newContext()
|
else:
|
||||||
|
if (@"kind").startsWith "hashtag-":
|
||||||
|
let channel = (@"kind").replace("hashtag-", "#")
|
||||||
|
|
||||||
ctx["listid"] = qid
|
rows = db.getAllRows(sql"SELECT * FROM quotes WHERE channel=? ORDER BY time desc LIMIT 20 OFFSET ?", channel, qid * 20)
|
||||||
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])
|
|
||||||
|
|
||||||
ctx["last"] = qid - 1
|
title = channel & " page " & @"id"
|
||||||
ctx["next"] = qid + 1
|
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()
|
runForever()
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
{{# quotes }}
|
||||||
|
<div class="entries">
|
||||||
|
<h3><a href="/{{ channelsafe }}">{{channel}}</a> - #{{listid}} - {{time}}</h3>
|
||||||
|
<p>({{nick}}) {{message}}</p>
|
||||||
|
<small><i>Added by {{adder}}. <a href="/quotes/{{id}}">permalink</a></i></small>
|
||||||
|
<br />
|
||||||
|
</div>
|
||||||
|
{{/ quotes }}
|
||||||
|
|
||||||
|
<br />
|
||||||
|
|
||||||
|
{{# paging }}
|
||||||
|
<center>
|
||||||
|
{{# isnt1}}<a href="/{{ kind }}/{{ prev }}">prev</a> {{/ isnt1 }} this <a href="/{{ kind }}/{{ next }}">next</a>
|
||||||
|
</center>
|
||||||
|
{{/ paging }}
|
|
@ -1,12 +0,0 @@
|
||||||
{{# quotes }}
|
|
||||||
<div class="entries">
|
|
||||||
<h3><a href="/channel/{{ channelsafe }}">{{channel}}</a> - #{{listid}} - {{time}}</h3>
|
|
||||||
<p>({{nick}}) {{message}}</p>
|
|
||||||
<small><i>Added by {{adder}}. <a href="/quotes/{{id}}">Permalink.</a></i></small>
|
|
||||||
<br />
|
|
||||||
</div>
|
|
||||||
{{/ quotes }}
|
|
||||||
|
|
||||||
<br />
|
|
||||||
|
|
||||||
<center><a href="/browse/2">next</a></center>
|
|
|
@ -1,6 +1,6 @@
|
||||||
<!doctype html>
|
<!doctype html>
|
||||||
<title>quotes{{# title }} - {{ title }}{{/ title }}</title>
|
<title>quotes{{# title }} - {{ title }}{{/ title }}</title>
|
||||||
<link rel=stylesheet type=text/css href="/css/style.css">
|
<link rel=stylesheet type=text/css href="/style.css">
|
||||||
<div class=page>
|
<div class=page>
|
||||||
<h1>{{# title }}{{ title }}{{/ title }}</h1>
|
<h1>{{# title }}{{ title }}{{/ title }}</h1>
|
||||||
<div class=metanav>
|
<div class=metanav>
|
||||||
|
|
|
@ -1,12 +0,0 @@
|
||||||
<div class="entries">
|
|
||||||
<h3><a href="/channel/{{ channelsafe }}">{{channel}}</a> - #{{ listid }} - {{time}}</h3>
|
|
||||||
<p>({{nick}}) {{message}}</p>
|
|
||||||
<small><i>Added by {{adder}}. <a href="/quotes/{{id}}">Permalink.</a></i></small>
|
|
||||||
<br />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<br />
|
|
||||||
|
|
||||||
<center>
|
|
||||||
<p><a href="/quotes/{{ last }}">prev</a> this <a href="/quotes/{{ next }}">next</p>
|
|
||||||
</center>
|
|
Loading…
Reference in New Issue