prefs_impl.nim 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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, "invidio.us"):
  49. "Replace YouTube links with Invidious (blank to disable)"
  50. placeholder: "Invidious 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. hideTweetStats(checkbox, false):
  73. "Hide tweet stats (replies, retweets, likes)"
  74. hideBanner(checkbox, false):
  75. "Hide profile banner"
  76. hidePins(checkbox, false):
  77. "Hide pinned tweets"
  78. hideReplies(checkbox, false):
  79. "Hide tweet replies"
  80. iterator allPrefs*(): Pref =
  81. for k, v in prefList:
  82. for pref in v:
  83. yield pref
  84. macro genDefaultPrefs*(): untyped =
  85. result = nnkStmtList.newTree()
  86. for pref in allPrefs():
  87. let
  88. ident = ident(pref.name)
  89. name = newLit(pref.name)
  90. default =
  91. case pref.kind
  92. of checkbox: newLit(pref.defaultState)
  93. of select: newLit(pref.defaultOption)
  94. of input: newLit(pref.defaultInput)
  95. result.add quote do:
  96. defaultPrefs.`ident` = cfg.get("Preferences", `name`, `default`)
  97. macro genCookiePrefs*(cookies): untyped =
  98. result = nnkStmtList.newTree()
  99. for pref in allPrefs():
  100. let
  101. name = pref.name
  102. ident = ident(pref.name)
  103. kind = newLit(pref.kind)
  104. options = pref.options
  105. result.add quote do:
  106. if `name` in `cookies`:
  107. when `kind` == input or `name` == "theme":
  108. result.`ident` = `cookies`[`name`]
  109. elif `kind` == checkbox:
  110. result.`ident` = `cookies`[`name`] == "on"
  111. else:
  112. let value = `cookies`[`name`]
  113. if value in `options`: result.`ident` = value
  114. macro genCookiePref*(cookies, prefName, res): untyped =
  115. result = nnkStmtList.newTree()
  116. for pref in allPrefs():
  117. let ident = ident(pref.name)
  118. if ident != prefName:
  119. continue
  120. let
  121. name = pref.name
  122. kind = newLit(pref.kind)
  123. options = pref.options
  124. result.add quote do:
  125. if `name` in `cookies`:
  126. when `kind` == input or `name` == "theme":
  127. `res` = `cookies`[`name`]
  128. elif `kind` == checkbox:
  129. `res` = `cookies`[`name`] == "on"
  130. else:
  131. let value = `cookies`[`name`]
  132. if value in `options`: `res` = value
  133. macro genUpdatePrefs*(): untyped =
  134. result = nnkStmtList.newTree()
  135. let req = ident("request")
  136. for pref in allPrefs():
  137. let
  138. name = newLit(pref.name)
  139. kind = newLit(pref.kind)
  140. options = newLit(pref.options)
  141. default = nnkDotExpr.newTree(ident("defaultPrefs"), ident(pref.name))
  142. result.add quote do:
  143. let val = @`name`
  144. let isDefault =
  145. when `kind` == input or `name` == "theme":
  146. if `default`.len != val.len: false
  147. else: val == `default`
  148. elif `kind` == checkbox:
  149. (val == "on") == `default`
  150. else:
  151. val notin `options` or val == `default`
  152. if isDefault:
  153. savePref(`name`, "", `req`, expire=true)
  154. else:
  155. savePref(`name`, val, `req`)
  156. macro genResetPrefs*(): untyped =
  157. result = nnkStmtList.newTree()
  158. let req = ident("request")
  159. for pref in allPrefs():
  160. let name = newLit(pref.name)
  161. result.add quote do:
  162. savePref(`name`, "", `req`, expire=true)
  163. macro genPrefsType*(): untyped =
  164. let name = nnkPostfix.newTree(ident("*"), ident("Prefs"))
  165. result = quote do:
  166. type `name` = object
  167. discard
  168. for pref in allPrefs():
  169. result[0][2][2].add nnkIdentDefs.newTree(
  170. nnkPostfix.newTree(ident("*"), ident(pref.name)),
  171. (case pref.kind
  172. of checkbox: ident("bool")
  173. of input, select: ident("string")),
  174. newEmptyNode())