nitter/src/prefs_impl.nim

214 lines
5.8 KiB
Nim
Raw Normal View History

2019-08-17 19:49:41 +00:00
import macros, tables, strutils, xmltree
type
PrefKind* = enum
checkbox, select, input
Pref* = object
name*: string
label*: string
2019-10-29 20:48:27 +00:00
kind*: PrefKind
2020-06-01 00:25:39 +00:00
# checkbox
2019-10-29 20:48:27 +00:00
defaultState*: bool
2020-06-01 00:25:39 +00:00
# select
2019-10-29 20:48:27 +00:00
defaultOption*: string
2020-06-01 00:25:39 +00:00
options*: seq[string]
# input
2019-10-29 20:48:27 +00:00
defaultInput*: string
2020-06-01 00:25:39 +00:00
placeholder*: string
2019-10-29 20:48:27 +00:00
PrefList* = OrderedTable[string, seq[Pref]]
macro genPrefs*(prefDsl: untyped) =
2019-10-29 20:48:27 +00: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-29 20:48:27 +00:00
genPrefs:
Privacy:
replaceTwitter(input, "nitter.net"):
"Replace Twitter links with Nitter (blank to disable)"
placeholder: "Nitter hostname"
2021-07-18 00:15:22 +00:00
replaceYouTube(input, "piped.kavin.rocks"):
"Replace YouTube links with Piped/Invidious (blank to disable)"
placeholder: "Piped hostname"
2019-10-29 20:48:27 +00:00
replaceInstagram(input, ""):
"Replace Instagram links with Bibliogram (blank to disable)"
placeholder: "Bibliogram hostname"
2019-10-29 20:48:27 +00:00
Media:
mp4Playback(checkbox, true):
"Enable mp4 video playback"
hlsPlayback(checkbox, false):
"Enable hls video streaming (requires JavaScript)"
proxyVideos(checkbox, true):
"Proxy video streaming through the server (might be slow)"
muteVideos(checkbox, false):
"Mute videos by default"
autoplayGifs(checkbox, true):
"Autoplay gifs"
Display:
theme(select, "Nitter"):
"Theme"
2020-01-07 02:00:16 +00:00
infiniteScroll(checkbox, false):
"Infinite scrolling (requires JavaScript, experimental!)"
2019-10-29 20:48:27 +00:00
stickyProfile(checkbox, true):
"Make profile sidebar stick to top"
bidiSupport(checkbox, false):
"Support bidirectional text (makes clicking on tweets harder)"
2019-10-29 20:48:27 +00:00
hideTweetStats(checkbox, false):
"Hide tweet stats (replies, retweets, likes)"
hideBanner(checkbox, false):
"Hide profile banner"
hidePins(checkbox, false):
"Hide pinned tweets"
hideReplies(checkbox, false):
"Hide tweet replies"
2019-08-17 19:49:41 +00:00
iterator allPrefs*(): Pref =
2019-08-17 19:49:41 +00:00
for k, v in prefList:
for pref in v:
yield pref
macro genDefaultPrefs*(): untyped =
result = nnkStmtList.newTree()
2019-08-17 19:49:41 +00: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 14:45:21 +00: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 14:45:21 +00:00
result.`ident` = `cookies`[`name`]
elif `kind` == checkbox:
2020-06-09 14:45:21 +00:00
result.`ident` = `cookies`[`name`] == "on"
else:
2020-06-09 14:45:21 +00:00
let value = `cookies`[`name`]
if value in `options`: result.`ident` = value
2019-08-17 19:49:41 +00:00
2020-06-09 14:45:21 +00: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-17 19:49:41 +00: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-17 19:49:41 +00:00
for pref in allPrefs():
let name = newLit(pref.name)
result.add quote do:
savePref(`name`, "", `req`, expire=true)
2019-08-17 19:49:41 +00:00
2019-09-08 11:01:20 +00:00
macro genPrefsType*(): untyped =
let name = nnkPostfix.newTree(ident("*"), ident("Prefs"))
result = quote do:
type `name` = object
discard
2019-09-08 11:01:20 +00: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())