Epub Table of Generation fixes for Sigil (#1689)

* Fixed generating table of contents where key lookup could fail with how Sigil packs the epubs.

* Tweaked Kavita's fallback ToC generation (when one doesn't exist in the epub) to also use CoalesceKey.

* Code smells
This commit is contained in:
Joe Milazzo 2022-12-11 07:04:22 -06:00 committed by GitHub
parent fcd9f8f118
commit ebbaf2d060
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 27 additions and 71 deletions

View file

@ -717,6 +717,20 @@ public class BookService : IBookService
return PrepareFinalHtml(doc, body);
}
private static string CoalesceKey(EpubBookRef book, IDictionary<string, int> mappings, string key)
{
if (mappings.ContainsKey(CleanContentKeys(key))) return key;
// Fallback to searching for key (bad epub metadata)
var correctedKey = book.Content.Html.Keys.SingleOrDefault(s => s.EndsWith(key));
if (!string.IsNullOrEmpty(correctedKey))
{
key = correctedKey;
}
return key;
}
/// <summary>
/// This will return a list of mappings from ID -> page num. ID will be the xhtml key and page num will be the reading order
/// this is used to rewrite anchors in the book text so that we always load properly in our reader.
@ -743,7 +757,7 @@ public class BookService : IBookService
foreach (var nestedChapter in navigationItem.NestedItems.Where(n => n.Link != null))
{
var key = BookService.CleanContentKeys(nestedChapter.Link.ContentFileName);
var key = CoalesceKey(book, mappings, nestedChapter.Link.ContentFileName);
if (mappings.ContainsKey(key))
{
nestedChapters.Add(new BookChapterItem()
@ -760,7 +774,7 @@ public class BookService : IBookService
}
if (chaptersList.Count != 0) return chaptersList;
// Generate from TOC
// Generate from TOC from links (any point past this, Kavita is generating as a TOC doesn't exist)
var tocPage = book.Content.Html.Keys.FirstOrDefault(k => k.ToUpper().Contains("TOC"));
if (tocPage == null) return chaptersList;
@ -775,16 +789,7 @@ public class BookService : IBookService
{
if (!anchor.Attributes.Contains("href")) continue;
var key = BookService.CleanContentKeys(anchor.Attributes["href"].Value).Split("#")[0];
if (!mappings.ContainsKey(key))
{
// Fallback to searching for key (bad epub metadata)
var correctedKey = book.Content.Html.Keys.SingleOrDefault(s => s.EndsWith(key));
if (!string.IsNullOrEmpty(correctedKey))
{
key = correctedKey;
}
}
var key = CoalesceKey(book, mappings, anchor.Attributes["href"].Value.Split("#")[0]);
if (string.IsNullOrEmpty(key) || !mappings.ContainsKey(key)) continue;
var part = string.Empty;

View file

@ -90,12 +90,6 @@ public class CacheService : ICacheService
return path;
}
/// <summary>
/// Caches the files for the given chapter to CacheDirectory
/// </summary>
/// <param name="chapterId"></param>
/// <returns>This will always return the Chapter for the chapterId</returns>
public async Task<Chapter> Ensure(int chapterId, bool extractPdfToImages = false)
{
_directoryService.ExistOrCreate(_directoryService.CacheDirectory);