From fa3cf5c1788b61c4512ee69577b3c1579ae6966e Mon Sep 17 00:00:00 2001 From: gryf Date: Mon, 13 Apr 2020 12:11:48 +0200 Subject: [PATCH] Added FB2 format. --- ebook_converter/data/fb2.xsl | 448 ++++++++++++++++++ .../ebooks/conversion/plugins/fb2_input.py | 2 +- ebook_converter/ebooks/fb2/__init__.py | 2 +- ebook_converter/ebooks/fb2/fb2ml.py | 32 +- ebook_converter/ebooks/metadata/fb2.py | 26 +- 5 files changed, 479 insertions(+), 31 deletions(-) create mode 100644 ebook_converter/data/fb2.xsl diff --git a/ebook_converter/data/fb2.xsl b/ebook_converter/data/fb2.xsl new file mode 100644 index 0000000..ab68a59 --- /dev/null +++ b/ebook_converter/data/fb2.xsl @@ -0,0 +1,448 @@ + + + + + + + + + + + + <xsl:value-of select="fb:description/fb:title-info/fb:book-title"/> + + + + + + +
+ +
+
+
+ +
    + +
+
+ + + + +
+
+ +

+ +

+
+ + +
+ + +
+ + + + + + + +
+
+ + +
  • + + + , # + + + +
      + + + +
    +
    + + + + + + + + + +
  • + + +
    +
    +
  • + + +
    + + + + + + +
    +
    + + + + + + None + + + +
    + + + + + +
    +
    + + + +
    + + + + + + + + + + + + + + TOC_ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    +
    + + + + + + + +
    + +
    +
    + + + + paragraph + + + + + + + + + + + + + + + + + + + + + + + + + +
    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

    Annotation

    + +
    + + + + +
    +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    + + + + + +
    + +
    +
    +
    + + +
    + +
    +
    + + +
    + + + + + +
    + +
    +
    +
    + + +
    +
    +
    + + + + +     +
    +
    + +     +
    +
    +
    +
    + + +
    + + + + + +
    + +
    +
    +
    + + + + +
    +
    + + + + + + + +
    +
    + + +
    + + + + + + + + + + + + + +
    +
    + + + + + + + + + + + + + + + + + + + + + + + + +
    diff --git a/ebook_converter/ebooks/conversion/plugins/fb2_input.py b/ebook_converter/ebooks/conversion/plugins/fb2_input.py index e6ee0ab..2e108ef 100644 --- a/ebook_converter/ebooks/conversion/plugins/fb2_input.py +++ b/ebook_converter/ebooks/conversion/plugins/fb2_input.py @@ -89,7 +89,7 @@ class FB2Input(InputFormatPlugin): log.debug('Converting XML to HTML...') with open(pkg_resources.resource_filename('ebook_converter', 'data/fb2.xsl')) as f: - ss = f.read().decode() + ss = f.read() ss = ss.replace("__FB_NS__", fb_ns) if options.no_inline_fb2_toc: log('Disabling generation of inline FB2 TOC') diff --git a/ebook_converter/ebooks/fb2/__init__.py b/ebook_converter/ebooks/fb2/__init__.py index e42aba2..5dfd502 100644 --- a/ebook_converter/ebooks/fb2/__init__.py +++ b/ebook_converter/ebooks/fb2/__init__.py @@ -9,7 +9,7 @@ __docformat__ = 'restructuredtext en' def base64_decode(raw): from io import BytesIO - from polyglot.binary import from_base64_bytes + from ebook_converter.polyglot.binary import from_base64_bytes # First try the python implementation as it is faster try: diff --git a/ebook_converter/ebooks/fb2/fb2ml.py b/ebook_converter/ebooks/fb2/fb2ml.py index d8a0098..edf5583 100644 --- a/ebook_converter/ebooks/fb2/fb2ml.py +++ b/ebook_converter/ebooks/fb2/fb2ml.py @@ -14,15 +14,15 @@ from datetime import datetime from lxml import etree -from calibre import prepare_string_for_xml -from calibre.constants import __appname__, __version__ -from calibre.utils.localization import lang_as_iso639_1 -from calibre.utils.xml_parse import safe_xml_fromstring -from calibre.utils.img import save_cover_data_to -from calibre.ebooks.oeb.base import urlnormalize -from polyglot.builtins import unicode_type, string_or_bytes, range, filter -from polyglot.binary import as_base64_unicode -from polyglot.urllib import urlparse +from ebook_converter import prepare_string_for_xml +from ebook_converter.constants import __appname__, __version__ +from ebook_converter.utils.localization import lang_as_iso639_1 +from ebook_converter.utils.xml_parse import safe_xml_fromstring +from ebook_converter.utils.img import save_cover_data_to +from ebook_converter.ebooks.oeb.base import urlnormalize +from ebook_converter.polyglot.builtins import unicode_type, string_or_bytes, range, filter +from ebook_converter.polyglot.binary import as_base64_unicode +from ebook_converter.polyglot.urllib import urlparse class FB2MLizer(object): @@ -112,7 +112,7 @@ class FB2MLizer(object): return text def fb2_header(self): - from calibre.ebooks.oeb.base import OPF + from ebook_converter.ebooks.oeb.base import OPF metadata = {} metadata['title'] = self.oeb_book.metadata.title[0].value metadata['appname'] = __appname__ @@ -204,7 +204,7 @@ class FB2MLizer(object): except Exception: metadata['comments'] = '' else: - from calibre.utils.html2text import html2text + from ebook_converter.utils.html2text import html2text metadata['comments'] = '

    {}

    '.format(prepare_string_for_xml(html2text(comments.value).strip())) # Keep the indentation level of the description the same as the body. @@ -242,7 +242,7 @@ class FB2MLizer(object): return '' def get_cover(self): - from calibre.ebooks.oeb.base import OEB_RASTER_IMAGES + from ebook_converter.ebooks.oeb.base import OEB_RASTER_IMAGES cover_href = None @@ -276,8 +276,8 @@ class FB2MLizer(object): return '' def get_text(self): - from calibre.ebooks.oeb.base import XHTML - from calibre.ebooks.oeb.stylizer import Stylizer + from ebook_converter.ebooks.oeb.base import XHTML + from ebook_converter.ebooks.oeb.stylizer import Stylizer text = [''] # Create main section if there are no others to create @@ -314,7 +314,7 @@ class FB2MLizer(object): ''' This function uses the self.image_hrefs dictionary mapping. It is populated by the dump_text function. ''' - from calibre.ebooks.oeb.base import OEB_RASTER_IMAGES + from ebook_converter.ebooks.oeb.base import OEB_RASTER_IMAGES images = [] for item in self.oeb_book.manifest: @@ -405,7 +405,7 @@ class FB2MLizer(object): @return: List of string representing the XHTML converted to FB2 markup. ''' - from calibre.ebooks.oeb.base import XHTML_NS, barename, namespace + from ebook_converter.ebooks.oeb.base import XHTML_NS, barename, namespace elem = elem_tree # Ensure what we are converting is not a string and that the fist tag is part of the XHTML namespace. diff --git a/ebook_converter/ebooks/metadata/fb2.py b/ebook_converter/ebooks/metadata/fb2.py index ceaf047..2b84668 100644 --- a/ebook_converter/ebooks/metadata/fb2.py +++ b/ebook_converter/ebooks/metadata/fb2.py @@ -13,15 +13,15 @@ from string import ascii_letters, digits from lxml import etree -from calibre.utils.date import parse_only_date -from calibre.utils.img import save_cover_data_to -from calibre.utils.xml_parse import safe_xml_fromstring -from calibre.utils.imghdr import identify -from calibre import guess_type, guess_all_extensions, prints, force_unicode -from calibre.ebooks.metadata import MetaInformation, check_isbn -from calibre.ebooks.chardet import xml_to_unicode -from polyglot.builtins import unicode_type -from polyglot.binary import as_base64_unicode +from ebook_converter.utils.date import parse_only_date +from ebook_converter.utils.img import save_cover_data_to +from ebook_converter.utils.xml_parse import safe_xml_fromstring +from ebook_converter.utils.imghdr import identify +from ebook_converter import guess_type, guess_all_extensions, prints, force_unicode +from ebook_converter.ebooks.metadata import MetaInformation, check_isbn +from ebook_converter.ebooks.chardet import xml_to_unicode +from ebook_converter.polyglot.builtins import unicode_type +from ebook_converter.polyglot.binary import as_base64_unicode NAMESPACES = { @@ -90,7 +90,7 @@ class Context(object): def get_fb2_data(stream): - from calibre.utils.zipfile import ZipFile, BadZipfile + from ebook_converter.utils.zipfile import ZipFile, BadZipfile pos = stream.tell() try: zf = ZipFile(stream) @@ -225,7 +225,7 @@ def _parse_cover(root, mi, ctx): def _parse_cover_data(root, imgid, mi, ctx): - from calibre.ebooks.fb2 import base64_decode + from ebook_converter.ebooks.fb2 import base64_decode elm_binary = ctx.XPath('//fb:binary[@id="%s"]'%imgid)(root) if elm_binary: mimetype = elm_binary[0].get('content-type', 'image/jpeg') @@ -330,7 +330,7 @@ def _set_title(title_info, mi, ctx): def _set_comments(title_info, mi, ctx): if not mi.is_null('comments'): - from calibre.utils.html2text import html2text + from ebook_converter.utils.html2text import html2text ctx.clear_meta_tags(title_info, 'annotation') title = ctx.get_or_create(title_info, 'annotation') ctx.text2fb2(title, html2text(mi.comments)) @@ -432,7 +432,7 @@ def set_metadata(stream, mi, apply_null=False, update_timestamp=False): stream.seek(0) stream.truncate() if zip_file_name: - from calibre.utils.zipfile import ZipFile + from ebook_converter.utils.zipfile import ZipFile with ZipFile(stream, 'w') as zf: zf.writestr(zip_file_name, raw) else: