Skip to content

Commit b4ded8a

Browse files
committed
Fix relative links, better resolve "./" and "../" links (Issue #534)
1 parent f6fb9e4 commit b4ded8a

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
lines changed

CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

77

88
# Changes in HTMLDOC v1.9.19

doc/Makefile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#
22
# Makefile for HTMLDOC documentation files.
33
#
4-
# Copyright © 2011-2018 by Michael R Sweet.
4+
# Copyright © 2011-2024 by Michael R Sweet.
55
# Copyright © 1997-2010 by Easy Software Products.
66
#
77
# This program is free software. Distribution and use rights are outlined in
@@ -80,7 +80,7 @@ htmldoc.d: $(SOURCES) ../htmldoc/htmldoc$(EXEEXT)
8080
$(RM) -r htmldoc.d; \
8181
fi
8282
$(MKDIR) htmldoc.d
83-
$(VALGRIND) $(HTMLDOC) --batch htmldoc.book -t htmlsep -d htmldoc.d
83+
$(VALGRIND) $(HTMLDOC) --batch htmldoc.book --strict -t htmlsep -d htmldoc.d
8484

8585

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

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

9494

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

9999
htmldoc.html: $(SOURCES) ../htmldoc/htmldoc$(EXEEXT)
100100
echo Formatting htmldoc.html...
101-
$(VALGRIND) $(HTMLDOC) --batch htmldoc.book -f htmldoc.html
101+
$(VALGRIND) $(HTMLDOC) --batch htmldoc.book --strict -f htmldoc.html
102102

103103

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

108108
htmldoc.pdf: $(SOURCES) ../htmldoc/htmldoc$(EXEEXT)
109109
echo Formatting htmldoc.pdf...
110-
$(VALGRIND) $(HTMLDOC) --batch htmldoc.book -f htmldoc.pdf
110+
$(VALGRIND) $(HTMLDOC) --batch htmldoc.book --strict -f htmldoc.pdf
111111

112112

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

117117
htmldoc.ps: $(SOURCES) ../htmldoc/htmldoc$(EXEEXT)
118118
echo Formatting htmldoc.ps...
119-
$(VALGRIND) $(HTMLDOC) --batch htmldoc.book -f htmldoc.ps
119+
$(VALGRIND) $(HTMLDOC) --batch htmldoc.book --strict -f htmldoc.ps

htmldoc/htmllib.cxx

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3584,22 +3584,28 @@ htmlFixLinks(tree_t *doc, // I - Top node
35843584
progress_error(HD_ERROR_NONE, "DEBUG: Updating links in document.");
35853585
}
35863586

3587+
DEBUG_printf(("htmlFixLinks: base=\"%s\"\n", (char *)base));
3588+
35873589
while (tree)
35883590
{
35893591
if (tree->markup == MARKUP_A && base && base[0] &&
35903592
(href = htmlGetVariable(tree, (uchar *)"HREF")) != NULL)
35913593
{
35923594
// Check if the link needs to be localized...
3593-
if (href[0] != '#' && file_method((char *)href) == NULL &&
3594-
file_method((char *)base) != NULL &&
3595-
htmlFindFile(doc, (uchar *)file_basename((char *)href)) == NULL)
3595+
DEBUG_printf(("htmlFixLinks: href=\"%s\", file_method(href)=\"%s\", file_method(base)=\"%s\"\\n", (char *)href, file_method((char *)href), file_method((char *)base)));
3596+
3597+
if (href[0] != '#' && file_method((char *)href) == NULL)
35963598
{
35973599
// Yes, localize it...
3600+
DEBUG_puts("htmlFixLinks: Localizing");
3601+
35983602
if (href[0] == '/')
35993603
{
36003604
// Absolute URL, just copy scheme, server, etc.
36013605
char *ptr; // Pointer into URL...
36023606

3607+
DEBUG_puts("htmlFixLinks: Absolute");
3608+
36033609
strlcpy(full_href, (char *)base, sizeof(full_href));
36043610

36053611
if (href[1] == '/')
@@ -3608,8 +3614,7 @@ htmlFixLinks(tree_t *doc, // I - Top node
36083614
if ((ptr = strstr(full_href, "//")) != NULL)
36093615
*ptr ='\0';
36103616
}
3611-
else if ((ptr = strstr(full_href, "//")) != NULL &&
3612-
(ptr = strchr(ptr + 2, '/')) != NULL)
3617+
else if ((ptr = strstr(full_href, "//")) != NULL && (ptr = strchr(ptr + 2, '/')) != NULL)
36133618
*ptr ='\0';
36143619

36153620
strlcat(full_href, (char *)href, sizeof(full_href));
@@ -3618,12 +3623,35 @@ htmlFixLinks(tree_t *doc, // I - Top node
36183623
{
36193624
// Relative URL of the form "./foo/bar", append href sans
36203625
// "./" to base to form full href...
3626+
DEBUG_puts("htmlFixLinks: Current directory");
3627+
36213628
snprintf(full_href, sizeof(full_href), "%s/%s", base, href + 2);
36223629
}
3630+
else if (!strncmp((char *)href, "../", 3))
3631+
{
3632+
// Relative URL of the form "../foo/bar", append href sans
3633+
// "../" to parent to form full href...
3634+
char parent[1024], *pptr; // Parent directory
3635+
3636+
strlcpy(parent, (char *)base, sizeof(parent));
3637+
if ((pptr = strrchr(parent, '/')) != NULL)
3638+
pptr[1] = '\0';
3639+
else
3640+
parent[0] = '\0';
3641+
3642+
DEBUG_printf(("htmlFixLinks: Subdirectory, parent=\"%s\"\n", parent));
3643+
3644+
snprintf(full_href, sizeof(full_href), "%s%s", parent, href + 3);
3645+
}
36233646
else
36243647
{
36253648
// Relative URL, append href to base to form full href...
3626-
snprintf(full_href, sizeof(full_href), "%s/%s", base, href);
3649+
DEBUG_puts("htmlFixLinks: Relative");
3650+
3651+
if (strcmp((char *)base, "."))
3652+
snprintf(full_href, sizeof(full_href), "%s/%s", (char *)base, (char *)href);
3653+
else
3654+
strlcpy(full_href, (char *)href, sizeof(full_href));
36273655
}
36283656

36293657
if (show_debug)

htmldoc/ps-pdf.cxx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3425,6 +3425,7 @@ pdf_write_links(FILE *out) /* I - Output file */
34253425
/*
34263426
* Local link...
34273427
*/
3428+
34283429
if (link->page < (int)num_pages)
34293430
{
34303431
float x1, y1, x2, y2;

0 commit comments

Comments
 (0)