Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remote manifest items support, part 2 #69

Merged
merged 1 commit into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Source/VersOne.Epub.ConsoleDemo/ExtractPlainText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ internal static class ExtractPlainText
public static void Run(string filePath)
{
EpubBook book = EpubReader.ReadBook(filePath);
foreach (EpubTextContentFile textContentFile in book.ReadingOrder)
foreach (EpubLocalTextContentFile textContentFile in book.ReadingOrder)
{
PrintTextContentFile(textContentFile);
}
}

private static void PrintTextContentFile(EpubTextContentFile textContentFile)
private static void PrintTextContentFile(EpubLocalTextContentFile textContentFile)
{
HtmlDocument htmlDocument = new();
htmlDocument.LoadHtml(textContentFile.Content);
Expand Down
4 changes: 2 additions & 2 deletions Source/VersOne.Epub.ConsoleDemo/TestDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ private static void TestEpubFile(string epubFilePath, Dictionary<string, int> fi
filesByVersion[epubVersionString] = 1;
}
Console.WriteLine($"EPUB version: {epubVersionString}");
Console.WriteLine($"Total files: {bookRef.Content.AllFiles.Count}, HTML files: {bookRef.Content.Html.Count}," +
$" CSS files: {bookRef.Content.Css.Count}, image files: {bookRef.Content.Images.Count}, font files: {bookRef.Content.Fonts.Count}.");
Console.WriteLine($"Total files: {bookRef.Content.AllFiles.Local.Count}, HTML files: {bookRef.Content.Html.Local.Count}," +
$" CSS files: {bookRef.Content.Css.Local.Count}, image files: {bookRef.Content.Images.Local.Count}, font files: {bookRef.Content.Fonts.Local.Count}.");
Console.WriteLine($"Reading order: {bookRef.GetReadingOrder().Count} file(s).");
Console.WriteLine("Navigation:");
foreach (EpubNavigationItemRef navigationItemRef in bookRef.GetNavigation())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
<TargetFramework>net6.0</TargetFramework>
<RuntimeIdentifiers>win10-x64</RuntimeIdentifiers>
<Authors>vers</Authors>
<Company />
<Product />
<Description />
<Copyright>vers, 2015-2022</Copyright>
<Version>3.3</Version>
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
Expand Down
2 changes: 1 addition & 1 deletion Source/VersOne.Epub.Test/Comparers/EpubBookComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static void CompareEpubBooks(EpubBook expected, EpubBook actual)
Assert.Equal(expected.AuthorList, actual.AuthorList);
Assert.Equal(expected.Description, actual.Description);
Assert.Equal(expected.CoverImage, actual.CoverImage);
CollectionComparer.CompareCollections(expected.ReadingOrder, actual.ReadingOrder, EpubContentComparer.CompareEpubTextContentFiles);
CollectionComparer.CompareCollections(expected.ReadingOrder, actual.ReadingOrder, EpubContentComparer.CompareEpubLocalTextContentFiles);
EpubNavigationItemComparer.CompareNavigationItemLists(expected.Navigation, actual.Navigation);
EpubContentComparer.CompareEpubContents(expected.Content, actual.Content);
EpubSchemaComparer.CompareEpubSchemas(expected.Schema, actual.Schema);
Expand Down
97 changes: 78 additions & 19 deletions Source/VersOne.Epub.Test/Comparers/EpubContentComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,48 @@ internal static class EpubContentComparer
{
public static void CompareEpubContents(EpubContent expected, EpubContent actual)
{
CompareEpubContentFiles(expected.Cover, actual.Cover);
CompareEpubContentFiles(expected.NavigationHtmlFile, actual.NavigationHtmlFile);
CollectionComparer.CompareDictionaries(expected.Html, actual.Html, CompareEpubTextContentFiles);
CollectionComparer.CompareDictionaries(expected.Css, actual.Css, CompareEpubTextContentFiles);
CollectionComparer.CompareDictionaries(expected.Images, actual.Images, CompareEpubByteContentFiles);
CollectionComparer.CompareDictionaries(expected.Fonts, actual.Fonts, CompareEpubByteContentFiles);
CollectionComparer.CompareDictionaries(expected.AllFiles, actual.AllFiles, CompareEpubContentFilesWithContent);
CompareEpubLocalByteContentFiles(expected.Cover, actual.Cover);
CompareEpubLocalTextContentFiles(expected.NavigationHtmlFile, actual.NavigationHtmlFile);
CompareContentCollections(expected.Html, actual.Html, CompareEpubLocalTextContentFiles, CompareEpubRemoteTextContentFiles);
CompareContentCollections(expected.Css, actual.Css, CompareEpubLocalTextContentFiles, CompareEpubRemoteTextContentFiles);
CompareContentCollections(expected.Images, actual.Images, CompareEpubLocalByteContentFiles, CompareEpubRemoteByteContentFiles);
CompareContentCollections(expected.Fonts, actual.Fonts, CompareEpubLocalByteContentFiles, CompareEpubRemoteByteContentFiles);
CompareContentCollections(expected.AllFiles, actual.AllFiles, CompareLocalEpubContentFilesWithContent, CompareRemoteEpubContentFilesWithContent);
}

public static void CompareEpubTextContentFiles(EpubTextContentFile expected, EpubTextContentFile actual)
public static void CompareEpubLocalTextContentFiles(EpubLocalTextContentFile expected, EpubLocalTextContentFile actual)
{
CompareEpubContentFiles(expected, actual);
CompareEpubLocalContentFiles(expected, actual);
if (expected != null)
{
Assert.Equal(expected.Content, actual.Content);
}
}

public static void CompareEpubByteContentFiles(EpubByteContentFile expected, EpubByteContentFile actual)
public static void CompareEpubLocalByteContentFiles(EpubLocalByteContentFile expected, EpubLocalByteContentFile actual)
{
CompareEpubContentFiles(expected, actual);
CompareEpubLocalContentFiles(expected, actual);
if (expected != null)
{
Assert.Equal(expected.Content, actual.Content);
}
}

private static void CompareEpubContentFilesWithContent(EpubContentFile expected, EpubContentFile actual)
public static void CompareEpubRemoteTextContentFiles(EpubRemoteTextContentFile expected, EpubRemoteTextContentFile actual)
{
if (expected is EpubTextContentFile)
CompareEpubRemoteContentFiles(expected, actual);
if (expected != null)
{
CompareEpubTextContentFiles(expected as EpubTextContentFile, actual as EpubTextContentFile);
Assert.Equal(expected.Content, actual.Content);
}
else if (expected is EpubByteContentFile)
}

public static void CompareEpubRemoteByteContentFiles(EpubRemoteByteContentFile expected, EpubRemoteByteContentFile actual)
{
CompareEpubRemoteContentFiles(expected, actual);
if (expected != null)
{
CompareEpubByteContentFiles(expected as EpubByteContentFile, actual as EpubByteContentFile);
Assert.Equal(expected.Content, actual.Content);
}
}

Expand All @@ -52,13 +58,66 @@ private static void CompareEpubContentFiles(EpubContentFile expected, EpubConten
else
{
Assert.NotNull(actual);
Assert.Equal(expected.FileName, actual.FileName);
Assert.Equal(expected.FilePathInEpubArchive, actual.FilePathInEpubArchive);
Assert.Equal(expected.Href, actual.Href);
Assert.Equal(expected.GetType(), actual.GetType());
Assert.Equal(expected.Key, actual.Key);
Assert.Equal(expected.ContentLocation, actual.ContentLocation);
Assert.Equal(expected.ContentType, actual.ContentType);
Assert.Equal(expected.ContentMimeType, actual.ContentMimeType);
}
}

private static void CompareContentCollections<TLocalContentFile, TRemoteContentFile>(
EpubContentCollection<TLocalContentFile, TRemoteContentFile> expected,
EpubContentCollection<TLocalContentFile, TRemoteContentFile> actual,
Action<TLocalContentFile, TLocalContentFile> localContentFileComprarer,
Action<TRemoteContentFile, TRemoteContentFile> remoteContentFileComparer)
where TLocalContentFile : EpubLocalContentFile
where TRemoteContentFile : EpubRemoteContentFile
{
CollectionComparer.CompareDictionaries(expected.Local, actual.Local, localContentFileComprarer);
CollectionComparer.CompareDictionaries(expected.Remote, actual.Remote, remoteContentFileComparer);
}

private static void CompareLocalEpubContentFilesWithContent(EpubLocalContentFile expected, EpubLocalContentFile actual)
{
if (expected is EpubLocalTextContentFile)
{
CompareEpubLocalTextContentFiles(expected as EpubLocalTextContentFile, actual as EpubLocalTextContentFile);
}
else if (expected is EpubLocalByteContentFile)
{
CompareEpubLocalByteContentFiles(expected as EpubLocalByteContentFile, actual as EpubLocalByteContentFile);
}
}

private static void CompareRemoteEpubContentFilesWithContent(EpubRemoteContentFile expected, EpubRemoteContentFile actual)
{
if (expected is EpubRemoteTextContentFile)
{
CompareEpubRemoteTextContentFiles(expected as EpubRemoteTextContentFile, actual as EpubRemoteTextContentFile);
}
else if (expected is EpubRemoteByteContentFile)
{
CompareEpubRemoteByteContentFiles(expected as EpubRemoteByteContentFile, actual as EpubRemoteByteContentFile);
}
}

private static void CompareEpubLocalContentFiles(EpubLocalContentFile expected, EpubLocalContentFile actual)
{
CompareEpubContentFiles(expected, actual);
if (expected != null)
{
Assert.Equal(expected.FilePath, actual.FilePath);
}
}

private static void CompareEpubRemoteContentFiles(EpubRemoteContentFile expected, EpubRemoteContentFile actual)
{
CompareEpubContentFiles(expected, actual);
if (expected != null)
{
Assert.Equal(expected.Url, actual.Url);
}
}
}
}
46 changes: 36 additions & 10 deletions Source/VersOne.Epub.Test/Comparers/EpubContentRefComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,41 @@ internal static class EpubContentRefComparer
{
public static void CompareEpubContentRefs(EpubContentRef expected, EpubContentRef actual)
{
CompareEpubContentFileRefs(expected.Cover, actual.Cover);
CompareEpubContentFileRefs(expected.NavigationHtmlFile, actual.NavigationHtmlFile);
CollectionComparer.CompareDictionaries(expected.Html, actual.Html, CompareEpubContentFileRefs);
CollectionComparer.CompareDictionaries(expected.Css, actual.Css, CompareEpubContentFileRefs);
CollectionComparer.CompareDictionaries(expected.Images, actual.Images, CompareEpubContentFileRefs);
CollectionComparer.CompareDictionaries(expected.Fonts, actual.Fonts, CompareEpubContentFileRefs);
CollectionComparer.CompareDictionaries(expected.AllFiles, actual.AllFiles, CompareEpubContentFileRefs);
CompareEpubLocalContentFileRefs(expected.Cover, actual.Cover);
CompareEpubLocalContentFileRefs(expected.NavigationHtmlFile, actual.NavigationHtmlFile);
CompareContentCollectionRefs(expected.Html, actual.Html);
CompareContentCollectionRefs(expected.Css, actual.Css);
CompareContentCollectionRefs(expected.Images, actual.Images);
CompareContentCollectionRefs(expected.Fonts, actual.Fonts);
CompareContentCollectionRefs(expected.AllFiles, actual.AllFiles);
}

private static void CompareContentCollectionRefs<TLocalContentFileRef, TRemoteContentFileRef>(
EpubContentCollectionRef<TLocalContentFileRef, TRemoteContentFileRef> expected,
EpubContentCollectionRef<TLocalContentFileRef, TRemoteContentFileRef> actual)
where TLocalContentFileRef : EpubLocalContentFileRef
where TRemoteContentFileRef : EpubRemoteContentFileRef
{
CollectionComparer.CompareDictionaries(expected.Local, actual.Local, CompareEpubLocalContentFileRefs);
CollectionComparer.CompareDictionaries(expected.Remote, actual.Remote, CompareEpubRemoteContentFileRefs);
}

private static void CompareEpubLocalContentFileRefs(EpubLocalContentFileRef expected, EpubLocalContentFileRef actual)
{
CompareEpubContentFileRefs(expected, actual);
if (expected != null)
{
Assert.Equal(expected.FilePath, actual.FilePath);
}
}

private static void CompareEpubRemoteContentFileRefs(EpubRemoteContentFileRef expected, EpubRemoteContentFileRef actual)
{
CompareEpubContentFileRefs(expected, actual);
if (expected != null)
{
Assert.Equal(expected.Url, actual.Url);
}
}

private static void CompareEpubContentFileRefs(EpubContentFileRef expected, EpubContentFileRef actual)
Expand All @@ -23,9 +51,7 @@ private static void CompareEpubContentFileRefs(EpubContentFileRef expected, Epub
{
Assert.NotNull(actual);
Assert.Equal(expected.GetType(), actual.GetType());
Assert.Equal(expected.FileName, actual.FileName);
Assert.Equal(expected.FilePathInEpubArchive, actual.FilePathInEpubArchive);
Assert.Equal(expected.Href, actual.Href);
Assert.Equal(expected.Key, actual.Key);
Assert.Equal(expected.ContentLocation, actual.ContentLocation);
Assert.Equal(expected.ContentType, actual.ContentType);
Assert.Equal(expected.ContentMimeType, actual.ContentMimeType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private static void CompareNavigationItems(EpubNavigationItem expected, EpubNavi
Assert.Equal(expected.Type, actual.Type);
Assert.Equal(expected.Title, actual.Title);
CompareNavigationItemLinks(expected.Link, actual.Link);
EpubContentComparer.CompareEpubTextContentFiles(expected.HtmlContentFile, actual.HtmlContentFile);
EpubContentComparer.CompareEpubLocalTextContentFiles(expected.HtmlContentFile, actual.HtmlContentFile);
CompareNavigationItemLists(expected.NestedItems, actual.NestedItems);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public AmbiguousTypeBinder()
{
ambiguousTypes = new List<Type>
{
typeof(EpubByteContentFile),
typeof(EpubTextContentFile)
typeof(EpubLocalByteContentFile),
typeof(EpubLocalTextContentFile)
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ protected override JsonObjectContract CreateObjectContract(Type objectType)
result.ExtensionDataGetter = new ExtensionDataGetter(testCaseExtensionDataHandler.GetEpubBookExtensionData);
result.ExtensionDataSetter = new ExtensionDataSetter(testCaseExtensionDataHandler.SetEpubBookExtensionData);
}
else if (objectType == typeof(EpubByteContentFile))
else if (objectType == typeof(EpubLocalByteContentFile))
{
result.Properties.Remove(nameof(EpubByteContentFile.Content));
result.ExtensionDataGetter = new ExtensionDataGetter(testCaseExtensionDataHandler.GetEpubContentFileExtensionData);
result.ExtensionDataSetter = new ExtensionDataSetter(testCaseExtensionDataHandler.SetEpubByteContentFileExtensionData);
result.Properties.Remove(nameof(EpubLocalByteContentFile.Content));
result.ExtensionDataGetter = new ExtensionDataGetter(testCaseExtensionDataHandler.GetEpubLocalContentFileExtensionData);
result.ExtensionDataSetter = new ExtensionDataSetter(testCaseExtensionDataHandler.SetEpubLocalByteContentFileExtensionData);
}
else if (objectType == typeof(EpubTextContentFile))
else if (objectType == typeof(EpubLocalTextContentFile))
{
result.Properties.Remove(nameof(EpubTextContentFile.Content));
result.ExtensionDataGetter = new ExtensionDataGetter(testCaseExtensionDataHandler.GetEpubContentFileExtensionData);
result.ExtensionDataSetter = new ExtensionDataSetter(testCaseExtensionDataHandler.SetEpubTextContentFileExtensionData);
result.Properties.Remove(nameof(EpubLocalTextContentFile.Content));
result.ExtensionDataGetter = new ExtensionDataGetter(testCaseExtensionDataHandler.GetEpubLocalContentFileExtensionData);
result.ExtensionDataSetter = new ExtensionDataSetter(testCaseExtensionDataHandler.SetEpubLocalTextContentFileExtensionData);
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public IEnumerable<KeyValuePair<object, object>> GetEpubBookExtensionData(object
yield return new KeyValuePair<object, object>("$file", Path.GetFileName(epubBook.FilePath));
if (epubBook.CoverImage != null)
{
yield return new KeyValuePair<object, object>("$cover", FILE_PATH_IN_EPUB_PREFIX + epubBook.Content.Cover.FilePathInEpubArchive);
yield return new KeyValuePair<object, object>("$cover", FILE_PATH_IN_EPUB_PREFIX + epubBook.Content.Cover.FilePath);
}
}

Expand All @@ -48,28 +48,28 @@ public void SetEpubBookExtensionData(object deserializingObject, string key, obj
}
}

public IEnumerable<KeyValuePair<object, object>> GetEpubContentFileExtensionData(object serializingObject)
public IEnumerable<KeyValuePair<object, object>> GetEpubLocalContentFileExtensionData(object serializingObject)
{
EpubContentFile epubContentFile = (EpubContentFile)serializingObject;
if (!String.IsNullOrEmpty(epubContentFile.FilePathInEpubArchive))
EpubLocalContentFile epubLocalContentFile = (EpubLocalContentFile)serializingObject;
if (!String.IsNullOrEmpty(epubLocalContentFile.FilePath))
{
yield return new KeyValuePair<object, object>("$content", FILE_PATH_IN_EPUB_PREFIX + epubContentFile.FilePathInEpubArchive);
yield return new KeyValuePair<object, object>("$content", FILE_PATH_IN_EPUB_PREFIX + epubLocalContentFile.FilePath);
}
}

public void SetEpubByteContentFileExtensionData(object deserializingObject, string key, object value)
public void SetEpubLocalByteContentFileExtensionData(object deserializingObject, string key, object value)
{
if (key == "$content" && value != null && value.ToString().StartsWith(FILE_PATH_IN_EPUB_PREFIX))
{
(deserializingObject as EpubByteContentFile).Content = ReadFileAsBytes(value.ToString()[FILE_PATH_IN_EPUB_PREFIX.Length..]);
(deserializingObject as EpubLocalByteContentFile).Content = ReadFileAsBytes(value.ToString()[FILE_PATH_IN_EPUB_PREFIX.Length..]);
}
}

public void SetEpubTextContentFileExtensionData(object deserializingObject, string key, object value)
public void SetEpubLocalTextContentFileExtensionData(object deserializingObject, string key, object value)
{
if (key == "$content" && value != null && value.ToString().StartsWith(FILE_PATH_IN_EPUB_PREFIX))
{
(deserializingObject as EpubTextContentFile).Content = ReadFileAsText(value.ToString()[FILE_PATH_IN_EPUB_PREFIX.Length..]);
(deserializingObject as EpubLocalTextContentFile).Content = ReadFileAsText(value.ToString()[FILE_PATH_IN_EPUB_PREFIX.Length..]);
}
}

Expand Down
Loading