formatters.nim 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import strutils, strformat, htmlgen, xmltree
  2. import regex
  3. import ./types, ./utils
  4. const
  5. urlRegex = re"((https?|ftp)://(-\.)?([^\s/?\.#]+\.?)+(/[^\s]*)?)"
  6. emailRegex = re"([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)"
  7. usernameRegex = re"(^|[^\S\n]|\.)@([A-z0-9_]+)"
  8. picRegex = re"pic.twitter.com/[^ ]+"
  9. cardRegex = re"(https?://)?cards.twitter.com/[^ ]+"
  10. ellipsisRegex = re" ?…"
  11. proc shortLink*(text: string; length=28): string =
  12. result = text.replace(re"https?://(www.)?", "")
  13. if result.len > length:
  14. result = result[0 ..< length] & "…"
  15. proc toLink*(url, text: string; class="timeline-link"): string =
  16. htmlgen.a(text, class=class, href=url)
  17. proc reUrlToLink*(m: RegexMatch; s: string): string =
  18. let url = s[m.group(0)[0]]
  19. toLink(url, " " & shortLink(url))
  20. proc reEmailToLink*(m: RegexMatch; s: string): string =
  21. let url = s[m.group(0)[0]]
  22. toLink("mailto://" & url, url)
  23. proc reUsernameToLink*(m: RegexMatch; s: string): string =
  24. var
  25. username = ""
  26. pretext = ""
  27. let
  28. pre = m.group(0)
  29. match = m.group(1)
  30. username = s[match[0]]
  31. if pre.len > 0:
  32. pretext = s[pre[0]]
  33. pretext & toLink("/" & username, "@" & username)
  34. proc linkifyText*(text: string): string =
  35. result = text.replace("\n", "<br>")
  36. result = result.replace(ellipsisRegex, "")
  37. result = result.replace(usernameRegex, reUsernameToLink)
  38. result = result.replace(emailRegex, reEmailToLink)
  39. result = result.replace(urlRegex, reUrlToLink)
  40. result = result.replace(re"</a>\s+", "</a> ")
  41. result = result.replace(re"</a> ([.,\)])", "</a>$1")
  42. proc stripTwitterUrls*(text: string): string =
  43. result = text
  44. result = result.replace(picRegex, "")
  45. result = result.replace(cardRegex, "")
  46. result = result.replace(ellipsisRegex, "")
  47. proc getUserpic*(userpic: string; style=""): string =
  48. let pic = userpic.replace(re"_(normal|bigger|mini|200x200)(\.[A-z]+)$", "$2")
  49. pic.replace(re"(\.[A-z]+)$", style & "$1")
  50. proc getUserpic*(profile: Profile; style=""): string =
  51. getUserPic(profile.userpic, style)
  52. proc formatName*(profile: Profile): string =
  53. result = xmltree.escape(profile.fullname)
  54. if profile.verified:
  55. result &= htmlgen.span("✔", class="verified-icon")
  56. elif profile.protected:
  57. result &= " 🔒"
  58. proc linkUser*(profile: Profile; h: string; username=true; class=""): string =
  59. let text =
  60. if username: "@" & profile.username
  61. else: formatName(profile)
  62. if h.len == 0:
  63. return htmlgen.a(text, href = &"/{profile.username}", class=class)
  64. let element = &"<{h} class=\"{class}\">{text}</{h}>"
  65. htmlgen.a(element, href = &"/{profile.username}")