import strutils, strformat, htmlgen, xmltree, times
import regex
import types, utils
from unicode import Rune, `$`
const
urlRegex = re"((https?|ftp)://(-\.)?([^\s/?\.#]+\.?)+([/\?][^\s\)]*)?)"
emailRegex = re"([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)"
usernameRegex = re"(^|[^A-z0-9_?])@([A-z0-9_]+)"
picRegex = re"pic.twitter.com/[^ ]+"
ellipsisRegex = re" ?…"
nbsp = $Rune(0x000A0)
proc stripText*(text: string): string =
text.replace(nbsp, " ").strip()
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)
proc reUrlToLink*(m: RegexMatch; s: string): string =
let url = s[m.group(0)[0]]
toLink(url, shortLink(url))
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 =
var username = ""
var pretext = ""
let pre = m.group(0)
let match = m.group(1)
username = s[match[0]]
if pre.len > 0:
pretext = s[pre[0]]
pretext & toLink("/" & username, "@" & username)
proc linkifyText*(text: string): string =
result = xmltree.escape(stripText(text))
result = result.replace(ellipsisRegex, "")
result = result.replace(emailRegex, reEmailToLink)
result = result.replace(urlRegex, reUrlToLink)
result = result.replace(usernameRegex, reUsernameToLink)
result = result.replace(re"([^\s\(\n%])\s+([;.,!\)'%]|')", "$1")
result = result.replace(re"^\.