Commit e91b1f3d authored by Michael Fleming's avatar Michael Fleming
Browse files

Bug 6761: promote html help uris in the {gnome-,g,}help

	uri scheme to point to sgml files if they exist

	* components/help/Makefile.am:
	* components/help/help-method.c: (string_ends_in),
	(strdup_string_to_substring_end), (check_sgml_promotion),
	(help_name_to_local_path), (transform_file), (help_do_transform):
	* components/help/test-help-method.c: (test_failed),
	(strcmp_tolerate_nulls), (test_uri_transform), (main):
parent 11588ea6
2001-02-22 Mike Fleming <mfleming@eazel.com>
Bug 6761: promote html help uris in the {gnome-,g,}help
uri scheme to point to sgml files if they exist
* components/help/Makefile.am:
* components/help/help-method.c: (string_ends_in),
(strdup_string_to_substring_end), (check_sgml_promotion),
(help_name_to_local_path), (transform_file), (help_do_transform):
* components/help/test-help-method.c: (test_failed),
(strcmp_tolerate_nulls), (test_uri_transform), (main):
2001-02-22 Robey Pointer <robey@eazel.com>
reviewed by: Eskil Heyn Olsen <eskil@eazel.com>
......
......@@ -4,6 +4,7 @@ SUBDIRS=converters
bin_PROGRAMS=hyperbola
modules_LTLIBRARIES=libvfs-help.la
noinst_PROGRAMS=test-help-method
INCLUDES = \
-I$(top_srcdir) \
......@@ -48,6 +49,11 @@ libvfs_help_la_SOURCES = help-method.c help-method.h
libvfs_help_la_LDFLAGS = $(module_flags)
libvfs_help_la_LIBADD = $(GLIB_LIBS)
test_help_method_SOURCES = test-help-method.c
test_help_method_LIBASS = $(GLIB_LIBS)
modulesconfdir=$(sysconfdir)/vfs/modules
modulesconf_DATA = help-methods.conf
......
......@@ -226,75 +226,113 @@ convert_file_to_uri (HelpURI *help_uri, char *file)
return TRUE;
}
static HelpURI *
transform_file (const char *old_uri,
char * (* compute_uri_function) (const char *base))
static gboolean
string_ends_in (const char *string, const char *suffix)
{
HelpURI *help_uri;
char *p;
char *base, *new_uri;
size_t string_len, suffix_len;
help_uri = help_uri_new ();
string_len = strlen (string);
suffix_len = strlen (suffix);
/* Find the part after either a "?" or a "#". Only look for a
* "#" if there is no "?". (We could instead use strpbrk to
* search for the first occurence of either "?" or "#".)
*/
p = strrchr (old_uri, '?');
if (p == NULL) {
p = strrchr (old_uri, '#');
}
if (p == NULL) {
base = g_strdup (old_uri);
if (suffix_len > string_len) {
return FALSE;
} else {
help_uri->section = g_strdup (p + 1);
base = g_strndup (old_uri, p - old_uri);
return 0 == strcmp (string + strlen (string) - strlen (suffix), suffix);
}
}
/* We do not want trailing spaces or it can screw things up. */
g_strchomp (base);
static char *
strdup_string_to_substring_end (const char *string, const char *substring)
{
const char *marker;
size_t substring_length;
/* Call the passed in function to compute the URI. */
new_uri = (* compute_uri_function) (base);
g_free (base);
if (string == NULL || substring == NULL) {
return NULL;
}
if (new_uri == NULL) {
/* there is no SGML/XML or old HTML help path */
help_uri_free (help_uri);
return NULL;
}
substring_length = strlen (substring);
marker = strstr (string, substring);
/* Try the URI. */
if (convert_file_to_uri (help_uri, new_uri)) {
return help_uri;
if (marker == NULL) {
return NULL;
}
/* Failed, so return. */
g_free (new_uri);
help_uri_free (help_uri);
return NULL;
marker += substring_length;
return g_strndup (string, marker-string);
}
/* We can handle sgml, info and html files only.
/*
* bugzilla.eazel.com 6761:
* Automatically promote requests for html help to sgml help
* if available
*
* possible formats:
*
* /path/to/file[.sgml][?section]
* /path/to/file[.html][#section]
* /absolute/path/to/file[.sgml]
* ghelp:/.../gfoo/C/index.html -> ghelp:/.../gfoo/C/gfoo.sgml#index
* ghelp:/.../gfoo/C/index.html#abc -> ghelp:/.../gfoo/C/gfoo.sgml#abc
* ghelp:/.../gfoo/C/stuff.html -> ghelp:/.../gfoo/C/gfoo.sgml#stuff
* ghelp:/.../gfoo/C/stuff.html#def -> ghelp:/.../gfoo/C/gfoo.sgml#def
*/
static HelpURI *
transform_absolute_file (const char *file)
static void
check_sgml_promotion (const char *base, /*OUT*/ char **p_new_uri, /*INOUT*/ char **p_section)
{
return transform_file (file, g_strdup);
}
gchar **path_split;
char *help_dir_base;
char *sgml_path;
/* Possible cases for 'path' in this function are:
* path/to/file[.sgml]
* file[.sgml]
*/
g_return_if_fail (p_new_uri != NULL);
g_return_if_fail (p_section != NULL);
if (!string_ends_in (base, ".html")) {
*p_new_uri = g_strdup (base);
return;
}
/*
* The path format is assumed to be
* share/gnome/help/ <application> / <locale> / <resource>
* Note that the fragment has already been stripped and is passed in
* separately
*/
help_dir_base = strdup_string_to_substring_end (base, "share/gnome/help/");
if (help_dir_base == NULL) {
*p_new_uri = g_strdup (base);
return;
}
path_split = g_strsplit (base + strlen(help_dir_base), "/" , 3);
if (path_split[0] == NULL || path_split[1] == NULL || path_split[2] == NULL
|| strchr (path_split[2], '/') != NULL ) {
g_strfreev (path_split);
*p_new_uri = g_strdup (base);
return;
}
/* sgml document name should be "application.sgml" */
sgml_path = g_strconcat (help_dir_base, path_split[0], "/", path_split[1], "/", path_split[0], ".sgml", NULL);
if (g_file_exists (sgml_path)) {
*p_new_uri = sgml_path;
sgml_path = NULL;
/* resources not equal to index.html turn into sections
* if there is no section already defined
* (presense of ".html" suffix was asserted above)
*/
if (0 != strcmp (path_split[2], "index.html") && *p_section == NULL) {
/* chew off .html */
path_split[2][strlen (path_split[2]) - strlen (".html")] = '\0';
g_free (*p_section);
*p_section = g_strdup (path_split[2]);
}
} else {
*p_new_uri = g_strdup (base);
}
}
static char *
file_from_path (const char *path)
{
......@@ -331,7 +369,7 @@ file_from_path (const char *path)
/* If the help file exists returns the appropriate PATH (taking locale into account)
* otherwise it returns NULL */
static char *
find_help_file (const char *old_uri)
help_name_to_local_path (const char *old_uri)
{
char *base_name, *new_uri, *buf;
GList *language_list;
......@@ -386,10 +424,73 @@ find_help_file (const char *old_uri)
return new_uri_with_extension;
}
/* We can handle sgml, info and html files only.
*
* Possible cases for absolute paths:
*
* /path/to/file[.sgml][?section]
* /path/to/file[.html][#section]
* /absolute/path/to/file[.sgml]
*
* Possible cases for relative paths:
* path/to/file[.sgml]
* file[.sgml]
*/
static HelpURI *
transform_relative_file (const char *file)
transform_file (const char *old_uri)
{
return transform_file (file, find_help_file);
HelpURI *help_uri;
char *p;
char *base, *new_uri;
help_uri = help_uri_new ();
/* Find the part after either a "?" or a "#". Only look for a
* "#" if there is no "?". (We could instead use strpbrk to
* search for the first occurence of either "?" or "#".)
*/
p = strrchr (old_uri, '?');
if (p == NULL) {
p = strrchr (old_uri, '#');
}
if (p == NULL) {
base = g_strdup (old_uri);
} else {
help_uri->section = g_strdup (p + 1);
base = g_strndup (old_uri, p - old_uri);
}
/* We do not want trailing spaces or it can screw things up. */
g_strchomp (base);
if (base != NULL && base[0] == '/') {
/* If an html file is specifed but an sgml file is present
* we want to use that instead
*/
check_sgml_promotion (base, &new_uri, &(help_uri->section));
} else {
new_uri = help_name_to_local_path (base);
}
g_free (base);
if (new_uri == NULL) {
/* there is no SGML/XML or old HTML help path */
help_uri_free (help_uri);
return NULL;
}
/* Try the URI. */
if (convert_file_to_uri (help_uri, new_uri)) {
return help_uri;
}
/* Failed, so return. */
g_free (new_uri);
help_uri_free (help_uri);
return NULL;
}
static GnomeVFSResult
......@@ -405,11 +506,7 @@ help_do_transform (GnomeVFSTransform *transform,
return GNOME_VFS_ERROR_NOT_FOUND;
}
if (old_uri[0] == '/') {
help_uri = transform_absolute_file (old_uri);
} else {
help_uri = transform_relative_file (old_uri);
}
help_uri = transform_file (old_uri);
if (help_uri == NULL) {
return GNOME_VFS_ERROR_NOT_FOUND;
......
#include <libgnomevfs/gnome-vfs.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <glib.h>
#define STRING_OR_NULL(x) (NULL == x ? "NULL" : x)
guint gl_tests_failed = 0;
static void
test_failed (const char *uri, const char *expected, const char *got)
{
printf ("Test failed: '%s' expected '%s' got '%s'\n",
STRING_OR_NULL(uri), STRING_OR_NULL(expected), STRING_OR_NULL(got));
gl_tests_failed++;
}
static int
strcmp_tolerate_nulls (const char *s1, const char *s2)
{
if (s1 == NULL && s2 == NULL) {
return 0;
} else if (s1 == NULL) {
return -1;
} else if (s2 == NULL) {
return 1;
} else {
return strcmp (s1, s2);
}
}
static void
test_uri_transform (const char *orig, const char *expected)
{
GnomeVFSURI *uri;
char *result;
uri = gnome_vfs_uri_new (orig);
if (uri) {
result = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_NONE);
if (0 != strcmp_tolerate_nulls (expected, result)) {
test_failed (orig, expected, result);
}
} else if (expected != NULL) {
test_failed (orig, expected, NULL);
}
}
int
main (int argc, char **argv)
{
gnome_vfs_init ();
/* These test cases are bad as they assume an eazel-hacking
* install with the docs in /gnome.
*
* Note that I had HTML control-center documentation
* and SGML nautilus documentation when running the tests below
*/
test_uri_transform (
"gnome-help:control-center",
"file:///gnome/share/gnome/help/control-center/C/index.html");
test_uri_transform (
"gnome-help:/gnome/share/gnome/help/control-center/C/index.html",
"file:///gnome/share/gnome/help/control-center/C/index.html");
test_uri_transform (
"gnome-help:/gnome/share/gnome/help/control-center/C/index",
NULL);
test_uri_transform (
"ghelp:control-center",
"file:///gnome/share/gnome/help/control-center/C/index.html");
test_uri_transform (
"help:control-center",
"file:///gnome/share/gnome/help/control-center/C/index.html");
test_uri_transform (
"gnome-help:control-center#foo",
"file:///gnome/share/gnome/help/control-center/C/index.html#foo");
test_uri_transform (
"ghelp:nautilus",
"pipe:gnome-db2html2%20'%2Fgnome%2Fshare%2Fgnome%2Fhelp%2Fnautilus%2FC%2Fnautilus.sgml'%3Bmime-type%3Dtext%2Fhtml");
test_uri_transform (
"ghelp:nautilus#fragment",
"pipe:gnome-db2html2%20'%2Fgnome%2Fshare%2Fgnome%2Fhelp%2Fnautilus%2FC%2Fnautilus.sgml%3Ffragment'%3Bmime-type%3Dtext%2Fhtml");
test_uri_transform (
"ghelp:///gnome/share/gnome/help/nautilus/C/nautilus.sgml",
"pipe:gnome-db2html2%20'%2F%2F%2Fgnome%2Fshare%2Fgnome%2Fhelp%2Fnautilus%2FC%2Fnautilus.sgml'%3Bmime-type%3Dtext%2Fhtml");
test_uri_transform (
"ghelp:/gnome/share/gnome/help/nautilus/C/nautilus.sgml",
"pipe:gnome-db2html2%20'%2Fgnome%2Fshare%2Fgnome%2Fhelp%2Fnautilus%2FC%2Fnautilus.sgml'%3Bmime-type%3Dtext%2Fhtml");
test_uri_transform (
"ghelp:/gnome/share/gnome/help/nautilus/C/krak/index.html",
NULL);
test_uri_transform (
"ghelp:/gnome/share/gnome/help/nautilus/C/index.html",
"pipe:gnome-db2html2%20'%2Fgnome%2Fshare%2Fgnome%2Fhelp%2Fnautilus%2FC%2Fnautilus.sgml'%3Bmime-type%3Dtext%2Fhtml");
test_uri_transform (
"ghelp:/gnome/share/gnome/help/nautilus/C/index.html#fragment",
"pipe:gnome-db2html2%20'%2Fgnome%2Fshare%2Fgnome%2Fhelp%2Fnautilus%2FC%2Fnautilus.sgml%3Ffragment'%3Bmime-type%3Dtext%2Fhtml");
test_uri_transform (
"ghelp:/gnome/share/gnome/help/nautilus/C/stuff.html",
"pipe:gnome-db2html2%20'%2Fgnome%2Fshare%2Fgnome%2Fhelp%2Fnautilus%2FC%2Fnautilus.sgml%3Fstuff'%3Bmime-type%3Dtext%2Fhtml");
test_uri_transform (
"ghelp:/gnome/share/gnome/help/nautilus/C/stuff.html#fragment",
"pipe:gnome-db2html2%20'%2Fgnome%2Fshare%2Fgnome%2Fhelp%2Fnautilus%2FC%2Fnautilus.sgml%3Ffragment'%3Bmime-type%3Dtext%2Fhtml");
if (gl_tests_failed == 0) {
exit (0);
} else {
exit (-1);
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment