From 67313ac12a94e13fc64d2ffe08b646eecf454ab5 Mon Sep 17 00:00:00 2001 From: Zed Date: Sat, 6 Jun 2020 10:17:19 +0200 Subject: [PATCH] Use regex for hashtags instead --- src/parserutils.nim | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/src/parserutils.nim b/src/parserutils.nim index 4069c7c..62813d2 100644 --- a/src/parserutils.nim +++ b/src/parserutils.nim @@ -6,8 +6,8 @@ const unRegex = re"(^|[^A-z0-9-_./?])@([A-z0-9_]{1,15})" unReplace = "$1@$2" - htRegex = re"(^|[^\w-_./?])#([\w_]+)" - htReplace = "$1#$2" + htRegex = re"(^|[^\w-_./?])([#$])([\w_]+)" + htReplace = "$1$2$3" template isNull*(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: 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) = let name = js{"name"}.getStr @@ -207,11 +195,8 @@ proc expandTweetEntities*(tweet: Tweet; js: JsonNode) = with media, ent{"media"}: for m in media: tweet.text.expandUrl(m, slice[1], hideTwitter=true) - with hashes, ent{"hashtags"}: - for h in hashes: tweet.text.expandTag(h, '#') - - with symbols, ent{"symbols"}: - for s in symbols: tweet.text.expandTag(s, '$') + if "hashtags" in ent or "symbols" in ent: + tweet.text = tweet.text.replace(htRegex, htReplace) for mention in ? ent{"user_mentions"}: let