prefs_impl.nim 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import macros, tables, strutils, xmltree
  2. type
  3. PrefKind* = enum
  4. checkbox, select, input
  5. Pref* = object
  6. name*: string
  7. label*: string
  8. kind*: PrefKind
  9. # checkbox
  10. defaultState*: bool
  11. # select
  12. defaultOption*: string
  13. options*: seq[string]
  14. # input
  15. defaultInput*: string
  16. placeholder*: string
  17. PrefList* = OrderedTable[string, seq[Pref]]
  18. macro genPrefs*(prefDsl: untyped) =
  19. var table = nnkTableConstr.newTree()
  20. for category in prefDsl:
  21. table.add nnkExprColonExpr.newTree(newLit($category[0]))
  22. table[^1].add nnkPrefix.newTree(newIdentNode("@"), nnkBracket.newTree())
  23. for pref in category[1]:
  24. let
  25. name = newLit($pref[0])
  26. kind = pref[1]
  27. label = pref[3][0]
  28. default = pref[2]
  29. defaultField =
  30. case parseEnum[PrefKind]($kind)
  31. of checkbox: ident("defaultState")
  32. of select: ident("defaultOption")
  33. of input: ident("defaultInput")
  34. var newPref = quote do:
  35. Pref(kind: `kind`, name: `name`, label: `label`, `defaultField`: `default`)
  36. for node in pref[3]:
  37. if node.kind == nnkCall:
  38. newPref.add nnkExprColonExpr.newTree(node[0], node[1][0])
  39. table[^1][1][1].add newPref
  40. let name = ident("prefList")
  41. result = quote do:
  42. const `name`*: PrefList = toOrderedTable(`table`)
  43. genPrefs:
  44. Privacy:
  45. replaceTwitter(input, "nitter.net"):
  46. "Replace Twitter links with Nitter (blank to disable)"
  47. placeholder: "Nitter hostname"
  48. replaceYouTube(input, "piped.kavin.rocks"):
  49. "Replace YouTube links with Piped/Invidious (blank to disable)"
  50. placeholder: "Piped hostname"
  51. replaceInstagram(input, ""):
  52. "Replace Instagram links with Bibliogram (blank to disable)"
  53. placeholder: "Bibliogram hostname"
  54. Media:
  55. mp4Playback(checkbox, true):
  56. "Enable mp4 video playback"
  57. hlsPlayback(checkbox, false):
  58. "Enable hls video streaming (requires JavaScript)"
  59. proxyVideos(checkbox, true):
  60. "Proxy video streaming through the server (might be slow)"
  61. muteVideos(checkbox, false):
  62. "Mute videos by default"
  63. autoplayGifs(checkbox, true):
  64. "Autoplay gifs"
  65. Display:
  66. theme(select, "Nitter"):
  67. "Theme"
  68. infiniteScroll(checkbox, false):
  69. "Infinite scrolling (requires JavaScript, experimental!)"
  70. stickyProfile(checkbox, true):
  71. "Make profile sidebar stick to top"
  72. bidiSupport(checkbox, false):
  73. "Support bidirectional text (makes clicking on tweets harder)"
  74. hideTweetStats(checkbox, false):
  75. "Hide tweet stats (replies, retweets, likes)"
  76. hideBanner(checkbox, false):
  77. "Hide profile banner"
  78. hidePins(checkbox, false):
  79. "Hide pinned tweets"
  80. hideReplies(checkbox, false):
  81. "Hide tweet replies"
  82. iterator allPrefs*(): Pref =
  83. for k, v in prefList:
  84. for pref in v:
  85. yield pref
  86. macro genDefaultPrefs*(): untyped =
  87. result = nnkStmtList.newTree()
  88. for pref in allPrefs():
  89. let
  90. ident = ident(pref.name)
  91. name = newLit(pref.name)
  92. default =
  93. case pref.kind
  94. of checkbox: newLit(pref.defaultState)
  95. of select: newLit(pref.defaultOption)
  96. of input: newLit(pref.defaultInput)
  97. result.add quote do:
  98. defaultPrefs.`ident` = cfg.get("Preferences", `name`, `default`)
  99. macro genCookiePrefs*(cookies): untyped =
  100. result = nnkStmtList.newTree()
  101. for pref in allPrefs():
  102. let
  103. name = pref.name
  104. ident = ident(pref.name)
  105. kind = newLit(pref.kind)
  106. options = pref.options
  107. result.add quote do:
  108. if `name` in `cookies`:
  109. when `kind` == input or `name` == "theme":
  110. result.`ident` = `cookies`[`name`]
  111. elif `kind` == checkbox:
  112. result.`ident` = `cookies`[`name`] == "on"
  113. else:
  114. let value = `cookies`[`name`]
  115. if value in `options`: result.`ident` = value
  116. macro genCookiePref*(cookies, prefName, res): untyped =
  117. result = nnkStmtList.newTree()
  118. for pref in allPrefs():
  119. let ident = ident(pref.name)
  120. if ident != prefName:
  121. continue
  122. let
  123. name = pref.name
  124. kind = newLit(pref.kind)
  125. options = pref.options
  126. result.add quote do:
  127. if `name` in `cookies`:
  128. when `kind` == input or `name` == "theme":
  129. `res` = `cookies`[`name`]
  130. elif `kind` == checkbox:
  131. `res` = `cookies`[`name`] == "on"
  132. else:
  133. let value = `cookies`[`name`]
  134. if value in `options`: `res` = value
  135. macro genUpdatePrefs*(): untyped =
  136. result = nnkStmtList.newTree()
  137. let req = ident("request")
  138. for pref in allPrefs():
  139. let
  140. name = newLit(pref.name)
  141. kind = newLit(pref.kind)
  142. options = newLit(pref.options)
  143. default = nnkDotExpr.newTree(ident("defaultPrefs"), ident(pref.name))
  144. result.add quote do:
  145. let val = @`name`
  146. let isDefault =
  147. when `kind` == input or `name` == "theme":
  148. if `default`.len != val.len: false
  149. else: val == `default`
  150. elif `kind` == checkbox:
  151. (val == "on") == `default`
  152. else:
  153. val notin `options` or val == `default`
  154. if isDefault:
  155. savePref(`name`, "", `req`, expire=true)
  156. else:
  157. savePref(`name`, val, `req`)
  158. macro genResetPrefs*(): untyped =
  159. result = nnkStmtList.newTree()
  160. let req = ident("request")
  161. for pref in allPrefs():
  162. let name = newLit(pref.name)
  163. result.add quote do:
  164. savePref(`name`, "", `req`, expire=true)
  165. macro genPrefsType*(): untyped =
  166. let name = nnkPostfix.newTree(ident("*"), ident("Prefs"))
  167. result = quote do:
  168. type `name` = object
  169. discard
  170. for pref in allPrefs():
  171. result[0][2][2].add nnkIdentDefs.newTree(
  172. nnkPostfix.newTree(ident("*"), ident(pref.name)),
  173. (case pref.kind
  174. of checkbox: ident("bool")
  175. of input, select: ident("string")),
  176. newEmptyNode())