diff --git a/src/formatters.nim b/src/formatters.nim index a7e8bc1..e276bc3 100644 --- a/src/formatters.nim +++ b/src/formatters.nim @@ -15,6 +15,8 @@ const twRegex = re"(www.|mobile.)?twitter.com" nbsp = $Rune(0x000A0) +const hostname {.strdefine.} = "nitter.net" + proc stripText*(text: string): string = text.replace(nbsp, " ").strip() @@ -23,12 +25,16 @@ proc shortLink*(text: string; length=28): string = if result.len > length: result = result[0 ..< length] & "…" -proc toLink*(url, text: string; class="timeline-link"): string = - a(text, class=class, href=url) +proc toLink*(url, text: string): string = + a(text, href=url) + +proc reUrlToShortLink*(m: RegexMatch; s: string): string = + let url = s[m.group(0)[0]] + toLink(url, shortLink(url)) proc reUrlToLink*(m: RegexMatch; s: string): string = let url = s[m.group(0)[0]] - toLink(url, shortLink(url)) + toLink(url, url.replace(re"https?://(www.)?", "")) proc reEmailToLink*(m: RegexMatch; s: string): string = let url = s[m.group(0)[0]] @@ -48,19 +54,9 @@ proc reUsernameToLink*(m: RegexMatch; s: string): string = pretext & toLink("/" & username, "@" & username) -proc linkifyText*(text: string; prefs: Prefs): 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"^\. 0: - result = result.replace(ytRegex, prefs.replaceYouTube) - if prefs.replaceTwitter.len > 0: - result = result.replace(twRegex, prefs.replaceTwitter) +proc reUsernameToFullLink*(m: RegexMatch; s: string): string = + result = reUsernameToLink(m, s) + result = result.replace("href=\"/", &"href=\"https://{hostname}/") proc replaceUrl*(url: string; prefs: Prefs): string = result = url @@ -69,6 +65,21 @@ proc replaceUrl*(url: string; prefs: Prefs): string = if prefs.replaceTwitter.len > 0: result = result.replace(twRegex, prefs.replaceTwitter) +proc linkifyText*(text: string; prefs: Prefs; rss=false): string = + result = xmltree.escape(stripText(text)) + result = result.replace(ellipsisRegex, "") + result = result.replace(emailRegex, reEmailToLink) + if rss: + result = result.replace(urlRegex, reUrlToLink) + result = result.replace(usernameRegex, reUsernameToFullLink) + else: + result = result.replace(urlRegex, reUrlToShortLink) + result = result.replace(usernameRegex, reUsernameToLink) + result = result.replace(re"([^\s\(\n%])\s+([;.,!\)'%]|')", "$1") + result = result.replace(re"^\. ${text}
${quoteLink}

+#else: +

${text}

+#end if +#if tweet.photos.len > 0: + +#elif tweet.video.isSome: + +#elif tweet.gif.isSome: +#let thumb = &"https://{hostname}{getPicUrl(get(tweet.gif).thumb)}" +#let url = &"https://{hostname}{getGifUrl(get(tweet.gif).url)}" + +#end if +#end proc +# +#proc getTitle(tweet: Tweet; prefs: Prefs): string = +#if tweet.pinned: result = "Pinned: " +#elif tweet.retweet.isSome: result = "RT: " +#end if +#result &= xmltree.escape(replaceUrl(tweet.text, prefs)) +#if result.len > 0: return +#end if +#if tweet.photos.len > 0: +# result &= "Image" +#elif tweet.video.isSome: +# result &= "Video" +#elif tweet.gif.isSome: +# result &= "Gif" +#end if +#end proc +# +#proc renderTimelineRss*(tweets: seq[Tweet]; profile: Profile): string = +#let prefs = Prefs(replaceTwitter: hostname) +#result = "" + + + + + ${profile.fullname} / @${profile.username} + https://${hostname}${profile.username} + Twitter feed for: ${profile.username}. Generated by ${hostname} + en-us + 40 + + https://${hostname}${getPicUrl(profile.getUserPic(style="_400x400"))} + + #for tweet in tweets: + + ${getTitle(tweet, prefs)} + (@${tweet.profile.username}) + + ${getTime(tweet)} + https://${hostname}${getLink(tweet)} + https://${hostname}${getLink(tweet)} + + #end for + + +#end proc