Skip to content

Commit

Permalink
Merge pull request creatint#1 from jarontai/jarontai
Browse files Browse the repository at this point in the history
Merge from Jarontai
  • Loading branch information
orthros authored Dec 31, 2017
2 parents b06dd2b + b6d1f5f commit da6e683
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 21 deletions.
107 changes: 107 additions & 0 deletions example/example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import 'dart:io' as io;

import 'package:path/path.dart' as path;
import 'package:epub/epub.dart';

main(List<String> args) async {
//Get the epub into memory somehow
String fileName = "hittelOnGoldMines.epub";
String fullPath = path.join(io.Directory.current.path, 'test', fileName);
var targetFile = new io.File(fullPath);
List<int> bytes = await targetFile.readAsBytes();

// Opens a book and reads all of its content into the memory
EpubBook epubBook = await EpubReader.readBook(bytes);

// COMMON PROPERTIES

// Book's title
String title = epubBook.Title;

// Book's authors (comma separated list)
String author = epubBook.Author;

// Book's authors (list of authors names)
List<String> authors = epubBook.AuthorList;

// Book's cover image (null if there is no cover)
Image coverImage = epubBook.CoverImage;

// CHAPTERS

// Enumerating chapters
epubBook.Chapters.forEach((EpubChapter chapter) {
// Title of chapter
String chapterTitle = chapter.Title;

// HTML content of current chapter
String chapterHtmlContent = chapter.HtmlContent;

// Nested chapters
List<EpubChapter> subChapters = chapter.SubChapters;
});

// CONTENT

// Book's content (HTML files, stlylesheets, images, fonts, etc.)
EpubContent bookContent = epubBook.Content;

// IMAGES

// All images in the book (file name is the key)
Map<String, EpubByteContentFile> images = bookContent.Images;

EpubByteContentFile firstImage = images.isNotEmpty ? images.values.first : null;

// Content type (e.g. EpubContentType.IMAGE_JPEG, EpubContentType.IMAGE_PNG)
EpubContentType contentType = firstImage?.ContentType;

// MIME type (e.g. "image/jpeg", "image/png")
String mimeContentType = firstImage?.ContentMimeType;

// HTML & CSS

// All XHTML files in the book (file name is the key)
Map<String, EpubTextContentFile> htmlFiles = bookContent.Html;

// All CSS files in the book (file name is the key)
Map<String, EpubTextContentFile> cssFiles = bookContent.Css;

// Entire HTML content of the book
htmlFiles.values.forEach((EpubTextContentFile htmlFile) {
String htmlContent = htmlFile.Content;
});

// All CSS content in the book
cssFiles.values.forEach((EpubTextContentFile cssFile) {
String cssContent = cssFile.Content;
});

// OTHER CONTENT

// All fonts in the book (file name is the key)
Map<String, EpubByteContentFile> fonts = bookContent.Fonts;

// All files in the book (including HTML, CSS, images, fonts, and other types of files)
Map<String, EpubContentFile> allFiles = bookContent.AllFiles;

// ACCESSING RAW SCHEMA INFORMATION

// EPUB OPF data
EpubPackage package = epubBook.Schema.Package;

// Enumerating book's contributors
package.Metadata.Contributors.forEach((contributor) {
String contributorName = contributor.Contributor;
String contributorRole = contributor.Role;
});

// EPUB NCX data
EpubNavigation navigation = epubBook.Schema.Navigation;

// Enumerating NCX metadata
navigation.Head.Metadata.forEach((meta) {
String metadataItemName = meta.Name;
String metadataItemContent = meta.Content;
});
}
9 changes: 9 additions & 0 deletions lib/epub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@ export 'src/utils/enum_from_string.dart';
export 'src/epub_reader.dart';
export 'src/ref_entities/epub_book_ref.dart';
export 'src/entities/epub_book.dart';
export 'src/entities/epub_chapter.dart';
export 'src/entities/epub_content.dart';
export 'src/entities/epub_content_type.dart';
export 'src/entities/epub_byte_content_file.dart';
export 'src/entities/epub_content_file.dart';
export 'src/entities/epub_text_content_file.dart';
export 'src/schema/opf/epub_package.dart';
export 'src/schema/navigation/epub_navigation.dart';
export 'package:image/image.dart' show Image;
17 changes: 8 additions & 9 deletions lib/src/epub_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,9 @@ class EpubReader {
result.AllFiles[key] = value;
});

