From aae0e51154a841abeaff036aed2b4b4b2568011b Mon Sep 17 00:00:00 2001 From: Zed Date: Tue, 25 Jun 2019 04:52:38 +0200 Subject: [PATCH] Ensure correct text formatting --- public/style.css | 3 ++- src/formatters.nim | 17 ++++++++++------- src/parser.nim | 2 +- src/parserutils.nim | 11 +++++++++++ src/views/general.nimf | 2 +- src/views/tweet.nimf | 18 +++++++----------- src/views/user.nimf | 2 +- 7 files changed, 33 insertions(+), 22 deletions(-) diff --git a/public/style.css b/public/style.css index cefd973..2410e8c 100644 --- a/public/style.css +++ b/public/style.css @@ -145,9 +145,10 @@ a:hover { line-height: 1.4em; } -.status-el .media-body { +.status-content.media-body { flex: 1; padding: 0; + white-space: pre-wrap; } .container, .item { diff --git a/src/formatters.nim b/src/formatters.nim index 0026040..4662c68 100644 --- a/src/formatters.nim +++ b/src/formatters.nim @@ -1,4 +1,4 @@ -import strutils, strformat, htmlgen, xmltree +import strutils, strformat, htmlgen, xmltree, times import regex import ./types, ./utils @@ -8,7 +8,7 @@ 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"(^|[^\S\n]|\.)@([A-z0-9_]+)" + usernameRegex = re"(^|[^\S\n]|\.|>)@([A-z0-9_]+)" picRegex = re"pic.twitter.com/[^ ]+" cardRegex = re"(https?://)?cards.twitter.com/[^ ]+" ellipsisRegex = re" ?…" @@ -48,14 +48,14 @@ proc reUsernameToLink*(m: RegexMatch; s: string): string = pretext & toLink("/" & username, "@" & username) proc linkifyText*(text: string): string = - result = text.stripText() - result = result.replace("\n", "
") + result = xmltree.escape(stripText(text)) result = result.replace(ellipsisRegex, "") - result = result.replace(usernameRegex, reUsernameToLink) result = result.replace(emailRegex, reEmailToLink) result = result.replace(urlRegex, reUrlToLink) - result = result.replace(re"([A-z0-9\):;.])\s+([;.,\)])", "$1") + result = result.replace("\n", "
") + result = result.replace(usernameRegex, reUsernameToLink) + result = result.replace(re"([^\s\(\n])\s+([;.,!\)']|')", "$1") proc stripTwitterUrls*(text: string): string = result = text @@ -92,3 +92,6 @@ proc pageTitle*(profile: Profile): string = proc pageTitle*(page: string): string = &"{page} | Nitter" + +proc getTime*(tweet: Tweet): string = + tweet.time.format("d/M/yyyy', ' HH:mm:ss") diff --git a/src/parser.nim b/src/parser.nim index e95db5b..d22fd03 100644 --- a/src/parser.nim +++ b/src/parser.nim @@ -44,7 +44,7 @@ proc parseQuote*(quote: XmlNode): Quote = result = Quote( id: quote.getAttr("data-item-id"), link: quote.getAttr("href"), - text: quote.selectText(".QuoteTweet-text").stripTwitterUrls() + text: getQuoteText(quote) ) result.profile = Profile( diff --git a/src/parserutils.nim b/src/parserutils.nim index 0771eb3..1f52e48 100644 --- a/src/parserutils.nim +++ b/src/parserutils.nim @@ -42,6 +42,17 @@ proc emojify*(node: XmlNode) = for i in node.querySelectorAll(".Emoji"): i.add newText(i.getAttr("alt")) +proc getQuoteText*(tweet: XmlNode): string = + let + text = tweet.querySelector(".QuoteTweet-text") + hasEmojis = not text.querySelector(".Emoji").isNil + + if hasEmojis: + emojify(text) + + result = stripText(selectText(text, ".tweet-text")) + result = stripTwitterUrls(result) + proc getTweetText*(tweet: XmlNode): string = let selector = ".tweet-text > a.twitter-timeline-link.u-hidden" diff --git a/src/views/general.nimf b/src/views/general.nimf index aeacda0..31b325c 100644 --- a/src/views/general.nimf +++ b/src/views/general.nimf @@ -3,7 +3,7 @@ # #proc renderMain*(body: string; title="Nitter"): string = - + ${xmltree.escape(title)} diff --git a/src/views/tweet.nimf b/src/views/tweet.nimf index 14ee51f..362adb5 100644 --- a/src/views/tweet.nimf +++ b/src/views/tweet.nimf @@ -21,9 +21,7 @@ ${linkUser(tweet.profile, class="username")} - - - + ${tweet.shortTime} @@ -32,7 +30,7 @@ #proc renderQuote(quote: Quote): string = #let hasMedia = quote.thumb.isSome() or quote.sensitive
-
+
#if hasMedia:
@@ -56,7 +54,7 @@ ${linkUser(quote.profile, class="fullname")} ${linkUser(quote.profile, class="username")}
-
${linkifyText(xmltree.escape(quote.text))}
+
${linkifyText(quote.text)}
#end proc @@ -65,7 +63,7 @@ #let groups = if tweet.photos.len > 2: tweet.photos.distribute(2) else: @[tweet.photos] #let display = if groups.len == 1 and groups[0].len == 1: "display: table-caption;" else: "" #var first = true -
+
#for photos in groups: #let margin = if not first: "margin-top: .25em;" else: "" #let flex = if photos.len > 1 or groups.len > 1: "display: flex;" else: "" @@ -87,7 +85,7 @@ #end proc # #proc renderVideo(video: Video): string = -
+