Skip to content

Commit a33f166

Browse files
committed
Fix EPUB output with HTML title pages.
1 parent a648b31 commit a33f166

File tree

2 files changed

+64
-49
lines changed

2 files changed

+64
-49
lines changed

CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
HTML keywords and lang values.
99
- Added support for the subject and language metadata in markdown input.
1010
- Fixed a buffer underflow bug (Issue #338)
11-
11+
- Fixed an issue with HTML title pages and EPUB output.
1212

1313
# Changes in HTMLDOC v1.9.4
1414

htmldoc/epub.cxx

Lines changed: 63 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ typedef int (*compare_func_t)(const void *, const void *);
5151
}
5252

5353
static int write_header(zipc_file_t *out, uchar *title, uchar *author, uchar *copyright, uchar *docnumber, tree_t *t);
54-
static int write_title(zipc_file_t *out, uchar *title, uchar *author, uchar *copyright, uchar *docnumber);
54+
static int write_title(zipc_file_t *out, tree_t *title_tree, uchar *title, uchar *author, uchar *copyright, uchar *docnumber);
5555
static int write_all(zipc_file_t *out, tree_t *t);
5656
static int write_node(zipc_file_t *out, tree_t *t);
5757
static int write_nodeclose(zipc_file_t *out, tree_t *t);
@@ -90,6 +90,7 @@ epub_export(tree_t *document, /* I - Document to export */
9090
zipc_file_t *epubf; /* File in container */
9191
struct stat epubinfo; /* EPUB file information */
9292
const char *title_ext; /* Extension of title image */
93+
tree_t *title_tree = NULL; /* Title file document tree */
9394
const char *cover_image = NULL; /* Do we have a cover image? */
9495
int status = 0; /* Return status */
9596
static const char *mimetype = /* mimetype file as a string */
@@ -158,15 +159,44 @@ epub_export(tree_t *document, /* I - Document to export */
158159

159160
title_ext = file_extension(TitleImage);
160161

161-
if (TitleImage[0] && TitlePage &&
162+
if (TitleImage[0] && TitlePage)
163+
{
162164
#ifdef WIN32
163-
(!stricmp(title_ext, "bmp") || !stricmp(title_ext, "gif") || !stricmp(title_ext, "jpg") || !stricmp(title_ext, "png")))
165+
if (!stricmp(title_ext, "bmp") || !stricmp(title_ext, "gif") || !stricmp(title_ext, "jpg") || !stricmp(title_ext, "png"))
164166
#else
165-
(!strcmp(title_ext, "bmp") || !strcmp(title_ext, "gif") || !strcmp(title_ext, "jpg") || !strcmp(title_ext, "png")))
167+
if (!strcmp(title_ext, "bmp") || !strcmp(title_ext, "gif") || !strcmp(title_ext, "jpg") || !strcmp(title_ext, "png"))
166168
#endif // WIN32
167-
{
168-
status |= copy_image(epub, file_find(Path, TitleImage));
169-
cover_image = file_basename(TitleImage);
169+
{
170+
status |= copy_image(epub, file_find(Path, TitleImage));
171+
cover_image = file_basename(TitleImage);
172+
}
173+
else
174+
{
175+
FILE *fp; /* Title file */
176+
const char *title_file; /* Location of title file */
177+
178+
// Find the title page file...
179+
if ((title_file = file_find(Path, TitleImage)) == NULL)
180+
{
181+
progress_error(HD_ERROR_FILE_NOT_FOUND, "Unable to find title file \"%s\".", TitleImage);
182+
return (-1);
183+
}
184+
185+
// Read a HTML title page...
186+
if ((fp = fopen(title_file, "rb")) == NULL)
187+
{
188+
progress_error(HD_ERROR_FILE_NOT_FOUND,
189+
"Unable to open title file \"%s\" - %s!",
190+
TitleImage, strerror(errno));
191+
return (-1);
192+
}
193+
194+
title_tree = htmlReadFile(NULL, fp, file_directory(TitleImage));
195+
htmlFixLinks(title_tree, title_tree, (uchar *)file_directory(TitleImage));
196+
fclose(fp);
197+
198+
status |= copy_images(epub, title_tree);
199+
}
170200
}
171201

172202
status |= copy_images(epub, document);
@@ -175,19 +205,30 @@ epub_export(tree_t *document, /* I - Document to export */
175205
* Get document strings...
176206
*/
177207

178-
title = get_title(document);
179-
author = htmlGetMeta(document, (uchar *)"author");
180-
copyright = htmlGetMeta(document, (uchar *)"copyright");
181-
docnumber = htmlGetMeta(document, (uchar *)"docnumber");
182-
language = htmlGetMeta(document, (uchar *)"lang");
183-
subject = htmlGetMeta(document, (uchar *)"keywords");
208+
if ((title = get_title(title_tree)) == NULL)
209+
title = get_title(document);
210+
211+
if ((author = htmlGetMeta(title_tree, (uchar *)"author")) == NULL)
212+
author = htmlGetMeta(document, (uchar *)"author");
213+
214+
if ((copyright = htmlGetMeta(title_tree, (uchar *)"copyright")) == NULL)
215+
copyright = htmlGetMeta(document, (uchar *)"copyright");
184216

217+
if ((docnumber = htmlGetMeta(title_tree, (uchar *)"docnumber")) == NULL)
218+
docnumber = htmlGetMeta(document, (uchar *)"docnumber");
185219
if (!docnumber)
186-
docnumber = htmlGetMeta(document, (uchar *)"version");
220+
{
221+
if ((docnumber = htmlGetMeta(title_tree, (uchar *)"version")) == NULL)
222+
docnumber = htmlGetMeta(document, (uchar *)"version");
223+
}
187224

225+
if ((language = htmlGetMeta(title_tree, (uchar *)"lang")) == NULL)
226+
language = htmlGetMeta(document, (uchar *)"lang");
188227
if (!language)
189228
language = (uchar *)"en-US";
190229

230+
if ((subject = htmlGetMeta(title_tree, (uchar *)"keywords")) == NULL)
231+
subject = htmlGetMeta(document, (uchar *)"keywords");
191232
if (!subject)
192233
subject = (uchar *)"Unknown";
193234

@@ -214,7 +255,7 @@ epub_export(tree_t *document, /* I - Document to export */
214255
{
215256
progress_show("Copying title page to EPUB container...");
216257

217-
status |= write_title(epubf, title, author, copyright, docnumber);
258+
status |= write_title(epubf, title_tree, title, author, copyright, docnumber);
218259
}
219260

220261
while (document != NULL)
@@ -323,6 +364,9 @@ epub_export(tree_t *document, /* I - Document to export */
323364
if (title != NULL)
324365
free(title);
325366

367+
if (title_tree)
368+
htmlDeleteTree(title_tree);
369+
326370
if (alloc_links)
327371
{
328372
free(links);
@@ -496,48 +540,19 @@ write_header(
496540

497541
static int /* O - 0 on success, -1 on failure */
498542
write_title(zipc_file_t *out, /* I - Output file */
543+
tree_t *title_tree, /* I - Title tree, if any */
499544
uchar *title, /* I - Title for document */
500545
uchar *author, /* I - Author for document */
501546
uchar *copyright, /* I - Copyright for document */
502547
uchar *docnumber) /* I - ID number for document */
503548
{
504549
int status = 0; /* Write status */
505-
FILE *fp; /* Title file */
506-
const char *title_file, /* Location of title file */
507-
*title_ext; /* Extension of title file */
508-
tree_t *t; /* Title file document tree */
509550

510551

511-
title_ext = file_extension(TitleImage);
512-
513-
#ifdef WIN32
514-
if (TitleImage[0] && stricmp(title_ext, "bmp") && stricmp(title_ext, "gif") && stricmp(title_ext, "jpg") && stricmp(title_ext, "png"))
515-
#else
516-
if (TitleImage[0] && strcmp(title_ext, "bmp") && strcmp(title_ext, "gif") && strcmp(title_ext, "jpg") && strcmp(title_ext, "png"))
517-
#endif // WIN32
552+
if (title_tree)
518553
{
519-
// Find the title page file...
520-
if ((title_file = file_find(Path, TitleImage)) == NULL)
521-
{
522-
progress_error(HD_ERROR_FILE_NOT_FOUND, "Unable to find title file \"%s\".", TitleImage);
523-
return (-1);
524-
}
525-
526-
// Write a title page from HTML source...
527-
if ((fp = fopen(title_file, "rb")) == NULL)
528-
{
529-
progress_error(HD_ERROR_FILE_NOT_FOUND,
530-
"Unable to open title file \"%s\" - %s!",
531-
TitleImage, strerror(errno));
532-
return (-1);
533-
}
534-
535-
t = htmlReadFile(NULL, fp, file_directory(TitleImage));
536-
htmlFixLinks(t, t, (uchar *)file_directory(TitleImage));
537-
fclose(fp);
538-
539-
status |= write_all(out, t);
540-
htmlDeleteTree(t);
554+
// Write a custom HTML title page...
555+
status |= write_all(out, title_tree);
541556
}
542557
else
543558
{

0 commit comments

Comments
 (0)