await contentRef.AllFiles
.forEach((String key, EpubContentFileRef value) async {
await Future.forEach(contentRef.AllFiles.keys, (key) async {
if (!result.AllFiles.containsKey(key)) {
result.AllFiles[key] = await readByteContentFile(value);
result.AllFiles[key] = await readByteContentFile(contentRef.AllFiles[key]);
}
});

Expand All @@ -90,8 +89,9 @@ class EpubReader {
Map<String, EpubTextContentFileRef> textContentFileRefs) async {
Map<String, EpubTextContentFile> result =
new Map<String, EpubTextContentFile>();
await textContentFileRefs
.forEach((String key, EpubContentFileRef value) async {

await Future.forEach(textContentFileRefs.keys, (key) async {
EpubContentFileRef value = textContentFileRefs[key];
EpubTextContentFile textContentFile = new EpubTextContentFile();
textContentFile.FileName = value.FileName;
textContentFile.ContentType = value.ContentType;
Expand All @@ -106,9 +106,8 @@ class EpubReader {
Map<String, EpubByteContentFileRef> byteContentFileRefs) async {
Map<String, EpubByteContentFile> result =
new Map<String, EpubByteContentFile>();
byteContentFileRefs
.forEach((String key, EpubByteContentFileRef value) async {
result[key] = await readByteContentFile(value);
await Future.forEach(byteContentFileRefs.keys, (key) async {
result[key] = await readByteContentFile(byteContentFileRefs[key]);
});
return result;
}
Expand All @@ -128,7 +127,7 @@ class EpubReader {
static Future<List<EpubChapter>> readChapters(
List<EpubChapterRef> chapterRefs) async {
List<EpubChapter> result = new List<EpubChapter>();
await chapterRefs.forEach((EpubChapterRef chapterRef) async {
await Future.forEach(chapterRefs, (EpubChapterRef chapterRef) async {
EpubChapter chapter = new EpubChapter();

chapter.Title = chapterRef.Title;
Expand Down
8 changes: 4 additions & 4 deletions lib/src/readers/navigation_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ class NavigationReader {
.where((xml.XmlNode node) => node is xml.XmlElement)
.map((xml.XmlNode node) => node as xml.XmlElement)
.forEach((xml.XmlElement navigationPointNode) {
if (navigationPointNode.name.local.toLowerCase() == "navPoint") {
if (navigationPointNode.name.local.toLowerCase() == "navpoint") {
EpubNavigationPoint navigationPoint =
readNavigationPoint(navigationPointNode);
result.Points.add(navigationPoint);
Expand All @@ -222,7 +222,7 @@ class NavigationReader {
case "class":
result.Class = attributeValue;
break;
case "playOrder":
case "playorder":
result.PlayOrder = attributeValue;
break;
}
Expand Down Expand Up @@ -409,7 +409,7 @@ class NavigationReader {
readNavigationLabel(navigationListChildNode);
result.NavigationLabels.add(navigationLabel);
break;
case "navTarget":
case "navtarget":
EpubNavigationTarget navigationTarget =
readNavigationTarget(navigationListChildNode);
result.NavigationTargets.add(navigationTarget);
Expand Down Expand Up @@ -439,7 +439,7 @@ class NavigationReader {
case "class":
result.Class = attributeValue;
break;
case "playOrder":
case "playorder":
result.PlayOrder = attributeValue;
break;
}
Expand Down
16 changes: 8 additions & 8 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ author: Colin Nelson <[email protected]>
description: Epub Parser for Dart
homepage: https://github.com/orthros/dart-epub
environment:
sdk: '>=1.13.2 <2.0.0'
sdk: ">=1.21.0 <2.0.0"
dependencies:
async: ^1.13.2
archive: ^1.0.28
xml: ^2.5.0
image: ^1.1.26
async: "^1.8.0"
archive: "^1.0.33"
xml: "^2.6.0"
image: "^1.1.29"
dev_dependencies:
scheduled_test: '^0.12.0'
test: '^0.12.7'
path: any
scheduled_test: "^0.12.0"
test: "^0.12.0"
path: "^1.5.1"

0 comments on commit da6e683

Please sign in to comment.