Skip to content

Commit 4620dee

Browse files
committed
Fix file-based links in PDF output (Issue #514)
1 parent 8003be2 commit 4620dee

File tree

3 files changed

+86
-57
lines changed

3 files changed

+86
-57
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Changes in HTMLDOC v1.9.18
22

3+
- Fixed support for links of the form "filename.html#anchor" in PDF output
4+
(Issue #514)
35
- Fixed `--header1` support for web page output (Issue #515)
46
- Fixed markdown emphasized, strong, and struck-through text (Issue 517)
57

doc/1-intro.html

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<html>
22
<head>
33
<meta name="author" content="Michael R Sweet">
4-
<meta name="copyright" content="Copyright &copy; 1997-2023, All Rights Reserved.">
5-
<meta name="docversion" content="htmldoc-1.9.17">
4+
<meta name="copyright" content="Copyright &copy; 1997-2024, All Rights Reserved.">
5+
<meta name="docversion" content="htmldoc-1.9.18">
66
<meta name="keywords" content="Software">
77
<title>HTMLDOC Users Manual</title>
88
</head>
@@ -35,19 +35,19 @@ <h2>Organization of This Manual</h2>
3535

3636
<ul>
3737

38-
<li><a href="#INTRO">Chapter 1</a> - Introduction</li>
38+
<li><a href="1-intro.html#INTRO">Chapter 1</a> - Introduction</li>
3939

40-
<li><a href="#USING">Chapter 2</a> - Using HTMLDOC</li>
40+
<li><a href="2-using.html#USING">Chapter 2</a> - Using HTMLDOC</li>
4141

42-
<li><a href="#CMDREF">Chapter 3</a> - Command-Line Reference</li>
42+
<li><a href="3-cmdref.html#CMDREF">Chapter 3</a> - Command-Line Reference</li>
4343

44-
<li><a href="#HTMLREF">Chapter 4</a> - HTML Reference</li>
44+
<li><a href="4-htmlref.html#HTMLREF">Chapter 4</a> - HTML Reference</li>
4545

46-
<li><a href="#MDREF">Chapter 5</a> - Markdown Reference</li>
46+
<li><a href="5-mdref.html#MDREF">Chapter 5</a> - Markdown Reference</li>
4747

48-
<li><a href="#LICENSE">Appendix A</a> - License Agreement</li>
48+
<li><a href="a-license.html#LICENSE">Appendix A</a> - License Agreement</li>
4949

50-
<li><a href="#BOOKFORMAT">Appendix B</a> - Book File Format</li>
50+
<li><a href="b-book.html#BOOKFORMAT">Appendix B</a> - Book File Format</li>
5151

5252
</ul>
5353

@@ -57,7 +57,7 @@ <h2>Encryption Support</h2>
5757

5858
<h2>Legal Stuff</h2>
5959

60-
<p>HTMLDOC is copyright &copy; 1997-2020 by Michael R Sweet. See <a href="#LICENSE">Appendix A - License Agreement</a> for the terms of use. This software is based in part on the work of the Independent JPEG Group and FLTK project.</p>
60+
<p>HTMLDOC is copyright &copy; 1997-2024 by Michael R Sweet. See <a href="#LICENSE">Appendix A - License Agreement</a> for the terms of use. This software is based in part on the work of the Independent JPEG Group and FLTK project.</p>
6161

6262
</div>
6363

htmldoc/ps-pdf.cxx

Lines changed: 74 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ typedef struct /**** Named link position structure */
116116
{
117117
short page, /* Page # */
118118
top; /* Top position */
119-
uchar name[124]; /* Reference name */
119+
uchar name[252]; /* Reference name */
120120
} link_t;
121121

122122
typedef struct //// Page information
@@ -320,7 +320,7 @@ static void parse_comment(tree_t *t, float *left, float *width, float *bottom,
320320

321321
static void check_pages(int page);
322322

323-
static void add_link(uchar *name, int page, int top);
323+
static void add_link(tree_t *html, uchar *name, int page, int top);
324324
static link_t *find_link(uchar *name);
325325
static int compare_links(link_t *n1, link_t *n2);
326326

@@ -3835,7 +3835,8 @@ render_contents(tree_t *t, /* I - Tree to parse */
38353835
* Add a target link...
38363836
*/
38373837

3838-
add_link(link, *page, (int)(*y + height));
3838+
add_link(NULL, link, *page, (int)(*y + height));
3839+
add_link(temp, link, *page, (int)(*y + height));
38393840
}
38403841

38413842
switch (temp->markup)
@@ -3847,7 +3848,8 @@ render_contents(tree_t *t, /* I - Tree to parse */
38473848
* Add a target link...
38483849
*/
38493850

3850-
add_link(link, *page, (int)(*y + height));
3851+
add_link(NULL, link, *page, (int)(*y + height));
3852+
add_link(temp, link, *page, (int)(*y + height));
38513853
}
38523854
break;
38533855

@@ -4156,7 +4158,8 @@ parse_doc(tree_t *t, /* I - Tree to parse */
41564158
* Add a link target using the ID=name variable...
41574159
*/
41584160

4159-
add_link(name, *page, (int)*y);
4161+
add_link(NULL, name, *page, (int)*y);
4162+
add_link(t, name, *page, (int)*y);
41604163
}
41614164
else if (t->markup == MARKUP_FILE)
41624165
{
@@ -4176,7 +4179,7 @@ parse_doc(tree_t *t, /* I - Tree to parse */
41764179
*sep = '\0';
41774180

41784181
// Add the link
4179-
add_link(newname, *page, (int)*y);
4182+
add_link(NULL, newname, *page, (int)*y);
41804183
}
41814184

41824185
if (chapter == 0 && !title_page)
@@ -5345,7 +5348,8 @@ parse_paragraph(tree_t *t, /* I - Tree to parse */
53455348
* Add a target link...
53465349
*/
53475350

5348-
add_link(link, *page, (int)(*y + height));
5351+
add_link(NULL, link, *page, (int)(*y + height));
5352+
add_link(temp, link, *page, (int)(*y + height));
53495353
}
53505354

53515355
switch (temp->markup)
@@ -5357,7 +5361,8 @@ parse_paragraph(tree_t *t, /* I - Tree to parse */
53575361
* Add a target link...
53585362
*/
53595363

5360-
add_link(link, *page, (int)(*y + height));
5364+
add_link(NULL, link, *page, (int)(*y + height));
5365+
add_link(temp, link, *page, (int)(*y + height));
53615366
}
53625367

53635368
default :
@@ -5673,7 +5678,8 @@ parse_pre(tree_t *t, /* I - Tree to parse */
56735678
* Add a target link...
56745679
*/
56755680

5676-
add_link(link, *page, (int)(*y + height));
5681+
add_link(NULL, link, *page, (int)(*y + height));
5682+
add_link(start, link, *page, (int)(*y + height));
56775683
}
56785684

56795685
switch (start->markup)
@@ -5689,7 +5695,8 @@ parse_pre(tree_t *t, /* I - Tree to parse */
56895695
* Add a target link...
56905696
*/
56915697

5692-
add_link(link, *page, (int)(*y + height));
5698+
add_link(NULL, link, *page, (int)(*y + height));
5699+
add_link(start, link, *page, (int)(*y + height));
56935700
}
56945701
break;
56955702

@@ -9035,9 +9042,10 @@ check_pages(int page) // I - Current page
90359042
*/
90369043

90379044
static void
9038-
add_link(uchar *name, /* I - Name of link */
9039-
int page, /* I - Page # */
9040-
int top) /* I - Y position */
9045+
add_link(tree_t *html, /* I - HTML node */
9046+
uchar *name, /* I - Name of link */
9047+
int page, /* I - Page # */
9048+
int top) /* I - Y position */
90419049
{
90429050
link_t *temp; /* New name */
90439051

@@ -9047,48 +9055,57 @@ add_link(uchar *name, /* I - Name of link */
90479055

90489056
DEBUG_printf(("add_link(name=\"%s\", page=%d, top=%d)\n", name, page, top));
90499057

9050-
if ((temp = find_link(name)) != NULL)
9058+
if (!html && (temp = find_link(name)) != NULL)
90519059
{
90529060
temp->page = (short)page;
90539061
temp->top = (short)top;
9062+
return;
90549063
}
9055-
else
9056-
{
9057-
// See if we need to allocate memory for links...
9058-
if (num_links >= alloc_links)
9059-
{
9060-
// Allocate more links...
9061-
alloc_links += ALLOC_LINKS;
90629064

9063-
if (num_links == 0)
9064-
temp = (link_t *)malloc(sizeof(link_t) * alloc_links);
9065-
else
9066-
temp = (link_t *)realloc(links, sizeof(link_t) * alloc_links);
9065+
// See if we need to allocate memory for links...
9066+
if (num_links >= alloc_links)
9067+
{
9068+
// Allocate more links...
9069+
alloc_links += ALLOC_LINKS;
90679070

9068-
if (temp == NULL)
9069-
{
9070-
progress_error(HD_ERROR_OUT_OF_MEMORY,
9071-
"Unable to allocate memory for %d links - %s",
9072-
(int)alloc_links, strerror(errno));
9073-
alloc_links -= ALLOC_LINKS;
9074-
return;
9075-
}
9071+
if (num_links == 0)
9072+
temp = (link_t *)malloc(sizeof(link_t) * alloc_links);
9073+
else
9074+
temp = (link_t *)realloc(links, sizeof(link_t) * alloc_links);
90769075

9077-
links = temp;
9076+
if (temp == NULL)
9077+
{
9078+
progress_error(HD_ERROR_OUT_OF_MEMORY, "Unable to allocate memory for %d links - %s", (int)alloc_links, strerror(errno));
9079+
alloc_links -= ALLOC_LINKS;
9080+
return;
90789081
}
90799082

9080-
// Add a new link...
9081-
temp = links + num_links;
9082-
num_links ++;
9083+
links = temp;
9084+
}
90839085

9084-
strlcpy((char *)temp->name, (char *)name, sizeof(temp->name));
9085-
temp->page = (short)page;
9086-
temp->top = (short)top;
9086+
// Add a new link...
9087+
temp = links + num_links;
9088+
num_links ++;
90879089

9088-
if (num_links > 1)
9089-
qsort(links, num_links, sizeof(link_t),
9090-
(compare_func_t)compare_links);
9090+
if (html)
9091+
{
9092+
uchar *filename; /* Filename */
9093+
9094+
if ((filename = htmlGetVariable(html->parent, (uchar *)"_HD_FILENAME")) != NULL)
9095+
snprintf((char *)temp->name, sizeof(temp->name), "%s#%s", (char *)filename, (char *)name);
9096+
else
9097+
strlcpy((char *)temp->name, (char *)name, sizeof(temp->name));
9098+
}
9099+
else
9100+
{
9101+
strlcpy((char *)temp->name, (char *)name, sizeof(temp->name));
90919102
}
9103+
9104+
temp->page = (short)page;
9105+
temp->top = (short)top;
9106+
9107+
if (num_links > 1)
9108+
qsort(links, num_links, sizeof(link_t), (compare_func_t)compare_links);
90929109
}
90939110

90949111

@@ -9662,10 +9679,20 @@ static tree_t * /* O - Flattened markup tree */
96629679
flatten_tree(tree_t *t) /* I - Markup tree to flatten */
96639680
{
96649681
tree_t *temp, /* New tree node */
9682+
*parent, /* Parent node (for file info) */
96659683
*flat; /* Flattened tree */
96669684

96679685

9668-
flat = NULL;
9686+
flat = NULL;
9687+
parent = NULL;
9688+
for (temp = t; temp != NULL; temp = temp->parent)
9689+
{
9690+
if (temp->markup == MARKUP_FILE)
9691+
{
9692+
parent = temp;
9693+
break;
9694+
}
9695+
}
96699696

96709697
while (t != NULL)
96719698
{
@@ -9680,7 +9707,7 @@ flatten_tree(tree_t *t) /* I - Markup tree to flatten */
96809707
case MARKUP_IMG :
96819708
temp = (tree_t *)calloc(sizeof(tree_t), 1);
96829709
memcpy(temp, t, sizeof(tree_t));
9683-
temp->parent = NULL;
9710+
temp->parent = parent;
96849711
temp->child = NULL;
96859712
temp->prev = flat;
96869713
temp->next = NULL;
@@ -9697,7 +9724,7 @@ flatten_tree(tree_t *t) /* I - Markup tree to flatten */
96979724
{
96989725
temp = (tree_t *)calloc(sizeof(tree_t), 1);
96999726
memcpy(temp, t, sizeof(tree_t));
9700-
temp->parent = NULL;
9727+
temp->parent = parent;
97019728
temp->child = NULL;
97029729
temp->prev = flat;
97039730
temp->next = NULL;
@@ -9736,7 +9763,7 @@ flatten_tree(tree_t *t) /* I - Markup tree to flatten */
97369763
case MARKUP_CAPTION :
97379764
temp = (tree_t *)calloc(sizeof(tree_t), 1);
97389765
temp->markup = MARKUP_BR;
9739-
temp->parent = NULL;
9766+
temp->parent = parent;
97409767
temp->child = NULL;
97419768
temp->prev = flat;
97429769
temp->next = NULL;

0 commit comments

Comments
 (0)