Skip to content

Commit

Permalink
Support Epub chapters that are split accross multiple files (ScerIO#27)
Browse files Browse the repository at this point in the history
* Support split epub chapters

* Fix merge issues

* Formatting fix
  • Loading branch information
jasmeet0817 authored Oct 22, 2024
1 parent 7afea4a commit b35e5d1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/src/entities/epub_chapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ class EpubChapter {
String? Anchor;
String? HtmlContent;
List<EpubChapter>? SubChapters;
List<String> OtherContentFileNames = [];

@override
int get hashCode {
var objects = [
Title.hashCode,
ContentFileName.hashCode,
OtherContentFileNames.hashCode,
Anchor.hashCode,
HtmlContent.hashCode,
...SubChapters?.map((subChapter) => subChapter.hashCode) ?? [0],
Expand All @@ -27,6 +29,7 @@ class EpubChapter {
}
return Title == other.Title &&
ContentFileName == other.ContentFileName &&
OtherContentFileNames == other.OtherContentFileNames &&
Anchor == other.Anchor &&
HtmlContent == other.HtmlContent &&
collections.listsEqual(SubChapters, other.SubChapters);
Expand Down
12 changes: 12 additions & 0 deletions lib/src/readers/chapter_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ class ChapterReader {
chapterRef.Title = navigationPoint.NavigationLabels!.first.Text;
chapterRef.SubChapters =
getChaptersImpl(bookRef, navigationPoint.ChildNavigationPoints!);
if(chapterRef.ContentFileName!.contains('_split_')) {
var fileNamePart = chapterRef.ContentFileName!.split('_split_')[0];
for (var fileName in bookRef.Content!.Html!.keys) {
if(fileName.contains(fileNamePart)) {
if (fileName == contentFileName) {
continue;
}
chapterRef.otherTextContentFileRefs.add(bookRef.Content!.Html![fileName]!);
chapterRef.OtherContentFileNames.add(fileName);
}
}
}

result.add(chapterRef);
};
Expand Down
22 changes: 21 additions & 1 deletion lib/src/ref_entities/epub_chapter_ref.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ import 'package:quiver/core.dart';
import 'epub_text_content_file_ref.dart';

class EpubChapterRef {
// Referece to Text content reader.
EpubTextContentFileRef? epubTextContentFileRef;
// If the chapter is split into multiple files, this list contains the references to content readers of the other files.
List<EpubTextContentFileRef> otherTextContentFileRefs = [];

String? Title;
String? ContentFileName;
String? Anchor;
List<EpubChapterRef>? SubChapters;
// If the chapter is split into multiple files, this list contains the names of the other files.
List<String> OtherContentFileNames = [];

EpubChapterRef(EpubTextContentFileRef? epubTextContentFileRef) {
this.epubTextContentFileRef = epubTextContentFileRef;
Expand All @@ -22,8 +27,10 @@ class EpubChapterRef {
var objects = [
Title.hashCode,
ContentFileName.hashCode,
OtherContentFileNames.hashCode,
Anchor.hashCode,
epubTextContentFileRef.hashCode,
otherTextContentFileRefs.hashCode,
...SubChapters?.map((subChapter) => subChapter.hashCode) ?? [0],
];
return hashObjects(objects);
Expand All @@ -36,13 +43,26 @@ class EpubChapterRef {
}
return Title == other.Title &&
ContentFileName == other.ContentFileName &&
OtherContentFileNames == other.OtherContentFileNames &&
Anchor == other.Anchor &&
epubTextContentFileRef == other.epubTextContentFileRef &&
otherTextContentFileRefs == other.otherTextContentFileRefs &&
collections.listsEqual(SubChapters, other.SubChapters);
}

Future<String> readHtmlContent() async {
return epubTextContentFileRef!.readContentAsText();
var contentFuture = epubTextContentFileRef!.readContentAsText();
if (OtherContentFileNames.isNotEmpty) {
var allContentFutures = <Future<String>>[contentFuture];
for (var otherContentFileRef in otherTextContentFileRefs) {
allContentFutures.add(otherContentFileRef.readContentAsText());
}
return Future.wait(allContentFutures).then((List<String> contents) {
return contents.join('');
});
} else {
return contentFuture;
}
}

@override
Expand Down

0 comments on commit b35e5d1

Please sign in to comment.