Skip to content

Commit

Permalink
Fix file-based links in PDF output (Issue #514)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelrsweet committed Feb 11, 2024
1 parent 8003be2 commit 4620dee
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 57 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Changes in HTMLDOC v1.9.18

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

Expand Down
20 changes: 10 additions & 10 deletions doc/1-intro.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<html>
<head>
<meta name="author" content="Michael R Sweet">
<meta name="copyright" content="Copyright &copy; 1997-2023, All Rights Reserved.">
<meta name="docversion" content="htmldoc-1.9.17">
<meta name="copyright" content="Copyright &copy; 1997-2024, All Rights Reserved.">
<meta name="docversion" content="htmldoc-1.9.18">
<meta name="keywords" content="Software">
<title>HTMLDOC Users Manual</title>
</head>
Expand Down Expand Up @@ -35,19 +35,19 @@ <h2>Organization of This Manual</h2>

<ul>

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

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

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

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

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

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

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

</ul>

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

<h2>Legal Stuff</h2>

<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>
<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>

</div>

Expand Down
121 changes: 74 additions & 47 deletions htmldoc/ps-pdf.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ typedef struct /**** Named link position structure */
{
short page, /* Page # */
top; /* Top position */
uchar name[124]; /* Reference name */
uchar name[252]; /* Reference name */
} link_t;

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

static void check_pages(int page);

static void add_link(uchar *name, int page, int top);
static void add_link(tree_t *html, uchar *name, int page, int top);
static link_t *find_link(uchar *name);
static int compare_links(link_t *n1, link_t *n2);

Expand Down Expand Up @@ -3835,7 +3835,8 @@ render_contents(tree_t *t, /* I - Tree to parse */
* Add a target link...
*/

add_link(link, *page, (int)(*y + height));
add_link(NULL, link, *page, (int)(*y + height));
add_link(temp, link, *page, (int)(*y + height));
}

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

add_link(link, *page, (int)(*y + height));
add_link(NULL, link, *page, (int)(*y + height));
add_link(temp, link, *page, (int)(*y + height));
}
break;

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

add_link(name, *page, (int)*y);
add_link(NULL, name, *page, (int)*y);
add_link(t, name, *page, (int)*y);
}
else if (t->markup == MARKUP_FILE)
{
Expand All @@ -4176,7 +4179,7 @@ parse_doc(tree_t *t, /* I - Tree to parse */
*sep = '\0';

// Add the link
add_link(newname, *page, (int)*y);
add_link(NULL, newname, *page, (int)*y);
}

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

add_link(link, *page, (int)(*y + height));
add_link(NULL, link, *page, (int)(*y + height));
add_link(temp, link, *page, (int)(*y + height));
}

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

add_link(link, *page, (int)(*y + height));
add_link(NULL, link, *page, (int)(*y + height));
add_link(temp, link, *page, (int)(*y + height));
}

default :
Expand Down Expand Up @@ -5673,7 +5678,8 @@ parse_pre(tree_t *t, /* I - Tree to parse */
* Add a target link...
*/

add_link(link, *page, (int)(*y + height));
add_link(NULL, link, *page, (int)(*y + height));
add_link(start, link, *page, (int)(*y + height));
}

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

add_link(link, *page, (int)(*y + height));
add_link(NULL, link, *page, (int)(*y + height));
add_link(start, link, *page, (int)(*y + height));
}
break;

Expand Down Expand Up @@ -9035,9 +9042,10 @@ check_pages(int page) // I - Current page
*/

