-
Notifications
You must be signed in to change notification settings - Fork 4.3k
feat(epub): Add EPUB support #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cd6058e
feat(epub): Add EPUB support
0xRaduan 98f1cdb
add new dependencies
0xRaduan 33104e8
Merge branch 'main' into add-epub-support
gagb f1d9d1f
merge w/ main
0xRaduan 9db3fec
Merge remote-tracking branch 'origin/add-epub-support' into add-epub-…
0xRaduan 48f1216
migrate to use HTML converter + add convert_em method to it
0xRaduan 17fd153
merge w/ main + resolve merge conflicts
0xRaduan ef9e29e
address self-review
0xRaduan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
59 changes: 59 additions & 0 deletions
59
packages/markitdown/src/markitdown/converters/_epub_converter.py
This file contains hidden or 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,59 @@ | ||
from typing import Any | ||
|
||
from ebooklib import epub, ITEM_DOCUMENT | ||
|
||
from ._base import DocumentConverter, DocumentConverterResult | ||
from ._html_converter import HtmlConverter | ||
|
||
class EpubConverter(DocumentConverter): | ||
"""Converts EPUB files to Markdown. Preserves chapter structure and metadata.""" | ||
|
||
def convert(self, local_path: str, **kwargs: Any) -> DocumentConverterResult: | ||
"""Convert an EPUB file to markdown. | ||
|
||
Args: | ||
local_path: Path to the EPUB file | ||
**kwargs: Additional arguments (unused) | ||
|
||
Returns: | ||
DocumentConverterResult containing the converted markdown | ||
|
||
Raises: | ||
FileConversionException: If the file is not an EPUB file | ||
""" | ||
# Check if this is an EPUB file | ||
file_ext = kwargs.get("file_extension", "").lower() | ||
if not file_ext.endswith(".epub"): | ||
return None | ||
|
||
book = epub.read_epub(local_path) | ||
|
||
# Initialize result with book title | ||
result = DocumentConverterResult( | ||
title=( | ||
book.get_metadata("DC", "title")[0][0] | ||
if book.get_metadata("DC", "title") | ||
else None | ||
) | ||
) | ||
|
||
# Start with metadata | ||
metadata_md = [] | ||
if book.get_metadata("DC", "creator"): | ||
metadata_md.append(f"Author: {book.get_metadata('DC', 'creator')[0][0]}") | ||
if book.get_metadata("DC", "description"): | ||
metadata_md.append(f"\n{book.get_metadata('DC', 'description')[0][0]}") | ||
|
||
# Convert content | ||
content_md = [] | ||
for item in book.get_items(): | ||
if item.get_type() == ITEM_DOCUMENT: | ||
content = item.get_content().decode("utf-8") | ||
html_result = HtmlConverter()._convert(content) | ||
if html_result and html_result.text_content: | ||
content_md.append(html_result.text_content) | ||
|
||
# Combine all parts | ||
result.text_content = "\n\n".join(metadata_md + content_md) | ||
|
||
return result |
This file contains hidden or 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 |
---|---|---|
|
@@ -87,5 +87,14 @@ def convert_img(self, el: Any, text: str, convert_as_inline: bool) -> str: | |
|
||
return "" % (alt, src, title_part) | ||
|
||
def convert_em(self, el: Any, text: str, convert_as_inline: bool) -> str: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. noticed that it doesn't have an tag, and that's used in Epub as far as I know |
||
"""Convert emphasized text to Markdown format using underscores.""" | ||
if not text: | ||
return '' | ||
prefix, suffix, text = markdownify.chomp(text) # type: ignore | ||
if not text: | ||
return '' | ||
return '%s_%s_%s' % (prefix, text, suffix) | ||
|
||
def convert_soup(self, soup: Any) -> str: | ||
return super().convert_soup(soup) # type: ignore |
Binary file not shown.
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@