logo

badwolf

minimalist and privacy-oriented web browser based on WebKitGTK git clone https://hacktivis.me/git/badwolf.git
commit: 1afe2e700ebcf81cac37d95a9955620688eafa77
parent ab49503d9ff05e5894dc6c4f1d136c1dde2f6c50
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Tue,  4 Jun 2024 14:58:23 +0200

Add man: scheme handling

Diffstat:

Mbadwolf.18++++++++
Mbadwolf.c3+++
Mbadwolf.desktop2+-
Mbadwolf.h1+
Mconfig.h5+++++
Mconfigure4++--
Aman_uri_scheme.c45+++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 65 insertions(+), 3 deletions(-)

diff --git a/badwolf.1 b/badwolf.1 @@ -23,6 +23,14 @@ Disable Images. .It Fl S Enable JavaScript. .El +.Sh CUSTOM SCHEMES +.Nm +supports the +.Ql man: +scheme, where for example +.Xr man 1 +maps to +.Lk man:man.1 .Sh KEYBINDINGS The following section lists the keybinding by their action, each item is described by the widget the focus is on or .Aq any diff --git a/badwolf.c b/badwolf.c @@ -739,6 +739,9 @@ new_browser(const gchar *target_url, struct Client *old_browser) webkit_web_context_set_spell_checking_enabled(web_context, TRUE); } + + webkit_web_context_register_uri_scheme( + web_context, "man", man_uri_scheme_request_cb, NULL, NULL); } WebKitSettings *settings = webkit_settings_new_with_settings(BADWOLF_WEBKIT_SETTINGS); diff --git a/badwolf.desktop b/badwolf.desktop @@ -19,5 +19,5 @@ Keywords=Internet;WWW;Browser;Web;WebKit;WebKitGTK Keywords[fr]=Internet;WWW;Browser;Web;Surfer;Navigateur;WebKit;WebKitGTK Keywords[sr]=Internet;WWW;Browser;Web;WebKit;WebKitGTK;Интернет;Веб;Читач Keywords[tr]=Internet;WWW;Browser;Web;WebKit;WebKitGTK;İnternet;Tarayıcı; -MimeType=text/html;text/xml;application/xhtml+xml;x-scheme-handler/http;x-scheme-handler/https +MimeType=text/html;text/xml;application/xhtml+xml;x-scheme-handler/http;x-scheme-handler/https;x-scheme-handler/man Name=Badwolf diff --git a/badwolf.h b/badwolf.h @@ -58,4 +58,5 @@ void webView_tab_label_change(struct Client *browser, const gchar *title); struct Client *new_browser(const gchar *target_url, struct Client *old_browser); int badwolf_new_tab(GtkNotebook *notebook, struct Client *browser, bool auto_switch); gint badwolf_get_tab_position(GtkContainer *notebook, GtkWidget *child); +extern void man_uri_scheme_request_cb(WebKitURISchemeRequest *request, gpointer user_data); #endif /* BADWOLF_H_INCLUDED */ diff --git a/config.h b/config.h @@ -109,4 +109,9 @@ // BADWOLF_LOCATION_INLINE_SELECTION: show selected completion as a selection in location entry #define BADWOLF_LOCATION_INLINE_SELECTION TRUE +// BADWOLF_MAN_HTML_CMD: Command to render manpages to HTML. +// Needs to support `[section] name` as additional arguments. +// Default one made for mandoc +#define BADWOLF_MAN_HTML_CMD "man", "-Thtml", "-Oman=man:%N.%S", "-Otoc" + #endif /* CONFIG_H_INCLUDED */ diff --git a/configure b/configure @@ -6,8 +6,8 @@ VERSION=1.2.0 VERSION_FULL=${VERSION}$(./version.sh) DEPS="gtk+-3.0 libxml-2.0" -SRCS="bookmarks.c userscripts.c fmt.c fmt_test.c uri.c uri_test.c keybindings.c downloads.c badwolf.c" -OBJS="bookmarks.o userscripts.o fmt.o uri.o keybindings.o downloads.o badwolf.o" +SRCS="bookmarks.c userscripts.c fmt.c fmt_test.c uri.c uri_test.c keybindings.c downloads.c badwolf.c man_uri_scheme.c" +OBJS="bookmarks.o userscripts.o fmt.o uri.o keybindings.o downloads.o badwolf.o man_uri_scheme.o" OBJS_test="fmt_test.o uri_test.o bookmarks_test.o" EXE=badwolf EXE_test="fmt_test uri_test bookmarks_test" diff --git a/man_uri_scheme.c b/man_uri_scheme.c @@ -0,0 +1,45 @@ +// SPDX-FileCopyrightText: 2019 Badwolf Authors <https://hacktivis.me/projects/badwolf> +// SPDX-License-Identifier: BSD-3-Clause + +#include "badwolf.h" +#include "config.h" + +void +man_uri_scheme_request_cb(WebKitURISchemeRequest *request, gpointer user_data) +{ + const char *req_path = webkit_uri_scheme_request_get_path(request); + + if(req_path == NULL || req_path[0] == 0) req_path = "man"; + + char *path = strdup(req_path); + + char *man_ref[2] = {path, NULL}; + + char *sep = strrchr(path, '.'); + if(sep) + { + sep[0] = 0; + man_ref[0] = sep + 1; + man_ref[1] = path; + } + + GSubprocessFlags flags_subproc = G_SUBPROCESS_FLAGS_STDOUT_PIPE | G_SUBPROCESS_FLAGS_STDERR_MERGE; + + GError *error_proc = NULL; + GSubprocess *man_proc = g_subprocess_new( + flags_subproc, &error_proc, BADWOLF_MAN_HTML_CMD, man_ref[0], man_ref[1], NULL); + if(error_proc != NULL) + { + webkit_uri_scheme_request_finish_error(request, error_proc); + free(path); + return; + } + + GInputStream *stream = g_subprocess_get_stdout_pipe(man_proc); + + // Pipe the contents out, can't wait beforehand as the buffer needs to drain on large manpages + webkit_uri_scheme_request_finish(request, stream, -1, "text/html"); + + free(path); + g_object_unref(stream); +}