Answers.Body: <p>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 <a href="https://en.wikipedia.org/wiki/Fragment_identifier" rel="nofollow noreferrer">fragment identifiers</a> for HTML. So it is most likely a feature, not a bug. </p>
  
  <p>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.</p>
  
  <p>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! </p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-01T12:20:57.067Z
Answers.Text: 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**".
Answers.UserDisplayName: TripeHound
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-01T12:07:08.343Z
Answers.LastActivityDate: 2019-11-01T12:27:39.237Z
Answers.LastEditDate: 2019-11-01T12:27:39.237Z
Answers.UserDisplayName: idiotprogrammer
Body: <p>I use calibre's <code>ebook-convert</code> to convert HTML files into MOBI files. I have found that if the HTML filename contains a hash-sign (<code>#</code>), 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 <code>#</code> in the filenames)?</p>
  
  <p>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.</p>
  
  <hr>
  
  <p>If <code>Test.HTML</code> contains the following:</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
      &lt;meta http-equiv="Content-Type" content="text/html;charset=Windows-1252" /&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
    &lt;title&gt;Test Title&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;h1&gt;Chapter One&lt;/h1&gt;
  
  &lt;p&gt;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.&lt;/p&gt;
  &lt;p&gt;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.&lt;/p&gt;
  &lt;p&gt;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.&lt;/p&gt;
  &lt;p&gt;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.&lt;/p&gt;
  &lt;p&gt;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.&lt;/p&gt;
  
  &lt;h1&gt;Chapter Two&lt;/h1&gt;
  
  &lt;p&gt;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.&lt;/p&gt;
  &lt;p&gt;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.&lt;/p&gt;
  &lt;p&gt;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.&lt;/p&gt;
  &lt;p&gt;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.&lt;/p&gt;
  &lt;p&gt;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.&lt;/p&gt;
  
  &lt;/body&gt;
  </code></pre>
  
  <p>And I use the following command (all on one line; split here for easier reading):</p>
  
  <pre><code>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']"
  </code></pre>
  
  <p>Then the generated MOBI file behaves exactly as expected. Using calibre's ebook-reader, the <code>Up</code>/<code>Down</code> arrows navigate by pages, <code>Ctrl-Up</code> and <code>Ctrl-Down</code> navigate by chapter, and the links in the auto-generated table-of-contents appended to the end work.</p>
  
  <p>However, if I rename the source file to <code>Test#.html</code> and process that using the same command as above, then chapter information seems to have been lost. The <code>Up</code>/<code>Down</code> arrows still work, but <code>Ctrl-Up</code> and <code>Ctrl-Down</code> have no effect, and – while a TOC is still <em>generated</em> – the links in it do not do anything.</p>
  
  <p>The output from calibre, below, shows that chapters are being detected, and – apart from the filename – is identical to the output without a <code>#</code>.</p>
  
  <pre><code>Z:\Archive\$&gt;"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
  </code></pre>
  
Tags: <calibre><mobi><html><table-of-contents>
Title: Hash (#) in filename loses chapter information when converted with calibre
UserDisplayName: TripeHound
ThreadText: # Hash (#) in filename loses chapter information when converted with calibre
  
  (Asked by: username_0 on 2019-10-31)
  
  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
  
  ```
  
  **Tags:** calibre, mobi, html, table-of-contents
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-11-01. Score: 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](https://en.wikipedia.org/wiki/Fragment_identifier) 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!
  
  > **Comments:**
  > **username_0**: 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**".  
  
  ---
  
  