-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from pedia/model-structure
Model structure change
- Loading branch information
Showing
18 changed files
with
839 additions
and
891 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ test/res | |
example | ||
epubcheck-5.1.0 | ||
*.epub | ||
test | ||
t |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# 0.3.0 | ||
refine Book | ||
|
||
# 0.2.2 | ||
Fix utf8 decode in Book.readString | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,32 @@ | ||
import 'package:epub3/epub3_io.dart' as epub; | ||
// import 'package:image/image.dart'; | ||
|
||
void dumpChapter(epub.Chapter c, {int depth = 1}) { | ||
void dumpChapter(epub.Book b, epub.Chapter c, {int depth = 1}) { | ||
final char = '#'.codeUnits[0]; | ||
final prefix = String.fromCharCodes(List.generate(depth, (index) => char)); | ||
|
||
print('$prefix ${c.title}'); | ||
// final af = b.reader?.readFile(c.href!); | ||
// final cc = af != null ? af.size : 0; | ||
|
||
var cc = 0; | ||
|
||
print('🚩$prefix ${c.title} ${c.href} $cc'); | ||
for (var cc in c.children) { | ||
dumpChapter(cc, depth: depth + 1); | ||
dumpChapter(b, cc, depth: depth + 1); | ||
} | ||
} | ||
|
||
void main(List<String> args) { | ||
final book = | ||
epub.readFile(args.isEmpty ? 'test/res/std/epub30-spec.epub' : args[0]); | ||
for (var c in book!.navigation.chapters) { | ||
dumpChapter(c); | ||
for (var c in book!.chapters) { | ||
dumpChapter(book, c); | ||
} | ||
|
||
epub.writeFile(book, 'alice.epub'); | ||
|
||
final b2 = epub.readFile('alice.epub'); | ||
for (var c in b2!.navigation.chapters) { | ||
dumpChapter(c); | ||
for (var c in b2!.chapters) { | ||
dumpChapter(b2, c); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import 'dart:io'; | ||
import 'dart:convert'; | ||
import 'package:path/path.dart' as p; | ||
|
||
import 'package:epub3/epub3_io.dart' as epub; | ||
|
||
/// Parse "### title" => 3, "title" | ||
class LeveledTitle { | ||
final int level; | ||
epub.Chapter chapter; | ||
LeveledTitle(this.level, this.chapter); | ||
|
||
factory LeveledTitle.from(String line) { | ||
int level = 0; | ||
|
||
int i = 0; | ||
while (true) { | ||
i = line.indexOf('#', i); | ||
if (-1 == i) { | ||
break; | ||
} | ||
i++; | ||
level++; | ||
} | ||
|
||
final title = line.trim().replaceAll('#', "").trim(); | ||
return LeveledTitle(level, epub.Chapter(title: title, children: [])); | ||
} | ||
|
||
String get html { | ||
final tag = { | ||
1: 'h5', | ||
2: 'h4', | ||
3: 'h3', | ||
}[level]; | ||
return '<$tag>${HtmlEscape().convert(chapter.title)}</$tag>'; | ||
} | ||
} | ||
|
||
List<epub.Chapter> split(File file) { | ||
final stack = <LeveledTitle>[]; | ||
final body = <String>[]; // current Chapter's body in text(not html) | ||
|
||
final lines = file.readAsLinesSync(); | ||
for (String line in lines) { | ||
if (line.startsWith('#')) { | ||
// End previous chapter: set content | ||
if (body.isNotEmpty) { | ||
stack.last.chapter = | ||
epub.Chapter.textContent(stack.last.chapter.title, body.join('\n')); | ||
|
||
body.clear(); | ||
} | ||
|
||
// new Title | ||
final current = LeveledTitle.from(line); | ||
stack.add(current); | ||
} else { | ||
body.add(line.trim()); | ||
} | ||
} | ||
|
||
if (body.isNotEmpty) { | ||
stack.last.chapter = | ||
epub.Chapter.textContent(stack.last.chapter.title, body.join('\n')); | ||
|
||
body.clear(); | ||
} | ||
|
||
// Build tree liked chapters | ||
final res = <epub.Chapter>[]; | ||
final toplevel = stack.first.level; | ||
for (final lt in stack) { | ||
if (lt.level == toplevel) { | ||
// new top Chapter | ||
res.add(lt.chapter); | ||
} else { | ||
// Find and push | ||
int i = stack.last.level; | ||
var c = res; | ||
while (i != lt.level) { | ||
c = c.last.children; | ||
i -= 1; | ||
} | ||
c.add(lt.chapter); | ||
} | ||
} | ||
|
||
return res; | ||
} | ||
|
||
// Convert markdown to epub | ||
void main(List<String> args) { | ||
final fp = args[0]; | ||
final chs = split(File(fp)); | ||
final book = epub.Book.create(title: p.basenameWithoutExtension(fp), author: ''); | ||
for (final ch in chs) { | ||
book.add(ch); | ||
} | ||
epub.writeFile(book, p.basenameWithoutExtension(fp) + '.epub'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
library epub3; | ||
|
||
export 'src/model.dart'; | ||
export 'model.dart'; | ||
export 'src/reader.dart'; | ||
export 'src/writer.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.