Use strformat more

This commit is contained in:
Zed 2022-06-04 02:18:26 +02:00
parent 6709f6f1b5
commit 21e8f04fa4
11 changed files with 38 additions and 37 deletions

View File

@ -60,11 +60,11 @@ proc replaceUrls*(body: string; prefs: Prefs; absolute=""): string =
result = result.replace("/c/", "/") result = result.replace("/c/", "/")
if prefs.replaceTwitter.len > 0 and ("twitter.com" in body or tco in body): if prefs.replaceTwitter.len > 0 and ("twitter.com" in body or tco in body):
result = result.replace(tco, https & prefs.replaceTwitter & "/t.co") result = result.replace(tco, &"{https}{prefs.replaceTwitter}/t.co")
result = result.replace(cards, prefs.replaceTwitter & "/cards") result = result.replace(cards, prefs.replaceTwitter & "/cards")
result = result.replace(twRegex, prefs.replaceTwitter) result = result.replace(twRegex, prefs.replaceTwitter)
result = result.replacef(twLinkRegex, a( result = result.replacef(twLinkRegex, a(
prefs.replaceTwitter & "$2", href = https & prefs.replaceTwitter & "$1")) prefs.replaceTwitter & "$2", href = &"{https}{prefs.replaceTwitter}$1"))
if prefs.replaceReddit.len > 0 and ("reddit.com" in result or "redd.it" in result): if prefs.replaceReddit.len > 0 and ("reddit.com" in result or "redd.it" in result):
result = result.replace(rdShortRegex, prefs.replaceReddit & "/comments/") result = result.replace(rdShortRegex, prefs.replaceReddit & "/comments/")
@ -76,7 +76,7 @@ proc replaceUrls*(body: string; prefs: Prefs; absolute=""): string =
result = result.replace(igRegex, prefs.replaceInstagram) result = result.replace(igRegex, prefs.replaceInstagram)
if absolute.len > 0 and "href" in result: if absolute.len > 0 and "href" in result:
result = result.replace("href=\"/", "href=\"" & absolute & "/") result = result.replace("href=\"/", &"href=\"{absolute}/")
proc getM3u8Url*(content: string): string = proc getM3u8Url*(content: string): string =
var matches: array[1, string] var matches: array[1, string]

View File

@ -93,11 +93,11 @@ proc genQueryUrl*(query: Query): string =
if query.text.len > 0: if query.text.len > 0:
params.add "q=" & encodeUrl(query.text) params.add "q=" & encodeUrl(query.text)
for f in query.filters: for f in query.filters:
params.add "f-" & f & "=on" params.add &"f-{f}=on"
for e in query.excludes: for e in query.excludes:
params.add "e-" & e & "=on" params.add &"e-{e}=on"
for i in query.includes.filterIt(it != "nativeretweets"): for i in query.includes.filterIt(it != "nativeretweets"):
params.add "i-" & i & "=on" params.add &"i-{i}=on"
if query.since.len > 0: if query.since.len > 0:
params.add "since=" & query.since params.add "since=" & query.since

View File

@ -1,5 +1,5 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
import asyncdispatch, strutils, options import asyncdispatch, strutils, strformat, options
import jester, karax/vdom import jester, karax/vdom
import ".."/[types, api] import ".."/[types, api]
import ../views/[embed, tweet, general] import ../views/[embed, tweet, general]
@ -31,6 +31,6 @@ proc createEmbedRouter*(cfg: Config) =
let id = @"id" let id = @"id"
if id.len > 0: if id.len > 0:
redirect("/i/status/" & id & "/embed") redirect(&"/i/status/{id}/embed")
else: else:
resp Http404 resp Http404

View File

