Make links clickable as well as @usernames

Turns:

    @reednj Yeah I was gonna work on that today http://foo.bar <b>foo</b>

into:

    <a href="/users/reednj/0">@reednj</a> Yeah I was gonna work on that
    today <a href="http://foo.bar">http://foo.bar</a>
    &lt;b&gt;foo&lt;/b&gt;
This commit is contained in:
Christine Dodrill 2016-02-10 02:45:09 -08:00
parent de62f04bf4
commit edcddaedd8
4 changed files with 38 additions and 5 deletions

View File

@ -7,7 +7,7 @@
{{# tweets }}
<div class="entries">
<a href="/users/{{ username }}/0">@{{ username }}</a> - {{ time }}
<p>{{ tweet }}</p>
<p>{{{ tweet }}}</p>
<small><a href="/tweet/{{ permalink }}">permalink</a></small>
</div>
<br />

1
src/tweet/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
parse

32
src/tweet/parse.nim Normal file
View File

@ -0,0 +1,32 @@
import strutils, ../twtxt
from htmlgen import nil
type
TweetData* = seq[string]
proc `$`*(t: TweetData): string = t.join ""
proc prettyfyTweet*(t: string): TweetData =
result = newSeq[string]()
var tweet = t.replace("<", "&lt;").replace(">", "&gt;")
for word in tokenize(tweet):
if word.isSep:
result.add word.token
else:
if word.token.startsWith "@":
let
username = word.token.substr 1
ahref = htmlgen.a(href="/users/" & username & "/0", word.token)
result.add ahref
elif word.token.startsWith "http":
let ahref = htmlgen.a(href=word.token, word.token)
result.add ahref
else:
result.add word.token
when isMainModule:
const inp = "@reednj Yeah I was gonna work on that today http://foo.bar <b>foo</b>"
echo inp.prettyfyTweet

View File

@ -1,5 +1,5 @@
import asyncdispatch, db_sqlite, httpclient, jester, moustachu, os, shorturl,
strutils, tables, templates/all, times, twtxt, typetraits
strutils, tables, templates/all, times, tweet/parse, twtxt, typetraits
let
db = open("data/twtxt.db", nil, nil, nil)
@ -142,7 +142,7 @@ routes:
c["permalink"] = tweet[0].parseInt().encodeURLSimple()
c["username"] = tweet[1]
c["time"] = time
c["tweet"] = tweet[3]
c["tweet"] = $tweet[3].prettyfyTweet()
tweetList.add c
@ -177,7 +177,7 @@ routes:
c["permalink"] = tweet[0].parseInt().encodeURLSimple()
c["username"] = tweet[1]
c["time"] = time
c["tweet"] = tweet[3]
c["tweet"] = $tweet[3].prettyfyTweet()
tweetList.add c
@ -213,7 +213,7 @@ routes:
c["permalink"] = tweet[0].parseInt().encodeURLSimple()
c["username"] = tweet[1]
c["time"] = time
c["tweet"] = tweet[3]
c["tweet"] = $tweet[3].prettyfyTweet()
tweetList.add c