Skip to content

Commit

Permalink
Merge pull request #1 from SofieTorch/feature/metadata-shortcuts
Browse files Browse the repository at this point in the history
add getters for epub title, authors and cover
  • Loading branch information
SofieTorch authored Sep 16, 2024
2 parents 4eec20d + 935f49a commit cbf9a1c
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 4 deletions.
29 changes: 28 additions & 1 deletion lib/epub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,33 @@ class Epub extends Equatable {
late final Lazy<List<Item>> _items;
late final Lazy<List<Section>> _sections;

String get title =>
metadata
.firstWhere(
(element) =>
element is DublinCoreMetadata && element.key == 'title',
orElse: () => Metadata.empty)
.value ??
'';

List<String> get authors => metadata
.where((element) =>
element is DublinCoreMetadata && element.key == 'creator')
.map((element) => element.value ?? '')
.toList();

Item? get cover {
Item? result;
try {
result = items.firstWhere(
(element) => element.properties.contains(ItemProperty.coverImage),
);
} on StateError {
result = null;
}
return result;
}

/// Path to the root file (usually 'content.opf') in the EPUB.
///
/// Throws a [FormatException] if the container file or the root file path
Expand Down Expand Up @@ -128,7 +155,7 @@ class Epub extends Equatable {
final mediaOverlay = itemsxml.descendantElements.firstWhere(
(itemxml) => itemxml.getAttribute('id') == mediaOverlayId,
orElse: () => throw UnimplementedError(
'Referenced media overlay not found or not declared.'),
'Media overlay with id $mediaOverlayId not found or not declared.'),
);
item = Item.fromXmlElement(
element,
Expand Down
4 changes: 3 additions & 1 deletion lib/models/item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ class Item extends Equatable {
/// Media type of the represented file.
final ItemMediaType mediaType;

/// Special uses for this item.
/// Indicators that this item has some special use case, such as cover image, svg, etc.
///
/// See [ItemProperty] for all the available use cases.
final List<ItemProperty> properties;

/// Additional information for this item.
Expand Down
33 changes: 32 additions & 1 deletion test/epub_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ void main() {
setUp(() {
epub = Epub.fromFile(File('test/resources/demo.epub'));

final badFormattedEpubFile = File('test/resources/bad_formatted_demo.epub');
final badFormattedEpubFile =
File('test/resources/bad_formatted_demo01.epub');
badFormattedEpub = Epub.fromBytes(badFormattedEpubFile.readAsBytesSync());
});

Expand Down Expand Up @@ -89,4 +90,34 @@ void main() {
epub.sections;
expect(epub, isNot(equals(epub2)));
});

test('''Epub.title returns EPUB title correctly''', () {
expect(epub.title, '[DEMO002] How To Create EPUB 3 Read Aloud eBooks');
});

test('''Epub.title returns empty when <dc:title> does not exist''', () {
expect(badFormattedEpub.title, '');
});

test('''Epub.authors returns EPUB authors correctly''', () {
expect(epub.authors, ['Alberto Pettarin']);
});

test('''Epub.cover returns expected Item''', () {
final cover = epub.cover;
expect(cover, isNotNull);
expect(cover!.id, 'cover.png');
expect(cover.href, 'Images/cover.png');
expect(cover.mediaType, ItemMediaType.png);
expect(cover.properties, [ItemProperty.coverImage]);
});

test('''Epub.cover returns null when no
Item with "cover-image" in its properties exist''', () {
final epubWithoutCover = Epub.fromFile(
File('test/resources/bad_formatted_demo02_no_cover.epub'),
);

expect(epubWithoutCover.cover, isNull);
});
}
2 changes: 1 addition & 1 deletion test/models/section_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void main() {
test('''Section.audio throws exception
when the referenced audio file is not found''', () {
final badFormattedEpub = Epub.fromFile(
File('test/resources/bad_formatted_demo.epub'),
File('test/resources/bad_formatted_demo01.epub'),
);

final section = Section(
Expand Down
Binary file not shown.
Binary file not shown.

0 comments on commit cbf9a1c

Please sign in to comment.