From 45102385ad2374766444989a77deda8878ef27f4 Mon Sep 17 00:00:00 2001 From: rupor Date: Wed, 22 Nov 2017 09:39:11 -0500 Subject: [PATCH] Use only the first cover image, ignore duplicates --- modules/fb2html.py | 35 ++++++++++++++++++++++++++++------- version.py | 2 +- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/modules/fb2html.py b/modules/fb2html.py index c592e47..c58731a 100644 --- a/modules/fb2html.py +++ b/modules/fb2html.py @@ -50,11 +50,13 @@ def sanitize_id(string): else: return '' + def make_dir(filename): d = os.path.dirname(filename) if not os.path.exists(d): os.makedirs(d) + def write_file(buff, filename): make_dir(filename) with codecs.open(filename, 'w', 'utf-8') as f: @@ -195,7 +197,7 @@ def __init__(self, fb2file, mobifile, tempdir, config): # Для включения сносок и комментариев в текст книги self.notes_dict = {} # Словарь со сносками и комментариями self.notes_order = [] # Notes in order of discovery - self.notes_titles = {} # Dictionary of note body titles + self.notes_titles = {} # Dictionary of note body titles self.notes_mode = config.current_profile['notesMode'] # Режим отображения сносок: inline, block self.notes_bodies = config.current_profile['notesBodies'] self.current_notes = [] # Переменная для хранения текущей сноски @@ -289,6 +291,7 @@ def generate(self): self.parse_body(child) self.correct_links() + self.correct_cover_images() if self.generate_toc_page: self.generate_toc() @@ -364,11 +367,25 @@ def correct_links(self): except: pass - self.buff = str.replace(str(etree.tostring(root, encoding='utf-8', method='xml', xml_declaration=True), 'utf-8'),' encoding=\'utf-8\'', '', 1) + self.buff = str.replace(str(etree.tostring(root, encoding='utf-8', method='xml', xml_declaration=True), 'utf-8'), ' encoding=\'utf-8\'', '', 1) self.current_file = fl self.write_buff() + def correct_cover_images(self): + # Leave only first cover - drop the rest + if self.book_cover: + have_cover = False + covers = [] + for id, type, file in self.image_file_list: + if id == self.book_cover: + if have_cover: + covers.append((id, type, file)) + else: + have_cover = True + for item in covers: + self.image_file_list.remove(item) + def write_buff(self, dname='', fname=''): if len(fname) == 0: dirname = self.temp_content_dir @@ -404,7 +421,7 @@ def parse_note_elem(self, elem, body_name): # this is essetially a hack to preserve notes title (if any) for floating notes toc_title = etree.tostring(elem, method='text', encoding='utf-8').decode('utf-8').strip() - toc_title = re.compile('[\[{].*[\]}]').sub('', toc_title) # Удалим остатки ссылок + toc_title = re.compile('[\[{].*[\]}]').sub('', toc_title) # Удалим остатки ссылок if toc_title: # Do real title parsing (notes file is not in pages_list anyways) save_buff = self.buff @@ -578,7 +595,7 @@ def parse_binary(self, elem): img = Image.open(io.BytesIO(buff)) real_type = Image.MIME[img.format] format = img.format.lower() - filename = "bin{0:08}.{1}".format(self.image_count,format.lower().replace('jpeg','jpg')) + filename = "bin{0:08}.{1}".format(self.image_count, format.lower().replace('jpeg', 'jpg')) full_name = os.path.join(os.path.join(self.temp_content_dir, 'images'), filename) make_dir(full_name) @@ -646,7 +663,7 @@ def parse_emptyline(self): def parse_title(self, elem): toc_ref_id = 'tocref{0}'.format(self.toc_index) toc_title = etree.tostring(elem, method='text', encoding='utf-8').decode('utf-8').strip() - toc_title = re.compile('[\[{].*[\]}]').sub('', toc_title) # Удалим остатки ссылок + toc_title = re.compile('[\[{].*[\]}]').sub('', toc_title) # Удалим остатки ссылок if not self.body_name or self.first_header_in_body: self.header = True @@ -1134,7 +1151,9 @@ def parse_body(self, elem): back_ref = self.links_location[id_b] except: pass - self.buff.append('

{3}). {4}

'.format(back_ref, id_b, id, save_html(note[0]) if len(note[0]) > 0 else '***', save_html(note[1]))) + self.buff.append( + '

{3}). {4}

'.format(back_ref, id_b, id, save_html(note[0]) if len(note[0]) > 0 else '***', + save_html(note[1]))) else: continue else: @@ -1287,7 +1306,9 @@ def generate_cover(self): self.buff = [] self.buff.append(HTMLHEAD) - self.buff.append(''.format(self.screen_width, self.screen_height)) + self.buff.append( + ''.format( + self.screen_width, self.screen_height)) self.buff.append(''.format(self.screen_width, self.screen_height, filename)) self.buff.append('') self.buff.append(HTMLFOOT) diff --git a/version.py b/version.py index e9434ab..f568069 100644 --- a/version.py +++ b/version.py @@ -4,5 +4,5 @@ WINDOWS = platform.system().lower() == "windows" -VERSION = u'3.6.42' +VERSION = u'3.6.43'