formatters.nim 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import strutils, strformat, htmlgen, xmltree, times
  2. import regex
  3. import ./types, ./utils
  4. from unicode import Rune, `$`
  5. const
  6. urlRegex = re"((https?|ftp)://(-\.)?([^\s/?\.#]+\.?)+([/\?][^\s\)]*)?)"
  7. emailRegex = re"([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)"
  8. usernameRegex = re"(^|[\s\.,>])@([A-z0-9_]+)"
  9. picRegex = re"pic.twitter.com/[^ ]+"
  10. cardRegex = re"(https?://)?cards.twitter.com/[^ ]+"
  11. ellipsisRegex = re" ?…"
  12. nbsp = $Rune(0x000A0)
  13. proc stripText*(text: string): string =
  14. text.replace(nbsp, " ").strip()
  15. proc shortLink*(text: string; length=28): string =
  16. result = text.replace(re"https?://(www.)?", "")
  17. if result.len > length:
  18. result = result[0 ..< length] & "…"
  19. proc toLink*(url, text: string; class="timeline-link"): string =
  20. a(text, class=class, href=url)
  21. proc reUrlToLink*(m: RegexMatch; s: string): string =
  22. let url = s[m.group(0)[0]]
  23. toLink(url, shortLink(url))
  24. proc reEmailToLink*(m: RegexMatch; s: string): string =
  25. let url = s[m.group(0)[0]]
  26. toLink("mailto://" & url, url)
  27. proc reUsernameToLink*(m: RegexMatch; s: string): string =
  28. var username = ""
  29. var pretext = ""
  30. let pre = m.group(0)
  31. let match = m.group(1)
  32. username = s[match[0]]
  33. if pre.len > 0:
  34. pretext = s[pre[0]]
  35. pretext & toLink("/" & username, "@" & username)
  36. proc linkifyText*(text: string): string =
  37. result = xmltree.escape(stripText(text))
  38. result = result.replace(ellipsisRegex, "")
  39. result = result.replace(emailRegex, reEmailToLink)
  40. result = result.replace(urlRegex, reUrlToLink)
  41. result = result.replace(usernameRegex, reUsernameToLink)
  42. result = result.replace(re"([^\s\(\n%])<a", "$1 <a")
  43. result = result.replace(re"</a>\s+([;.,!\)'%]|&apos;)", "</a>$1")
  44. proc stripTwitterUrls*(text: string): string =
  45. result = text
  46. result = result.replace(picRegex, "")
  47. result = result.replace(cardRegex, "")
  48. result = result.replace(ellipsisRegex, "")
  49. proc getUserpic*(userpic: string; style=""): string =
  50. let pic = userpic.replace(re"_(normal|bigger|mini|200x200)(\.[A-z]+)$", "$2")
  51. pic.replace(re"(\.[A-z]+)$", style & "$1")
  52. proc getUserpic*(profile: Profile; style=""): string =
  53. getUserPic(profile.userpic, style)
  54. proc genImg*(url: string; class=""): string =
  55. result = img(src = url.getSigUrl("pic"), class = class, alt = "Image")
  56. proc linkUser*(profile: Profile; class=""): string =
  57. let
  58. username = "username" in class
  59. href = &"/{profile.username}"
  60. text = if username: "@" & profile.username
  61. else: xmltree.escape(profile.fullname)
  62. result = a(text, href = href, class = class, title = text)
  63. if not username and profile.verified:
  64. result &= span("✔", class="icon verified-icon", title="Verified account")
  65. if not username and profile.protected:
  66. result &= span("🔒", class="icon protected-icon", title="Protected account")
  67. proc pageTitle*(profile: Profile): string =
  68. &"{profile.fullname} (@{profile.username}) | Nitter"
  69. proc pageTitle*(page: string): string =
  70. &"{page} | Nitter"
  71. proc getTime*(tweet: Tweet): string =
  72. tweet.time.format("d/M/yyyy', ' HH:mm:ss")
  73. proc getLink*(tweet: Tweet | Quote): string =
  74. &"{tweet.profile.username}/status/{tweet.id}"