このリポジトリは2023-09-09にアーカイブされています。 ファイルの閲覧とクローンは可能ですが、プッシュ、イシューの作成、プルリクエストはできません。
Nitter-mod/src/prefs_impl.nim

226 行
6.2 KiB
Nim
Raw 通常表示 履歴

2021-12-27 10:37:38 +09:00
# SPDX-License-Identifier: AGPL-3.0-only
2019-08-18 04:49:41 +09:00
import macros, tables, strutils, xmltree
type
PrefKind* = enum
checkbox, select, input
Pref* = object
name*: string
label*: string
2019-10-30 05:48:27 +09:00
kind*: PrefKind
2020-06-01 09:25:39 +09:00
# checkbox
2019-10-30 05:48:27 +09:00
defaultState*: bool
2020-06-01 09:25:39 +09:00
# select
2019-10-30 05:48:27 +09:00
defaultOption*: string
2020-06-01 09:25:39 +09:00
options*: seq[string]
# input
2019-10-30 05:48:27 +09:00
defaultInput*: string
2020-06-01 09:25:39 +09:00
placeholder*: string
2019-10-30 05:48:27 +09:00
PrefList* = OrderedTable[string, seq[Pref]]
macro genPrefs*(prefDsl: untyped) =
2019-10-30 05:48:27 +09:00
var table = nnkTableConstr.newTree()
for category in prefDsl:
table.add nnkExprColonExpr.newTree(newLit($category[0]))
table[^1].add nnkPrefix.newTree(newIdentNode("@"), nnkBracket.newTree())
for pref in category[1]:
let
name = newLit($pref[0])
kind = pref[1]
label = pref[3][0]
default = pref[2]
defaultField =
case parseEnum[PrefKind]($kind)
of checkbox: ident("defaultState")
of select: ident("defaultOption")
of input: ident("defaultInput")
var newPref = quote do:
Pref(kind: `kind`, name: `name`, label: `label`, `defaultField`: `default`)
for node in pref[3]:
if node.kind == nnkCall:
newPref.add nnkExprColonExpr.newTree(node[0], node[1][0])
table[^1][1][1].add newPref
let name = ident("prefList")
result = quote do:
const `name`*: PrefList = toOrderedTable(`table`)
2019-10-30 05:48:27 +09:00
genPrefs:
Display:
theme(select, "Nitter"):
2021-12-28 10:41:37 +09:00
"テーマ"
2019-10-30 05:48:27 +09:00
2020-01-07 11:00:16 +09:00
infiniteScroll(checkbox, false):
2021-12-28 10:41:37 +09:00
"無限スクロール(JS必須)"
2019-10-30 05:48:27 +09:00
stickyProfile(checkbox, true):
"Make profile sidebar stick to top"
bidiSupport(checkbox, false):
"Support bidirectional text (makes clicking on tweets harder)"
2019-10-30 05:48:27 +09:00
hideTweetStats(checkbox, false):
2021-12-28 10:41:37 +09:00
"ツイートの詳細(返事、RT、いいね)を隠す"
2019-10-30 05:48:27 +09:00
hideBanner(checkbox, false):
2021-12-28 10:41:37 +09:00
"バナーを隠す"
2019-10-30 05:48:27 +09:00
hidePins(checkbox, false):
2021-12-28 10:41:37 +09:00
"ピン留めたツイートを隠す"
2019-10-30 05:48:27 +09:00
hideReplies(checkbox, false):
2021-12-28 10:41:37 +09:00
"ツイートに返事を隠す"
2021-11-04 15:16:57 +09:00
2022-01-14 06:58:07 +09:00
squareAvatars(checkbox, false):
"Square profile pictures"
2019-10-30 05:48:27 +09:00
Media:
mp4Playback(checkbox, true):
2021-12-28 10:41:37 +09:00
"MP4再生(GIFのみ)"
2019-10-30 05:48:27 +09:00
hlsPlayback(checkbox, false):
2021-12-28 10:41:37 +09:00
"HLS動画再生(JS必須)"
2019-10-30 05:48:27 +09:00
proxyVideos(checkbox, true):
2021-12-28 10:41:37 +09:00
"動画の再生をサーバーでプロキシーする(遅くなるかも)"
2019-10-30 05:48:27 +09:00
muteVideos(checkbox, false):
2021-12-28 10:41:37 +09:00
"デフォルトで動画のミュート"
2019-10-30 05:48:27 +09:00
autoplayGifs(checkbox, true):
2021-12-28 10:41:37 +09:00
"自動的にGIFの再生"
2019-10-30 05:48:27 +09:00
2021-12-28 02:05:09 +09:00
"Link replacements (blank to disable)":
2023-03-18 17:05:28 +09:00
replaceTwitter(input, "twitter.owacon.moe"):
2021-12-28 10:41:37 +09:00
"ツイッター → Nitter"
2023-03-18 17:05:28 +09:00
placeholder: "twitter.owacon.moe"
2023-03-18 17:05:28 +09:00
replaceYouTube(input, "youtube.owacon.moe"):
2021-12-28 10:41:37 +09:00
"ユーチューブ → Invidious"
2023-03-18 17:05:28 +09:00
placeholder: "youtube.owacon.moe"
2019-10-30 05:48:27 +09:00
2023-03-18 17:05:28 +09:00
replaceReddit(input, "reddit.owacon.moe"):
2021-12-28 10:41:37 +09:00
"Reddit → Teddit/Libreddit"
2023-03-18 17:05:28 +09:00
placeholder: "reddit.owacon.moe"
2019-10-30 05:48:27 +09:00
2023-03-18 17:05:28 +09:00
replaceInstagram(input, "instagram.owacon.moe"):
2021-12-28 10:41:37 +09:00
"インスタグラム → Bibliogram"
2023-03-18 17:05:28 +09:00
placeholder: "instagram.owacon.moe"
2019-10-30 05:48:27 +09:00
2023-03-18 17:05:28 +09:00
replaceOdysee(input, "odysee.owacon.moe"):
2021-12-28 10:41:37 +09:00
"オディシー → Librarian"
2023-03-18 17:05:28 +09:00
placeholder: "odysee.owacon.moe"
2019-08-18 04:49:41 +09:00
iterator allPrefs*(): Pref =
2019-08-18 04:49:41 +09:00
for k, v in prefList:
for pref in v:
yield pref
macro genDefaultPrefs*(): untyped =
result = nnkStmtList.newTree()
2019-08-18 04:49:41 +09:00
for pref in allPrefs():
let
ident = ident(pref.name)
name = newLit(pref.name)
default =
case pref.kind
of checkbox: newLit(pref.defaultState)
of select: newLit(pref.defaultOption)
of input: newLit(pref.defaultInput)
result.add quote do:
defaultPrefs.`ident` = cfg.get("Preferences", `name`, `default`)
2020-06-09 23:45:21 +09:00
macro genCookiePrefs*(cookies): untyped =
result = nnkStmtList.newTree()
for pref in allPrefs():
let
name = pref.name
ident = ident(pref.name)
kind = newLit(pref.kind)
options = pref.options
result.add quote do:
if `name` in `cookies`:
when `kind` == input or `name` == "theme":
2020-06-09 23:45:21 +09:00
result.`ident` = `cookies`[`name`]
elif `kind` == checkbox:
2020-06-09 23:45:21 +09:00
result.`ident` = `cookies`[`name`] == "on"
else:
2020-06-09 23:45:21 +09:00
let value = `cookies`[`name`]
if value in `options`: result.`ident` = value
2019-08-18 04:49:41 +09:00
2020-06-09 23:45:21 +09:00
macro genCookiePref*(cookies, prefName, res): untyped =
result = nnkStmtList.newTree()
for pref in allPrefs():
let ident = ident(pref.name)
if ident != prefName:
continue
let
name = pref.name
kind = newLit(pref.kind)
options = pref.options
result.add quote do:
if `name` in `cookies`:
when `kind` == input or `name` == "theme":
`res` = `cookies`[`name`]
elif `kind` == checkbox:
`res` = `cookies`[`name`] == "on"
else:
let value = `cookies`[`name`]
if value in `options`: `res` = value
2019-08-18 04:49:41 +09:00
macro genUpdatePrefs*(): untyped =
result = nnkStmtList.newTree()
let req = ident("request")
for pref in allPrefs():
let
name = newLit(pref.name)
kind = newLit(pref.kind)
options = newLit(pref.options)
default = nnkDotExpr.newTree(ident("defaultPrefs"), ident(pref.name))
result.add quote do:
let val = @`name`
let isDefault =
when `kind` == input or `name` == "theme":
if `default`.len != val.len: false
else: val == `default`
elif `kind` == checkbox:
(val == "on") == `default`
else:
val notin `options` or val == `default`
if isDefault:
savePref(`name`, "", `req`, expire=true)
else:
savePref(`name`, val, `req`)
macro genResetPrefs*(): untyped =
result = nnkStmtList.newTree()
let req = ident("request")
2019-08-18 04:49:41 +09:00
for pref in allPrefs():
let name = newLit(pref.name)
result.add quote do:
savePref(`name`, "", `req`, expire=true)
2019-08-18 04:49:41 +09:00
2019-09-08 20:01:20 +09:00
macro genPrefsType*(): untyped =
let name = nnkPostfix.newTree(ident("*"), ident("Prefs"))
result = quote do:
type `name` = object
discard
2019-09-08 20:01:20 +09:00
for pref in allPrefs():
result[0][2][2].add nnkIdentDefs.newTree(
nnkPostfix.newTree(ident("*"), ident(pref.name)),
(case pref.kind
of checkbox: ident("bool")
of input, select: ident("string")),
newEmptyNode())