static void
add_link(uchar *name, /* I - Name of link */
int page, /* I - Page # */
int top) /* I - Y position */
add_link(tree_t *html, /* I - HTML node */
uchar *name, /* I - Name of link */
int page, /* I - Page # */
int top) /* I - Y position */
{
link_t *temp; /* New name */

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

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

if ((temp = find_link(name)) != NULL)
if (!html && (temp = find_link(name)) != NULL)
{
temp->page = (short)page;
temp->top = (short)top;
return;
}
else
{
// See if we need to allocate memory for links...
if (num_links >= alloc_links)
{
// Allocate more links...
alloc_links += ALLOC_LINKS;

if (num_links == 0)
temp = (link_t *)malloc(sizeof(link_t) * alloc_links);
else
temp = (link_t *)realloc(links, sizeof(link_t) * alloc_links);
// See if we need to allocate memory for links...
if (num_links >= alloc_links)
{
// Allocate more links...
alloc_links += ALLOC_LINKS;

if (temp == NULL)
{
progress_error(HD_ERROR_OUT_OF_MEMORY,
"Unable to allocate memory for %d links - %s",
(int)alloc_links, strerror(errno));
alloc_links -= ALLOC_LINKS;
return;
}
if (num_links == 0)
temp = (link_t *)malloc(sizeof(link_t) * alloc_links);
else
temp = (link_t *)realloc(links, sizeof(link_t) * alloc_links);

links = temp;
if (temp == NULL)
{
progress_error(HD_ERROR_OUT_OF_MEMORY, "Unable to allocate memory for %d links - %s", (int)alloc_links, strerror(errno));
alloc_links -= ALLOC_LINKS;
return;
}

// Add a new link...
temp = links + num_links;
num_links ++;
links = temp;
}

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

if (num_links > 1)
qsort(links, num_links, sizeof(link_t),
(compare_func_t)compare_links);
if (html)
{
uchar *filename; /* Filename */

if ((filename = htmlGetVariable(html->parent, (uchar *)"_HD_FILENAME")) != NULL)
snprintf((char *)temp->name, sizeof(temp->name), "%s#%s", (char *)filename, (char *)name);
else
strlcpy((char *)temp->name, (char *)name, sizeof(temp->name));
}
else
{
strlcpy((char *)temp->name, (char *)name, sizeof(temp->name));
}

temp->page = (short)page;
temp->top = (short)top;

if (num_links > 1)
qsort(links, num_links, sizeof(link_t), (compare_func_t)compare_links);
}


Expand Down Expand Up @@ -9662,10 +9679,20 @@ static tree_t * /* O - Flattened markup tree */
flatten_tree(tree_t *t) /* I - Markup tree to flatten */
{
tree_t *temp, /* New tree node */
*parent, /* Parent node (for file info) */
*flat; /* Flattened tree */


flat = NULL;
flat = NULL;
parent = NULL;
for (temp = t; temp != NULL; temp = temp->parent)
{
if (temp->markup == MARKUP_FILE)
{
parent = temp;
break;
}
}

while (t != NULL)
{
Expand All @@ -9680,7 +9707,7 @@ flatten_tree(tree_t *t) /* I - Markup tree to flatten */
case MARKUP_IMG :
temp = (tree_t *)calloc(sizeof(tree_t), 1);
memcpy(temp, t, sizeof(tree_t));
temp->parent = NULL;
temp->parent = parent;
temp->child = NULL;
temp->prev = flat;
temp->next = NULL;
Expand All @@ -9697,7 +9724,7 @@ flatten_tree(tree_t *t) /* I - Markup tree to flatten */
{
temp = (tree_t *)calloc(sizeof(tree_t), 1);
memcpy(temp, t, sizeof(tree_t));
temp->parent = NULL;
temp->parent = parent;
temp->child = NULL;
temp->prev = flat;
temp->next = NULL;
Expand Down Expand Up @@ -9736,7 +9763,7 @@ flatten_tree(tree_t *t) /* I - Markup tree to flatten */
case MARKUP_CAPTION :
temp = (tree_t *)calloc(sizeof(tree_t), 1);
temp->markup = MARKUP_BR;
temp->parent = NULL;
temp->parent = parent;
temp->child = NULL;
temp->prev = flat;
temp->next = NULL;
Expand Down

0 comments on commit 4620dee

Please sign in to comment.