Hi,

Anton Lindqvist wrote on Sun, Feb 05, 2017 at 10:30:32AM +0100:

> Here's a proposal to add unique HTML titles to man-pages served
> using man.cgi.  The name of the man-page is used as a title prefix

That made sense to me, so i committed the version below
and installed it on man.openbsd.org.  Please tell me if you see
anything that goes wrong.

> (inspired by NetBSD's adoption of mandoc).

What?  They talked about wanting to use the mandoc-based man.cgi(8)
in the past, but it doesn't look like they actually implemented it.
The following still uses some other man.cgi implementation:

  http://netbsd.gw.com/cgi-bin/man-cgi

But it does have the manual page name and section prefixed to
the HTML title, maybe that's what you meant.

> There might be a more elegant way to produce the title given the
> filename.

True, ETOOMUCHMALLOC.

I implemented it as shown below, without any additional dynamic
memory allocation.

Thanks for the idea and the patch,
  Ingo


Log Message:
-----------
Mention the manual page name and section in the HTML page <title>.
Based on a patch from <Anton dot Lindqvist at gmail dot com>,
but simplified and also covering apropos(1) search results.

Modified Files:
--------------
    mdocml:
        cgi.c
        man.cgi.3

Revision Data
-------------
Index: cgi.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/cgi.c,v
retrieving revision 1.149
retrieving revision 1.150
diff -Lcgi.c -Lcgi.c -u -p -r1.149 -r1.150
--- cgi.c
+++ cgi.c
@@ -81,7 +81,7 @@ static        void             pg_search(const struct req
 static void             pg_searchres(const struct req *,
                                struct manpage *, size_t);
 static void             pg_show(struct req *, const char *);
-static void             resp_begin_html(int, const char *);
+static void             resp_begin_html(int, const char *, const char *);
 static void             resp_begin_http(int, const char *);
 static void             resp_catman(const struct req *, const char *);
 static void             resp_copy(const char *);
@@ -346,8 +346,9 @@ resp_copy(const char *filename)
 }
 
 static void
-resp_begin_html(int code, const char *msg)
+resp_begin_html(int code, const char *msg, const char *file)
 {
+       char    *cp;
 
        resp_begin_http(code, msg);
 
@@ -357,10 +358,20 @@ resp_begin_html(int code, const char *ms
               "  <meta charset=\"UTF-8\"/>\n"
               "  <link rel=\"stylesheet\" href=\"%s/mandoc.css\""
               " type=\"text/css\" media=\"all\">\n"
-              "  <title>%s</title>\n"
+              "  <title>",
+              CSS_DIR);
+       if (file != NULL) {
+               if ((cp = strrchr(file, '/')) != NULL)
+                       file = cp + 1;
+               if ((cp = strrchr(file, '.')) != NULL) {
+                       printf("%.*s(%s) - ", (int)(cp - file), file, cp + 1);
+               } else
+                       printf("%s - ", file);
+       }
+       printf("%s</title>\n"
               "</head>\n"
               "<body>\n",
-              CSS_DIR, CUSTOMIZE_TITLE);
+              CUSTOMIZE_TITLE);
 
        resp_copy(MAN_DIR "/header.html");
 }
