From 62f9816c5a6335a21d9307e2eadfad71effd3f97 Mon Sep 17 00:00:00 2001 From: Wesley Kerfoot Date: Sun, 2 Feb 2020 00:55:14 -0500 Subject: [PATCH] use karax --- src/twit2blogpkg/server.nim | 48 ++++++++-------------------------- src/twit2blogpkg/templates.nim | 41 +++++++++++++++++++++++++++++ src/twit2blogpkg/twitter.nim | 3 +-- twit2blog.nimble | 6 ++--- 4 files changed, 55 insertions(+), 43 deletions(-) create mode 100644 src/twit2blogpkg/templates.nim diff --git a/src/twit2blogpkg/server.nim b/src/twit2blogpkg/server.nim index a51148f..2454248 100644 --- a/src/twit2blogpkg/server.nim +++ b/src/twit2blogpkg/server.nim @@ -1,14 +1,8 @@ -import strutils, options, sugar, sequtils, asyncdispatch, threadpool, db_sqlite, strformat +import strutils, options, sugar, sequtils, asyncdispatch, threadpool, db_sqlite import twitter +import templates import jester -from htmlgen import nil - -# one thread just receives messages with thread ID / username -# thread then passes messages to worker threads in round-robin fashion -# worker threads gather thread contents, then update Redis DB (or sqlite) with thread ID mapped to content -# user can go back to page with thread ID / user combo (or unique ID we give them?) and see compiled thread - type ThreadRequest = object tweetID: string @@ -66,46 +60,26 @@ router twitblog: let thread = threadExists(tweetID, author) if thread.isSome: - let title = fmt"Thread by {author}" + # Lists all the tweets in a thread let tweets = thread.get.tweets.split("\n") - resp htmlgen.body( - htmlgen.a(href=fmt"/author/{author}/threads", fmt"See all of {author}'s threads"), - htmlgen.h4(title), - htmlgen.ul(tweets.map((t) => htmlgen.li(t)).join("")) - ) + resp renderThread(author, thread.get.tweets.split("\n")) else: + # Send it off to the rendering thread for processing + # Let them know to check back later chan.send(ThreadRequest(tweetID: tweetID, author: author)) - resp htmlgen.h4("Check back later") + resp checkBack() get "/": - # lists all authors + # Lists all authors let authors = allAuthors.toSeq let title = "Authors" - resp htmlgen.body( - htmlgen.h4(title), - htmlgen.ul( - authors.map((author) => - htmlgen.li( - htmlgen.a(href=fmt"/author/{author}/threads", author) - ) - ).join("") - ) - ) + resp authors.listAuthors get "/author/@author/threads": + # Lists all threads by an author let author = @"author" - let title = fmt"Threads for {author}" let threads = toSeq(threadIDs(author)) - resp htmlgen.body( - htmlgen.h4(title), - htmlgen.ul( - threads.map((thread) => - htmlgen.li( - htmlgen.a(href=fmt"/thread/{author}/status/{thread}", thread) - ) - ).join("") - ) - ) + resp author.listThreads(threads) proc startServer* = createTweetTable() diff --git a/src/twit2blogpkg/templates.nim b/src/twit2blogpkg/templates.nim new file mode 100644 index 0000000..959dfa9 --- /dev/null +++ b/src/twit2blogpkg/templates.nim @@ -0,0 +1,41 @@ +import strformat +import karax / [karaxdsl, vdom] + +proc renderThread*(author : string, + tweets : seq[string]): string = + + let title = fmt"Thread by {author}" + let vnode = buildHtml(tdiv(class = "mt-3")): + h4: text title + ul: + li: a(href="/"): text "Main Page" + li: a(href=fmt"/author/{author}/threads"): text (fmt"See all of {author}'s threads") + ul: + for tweet in tweets: + li: text tweet + result = $vnode + +proc checkBack*() : string = + let vnode = buildHtml(tdiv(class = "mt-3")): + h4: text "Check back later please" + result = $vnode + +proc listAuthors*(authors : seq[string]) : string = + let title = "Authors" + let vnode = buildHtml(tdiv(class = "mt-3")): + h4: text title + ul: + for author in authors: + li: a(href = fmt"/author/{author}/threads"): text author + result = $vnode + +proc listThreads*(author : string, + threads : seq[string]) : string = + let title = fmt"Threads for {author}" + let vnode = buildHtml(tdiv(class = "mt-3")): + a(href="/"): text "Main Page" + h4: text title + ul: + for thread in threads: + li: a(href = fmt"/thread/{author}/status/{thread}"): text thread + result = $vnode diff --git a/src/twit2blogpkg/twitter.nim b/src/twit2blogpkg/twitter.nim index ffe5dea..da51b05 100644 --- a/src/twit2blogpkg/twitter.nim +++ b/src/twit2blogpkg/twitter.nim @@ -1,5 +1,4 @@ import httpClient, base64, uri, json, os, strformat, sequtils, strutils, options -from htmlgen import nil from xmltree import escape proc buildAuthHeader() : string = @@ -78,7 +77,7 @@ proc convertWords(tweet : string) : string = let path = parsedUri.path if (scheme.len > 0 and hostname.len > 0): let url = xmltree.escape(fmt"{scheme}://{hostname}{path}") - stripped &= htmlgen.a(href=url, url) + stripped &= url elif word.len > 0 and word[0] != '@': stripped &= xmltree.escape(word) else: diff --git a/twit2blog.nimble b/twit2blog.nimble index 2282f97..205c31b 100644 --- a/twit2blog.nimble +++ b/twit2blog.nimble @@ -8,9 +8,7 @@ srcDir = "src" installExt = @["nim"] bin = @["twit2blog"] - - # Dependencies - -requires "nim >= 1.0.9", "regex" +requires "nim >= 1.0.9" requires "https://github.com/dom96/jester" +requires "https://github.com/pragmagic/karax"