Linkify hashtags

Fixes #34
This commit is contained in:
Zed 2019-09-19 05:22:45 +02:00
parent 70c42dd05a
commit 9917a69dc5

View file

@ -1,4 +1,4 @@
import strutils, strformat, htmlgen, xmltree, times import strutils, strformat, sequtils, htmlgen, xmltree, times, uri
import regex import regex
import types, utils import types, utils
@ -11,6 +11,7 @@ const
usernameRegex = re"(^|[^A-z0-9_?])@([A-z0-9_]+)" usernameRegex = re"(^|[^A-z0-9_?])@([A-z0-9_]+)"
picRegex = re"pic.twitter.com/[^ ]+" picRegex = re"pic.twitter.com/[^ ]+"
ellipsisRegex = re" ?…" ellipsisRegex = re" ?…"
hashtagRegex = re"([^\S])?([#$][A-z0-9]+)"
ytRegex = re"(www.|m.)?youtu(be.com|.be)" ytRegex = re"(www.|m.)?youtu(be.com|.be)"
twRegex = re"(www.|mobile.)?twitter.com" twRegex = re"(www.|mobile.)?twitter.com"
nbsp = $Rune(0x000A0) nbsp = $Rune(0x000A0)
@ -40,6 +41,15 @@ proc reEmailToLink*(m: RegexMatch; s: string): string =
let url = s[m.group(0)[0]] let url = s[m.group(0)[0]]
toLink("mailto://" & url, url) toLink("mailto://" & url, url)
proc reHashtagToLink*(m: RegexMatch; s: string): string =
result = if m.group(0).len > 0: s[m.group(0)[0]] else: ""
let hash = s[m.group(1)[0]]
let link = toLink("/search?text=" & encodeUrl(hash), hash)
if hash.any(isAlphaAscii):
result &= link
else:
result &= hash
proc reUsernameToLink*(m: RegexMatch; s: string): string = proc reUsernameToLink*(m: RegexMatch; s: string): string =
var username = "" var username = ""
var pretext = "" var pretext = ""
@ -69,6 +79,7 @@ proc linkifyText*(text: string; prefs: Prefs; rss=false): string =
result = xmltree.escape(stripText(text)) result = xmltree.escape(stripText(text))
result = result.replace(ellipsisRegex, "") result = result.replace(ellipsisRegex, "")
result = result.replace(emailRegex, reEmailToLink) result = result.replace(emailRegex, reEmailToLink)
result = result.replace(hashtagRegex, reHashtagToLink)
if rss: if rss:
result = result.replace(urlRegex, reUrlToLink) result = result.replace(urlRegex, reUrlToLink)
result = result.replace(usernameRegex, reUsernameToFullLink) result = result.replace(usernameRegex, reUsernameToFullLink)