Skip to content
This repository has been archived by the owner on Mar 15, 2020. It is now read-only.

Commit

Permalink
Use only the first cover image, ignore duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
rupor-github committed Nov 22, 2017
1 parent 428f5ba commit 4510238
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
35 changes: 28 additions & 7 deletions modules/fb2html.py
Expand Up @@ -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:
Expand Down Expand Up @@ -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 = [] # Переменная для хранения текущей сноски
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1134,7 +1151,9 @@ def parse_body(self, elem):
back_ref = self.links_location[id_b]
except:
pass
self.buff.append('<p class="floatnote"><a href="{0}#{1}" id="{2}">{3}).</a>&#160;{4}</p>'.format(back_ref, id_b, id, save_html(note[0]) if len(note[0]) > 0 else '***', save_html(note[1])))
self.buff.append(
'<p class="floatnote"><a href="{0}#{1}" id="{2}">{3}).</a>&#160;{4}</p>'.format(back_ref, id_b, id, save_html(note[0]) if len(note[0]) > 0 else '***',
save_html(note[1])))
else:
continue
else:
Expand Down Expand Up @@ -1287,7 +1306,9 @@ def generate_cover(self):

self.buff = []
self.buff.append(HTMLHEAD)
self.buff.append('<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="100%" viewBox="0 0 {0} {1}" preserveAspectRatio="xMidYMid meet">'.format(self.screen_width, self.screen_height))
self.buff.append(
'<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="100%" viewBox="0 0 {0} {1}" preserveAspectRatio="xMidYMid meet">'.format(
self.screen_width, self.screen_height))
self.buff.append('<image width="{0}" height="{1}" xlink:href="images/{2}" />'.format(self.screen_width, self.screen_height, filename))
self.buff.append('</svg>')
self.buff.append(HTMLFOOT)
Expand Down
2 changes: 1 addition & 1 deletion version.py
Expand Up @@ -4,5 +4,5 @@

WINDOWS = platform.system().lower() == "windows"

VERSION = u'3.6.42'
VERSION = u'3.6.43'

0 comments on commit 4510238

Please sign in to comment.