2019-06-25 02:52:38 +00:00
|
|
|
import strutils, strformat, htmlgen, xmltree, times
|
2019-06-20 14:16:20 +00:00
|
|
|
import regex
|
|
|
|
|
|
|
|
import ./types, ./utils
|
|
|
|
|
2019-06-25 00:38:18 +00:00
|
|
|
from unicode import Rune, `$`
|
|
|
|
|
2019-06-20 14:16:20 +00:00
|
|
|
const
|
2019-06-25 03:32:25 +00:00
|
|
|
urlRegex = re"((https?|ftp)://(-\.)?([^\s/?\.#]+\.?)+(/[^\s\)]*)?)"
|
2019-06-20 14:16:20 +00:00
|
|
|
emailRegex = re"([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)"
|
2019-06-25 03:32:25 +00:00
|
|
|
usernameRegex = re"(^|[^\S]|\.|>)@([A-z0-9_]+)"
|
2019-06-20 14:16:20 +00:00
|
|
|
picRegex = re"pic.twitter.com/[^ ]+"
|
|
|
|
cardRegex = re"(https?://)?cards.twitter.com/[^ ]+"
|
|
|
|
ellipsisRegex = re" ?…"
|
2019-06-25 00:38:18 +00:00
|
|
|
nbsp = $Rune(0x000A0)
|
|
|
|
|
|
|
|
proc stripText*(text: string): string =
|
|
|
|
text.replace(nbsp, " ").strip()
|
2019-06-20 14:16:20 +00:00
|
|
|
|
|
|
|
proc shortLink*(text: string; length=28): string =
|
|
|
|
result = text.replace(re"https?://(www.)?", "")
|
|
|
|
if result.len > length:
|
|
|
|
result = result[0 ..< length] & "…"
|
|
|
|
|
|
|
|
proc toLink*(url, text: string; class="timeline-link"): string =
|
2019-06-24 18:45:03 +00:00
|
|
|
a(text, class=class, href=url)
|
2019-06-20 14:16:20 +00:00
|
|
|
|
|
|
|
proc reUrlToLink*(m: RegexMatch; s: string): string =
|
|
|
|
let url = s[m.group(0)[0]]
|
2019-06-24 06:07:36 +00:00
|
|
|
toLink(url, shortLink(url))
|
2019-06-20 14:16:20 +00:00
|
|
|
|
|
|
|
proc reEmailToLink*(m: RegexMatch; s: string): string =
|
|
|
|
let url = s[m.group(0)[0]]
|
|
|
|
toLink("mailto://" & url, url)
|
|
|
|
|
|
|
|
proc reUsernameToLink*(m: RegexMatch; s: string): string =
|
2019-06-25 01:48:57 +00:00
|
|
|
var username = ""
|
|
|
|
var pretext = ""
|
2019-06-20 14:16:20 +00:00
|
|
|
|
2019-06-25 01:48:57 +00:00
|
|
|
let pre = m.group(0)
|
|
|
|
let match = m.group(1)
|
2019-06-20 14:16:20 +00:00
|
|
|
|
|
|
|
username = s[match[0]]
|
|
|
|
|
|
|
|
if pre.len > 0:
|
|
|
|
pretext = s[pre[0]]
|
|
|
|
|
|
|
|
pretext & toLink("/" & username, "@" & username)
|
|
|
|
|
|
|
|
proc linkifyText*(text: string): string =
|
2019-06-25 02:52:38 +00:00
|
|
|
result = xmltree.escape(stripText(text))
|
2019-06-20 14:16:20 +00:00
|
|
|
result = result.replace(ellipsisRegex, "")
|
|
|
|
result = result.replace(emailRegex, reEmailToLink)
|
|
|
|
result = result.replace(urlRegex, reUrlToLink)
|
2019-06-25 02:52:38 +00:00
|
|
|
result = result.replace(usernameRegex, reUsernameToLink)
|
2019-06-25 03:32:25 +00:00
|
|
|
result = result.replace(re"([^\s\(\n%])<a", "$1 <a")
|
|
|
|
result = result.replace(re"</a>\s+([;.,!\)'%]|')", "</a>$1")
|
2019-06-20 14:16:20 +00:00
|
|
|
|
|
|
|
proc stripTwitterUrls*(text: string): string =
|
|
|
|
result = text
|
|
|
|
result = result.replace(picRegex, "")
|
|
|
|
result = result.replace(cardRegex, "")
|
|
|
|
result = result.replace(ellipsisRegex, "")
|
|
|
|
|
|
|
|
proc getUserpic*(userpic: string; style=""): string =
|
|
|
|
let pic = userpic.replace(re"_(normal|bigger|mini|200x200)(\.[A-z]+)$", "$2")
|
|
|
|
pic.replace(re"(\.[A-z]+)$", style & "$1")
|
|
|
|
|
|
|
|
proc getUserpic*(profile: Profile; style=""): string =
|
|
|
|
getUserPic(profile.userpic, style)
|
|
|
|
|
2019-06-25 01:48:57 +00:00
|
|
|
proc genImg*(url: string; class=""): string =
|
|
|
|
result = img(src = url.getSigUrl("pic"), class = class, alt = "Image")
|
|
|
|
|
2019-06-24 18:45:03 +00:00
|
|
|
proc linkUser*(profile: Profile; class=""): string =
|
|
|
|
let
|
|
|
|
username = "username" in class
|
|
|
|
href = &"/{profile.username}"
|
|
|
|
text = if username: "@" & profile.username
|
|
|
|
else: xmltree.escape(profile.fullname)
|
2019-06-20 14:16:20 +00:00
|
|
|
|
2019-06-24 18:45:03 +00:00
|
|
|
result = a(text, href = href, class = class, title = text)
|
2019-06-20 14:16:20 +00:00
|
|
|
|
2019-06-24 18:45:03 +00:00
|
|
|
if not username and profile.verified:
|
2019-06-24 20:54:56 +00:00
|
|
|
result &= span("✔", class="icon verified-icon", title="Verified account")
|
2019-06-24 18:45:03 +00:00
|
|
|
if not username and profile.protected:
|
2019-06-24 20:54:56 +00:00
|
|
|
result &= span("🔒", class="icon protected-icon", title="Protected account")
|
2019-06-24 20:40:48 +00:00
|
|
|
|
|
|
|
proc pageTitle*(profile: Profile): string =
|
|
|
|
&"{profile.fullname} (@{profile.username}) | Nitter"
|
|
|
|
|
|
|
|
proc pageTitle*(page: string): string =
|
|
|
|
&"{page} | Nitter"
|
2019-06-25 02:52:38 +00:00
|
|
|
|
|
|
|
proc getTime*(tweet: Tweet): string =
|
|
|
|
tweet.time.format("d/M/yyyy', ' HH:mm:ss")
|