From 2ce345e5dd9dba1f54f9c74b63b553877d4d003c Mon Sep 17 00:00:00 2001
From: Andrey Efremov
Date: Sun, 6 Oct 2019 13:04:07 +0700
Subject: [PATCH 01/18] FB2 Output: Speed up image data splitting
---
src/calibre/ebooks/fb2/fb2ml.py | 12 +++---------
1 file changed, 3 insertions(+), 9 deletions(-)
diff --git a/src/calibre/ebooks/fb2/fb2ml.py b/src/calibre/ebooks/fb2/fb2ml.py
index ba1f775645..10ea49dc64 100644
--- a/src/calibre/ebooks/fb2/fb2ml.py
+++ b/src/calibre/ebooks/fb2/fb2ml.py
@@ -19,7 +19,7 @@ from calibre.constants import __appname__, __version__
from calibre.utils.localization import lang_as_iso639_1
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
+from polyglot.builtins import unicode_type, string_or_bytes, range
from polyglot.binary import as_base64_unicode
@@ -315,14 +315,8 @@ class FB2MLizer(object):
raw_data = as_base64_unicode(item.data)
content_type = item.media_type
# Don't put the encoded image on a single line.
- data = ''
- col = 1
- for char in raw_data:
- if col == 72:
- data += '\n'
- col = 1
- col += 1
- data += char
+ step = 72
+ data = '\n'.join(raw_data[i:i+step] for i in range(0, len(raw_data), step))
images.append('%s\n' % (self.image_hrefs[item.href], content_type, data))
except Exception as e:
self.log.error('Error: Could not include file %s because '
From 6071352d678fa1ad3dcd73f2c1c932a198a7a4c4 Mon Sep 17 00:00:00 2001
From: Andrey Efremov
Date: Sun, 6 Oct 2019 13:28:45 +0700
Subject: [PATCH 02/18] FB2 Output: Fix id for PNG images
---
src/calibre/ebooks/fb2/fb2ml.py | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/src/calibre/ebooks/fb2/fb2ml.py b/src/calibre/ebooks/fb2/fb2ml.py
index 10ea49dc64..3a191cd54e 100644
--- a/src/calibre/ebooks/fb2/fb2ml.py
+++ b/src/calibre/ebooks/fb2/fb2ml.py
@@ -253,9 +253,8 @@ class FB2MLizer(object):
if cover_href:
# Only write the image tag if it is in the manifest.
- if cover_href in self.oeb_book.manifest.hrefs.keys():
- if cover_href not in self.image_hrefs.keys():
- self.image_hrefs[cover_href] = '_%s.jpg' % len(self.image_hrefs.keys())
+ if cover_href in self.oeb_book.manifest.hrefs and cover_href not in self.image_hrefs:
+ self.image_hrefs[cover_href] = 'img_%s' % len(self.image_hrefs)
return '' % self.image_hrefs[cover_href]
return ''
@@ -462,7 +461,7 @@ class FB2MLizer(object):
ihref = urlnormalize(page.abshref(elem_tree.attrib['src']))
if ihref in self.oeb_book.manifest.hrefs:
if ihref not in self.image_hrefs:
- self.image_hrefs[ihref] = '_%s.jpg' % len(self.image_hrefs)
+ self.image_hrefs[ihref] = 'img_%s' % len(self.image_hrefs)
p_txt, p_tag = self.ensure_p()
fb2_out += p_txt
tags += p_tag
From 88159288a87d5e408932edae232378c077739753 Mon Sep 17 00:00:00 2001
From: Andrey Efremov
Date: Sun, 6 Oct 2019 13:07:17 +0700
Subject: [PATCH 03/18] FB2 Output: Remove extra line breaks and prettify XML
markup
---
src/calibre/ebooks/fb2/fb2ml.py | 15 ++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)
diff --git a/src/calibre/ebooks/fb2/fb2ml.py b/src/calibre/ebooks/fb2/fb2ml.py
index 3a191cd54e..91d05f0140 100644
--- a/src/calibre/ebooks/fb2/fb2ml.py
+++ b/src/calibre/ebooks/fb2/fb2ml.py
@@ -78,7 +78,7 @@ class FB2MLizer(object):
# Clean up pargraph endings.
text = re.sub(r'(?miu)\s*
', '', text)
# Put paragraphs following a paragraph on a separate line.
- text = re.sub(r'(?miu)\s*', '
\n\n', text)
+ text = re.sub(r'(?miu)
\s*', '
\n', text)
# Remove empty title elements.
text = re.sub(r'(?miu)
\s*', '', text)
@@ -88,11 +88,11 @@ class FB2MLizer(object):
text = re.sub(r'(?miu)', '', text)
# Clean up sections start and ends.
text = re.sub(r'(?miu)\s*', '\n', text)
- text = re.sub(r'(?miu)\s*', '\n\n', text)
+ text = re.sub(r'(?miu)\s*', '\n', text)
text = re.sub(r'(?miu)\s*', '\n', text)
text = re.sub(r'(?miu)\s*', '\n', text)
# Put sectnions followed by sections on a separate line.
- text = re.sub(r'(?miu)\s*\n\n', text)
+ text = re.sub(r'(?miu)\s*\n', text)
if self.opts.insert_blank_line:
text = re.sub(r'(?miu)', '', text)
@@ -223,7 +223,7 @@ class FB2MLizer(object):
\n''') % metadata
def fb2_footer(self):
- return '\n'
+ return ''
def get_cover(self):
from calibre.ebooks.oeb.base import OEB_RASTER_IMAGES
@@ -291,7 +291,8 @@ class FB2MLizer(object):
text.append('')
self.section_level -= 1
- return ''.join(text) + '