GeezerVison is showing github.com — #24 Make some function names more pythonic + update docs + update exa… · 06linux/ebooklib@64934cb
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: ebooklib/epub.py +3-4Lines changed: 3 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 28 | 28 |
| 29 | 29 | from lxml import etree | 30 | 30 |
| 31 | | - | 32 | | -# this really should not be here | | 31 | +# This really should not be here | 33 | 32 | mimetypes.init() | 34 | 33 |
| 35 | 34 | # Version of EPUB library | @@ -931,7 +930,7 @@ def _load(self): | 931 | 930 |
| 932 | 931 | ## WRITE | 933 | 932 |
| 934 | | -def writeEPUB(name, book, options = None): | | 933 | +def write_epub(name, book, options = None): | 935 | 934 | epub = EpubWriter(name, book, options) | 936 | 935 |
| 937 | 936 | epub.process() | @@ -943,7 +942,7 @@ def writeEPUB(name, book, options = None): | 943 | 942 |
| 944 | 943 | ## READ | 945 | 944 |
| 946 | | -def readEPUB(name): | | 945 | +def read_epub(name): | 947 | 946 | reader = EpubReader(name) | 948 | 947 |
| 949 | 948 | book = reader.load() | |
Collapse file Copy file name to clipboardExpand all lines: samples/01_basic_create/create.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 80 | 80 | book.spine = ['nav', c1, c2] | 81 | 81 |
| 82 | 82 | # create epub file | 83 | | - epub.writeEPUB('test.epub', book, {}) | | 83 | + epub.write_epub('test.epub', book, {}) | 84 | 84 |
|
Collapse file Copy file name to clipboardExpand all lines: samples/02_cover_create/create.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 84 | 84 | book.spine = ['cover', 'nav', c1, c2] | 85 | 85 |
| 86 | 86 | # create epub file | 87 | | - epub.writeEPUB('test.epub', book, {}) | | 87 | + epub.write_epub('test.epub', book, {}) | 88 | 88 |
|
Collapse file Copy file name to clipboardExpand all lines: samples/03_advanced_create/create.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 93 | 93 | book.spine = ['nav', c1, c2] | 94 | 94 |
| 95 | 95 | # create epub file | 96 | | - epub.writeEPUB('test.epub', book, {}) | | 96 | + epub.write_epub('test.epub', book, {}) | 97 | 97 |
|
Collapse file Copy file name to clipboardExpand all lines: samples/04_markdown_parse/epub2markdown.py +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 |
| 8 | 8 | from ebooklib import epub | 9 | 9 |
| | 10 | +# This is just a basic example which can easily break in real world. | | 11 | + | 10 | 12 | if __name__ == '__main__': | 11 | 13 | # read epub | 12 | | - book = epub.readEPUB(sys.argv[1]) | | 14 | + book = epub.read_epub(sys.argv[1]) | 13 | 15 |
| 14 | 16 | # get base filename from the epub | 15 | 17 | base_name = os.path.basename(os.path.splitext(sys.argv[1])[0]) | |
Collapse file Copy file name to clipboardExpand all lines: tests/epub_booktype.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -214,7 +214,7 @@ def export_booktype(bookid): | 214 | 214 |
| 215 | 215 |
| 216 | 216 | # write book to the file | 217 | | - epub.writeEPUB('test.epub', book, opts) | | 217 | + epub.write_epub('test.epub', book, opts) | 218 | 218 |
| 219 | 219 |
| 220 | 220 | if __name__ == '__main__': | |
Collapse file Copy file name to clipboardExpand all lines: tests/epub_create.py +22-13Lines changed: 22 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 11 | 11 | book.set_title('This is my book') | 12 | 12 | book.set_language('fi') | 13 | 13 |
| | 14 | + book.add_metadata('DC', 'title', 'Naslov', {'id': 't1'}) | | 15 | + book.add_metadata(None, 'meta', 'main', {'refines': '#t1', 'property': 'title-type'}) | | 16 | + book.add_metadata('DC', 'title', 'Naslov', {'id': 't2'}) | | 17 | + book.add_metadata(None, 'meta', 'edition', {'refines': '#t2', 'property': 'title-type'}) | | 18 | + | 14 | 19 | book.add_author('Mirko Bulaja') | 15 | 20 | book.add_author('Danko Bananko', file_as='Gospodin Danko Bananko', role='ill', uid='coauthor') | 16 | 21 |
| 17 | | - img = open('cover.jpg', 'r').read() | | 22 | + img = open('image.jpg', 'r').read() | 18 | 23 | book.set_cover("image.jpg", img) | 19 | 24 |
| 20 | 25 | # create chapters | 21 | 26 | c1 = epub.EpubHtml(title='Intro 1', file_name='chap_01.xhtml', lang='hr') | 22 | | - c1.content=u'<html><head></head><body><h1>This is I!</h1><p>This is something i am writing.</p><p>Šime Đodan ima puno posla.</p></body></html>' | | 27 | + c1.set_content(u'<html><head></head><body><h1>This is I!</h1><p>This is something i am writing.</p><p>Šime Đodan ima puno posla.</p></body></html>') | 23 | 28 |
| 24 | | - c2 = epub.EpubHtml(title='Intro 2', file_name='chap_02.xhtml') | 25 | | -# c2.content='<html><head></head><body><h1>This is I!</h1><p>Helou, helou!</p><p><img src="static/image.jpg"/><a href="http://ajme/">Ovo je link</a></body></html>' | 26 | | - c2.content='<h1>This is I!</h1><p>Helou, helou!</p><p><img src="image.jpg"/><a href="http://ajme/">Ovo je link</a></p>' | | 29 | + c2 = epub.EpubHtml(title='Intro 2', file_name=u'chap_02_đšž.xhtml') | | 30 | + c2.set_content('<h1>This is I!</h1><p>Helou, helou!</p><p><a href="http://ajme/">Ovo je link</a></p>') | 27 | 31 | c2.properties.append('rendition:layout-pre-paginated rendition:orientation-landscape rendition:spread-none') | 28 | 32 |
| 29 | 33 | # add all the items | 30 | 34 | book.add_item(c1) | 31 | 35 | book.add_item(c2) | 32 | 36 |
| 33 | | -# book.toc = (epub.Link('cover.xhtml', 'Cover', 'cover'), | 34 | | - book.toc = (epub.Link('chap_02.xhtml', 'Introduction', 'intro'), | 35 | | - (epub.Section('Languages'), | 36 | | - (c1, c2)) | | 37 | + book.toc = ( (epub.Section('Languages'), | | 38 | + (c1, c2)), | 37 | 39 | ) | 38 | 40 |
| 39 | | - # this should be maybe add by default or something | 40 | | - book.add_item(epub.EpubNcx()) | 41 | | - book.add_item(epub.EpubNav()) | 42 | 41 |
| 43 | 42 | style = ''' | 44 | 43 | @namespace epub "http://www.idpf.org/2007/ops"; | | 75 | 74 | nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content=style) | 76 | 75 | book.add_item(nav_css) | 77 | 76 |
| | 77 | + # this should be maybe add by default or something | | 78 | + book.add_item(epub.EpubNcx()) | | 79 | + nav = epub.EpubNav() | | 80 | + nav.add_item(nav_css) | | 81 | + book.add_item(nav) | | 82 | + | 78 | 83 | # this probably is not correct. should be able to define other properties also | 79 | 84 | book.spine = ['cover', 'nav', c1, c2] | 80 | 85 |
| 81 | 86 | # write book to the file | 82 | | - epub.writeEPUB('test.epub', book, {}) | | 87 | + epub.write_epub('test.epub', book) | | 88 | + | | 89 | + print '------------' | | 90 | + for x in nav.get_links_of_type('text/css'): | | 91 | + print x | 83 | 92 |
|
Collapse file Copy file name to clipboardExpand all lines: tests/epub_parse.py +8-7Lines changed: 8 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | from ebooklib import epub | 4 | 4 |
| | 5 | +book = epub.read_epub(sys.argv[1]) | 5 | 6 |
| 6 | | -book = epub.readEPUB(sys.argv[1]) | 7 | | - | 8 | | -print book.metadata | 9 | | - | | 7 | +epub.debug(book.metadata) | | 8 | +epub.debug(book.spine) | | 9 | +epub.debug(book.toc) | 10 | 10 | for it in book.items: | 11 | | - print it | | 11 | + epub.debug( it.file_name) | | 12 | + | | 13 | +for x in book.get_items_of_type(epub.ITEM_IMAGE): | | 14 | + print x | 12 | 15 |
| 13 | | -book.add_item(epub.EpubNav()) | 14 | | -epub.writeEPUB('test.epub', book, {}) | 15 | 16 |
|
|