@@ -493,7 +504,7 @@ static void
 pg_index(const struct req *req)
 {
 
-       resp_begin_html(200, NULL);
+       resp_begin_html(200, NULL, NULL);
        resp_searchform(req, FOCUS_QUERY);
        printf("<p>\n"
               "This web interface is documented in the\n"
@@ -510,7 +521,7 @@ pg_index(const struct req *req)
 static void
 pg_noresult(const struct req *req, const char *msg)
 {
-       resp_begin_html(200, NULL);
+       resp_begin_html(200, NULL, NULL);
        resp_searchform(req, FOCUS_QUERY);
        puts("<p>");
        puts(msg);
@@ -522,7 +533,7 @@ static void
 pg_error_badrequest(const char *msg)
 {
 
-       resp_begin_html(400, "Bad Request");
+       resp_begin_html(400, "Bad Request", NULL);
        puts("<h1>Bad Request</h1>\n"
             "<p>\n");
        puts(msg);
@@ -535,7 +546,7 @@ pg_error_badrequest(const char *msg)
 static void
 pg_error_internal(void)
 {
-       resp_begin_html(500, "Internal Server Error");
+       resp_begin_html(500, "Internal Server Error", NULL);
        puts("<p>Internal Server Error</p>");
        resp_end_html();
 }
@@ -591,36 +602,13 @@ pg_searchres(const struct req *req, stru
                return;
        }
 
-       resp_begin_html(200, NULL);
-       resp_searchform(req,
-           req->q.equal || sz == 1 ? FOCUS_NONE : FOCUS_QUERY);
-
-       if (sz > 1) {
-               puts("<table class=\"results\">");
-               for (i = 0; i < sz; i++) {
-                       printf("  <tr>\n"
-                              "    <td>"
-                              "<a class=\"Xr\" href=\"/%s%s%s/%s\">",
-                           scriptname, *scriptname == '\0' ? "" : "/",
-                           req->q.manpath, r[i].file);
-                       html_print(r[i].names);
-                       printf("</a></td>\n"
-                              "    <td><span class=\"Nd\">");
-                       html_print(r[i].output);
-                       puts("</span></td>\n"
-                            "  </tr>");
-               }
-               puts("</table>");
-       }
-
        /*
         * In man(1) mode, show one of the pages
         * even if more than one is found.
         */
 
+       iuse = 0;
        if (req->q.equal || sz == 1) {
-               puts("<hr>");
-               iuse = 0;
                priouse = 20;
                archpriouse = 3;
                for (i = 0; i < sz; i++) {
@@ -653,6 +641,33 @@ pg_searchres(const struct req *req, stru
                        priouse = prio;
                        iuse = i;
                }
+               resp_begin_html(200, NULL, r[iuse].file);
+       } else
+               resp_begin_html(200, NULL, NULL);
+
+       resp_searchform(req,
+           req->q.equal || sz == 1 ? FOCUS_NONE : FOCUS_QUERY);
+
+       if (sz > 1) {
+               puts("<table class=\"results\">");
+               for (i = 0; i < sz; i++) {
+                       printf("  <tr>\n"
+                              "    <td>"
+                              "<a class=\"Xr\" href=\"/%s%s%s/%s\">",
+                           scriptname, *scriptname == '\0' ? "" : "/",
+                           req->q.manpath, r[i].file);
+                       html_print(r[i].names);
+                       printf("</a></td>\n"
+                              "    <td><span class=\"Nd\">");
+                       html_print(r[i].output);
+                       puts("</span></td>\n"
+                            "  </tr>");
+               }
+               puts("</table>");
+       }
+
+       if (req->q.equal || sz == 1) {
+               puts("<hr>");
                resp_show(req, r[iuse].file);
        }
 
@@ -904,7 +919,7 @@ pg_show(struct req *req, const char *ful
                return;
        }
 
-       resp_begin_html(200, NULL);
+       resp_begin_html(200, NULL, file);
        resp_searchform(req, FOCUS_NONE);
        resp_show(req, file);
        resp_end_html();
Index: man.cgi.3
===================================================================
RCS file: /home/cvs/mdocml/mdocml/man.cgi.3,v
retrieving revision 1.3
retrieving revision 1.4
diff -Lman.cgi.3 -Lman.cgi.3 -u -p -r1.3 -r1.4
--- man.cgi.3
+++ man.cgi.3
@@ -1,6 +1,6 @@
 .\"    $Id$
 .\"
-.\" Copyright (c) 2016 Ingo Schwarze <[email protected]>
+.\" Copyright (c) 2016, 2017 Ingo Schwarze <[email protected]>
 .\"
 .\" Permission to use, copy, modify, and distribute this software for any
 .\" purpose with or without fee is hereby granted, provided that the above
@@ -237,13 +237,18 @@ and
 are used.
 The highest level result generators are:
 .Bl -tag -width 1n
-.It Ft void Fn resp_begin_html "int code" "const char *msg"
+.It Ft void Fn resp_begin_html "int code" "const char *msg" "const char *file"
 This generator calls
 .Fn resp_begin_http
 to print the HTTP headers, then prints the HTML header up to the
 opening tag of the <body> element, then copies the file
 .Pa header.html
 to the output, if it exists and is readable.
+If
+.Fa file
+is not
+.Dv NULL ,
+it is used for the <title> element.
 .It Ft void Fn resp_searchform "const struct req *req" "enum focus focus"
 This generator prints a search form, filling it with data
 from the provided request object.

Reply via email to