Skip to content

Commit

Permalink
Fix relative links, better resolve "./" and "../" links (Issue #534)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelrsweet committed Dec 5, 2024
1 parent f6fb9e4 commit b4ded8a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

- Fixed a regression that caused spaces to disappear between some words
(Issue #533)
- Fixed broken links to HTML files in the document (Issue #534)
- Fixed resolution of relative links within a document (Issue #534)


# Changes in HTMLDOC v1.9.19
Expand Down
12 changes: 6 additions & 6 deletions doc/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# Makefile for HTMLDOC documentation files.
#
# Copyright © 2011-2018 by Michael R Sweet.
# Copyright © 2011-2024 by Michael R Sweet.
# Copyright © 1997-2010 by Easy Software Products.
#
# This program is free software. Distribution and use rights are outlined in
Expand Down Expand Up @@ -80,7 +80,7 @@ htmldoc.d: $(SOURCES) ../htmldoc/htmldoc$(EXEEXT)
$(RM) -r htmldoc.d; \
fi
$(MKDIR) htmldoc.d
$(VALGRIND) $(HTMLDOC) --batch htmldoc.book -t htmlsep -d htmldoc.d
$(VALGRIND) $(HTMLDOC) --batch htmldoc.book --strict -t htmlsep -d htmldoc.d


#
Expand All @@ -89,7 +89,7 @@ htmldoc.d: $(SOURCES) ../htmldoc/htmldoc$(EXEEXT)

htmldoc.epub: $(SOURCES) ../htmldoc/htmldoc$(EXEEXT)
echo Formatting htmldoc.epub...
$(VALGRIND) $(HTMLDOC) --batch htmldoc.book --titleimage htmldoc-cover.png -f htmldoc.epub
$(VALGRIND) $(HTMLDOC) --batch htmldoc.book --strict --titleimage htmldoc-cover.png -f htmldoc.epub


#
Expand All @@ -98,7 +98,7 @@ htmldoc.epub: $(SOURCES) ../htmldoc/htmldoc$(EXEEXT)

htmldoc.html: $(SOURCES) ../htmldoc/htmldoc$(EXEEXT)
echo Formatting htmldoc.html...
$(VALGRIND) $(HTMLDOC) --batch htmldoc.book -f htmldoc.html
$(VALGRIND) $(HTMLDOC) --batch htmldoc.book --strict -f htmldoc.html


#
Expand All @@ -107,7 +107,7 @@ htmldoc.html: $(SOURCES) ../htmldoc/htmldoc$(EXEEXT)

htmldoc.pdf: $(SOURCES) ../htmldoc/htmldoc$(EXEEXT)
echo Formatting htmldoc.pdf...
$(VALGRIND) $(HTMLDOC) --batch htmldoc.book -f htmldoc.pdf
$(VALGRIND) $(HTMLDOC) --batch htmldoc.book --strict -f htmldoc.pdf


#
Expand All @@ -116,4 +116,4 @@ htmldoc.pdf: $(SOURCES) ../htmldoc/htmldoc$(EXEEXT)

htmldoc.ps: $(SOURCES) ../htmldoc/htmldoc$(EXEEXT)
echo Formatting htmldoc.ps...
$(VALGRIND) $(HTMLDOC) --batch htmldoc.book -f htmldoc.ps
$(VALGRIND) $(HTMLDOC) --batch htmldoc.book --strict -f htmldoc.ps
40 changes: 34 additions & 6 deletions htmldoc/htmllib.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3584,22 +3584,28 @@ htmlFixLinks(tree_t *doc, // I - Top node
progress_error(HD_ERROR_NONE, "DEBUG: Updating links in document.");
}

DEBUG_printf(("htmlFixLinks: base=\"%s\"\n", (char *)base));

while (tree)
{
if (tree->markup == MARKUP_A && base && base[0] &&
(href = htmlGetVariable(tree, (uchar *)"HREF")) != NULL)
{
// Check if the link needs to be localized...
if (href[0] != '#' && file_method((char *)href) == NULL &&
file_method((char *)base) != NULL &&
htmlFindFile(doc, (uchar *)file_basename((char *)href)) == NULL)
DEBUG_printf(("htmlFixLinks: href=\"%s\", file_method(href)=\"%s\", file_method(base)=\"%s\"\\n", (char *)href, file_method((char *)href), file_method((char *)base)));

if (href[0] != '#' && file_method((char *)href) == NULL)
{
// Yes, localize it...
DEBUG_puts("htmlFixLinks: Localizing");

if (href[0] == '/')
{
// Absolute URL, just copy scheme, server, etc.
char *ptr; // Pointer into URL...

DEBUG_puts("htmlFixLinks: Absolute");

strlcpy(full_href, (char *)base, sizeof(full_href));

if (href[1] == '/')
Expand All @@ -3608,8 +3614,7 @@ htmlFixLinks(tree_t *doc, // I - Top node
if ((ptr = strstr(full_href, "//")) != NULL)
*ptr ='\0';
}
else if ((ptr = strstr(full_href, "//")) != NULL &&
(ptr = strchr(ptr + 2, '/')) != NULL)
else if ((ptr = strstr(full_href, "//")) != NULL && (ptr = strchr(ptr + 2, '/')) != NULL)
*ptr ='\0';

strlcat(full_href, (char *)href, sizeof(full_href));
Expand All @@ -3618,12 +3623,35 @@ htmlFixLinks(tree_t *doc, // I - Top node
{
// Relative URL of the form "./foo/bar", append href sans
// "./" to base to form full href...
DEBUG_puts("htmlFixLinks: Current directory");

snprintf(full_href, sizeof(full_href), "%s/%s", base, href + 2);
}
else if (!strncmp((char *)href, "../", 3))
{
// Relative URL of the form "../foo/bar", append href sans
// "../" to parent to form full href...
char parent[1024], *pptr; // Parent directory

strlcpy(parent, (char *)base, sizeof(parent));
if ((pptr = strrchr(parent, '/')) != NULL)
pptr[1] = '\0';
else
parent[0] = '\0';

DEBUG_printf(("htmlFixLinks: Subdirectory, parent=\"%s\"\n", parent));

snprintf(full_href, sizeof(full_href), "%s%s", parent, href + 3);
}
else
{
// Relative URL, append href to base to form full href...
snprintf(full_href, sizeof(full_href), "%s/%s", base, href);
DEBUG_puts("htmlFixLinks: Relative");

if (strcmp((char *)base, "."))
snprintf(full_href, sizeof(full_href), "%s/%s", (char *)base, (char *)href);
else
strlcpy(full_href, (char *)href, sizeof(full_href));
}

if (show_debug)
Expand Down
1 change: 1 addition & 0 deletions htmldoc/ps-pdf.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3425,6 +3425,7 @@ pdf_write_links(FILE *out) /* I - Output file */
/*
* Local link...
*/

if (link->page < (int)num_pages)
{
float x1, y1, x2, y2;
Expand Down

0 comments on commit b4ded8a

Please sign in to comment.