[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[no subject]
From: |
Gavin D. Smith |
Date: |
Sun, 20 Mar 2022 04:55:11 -0400 (EDT) |
branch: master
commit 2b35cfa8894502ca5984b637e9a95aed85e36f03
Author: Gavin Smith <gavinsmith0123@gmail.com>
AuthorDate: Sun May 26 10:25:52 2019 +0100
intercept all links and do path search
---
js/wkinfo/Makefile.am | 2 +-
js/wkinfo/extension.c | 47 +++++++++++++++++++-
js/wkinfo/infopath.c | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++
js/wkinfo/infopath.h | 16 +++++++
js/wkinfo/main.c | 8 +++-
5 files changed, 184 insertions(+), 5 deletions(-)
diff --git a/js/wkinfo/Makefile.am b/js/wkinfo/Makefile.am
index 3d98a8185b..08a6bfbc51 100644
--- a/js/wkinfo/Makefile.am
+++ b/js/wkinfo/Makefile.am
@@ -3,7 +3,7 @@ ACLOCAL_AMFLAGS = -I m4
webextension_LTLIBRARIES = libmyappwebextension.la
webextensiondir = $(libdir)/MyApp/web-extension
-libmyappwebextension_la_SOURCES = extension.c common.h
+libmyappwebextension_la_SOURCES = extension.c common.h infopath.c infopath.h
libmyappwebextension_la_CFLAGS = $(AM_CFLAGS) $(WEB_EXTENSION_CFLAGS)
libmyappwebextension_la_LIBADD = $(WEB_EXTENSION_LIBS)
libmyappwebextension_la_LDFLAGS = -module -avoid-version -no-undefined
diff --git a/js/wkinfo/extension.c b/js/wkinfo/extension.c
index 5e4687cd37..8bab4cf599 100644
--- a/js/wkinfo/extension.c
+++ b/js/wkinfo/extension.c
@@ -13,6 +13,7 @@
#include <webkit2/webkit-web-extension.h>
#include "common.h"
+#include "infopath.h"
/* For communicating with the main Gtk process */
static struct sockaddr_un main_name;
@@ -30,6 +31,33 @@ remove_our_socket (void)
unlink (our_socket_file);
}
+
+static char *current_manual;
+
+/* Called from request_callback. */
+void
+load_manual (char *manual, WebKitURIRequest *request)
+{
+ char *new_manual = locate_manual (manual);
+ g_print ("NEW MANUAL AT %s\n", new_manual);
+
+ if (!new_manual)
+ {
+ free (manual);
+ return;
+ }
+
+ GString *s = g_string_new (NULL);
+ g_string_append (s, "file:");
+ g_string_append (s, new_manual);
+
+ current_manual = manual;
+
+ webkit_uri_request_set_uri (request, s->str);
+ g_string_free (s, TRUE);
+ free (new_manual);
+}
+
gboolean
request_callback (WebKitWebPage *web_page,
WebKitURIRequest *request,
@@ -57,11 +85,26 @@ request_callback (WebKitWebPage *web_page,
GINT_TO_POINTER(1));
}
+ return FALSE;
}
- /* Could block external links here */
+ if (!memcmp (uri, "file:", 5))
+ {
+ g_print ("finding manual and node\n");
+
+ char *manual, *node;
+ /* The links in the HTML files should be relative links like
+ "../MANUAL/NODE.html" but by the time this function is called
+ they are absolute paths beginning "file:/". */
+ parse_external_url (uri, &manual, &node);
+
+ if (!current_manual || strcmp(manual, current_manual) != 0)
+ {
+ load_manual (manual, request);
+ }
+ }
- return false;
+ return FALSE;
}
void
diff --git a/js/wkinfo/infopath.c b/js/wkinfo/infopath.c
new file mode 100644
index 0000000000..1a61051839
--- /dev/null
+++ b/js/wkinfo/infopath.c
@@ -0,0 +1,116 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <dirent.h>
+#include <sys/stat.h>
+
+#include "infopath.h"
+
+static char *datadir;
+long datadir_len;
+
+/* Return pathname of the main index.html file for a HTML manual.*/
+char *
+locate_manual (const char *manual)
+{
+ if (!datadir)
+ {
+ datadir = getenv ("INFO_HTML_DIR");
+ if (!datadir)
+ return 0;
+ datadir_len = strlen (datadir);
+ g_print ("datadir is %s\n", datadir);
+ }
+
+ /* Check if datadir exists. */
+ DIR *d = opendir (datadir);
+ if (!d)
+ {
+ fprintf (stderr, "Could not open %s\n", datadir);
+ return 0;
+ }
+ closedir (d);
+
+ char *s = malloc (datadir_len + strlen ("/") + strlen (manual) + 1);
+ sprintf (s, "%s/%s", datadir, manual);
+
+ d = opendir (s);
+ if (!d)
+ {
+ fprintf (stderr, "Could not open %s\n", s);
+ free (s);
+ return 0;
+ }
+ closedir (d);
+
+ free (s);
+ s = malloc (datadir_len + strlen ("/")
+ + strlen (manual) + strlen ("/index.html") + 1);
+ sprintf (s, "%s/%s/index.html", datadir, manual);
+
+ struct stat dummy;
+ if (stat (s, &dummy) == -1)
+ {
+ fprintf (stderr, "no file %s\n", s);
+ return 0;
+ }
+ return s;
+}
+
+/* Extract the manual and node from a URL like "file:/.../MANUAL/NODE.html".
*/
+void
+parse_external_url (const char *url, char **manual, char **node)
+{
+ char *m = 0, *n = 0;
+
+ /* Find the second last / in the url. */
+ char *p1 = 0, *p2 = 0;
+ char *p, *q;
+
+ p1 = strchr (url, '/');
+ if (!p1)
+ goto failure;
+ p2 = strchr (p1 + 1, '/');
+ if (!p2)
+ goto failure;
+
+ while (1)
+ {
+ /* p1 and p2 are two subsequent / in the string. */
+
+ q = strchr (p2+1, '/');
+ if (!q)
+ break;
+
+ p1 = p2;
+ p2 = q;
+ }
+
+ p = p1 + 1;
+ q = p2;
+
+ m = malloc (q - p + 1);
+ memcpy (m, p, q - p);
+ m[q - p] = '\0';
+
+ *manual = m;
+
+ q++; /* after '/' */
+ p = strchr (q, '.');
+ if (memcmp (p, ".html", 5) != 0)
+ goto failure;
+ n = malloc (p - q + 1);
+ memcpy (n, q, p - q);
+ n[p - q] = '\0';
+
+ *node = n;
+
+ return;
+
+failure:
+ g_print ("failure\n");
+ free (m); free(n);
+ *manual = 0;
+ *node = 0;
+ return;
+}
diff --git a/js/wkinfo/infopath.h b/js/wkinfo/infopath.h
new file mode 100644
index 0000000000..4749cef45d
--- /dev/null
+++ b/js/wkinfo/infopath.h
@@ -0,0 +1,16 @@
+#ifndef INFOPATH_H
+#define INFOPATH_H
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+char *locate_manual (const char *manual);
+void parse_external_url (const char *url, char **manual, char **node);
+
+#ifdef __cplusplus
+} // extern "C"
+#endif
+
+#endif // INFOPATH_H
diff --git a/js/wkinfo/main.c b/js/wkinfo/main.c
index 4b17584e5e..73f84237b8 100644
--- a/js/wkinfo/main.c
+++ b/js/wkinfo/main.c
@@ -377,7 +377,7 @@ main(int argc, char* argv[])
/* This is used to use a separate process for the web browser
that looks up the index files. This stops the program from freezing
while the index files are processed. */
- if (1)
+ if (0)
{
webkit_web_context_set_process_model (
webkit_web_context_get_default (),
@@ -435,10 +435,14 @@ main(int argc, char* argv[])
and keyboard events. */
gtk_widget_grab_focus (GTK_WIDGET(webView));
+#define MANUAL "hello"
+
GString *s = g_string_new (NULL);
g_string_append (s, "file:");
g_string_append (s, info_dir);
- g_string_append (s, "/hello/index.html");
+ g_string_append (s, "/");
+ g_string_append (s, MANUAL);
+ g_string_append (s, "/index.html");
webkit_web_view_load_uri (webView, s->str);
g_string_free (s, TRUE);
- [no subject], (continued)
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject],
Gavin D. Smith <=
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20
- [no subject], Gavin D. Smith, 2022/03/20