more work went into this

This commit is contained in:
Christine Dodrill 2016-02-05 21:02:12 -08:00
parent e5432053ff
commit f4165c96dc
6 changed files with 81 additions and 68 deletions

View File

@ -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()

View File

@ -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 }}

View File

@ -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>

View File

@ -1,6 +1,6 @@
<!doctype html>
<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>
<h1>{{# title }}{{ title }}{{/ title }}</h1>
<div class=metanav>

View File

@ -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>