dotfiles/qutebrowser/config/config.py
Marty Oehme 969e0a6288
qutebrowser: Refactor redirects as importable class
Make more extensive using of dataclasses for typing and simpler future
refactors.

The redirects 'plugin' is now a simple class which can be imported into
the configuration and is automatically active when instantiated. For
that to work it needs access to the qutebrowser python library but that
should hopefully be a given if wanting to have a qutebrowser plugin.

To make the default redirects active, simply import the class and
instantiate it:

```python
from fossredirect import Redirects
_ = Redirects()
```

This loads the defaults and activates them in qutebrowser. Try to go to
e.g. 'reddit.com' and it will automatically open in a libreddit
frontend.
To customize the redirects, provide a custom list of Services:

```python
from fossredirect import Redirects
_ = Redirects(services=[Service(source=["fromhere.com"], target=["tohere"])])
```

It works a little more flexibly now:
Redirects contains a list of Services.
Each service is a simple data container with the following:

```python
Service(
     source=["list.of", "hosts.to", "redirect.com"],
     target=["farside-redirect"]
)
```

The above redirects any of the source hosts to the far-side provided
target (in this case it would be
`farside.link/farside-redirect/<original-path>`).

However we can also specify 'custom' targets if farside does not have a
service that we want to redirect.

```python
Service(
     source=["list.of", "hosts.to", "redirect.com"],
     target=["my-redirected-host.org"],
     custom_targets=True
)
```

This directly rewrites the host to
`https://my-redirected-host.org/<original-path>`.

Lastly, we can have custom preprocess/postprocess functions which fix
some more involved redirect:

```python
Service(
     source=["list.of", "hosts.to", "redirect.com"],
     target=["my-redirected-host.org"],
     postprocess=lambda item: item
)
```

Be aware that the functions take 'QUrl' objects so you have to access
e.g. the actual host with `item.host()` before rewriting. Look at the
breezewiki rewrite function for an easy example.
2025-02-18 17:58:11 +01:00

97 lines
2.5 KiB
Python

import os
from typing import cast
# pylint: disable=C0111
from qutebrowser.config.config import ConfigContainer # noqa: F401
from qutebrowser.config.configfiles import ConfigAPI
from freedirect.freedirect import Redirects
config: ConfigAPI = cast(ConfigAPI, config) # noqa: F821 pylint: disable=E0602,C0103
c: ConfigContainer = cast(ConfigContainer, c) # noqa: F821 pylint: disable=E0602,C0103
# Autogenerated config.py
# Documentation:
# qute://help/configuring.html
# qute://help/settings.html
# load additional settings configured via autoconfig.yml
_ = config.load_autoconfig()
c.content.javascript.log_message.excludes = {
"userscript:_qute_stylesheet": [
"*Refused to apply inline style because it violates the following Content Security Policy directive: *"
],
"userscript:_qute_js": ["*TrustedHTML*"],
}
term = os.getenv("TERMINAL", "xterm")
c.completion.web_history.max_items = 1000
c.hints.uppercase = True
c.editor.command = [
term,
"start",
"--always-new-process",
"nvim",
"-f",
"{file}",
"-c",
"normal {line}G{column0}l",
]
# change filepicker
c.fileselect.handler = "external"
picker = [
term,
"start",
"--class",
"float",
"vifm",
"--choose-files",
"{}",
]
c.fileselect.single_file.command = picker
c.fileselect.multiple_files.command = picker
c.downloads.location.directory = os.getenv("XDG_DOWNLOAD_DIR", "~/downloads")
c.downloads.location.prompt = False
config.source("alias.py")
config.source("maps.py")
config.source("content.py")
config.source("searchengines.py")
_ = Redirects()
# Tab-Bar
# have tab bar on the right, not on the top
c.tabs.background = True
c.tabs.title.format = "{index} {audio}{perc}{current_title}"
c.tabs.position = "right"
c.tabs.width = "15%"
c.tabs.show = "multiple"
c.tabs.show_switching_delay = 2000
c.statusbar.show = "always"
c.colors.webpage.bg = "#555555"
# Prevents *all* tabs from being loaded on restore, only loads on activating them
c.session.lazy_restore = True
# for code_select.py userscript
# Allows copying code sections to clipboard easily
c.hints.selectors["code"] = [
# Selects all code tags whose direct parent is not a pre tag
":not(pre) > code",
"pre",
]
# give the browser nice theme colors
state_dir = os.environ.get("XDG_STATE_HOME", f"{os.environ['HOME']}/.local/state")
colorscheme = f"{state_dir}/qutebrowser/colorscheme.py"
if os.path.isfile(colorscheme):
config.source(colorscheme)
c.url.start_pages = "https://start.duckduckgo.com/html"