show individual quotes

This commit is contained in:
Christine Dodrill 2016-02-05 18:43:30 -08:00
parent cf933531b1
commit 23c80f907c
3 changed files with 47 additions and 8 deletions

View File

@ -1,10 +1,11 @@
import asyncdispatch, db_sqlite, jester, moustachu, os,
shorturl, strutils, tables, times
shorturl, strutils, tables, times, typetraits
const
baseTemplate*: string = staticRead "./templates/layout.mustache"
indexTemplate*: string = staticRead "./templates/index.mustache"
errorTemplate*: string = staticRead "./templates/error.mustache"
quoteTemplate*: string = staticRead "./templates/quote.mustache"
testTemplate*: string = """<p>hi!</p>"""
@ -37,7 +38,14 @@ template renderMustache*(title: string, templ: string, ctx: Context): expr =
template fail*(): expr =
var ctx = newContext()
ctx["exception"] = getCurrentExceptionMsg()
renderMustache("error", errorTemplate, ctx)
var
layoutCtx = moustachu.newContext()
layoutCtx["title"] = title
layoutCtx["body"] = render(templ, ctx)
halt render(baseTemplate, layoutCtx)
settings:
port = 5000.Port
@ -57,6 +65,7 @@ routes:
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-")
@ -72,16 +81,34 @@ routes:
renderMustache("most recent quotes", indexTemplate, ctx)
get "/quotes/@id":
var qid: int = -1
var qid: int = 0
try:
qid = (@"id").decodeURLSimple()
qid = (@"id").parseInt
except:
fail()
qid = (@"id").decodeURLSimple()
redirect "/quotes/" & $qid
let quote = db.getRow(sql"SELECT * FROM quotes WHERE id = ?", qid)
echo quote
if quote[1] == "":
halt Http404, "no such quote"
resp $quote
var
ctx = newContext()
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])
ctx["last"] = qid - 1
ctx["next"] = qid + 1
renderMustache("quote #" & $qid & " by " & quote[2], quoteTemplate, ctx)
runForever()

View File

@ -1,6 +1,6 @@
{{# quotes }}
<div class="entries">
<h3><a href="/channel/{{ channelsafe }}">{{channel}}</a> - {{time}}</h3>
<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 />

View File

@ -0,0 +1,12 @@
<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>