Hi,

Quoth Cole Young <c...@young.sh>:
> I thought it did have this feature. That’s wild.
> 
> > On Mar 15, 2025, at 5:05 PM, Avid Seeker <avidsee...@disroot.org> wrote:
> > 
> > I was kinda surprised suckless surf doesn't have this feature.
> > 
> > It's a surprisingly frequently asked [1] question and a straightforward
> > way to render HTML.

I found some legit use cases for this, and since it was not very hard
to achieve I wrote a small patch that consumes all the input and writes
a temporary file that then it is hijacked into a file:// uri. It is just
a proof of concept, and in case of being added would require also changes
to the man page.

--- 8< ---
>From 4d0572544e2301235edfbeae58cd3865413592f0 Mon Sep 17 00:00:00 2001
From: "Roberto E. Vargas Caballero" <k...@shike2.net>
Date: Thu, 20 Mar 2025 12:01:50 +0100
Subject: [PATCH] Render html from stdin

There are some use cases where consuming the html
from stdin can be convenient. The only possible
way to achieve  that is to write the full input
stream and then convert it to a file:// uri.
---
 surf.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

diff --git a/surf.c b/surf.c
index 18d65d7..5663a7e 100644
--- a/surf.c
+++ b/surf.c
@@ -179,6 +179,8 @@ static void spawn(Client *c, const Arg *a);
 static void msgext(Client *c, char type, const Arg *a);
 static void destroyclient(Client *c);
 static void cleanup(void);
+static void readstdin(Arg *a);
+static void removetmp(void);
 
 /* GTK/WebKit */
 static WebKitWebView *newview(Client *c, WebKitWebView *rv);
@@ -257,6 +259,7 @@ static const char *useragent;
 static Parameter *curconfig;
 static int modparams[ParameterLast];
 static int spair[2];
+static char *tmpfname;
 char *argv0;
 
 static ParamName loadtransient[] = {
@@ -1983,6 +1986,44 @@ clickexternplayer(Client *c, const Arg *a, 
WebKitHitTestResult *h)
        spawn(c, &arg);
 }
 
+void
+removetmp(void)
+{
+       remove(tmpfname);
+}
+
+void
+readstdin(Arg *a)
+{
+       FILE *fp;
+       int fd, c, r;
+       char *dir;
+       static char uri[FILENAME_MAX];
+
+       if ((dir = getenv("TMPDIR")) == NULL)
+               dir = "/tmp";
+       r =  snprintf(uri, sizeof(uri), "file://%s/%s", dir, "surf.XXXXXX");
+       if (r < 0 || r >= sizeof(uri))
+               die("temporary name too long");
+       tmpfname = uri + sizeof("file://")-1;
+       atexit(removetmp);
+
+       if ((fd = mkstemp(tmpfname)) < 0)
+               die("error creating temporary name");
+
+       if ((fp = fdopen(fd, "w")) == NULL)
+               die("error getting a temporary file pointer");
+
+       while ((c = getchar()) != EOF)
+               putc(c,  fp);
+       fflush(fp);
+
+       if (ferror(stdin) || ferror(fp))
+               die("error copying input data to temporary file");
+
+       a->v = uri;
+}
+
 int
 main(int argc, char *argv[])
 {
@@ -2117,6 +2158,9 @@ main(int argc, char *argv[])
        else
                arg.v = "about:blank";
 
+       if (strcmp(arg.v, "-") == 0)
+               readstdin(&arg);
+
        setup();
        c = newclient(NULL);
        showview(NULL, c);
-- 
2.45.3

Regards,


Reply via email to