Use regex for hashtags instead

This commit is contained in:
Zed 2020-06-06 10:17:19 +02:00
parent 181ef3bca7
commit 67313ac12a

View file

@ -6,8 +6,8 @@ const
unRegex = re"(^|[^A-z0-9-_./?])@([A-z0-9_]{1,15})" unRegex = re"(^|[^A-z0-9-_./?])@([A-z0-9_]{1,15})"
unReplace = "$1<a href=\"/$2\">@$2</a>" unReplace = "$1<a href=\"/$2\">@$2</a>"
htRegex = re"(^|[^\w-_./?])#([\w_]+)" htRegex = re"(^|[^\w-_./?])([#$])([\w_]+)"
htReplace = "$1<a href=\"/search?q=%23$2\">#$2</a>" htReplace = "$1<a href=\"/search?q=%23$3\">$2$3</a>"
template isNull*(js: JsonNode): bool = js.kind == JNull template isNull*(js: JsonNode): bool = js.kind == JNull
template notNull*(js: JsonNode): bool = js.kind != JNull template notNull*(js: JsonNode): bool = js.kind != JNull
@ -144,18 +144,6 @@ proc expandUrl(text: var string; js: JsonNode; tLen: int; hideTwitter=false) =
else: else:
text = text.replace(u, a(shortLink(url), href=url)) text = text.replace(u, a(shortLink(url), href=url))
proc expandTag(text: var string; js: JsonNode; prefix: char) =
let
tag = prefix & js{"text"}.getStr
html = a(tag, href=("/search?q=" & encodeUrl(tag)))
oldLen = text.len
text = text.replaceWord(tag, html)
# for edgecases with emojis or other characters around the tag
if text.len == oldLen:
text = text.replace(tag, html)
proc expandMention(text: var string; orig: string; js: JsonNode) = proc expandMention(text: var string; orig: string; js: JsonNode) =
let let
name = js{"name"}.getStr name = js{"name"}.getStr
@ -207,11 +195,8 @@ proc expandTweetEntities*(tweet: Tweet; js: JsonNode) =
with media, ent{"media"}: with media, ent{"media"}:
for m in media: tweet.text.expandUrl(m, slice[1], hideTwitter=true) for m in media: tweet.text.expandUrl(m, slice[1], hideTwitter=true)
with hashes, ent{"hashtags"}: if "hashtags" in ent or "symbols" in ent:
for h in hashes: tweet.text.expandTag(h, '#') tweet.text = tweet.text.replace(htRegex, htReplace)
with symbols, ent{"symbols"}:
for s in symbols: tweet.text.expandTag(s, '$')
for mention in ? ent{"user_mentions"}: for mention in ? ent{"user_mentions"}:
let let