Browse Source

use karax

master
Wesley Kerfoot 4 years ago
parent
commit
62f9816c5a
  1. 48
      src/twit2blogpkg/server.nim
  2. 41
      src/twit2blogpkg/templates.nim
  3. 3
      src/twit2blogpkg/twitter.nim
  4. 6
      twit2blog.nimble

48
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 twitter
import templates
import jester 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 type
ThreadRequest = object ThreadRequest = object
tweetID: string tweetID: string
@ -66,46 +60,26 @@ router twitblog:
let thread = threadExists(tweetID, author) let thread = threadExists(tweetID, author)
if thread.isSome: if thread.isSome:
let title = fmt"Thread by {author}" # Lists all the tweets in a thread
let tweets = thread.get.tweets.split("\n") let tweets = thread.get.tweets.split("\n")
resp htmlgen.body( resp renderThread(author, thread.get.tweets.split("\n"))
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(""))
)
else: else:
# Send it off to the rendering thread for processing
# Let them know to check back later
chan.send(ThreadRequest(tweetID: tweetID, author: author)) chan.send(ThreadRequest(tweetID: tweetID, author: author))
resp htmlgen.h4("Check back later") resp checkBack()
get "/": get "/":
# lists all authors # Lists all authors
let authors = allAuthors.toSeq let authors = allAuthors.toSeq
let title = "Authors" let title = "Authors"
resp htmlgen.body( resp authors.listAuthors
htmlgen.h4(title),
htmlgen.ul(
authors.map((author) =>
htmlgen.li(
htmlgen.a(href=fmt"/author/{author}/threads", author)
)
).join("")
)
)
get "/author/@author/threads": get "/author/@author/threads":
# Lists all threads by an author
let author = @"author" let author = @"author"
let title = fmt"Threads for {author}"
let threads = toSeq(threadIDs(author)) let threads = toSeq(threadIDs(author))
resp htmlgen.body( resp author.listThreads(threads)
htmlgen.h4(title),
htmlgen.ul(
threads.map((thread) =>
htmlgen.li(
htmlgen.a(href=fmt"/thread/{author}/status/{thread}", thread)
)
).join("")
)
)
proc startServer* = proc startServer* =
createTweetTable() createTweetTable()

41
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

3
src/twit2blogpkg/twitter.nim

@ -1,5 +1,4 @@
import httpClient, base64, uri, json, os, strformat, sequtils, strutils, options import httpClient, base64, uri, json, os, strformat, sequtils, strutils, options
from htmlgen import nil
from xmltree import escape from xmltree import escape
proc buildAuthHeader() : string = proc buildAuthHeader() : string =
@ -78,7 +77,7 @@ proc convertWords(tweet : string) : string =
let path = parsedUri.path let path = parsedUri.path
if (scheme.len > 0 and hostname.len > 0): if (scheme.len > 0 and hostname.len > 0):
let url = xmltree.escape(fmt"{scheme}://{hostname}{path}") let url = xmltree.escape(fmt"{scheme}://{hostname}{path}")
stripped &= htmlgen.a(href=url, url) stripped &= url
elif word.len > 0 and word[0] != '@': elif word.len > 0 and word[0] != '@':
stripped &= xmltree.escape(word) stripped &= xmltree.escape(word)
else: else:

6
twit2blog.nimble

@ -8,9 +8,7 @@ srcDir = "src"
installExt = @["nim"] installExt = @["nim"]
bin = @["twit2blog"] bin = @["twit2blog"]
# Dependencies # Dependencies
requires "nim >= 1.0.9"
requires "nim >= 1.0.9", "regex"
requires "https://github.com/dom96/jester" requires "https://github.com/dom96/jester"
requires "https://github.com/pragmagic/karax"

Loading…
Cancel
Save