@ -1,5 +1,5 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
import strutils, uri import strutils, strformat, uri
import jester import jester
@ -10,14 +10,17 @@ export getListTimeline, getGraphList
template respList*(list, timeline, title, vnode: typed) = template respList*(list, timeline, title, vnode: typed) =
if list.id.len == 0 or list.name.len == 0: if list.id.len == 0 or list.name.len == 0:
resp Http404, showError("List " & @"id" & " not found", cfg) resp Http404, showError(&"""List "{@"id"}" not found""", cfg)
let let
html = renderList(vnode, timeline.query, list) html = renderList(vnode, timeline.query, list)
rss = "/i/lists/$1/rss" % [@"id"] rss = &"""/i/lists/{@"id"}/rss"""
resp renderMain(html, request, cfg, prefs, titleText=title, rss=rss, banner=list.banner) resp renderMain(html, request, cfg, prefs, titleText=title, rss=rss, banner=list.banner)
proc title*(list: List): string =
&"@{list.username}/{list.name}"
proc createListRouter*(cfg: Config) = proc createListRouter*(cfg: Config) =
router list: router list:
get "/@name/lists/@slug/?": get "/@name/lists/@slug/?":
@ -28,24 +31,22 @@ proc createListRouter*(cfg: Config) =
slug = decodeUrl(@"slug") slug = decodeUrl(@"slug")
list = await getCachedList(@"name", slug) list = await getCachedList(@"name", slug)
if list.id.len == 0: if list.id.len == 0:
resp Http404, showError("List \"" & @"slug" & "\" not found", cfg) resp Http404, showError(&"""List "{@"slug"}" not found""", cfg)
redirect("/i/lists/" & list.id) redirect(&"/i/lists/{list.id}")
get "/i/lists/@id/?": get "/i/lists/@id/?":
cond '.' notin @"id" cond '.' notin @"id"
let let
prefs = cookiePrefs() prefs = cookiePrefs()
list = await getCachedList(id=(@"id")) list = await getCachedList(id=(@"id"))
title = "@" & list.username & "/" & list.name
timeline = await getListTimeline(list.id, getCursor()) timeline = await getListTimeline(list.id, getCursor())
vnode = renderTimelineTweets(timeline, prefs, request.path) vnode = renderTimelineTweets(timeline, prefs, request.path)
respList(list, timeline, title, vnode) respList(list, timeline, list.title, vnode)
get "/i/lists/@id/members": get "/i/lists/@id/members":
cond '.' notin @"id" cond '.' notin @"id"
let let
prefs = cookiePrefs() prefs = cookiePrefs()
list = await getCachedList(id=(@"id")) list = await getCachedList(id=(@"id"))
title = "@" & list.username & "/" & list.name
members = await getGraphListMembers(list, getCursor()) members = await getGraphListMembers(list, getCursor())
respList(list, members, title, renderTimelineUsers(members, prefs, request.path)) respList(list, members, list.title, renderTimelineUsers(members, prefs, request.path))

View File

