formatters.nim 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. proc stripTwitterUrls*(text: string): string =
  41. result = text
  42. result = result.replace(picRegex, "")
  43. result = result.replace(cardRegex, "")
  44. result = result.replace(ellipsisRegex, "")
  45. proc getUserpic*(userpic: string; style=""): string =
  46. let pic = userpic.replace(re"_(normal|bigger|mini|200x200)(\.[A-z]+)$", "$2")
  47. pic.replace(re"(\.[A-z]+)$", style & "$1")
  48. proc getUserpic*(profile: Profile; style=""): string =
  49. getUserPic(profile.userpic, style)
  50. proc getGifSrc*(tweet: Tweet): string =
  51. fmt"https://video.twimg.com/tweet_video/{tweet.gif.get()}.mp4"
  52. proc getGifThumb*(tweet: Tweet): string =
  53. fmt"https://pbs.twimg.com/tweet_video_thumb/{tweet.gif.get()}.jpg"
  54. proc formatName*(profile: Profile): string =
  55. result = xmltree.escape(profile.fullname)
  56. if profile.verified:
  57. result &= htmlgen.span("✔", class="verified-icon")
  58. elif profile.protected:
  59. result &= " 🔒"
  60. proc linkUser*(profile: Profile; h: string; username=true; class=""): string =
  61. let text =
  62. if username: "@" & profile.username
  63. else: formatName(profile)
  64. if h == "":
  65. return htmlgen.a(text, href = &"/{profile.username}", class=class)
  66. let element = &"<{h} class=\"{class}\">{text}</{h}>"
  67. htmlgen.a(element, href = &"/{profile.username}")