0

I use calibre's ebook-convert to convert HTML files into MOBI files. I have found that if the HTML filename contains a hash-sign (#), then the "chapter structure" of the generated MOBI file is broken. Does anyone know why this is happening, or a way of avoiding it (other than ensuring there are no # in the filenames)?

Because it has been doing everything I want it to for some time, I'm using what I suspect is quite an old version of calibre (1.10.0). I'm not against upgrading, if that's the only fix, but as the transition to this version from the one I had before it (0.4.129) was complicated, ideally I'd only try this if someone can confirm the issue no longer occurs in the latest version.


If Test.HTML contains the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <meta http-equiv="Content-Type" content="text/html;charset=Windows-1252" />
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Test Title</title>
</head>
<body>

<h1>Chapter One</h1>

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

<h1>Chapter Two</h1>

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

</body>

And I use the following command (all on one line; split here for easier reading):

ebook-convert" "Test.html" "Test.mobi"
     --title "Test Title"
     --output-profile=kindle
     --chapter="//*[name()='h1' or name()='h2']"
     --page-breaks-before="//*[name()='h1' or name()='h2']"

Then the generated MOBI file behaves exactly as expected. Using calibre's ebook-reader, the Up/Down arrows navigate by pages, Ctrl-Up and Ctrl-Down navigate by chapter, and the links in the auto-generated table-of-contents appended to the end work.

However, if I rename the source file to Test#.html and process that using the same command as above, then chapter information seems to have been lost. The Up/Down arrows still work, but Ctrl-Up and Ctrl-Down have no effect, and – while a TOC is still generated – the links in it do not do anything.

The output from calibre, below, shows that chapters are being detected, and – apart from the filename – is identical to the output without a #.

Z:\Archive\$>"C:\Program Files\Calibre\ebook-convert" "Test#.html" "Test.mobi" --title "Test Title" --output-profile=kindle --chapter="//*[name()='h1' or name()='h2']" --page-breaks-before="//*[name()='h1' or name()='h2']"
1% Converting input to HTML...
InputFormatPlugin: HTML Input running
on Z:\Archive\$\Test#.html
Language not specified
Creator not specified
Building file list...
Normalizing filename cases
Rewriting HTML links
34% Running transforms on ebook...
Merging user specified metadata...
Detecting structure...
        Detected chapter: Chapter One
        Detected chapter: Chapter Two
Auto generated TOC with 2 entries.
Flattening CSS and remapping font sizes...
Source base font size is 12.00000pt
Removing fake margins...
Cleaning up manifest...
Trimming unused files from manifest...
Creating MOBI Output...
67% Running MOBI Output plugin
Serializing resources...
Creating MOBI 6 output
Generating in-line TOC...
Applying case-transforming CSS...
Rasterizing SVG images...
Converting XHTML to Mobipocket markup...
Serializing markup content...
  Compressing markup content...
Generating MOBI index for a book
MOBI output written to Z:\Archive\$\Test.mobi
Output saved to   Z:\Archive\$\Test.mobi
1

First, I don't have any insight into CLI calibre and you might get a better answer from Mobileread or by posting a bug, but my guess is that the # character must be reserved exclusively for fragment identifiers for HTML. So it is most likely a feature, not a bug.

Think about it. If the file name were allowed to contain a #, then you would not be able to use HTML anchors on that page. But epub is required to support HTML which uses anchors. This is not an issue with calibre or mobi, but with the very nature of HTML.

I don't understand why you need a # symbol there, but it seems easy enough to switch a # symbol with another one (like + or - or $). Problem solved!

  • I've not really dug into yet, but after posting the question I did see some evidence that it's a problem with HTML anchors for the chapters, which appear to include the original (HTML) file name. At first glance (to me) this seems slightly odd / unnecessary as everything is in the same HTML file... As to why I want them, occasionally I'll have a series of books/documents, so one natural (to me) way of naming them is "Animals #01 - The Aardvark" through to "Animals #26 - The Zebra". – TripeHound Nov 1 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.