nitter/src/formatters.nim

104 lines
3.2 KiB
Nim
Raw Normal View History

2019-06-25 04:52:38 +02:00
import strutils, strformat, htmlgen, xmltree, times
2019-06-20 16:16:20 +02:00
import regex
2019-08-11 21:26:37 +02:00
import types
2019-06-20 16:16:20 +02:00
2019-06-25 02:38:18 +02:00
from unicode import Rune, `$`
2019-06-20 16:16:20 +02:00
const
2019-06-27 22:30:00 +02:00
urlRegex = re"((https?|ftp)://(-\.)?([^\s/?\.#]+\.?)+([/\?][^\s\)]*)?)"
2019-06-20 16:16:20 +02:00
emailRegex = re"([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)"
2019-07-03 12:20:23 +02:00
usernameRegex = re"(^|[^A-z0-9_?])@([A-z0-9_]+)"
2019-06-20 16:16:20 +02:00
picRegex = re"pic.twitter.com/[^ ]+"
ellipsisRegex = re" ?…"
ytRegex = re"youtu(be.com|.be)"
twRegex = re"twitter.com"
2019-06-25 02:38:18 +02:00
nbsp = $Rune(0x000A0)
proc stripText*(text: string): string =
text.replace(nbsp, " ").strip()
2019-06-20 16:16:20 +02: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 =
a(text, class=class, href=url)
2019-06-20 16:16:20 +02:00
proc reUrlToLink*(m: RegexMatch; s: string): string =
let url = s[m.group(0)[0]]
2019-06-24 08:07:36 +02:00
toLink(url, shortLink(url))
2019-06-20 16:16:20 +02: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 03:48:57 +02:00
var username = ""
var pretext = ""
2019-06-20 16:16:20 +02:00
2019-06-25 03:48:57 +02:00
let pre = m.group(0)
let match = m.group(1)
2019-06-20 16:16:20 +02:00
username = s[match[0]]
if pre.len > 0:
pretext = s[pre[0]]
pretext & toLink("/" & username, "@" & username)
proc linkifyText*(text: string; prefs: Prefs): string =
2019-06-25 04:52:38 +02:00
result = xmltree.escape(stripText(text))
2019-06-20 16:16:20 +02:00
result = result.replace(ellipsisRegex, "")
result = result.replace(emailRegex, reEmailToLink)
result = result.replace(urlRegex, reUrlToLink)
2019-06-25 04:52:38 +02:00
result = result.replace(usernameRegex, reUsernameToLink)
2019-06-25 05:32:25 +02:00
result = result.replace(re"([^\s\(\n%])<a", "$1 <a")
result = result.replace(re"</a>\s+([;.,!\)'%]|&apos;)", "</a>$1")
2019-07-03 12:20:23 +02:00
result = result.replace(re"^\. <a", ".<a")
if prefs.replaceYouTube.len > 0:
result = result.replace(ytRegex, prefs.replaceYouTube)
if prefs.replaceTwitter.len > 0:
result = result.replace(twRegex, prefs.replaceTwitter)
proc replaceUrl*(url: string; prefs: Prefs): string =
2019-08-15 18:45:56 +02:00
result = url
if prefs.replaceYouTube.len > 0:
2019-08-15 18:45:56 +02:00
result = url.replace(ytRegex, prefs.replaceYouTube)
if prefs.replaceTwitter.len > 0:
2019-08-15 18:45:56 +02:00
result = url.replace(twRegex, prefs.replaceTwitter)
2019-06-20 16:16:20 +02:00
proc stripTwitterUrls*(text: string): string =
result = text
result = result.replace(picRegex, "")
result = result.replace(ellipsisRegex, "")
proc getUserpic*(userpic: string; style=""): string =
let pic = userpic.replace(re"_(normal|bigger|mini|200x200|400x400)(\.[A-z]+)$", "$2")
2019-06-20 16:16:20 +02:00
pic.replace(re"(\.[A-z]+)$", style & "$1")
proc getUserpic*(profile: Profile; style=""): string =
getUserPic(profile.userpic, style)
2019-08-07 22:27:24 +02:00
proc getVideoEmbed*(id: string): string =
&"https://twitter.com/i/videos/{id}?embed_source=facebook"
2019-08-07 22:02:19 +02:00
2019-06-24 22:40:48 +02:00
proc pageTitle*(profile: Profile): string =
2019-07-31 02:15:43 +02:00
&"{profile.fullname} (@{profile.username})"
2019-06-25 04:52:38 +02:00
2019-08-07 22:02:19 +02:00
proc pageDesc*(profile: Profile): string =
"The latest tweets from " & profile.fullname
proc getJoinDate*(profile: Profile): string =
profile.joinDate.format("'Joined' MMMM YYYY")
proc getJoinDateFull*(profile: Profile): string =
profile.joinDate.format("h:mm tt - d MMM YYYY")
2019-06-25 04:52:38 +02:00
proc getTime*(tweet: Tweet): string =
tweet.time.format("d/M/yyyy', ' HH:mm:ss")
2019-07-01 23:14:36 +02:00
proc getLink*(tweet: Tweet | Quote): string =
2019-07-01 23:55:19 +02:00
&"/{tweet.profile.username}/status/{tweet.id}"