@ -1,5 +1,5 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
import asyncdispatch, strutils, tables, times, hashes, uri import asyncdispatch, strutils, strformat, tables, times, hashes, uri
import jester import jester
@ -42,8 +42,8 @@ proc timelineRss*(req: Request; cfg: Config; query: Query): Future[Rss] {.async.
template respRss*(rss, page) = template respRss*(rss, page) =
if rss.cursor.len == 0: if rss.cursor.len == 0:
let info = case page let info = case page
of "User": " \"$1\" " % @"name" of "User": &""" "{@"name"}" """
of "List": " $1 " % @"id" of "List": &""" "{@"id"}" """
else: " " else: " "
resp Http404, showError(page & info & "not found", cfg) resp Http404, showError(page & info & "not found", cfg)
@ -67,7 +67,7 @@ proc createRssRouter*(cfg: Config) =
let let
cursor = getCursor() cursor = getCursor()
key = "search:" & $hash(genQueryUrl(query)) & ":" & cursor key = &"search:{hash(genQueryUrl(query))}:cursor"
var rss = await getCachedRss(key) var rss = await getCachedRss(key)
if rss.cursor.len > 0: if rss.cursor.len > 0:
@ -86,7 +86,7 @@ proc createRssRouter*(cfg: Config) =
let let
cursor = getCursor() cursor = getCursor()
name = @"name" name = @"name"
key = "twitter:" & name & ":" & cursor key = &"twitter:{name}:{cursor}"
var rss = await getCachedRss(key) var rss = await getCachedRss(key)
if rss.cursor.len > 0: if rss.cursor.len > 0:
@ -109,7 +109,7 @@ proc createRssRouter*(cfg: Config) =
of "search": initQuery(params(request), name=name) of "search": initQuery(params(request), name=name)
else: Query(fromUser: @[name]) else: Query(fromUser: @[name])
var key = @"tab" & ":" & @"name" & ":" var key = &"""{@"tab"}:{@"name"}:"""
if @"tab" == "search": if @"tab" == "search":
key &= $hash(genQueryUrl(query)) & ":" key &= $hash(genQueryUrl(query)) & ":"
key &= getCursor() key &= getCursor()
@ -132,11 +132,11 @@ proc createRssRouter*(cfg: Config) =
cursor = getCursor() cursor = getCursor()
if list.id.len == 0: if list.id.len == 0:
resp Http404, showError("List \"" & @"slug" & "\" not found", cfg) resp Http404, showError(&"""List "{@"slug"}" not found""", cfg)
let url = "/i/lists/" & list.id & "/rss" let url = &"/i/lists/{list.id}/rss"
if cursor.len > 0: if cursor.len > 0:
redirect(url & "?cursor=" & encodeUrl(cursor, false)) redirect(&"{url}?cursor={encodeUrl(cursor, false)}")
else: else:
redirect(url) redirect(url)
@ -146,7 +146,7 @@ proc createRssRouter*(cfg: Config) =
cursor = getCursor() cursor = getCursor()
key = key =
if cursor.len == 0: "lists:" & @"id" if cursor.len == 0: "lists:" & @"id"
else: "lists:" & @"id" & ":" & cursor else: &"""lists:{@"id"}:{cursor}"""
var rss = await getCachedRss(key) var rss = await getCachedRss(key)
if rss.cursor.len > 0: if rss.cursor.len > 0:

View File

@ -1,5 +1,5 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
import strutils, uri import strutils, strformat, uri
import jester import jester
@ -37,7 +37,7 @@ proc createSearchRouter*(cfg: Config) =
resp Http404, showError("Invalid search", cfg) resp Http404, showError("Invalid search", cfg)
get "/hashtag/@hash": get "/hashtag/@hash":
redirect("/search?q=" & encodeUrl("#" & @"hash")) redirect(&"""/search?q={encodeUrl("#" & @"hash")}""")
get "/opensearch": get "/opensearch":
let url = getUrlPrefix(cfg) & "/search?q=" let url = getUrlPrefix(cfg) & "/search?q="

View File

@ -1,5 +1,5 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
import asyncdispatch, strutils, sequtils, uri, options, times import asyncdispatch, strutils, strformat, sequtils, uri, options, times
import jester, karax/vdom import jester, karax/vdom
import router_utils import router_utils
@ -102,7 +102,7 @@ proc showTimeline*(request: Request; query: Query; cfg: Config; prefs: Prefs;
template respTimeline*(timeline: typed) = template respTimeline*(timeline: typed) =
let t = timeline let t = timeline
if t.len == 0: if t.len == 0:
resp Http404, showError("User \"" & @"name" & "\" not found", cfg) resp Http404, showError(&"""User "{@"name"}" not found""", cfg)
resp t resp t
template respUserId*() = template respUserId*() =

View File

@ -81,7 +81,7 @@ proc renderHead*(prefs: Prefs; cfg: Config; req: Request; titleText=""; desc="";
title: title:
if titleText.len > 0: if titleText.len > 0:
text titleText & " | " & cfg.title text &"{titleText}|{cfg.title}"
else: else:
text cfg.title text cfg.title

View File

@ -1,11 +1,11 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
import strutils import strutils, strformat
import karax/[karaxdsl, vdom, vstyles] import karax/[karaxdsl, vdom, vstyles]
import ".."/[types, utils] import ".."/[types, utils]
proc icon*(icon: string; text=""; title=""; class=""; href=""): VNode = proc icon*(icon: string; text=""; title=""; class=""; href=""): VNode =
var c = "icon-" & icon var c = "icon-" & icon
if class.len > 0: c = c & " " & class if class.len > 0: c = &"{c} {class}"
buildHtml(tdiv(class="icon-container")): buildHtml(tdiv(class="icon-container")):
if href.len > 0: if href.len > 0:
a(class=c, title=title, href=href) a(class=c, title=title, href=href)

View File

@ -117,7 +117,7 @@ ${renderRssTweets(profile.tweets.content, cfg)}
<atom:link href="${link}" rel="self" type="application/rss+xml" /> <atom:link href="${link}" rel="self" type="application/rss+xml" />
<title>${xmltree.escape(list.name)} / @${list.username}</title> <title>${xmltree.escape(list.name)} / @${list.username}</title>
<link>${link}</link> <link>${link}</link>
<description>${getDescription(list.name & " by @" & list.username, cfg)}</description> <description>${getDescription(&"{list.name} by @{list.username}", cfg)}</description>
<language>en-us</language> <language>en-us</language>
<ttl>40</ttl> <ttl>40</ttl>
${renderRssTweets(tweets, cfg)} ${renderRssTweets(tweets, cfg)}
@ -135,7 +135,7 @@ ${renderRssTweets(tweets, cfg)}
<atom:link href="${link}" rel="self" type="application/rss+xml" /> <atom:link href="${link}" rel="self" type="application/rss+xml" />
<title>Search results for "${escName}"</title> <title>Search results for "${escName}"</title>
<link>${link}</link> <link>${link}</link>
<description>${getDescription("Search \"" & escName & "\"", cfg)}</description> <description>${getDescription(&"Search \"{escName}\"", cfg)}</description>
<language>en-us</language> <language>en-us</language>
<ttl>40</ttl> <ttl>40</ttl>
${renderRssTweets(tweets, cfg)} ${renderRssTweets(tweets, cfg)}

View File

@ -154,7 +154,7 @@ proc renderPoll(poll: Poll): VNode =
span(class="poll-choice-value"): text percStr span(class="poll-choice-value"): text percStr
span(class="poll-choice-option"): text poll.options[i] span(class="poll-choice-option"): text poll.options[i]
span(class="poll-info"): span(class="poll-info"):
text insertSep($poll.votes, ',') & " votes • " & poll.status text &"{insertSep($poll.votes, ',')} votes • {poll.status}"
proc renderCardImage(card: Card): VNode = proc renderCardImage(card: Card): VNode =
buildHtml(tdiv(class="card-image-container")): buildHtml(tdiv(class="card-image-container")):