diff options
author | bptato <nincsnevem662@gmail.com> | 2023-06-26 12:19:43 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2023-06-26 12:19:43 +0200 |
commit | 2dc72e8e131d91c4d0b46d3b4d8214a515fe6007 (patch) | |
tree | 92a6fc949207ff7bbb3ef2a02ef5fefa46b5ad82 | |
parent | ec2d2411a5bd97e847cc185cc0b3f84121b93b93 (diff) | |
download | chawan-2dc72e8e131d91c4d0b46d3b4d8214a515fe6007.tar.gz |
config: add stylesheet, apply siteconf to piped input
-rw-r--r-- | doc/config.md | 10 | ||||
-rw-r--r-- | src/config/config.nim | 11 | ||||
-rw-r--r-- | src/display/pager.nim | 32 |
3 files changed, 35 insertions, 18 deletions
diff --git a/doc/config.md b/doc/config.md index 2cf0a02d..f043541d 100644 --- a/doc/config.md +++ b/doc/config.md @@ -417,6 +417,16 @@ Defaults to false. in [encoding].</td> </tr> +<tr> +<td>stylesheet</td> +<td>CSS stylesheet</td> +<td>Specify an additional user-stylesheet for this site. +Note: other user-stylesheets (specified under [css] or additional matching +siteconfs) are not overridden. (In other words, they will be concatenated +with this stylesheet to get the final user stylesheet.)</td> +</tr> + + </table> ## Stylesheets diff --git a/src/config/config.nim b/src/config/config.nim index 533ac93f..51051048 100644 --- a/src/config/config.nim +++ b/src/config/config.nim @@ -36,6 +36,7 @@ type scripting: Opt[bool] document_charset: seq[Charset] images: Opt[bool] + stylesheet: Opt[string] StaticOmniRule = object match: string @@ -52,6 +53,7 @@ type scripting*: Opt[bool] document_charset*: seq[Charset] images*: Opt[bool] + stylesheet*: Opt[string] OmniRule* = object match*: Regex @@ -137,12 +139,11 @@ func getForkServerConfig*(config: Config): ForkServerConfig = ambiguous_double: config.display.double_width_ambiguous ) -proc getBufferConfig*(config: Config, location: URL, cookiejar: CookieJar = nil, - headers: Headers = nil, referer_from = false, scripting = false, - charsets = config.encoding.document_charset, - images = false): BufferConfig = +proc getBufferConfig*(config: Config, location: URL, cookiejar: CookieJar, + headers: Headers, referer_from, scripting: bool, charsets: seq[Charset], + images: bool, userstyle: string): BufferConfig = result = BufferConfig( - userstyle: config.css.stylesheet, + userstyle: userstyle, filter: newURLFilter(scheme = some(location.scheme), default = true), cookiejar: cookiejar, headers: headers, diff --git a/src/display/pager.nim b/src/display/pager.nim index 1163e6c9..be7485a8 100644 --- a/src/display/pager.nim +++ b/src/display/pager.nim @@ -562,30 +562,32 @@ proc windowChange*(pager: Pager, attrs: WindowAttributes) = pager.writeAskPrompt() pager.refreshStatusMsg() -proc applySiteconf(pager: Pager, request: Request): BufferConfig = - let url = $request.url - let host = request.url.host +# Apply siteconf settings to a request. +# Note that this may modify the URL passed. +proc applySiteconf(pager: Pager, url: var URL): BufferConfig = + let host = url.host var referer_from: bool var cookiejar: CookieJar var headers: Headers var scripting: bool var images: bool var charsets = pager.config.encoding.document_charset + var userstyle = pager.config.css.stylesheet for sc in pager.siteconf: - if sc.url.isSome and not sc.url.get.match(url): + if sc.url.isSome and not sc.url.get.match($url): continue elif sc.host.isSome and not sc.host.get.match(host): continue if sc.rewrite_url != nil: - let s = sc.rewrite_url(request.url) + let s = sc.rewrite_url(url) if s.isSome and s.get != nil: - request.url = s.get + url = s.get if sc.cookie.isSome: if sc.cookie.get: # host/url might have changed by now - let jarid = sc.share_cookiejar.get(request.url.host) + let jarid = sc.share_cookiejar.get(url.host) if jarid notin pager.cookiejars: - pager.cookiejars[jarid] = newCookieJar(request.url, + pager.cookiejars[jarid] = newCookieJar(url, sc.third_party_cookie) cookiejar = pager.cookiejars[jarid] else: @@ -598,8 +600,11 @@ proc applySiteconf(pager: Pager, request: Request): BufferConfig = charsets = sc.document_charset if sc.images.isSome: images = sc.images.get - return pager.config.getBufferConfig(request.url, cookiejar, headers, - referer_from, scripting, charsets) + if sc.stylesheet.isSome: + userstyle &= "\n" + userstyle &= sc.stylesheet.get + return pager.config.getBufferConfig(url, cookiejar, headers, + referer_from, scripting, charsets, images, userstyle) # Load request in a new buffer. proc gotoURL(pager: Pager, request: Request, prevurl = none(URL), @@ -607,7 +612,7 @@ proc gotoURL(pager: Pager, request: Request, prevurl = none(URL), redirectdepth = 0, referrer: Container = nil) = if referrer != nil and referrer.config.referer_from: request.referer = referrer.source.location - var bufferconfig = pager.applySiteconf(request) + var bufferconfig = pager.applySiteconf(request.url) if prevurl.isnone or not prevurl.get.equals(request.url, true) or request.url.hash == "" or request.httpmethod != HTTP_GET: # Basically, we want to reload the page *only* when @@ -681,14 +686,15 @@ proc loadURL*(pager: Pager, url: string, ctype = none(string), proc readPipe0*(pager: Pager, ctype: Option[string], cs: Option[Charset], fd: FileHandle, location: Option[URL], title: string): Container = + var location = location.get(newURL("file://-").get) + let bufferconfig = pager.applySiteconf(location) let source = BufferSource( t: LOAD_PIPE, fd: fd, contenttype: some(ctype.get("text/plain")), charset: cs, - location: location.get(newURL("file://-").get) + location: location ) - let bufferconfig = pager.config.getBufferConfig(source.location) return pager.dispatcher.newBuffer(bufferconfig, source, title = title) proc readPipe*(pager: Pager, ctype: Option[string], cs: Option[Charset], |