From b41352f2d01cb019cedafdc1a2f4bacb8c19e715 Mon Sep 17 00:00:00 2001 From: BasioMeusPuga Date: Sat, 10 Mar 2018 21:01:07 +0530 Subject: [PATCH] Make the first page a manual cover display Remove .css files to ensure fidelity to font settings --- TODO | 6 ++++-- ePub/read_epub.py | 11 +++++++++-- parsers/epub.py | 3 +-- parsers/mobi.py | 4 +++- widgets.py | 20 +++++++++++++++++++- 5 files changed, 36 insertions(+), 8 deletions(-) diff --git a/TODO b/TODO index e244e1a..6cb445f 100644 --- a/TODO +++ b/TODO @@ -49,16 +49,17 @@ TODO ✓ Deletion ✓ Cache next and previous images ✓ Set context menu for definitions and the like + ✓ Paragraph indentation Search document using QTextCursor? Comic view keyboard shortcuts Filetypes: + ✓ epub support + ✓ Homegrown solution please ✓ cbz, cbr support ✓ Keep font settings enabled but only for background color ✓ mobi, azw support Limit the extra files produced by KindleUnpack Have them save to memory - epub support - Homegrown solution please Other: ✓ Define every widget in code Bugs: @@ -77,6 +78,7 @@ TODO Pagination Use embedded fonts Scrolling: Smooth / By Line + Spacebar should not cut off lines at the top Shift to logging instead of print statements txt, doc, chm, djvu, fb2 support Include icons for filetype emblems diff --git a/ePub/read_epub.py b/ePub/read_epub.py index 514445b..cde3288 100644 --- a/ePub/read_epub.py +++ b/ePub/read_epub.py @@ -218,8 +218,8 @@ class EPUB: chapter_source = unquote(chapter_source.split('#')[0]) self.book['navpoint_dict'][chapter_source] = chapter_title - def parse_chapters(self, split_large_xml=False): - no_title_chapter = 1 + def parse_chapters(self, temp_dir=None, split_large_xml=False): + no_title_chapter = 0 self.book['book_list'] = [] for i in self.book['chapters_in_order']: chapter_data = self.read_from_zip(i).decode() @@ -234,6 +234,13 @@ class EPUB: (fallback_title, chapter_data)) no_title_chapter += 1 + cover_path = os.path.join(temp_dir, os.path.basename(self.filename)) + '- cover' + with open(cover_path, 'wb') as cover_temp: + cover_temp.write(self.book['cover']) + + self.book['book_list'][0] = ( + 'Cover', f'
Cover
') + else: # https://stackoverflow.com/questions/14444732/how-to-split-a-html-page-to-multiple-pages-using-python-and-beautiful-soup markup = BeautifulSoup(chapter_data, 'xml') diff --git a/parsers/epub.py b/parsers/epub.py index ccddeda..5b33831 100644 --- a/parsers/epub.py +++ b/parsers/epub.py @@ -17,7 +17,6 @@ # along with this program. If not, see . import os -import sys import zipfile from ePub.read_epub import EPUB @@ -63,7 +62,7 @@ class ParseEPUB: extract_path = os.path.join(self.temp_dir, self.file_md5) zipfile.ZipFile(self.filename).extractall(extract_path) - self.book_ref.parse_chapters() + self.book_ref.parse_chapters(temp_dir=self.temp_dir) file_settings = { 'images_only': False} return self.book['book_list'], file_settings diff --git a/parsers/mobi.py b/parsers/mobi.py index 604a3ff..50287c4 100644 --- a/parsers/mobi.py +++ b/parsers/mobi.py @@ -35,6 +35,7 @@ class ParseMOBI: self.filename = filename self.epub_filepath = None self.split_large_xml = False + self.temp_dir = temp_dir self.extract_dir = os.path.join(temp_dir, file_md5) def read_book(self): @@ -82,7 +83,8 @@ class ParseMOBI: extract_path = os.path.join(self.extract_dir) zipfile.ZipFile(self.epub_filepath).extractall(extract_path) - self.book_ref.parse_chapters(self.split_large_xml) + self.book_ref.parse_chapters( + temp_dir=self.temp_dir, split_large_xml=self.split_large_xml) file_settings = { 'images_only': False} return self.book['book_list'], file_settings diff --git a/widgets.py b/widgets.py index 0bd1f2c..4c84f58 100644 --- a/widgets.py +++ b/widgets.py @@ -72,6 +72,17 @@ class Tab(QtWidgets.QWidget): self.window().temp_dir.path(), self.metadata['hash']) relative_paths = [] for i in os.walk(relative_path_root): + + # TODO + # Rename the .css files to something else here and keep + # a record of them + # Currently, I'm just removing them for the sake of simplicity + for j in i[2]: + file_extension = os.path.splitext(j)[1] + if file_extension == '.css': + file_path = os.path.join(relative_path_root, j) + os.remove(file_path) + relative_paths.append(os.path.join(relative_path_root, i[0])) self.contentView.setSearchPaths(relative_paths) @@ -283,13 +294,20 @@ class Tab(QtWidgets.QWidget): block_format.setLineHeight( line_spacing, QtGui.QTextBlockFormat.ProportionalHeight) + block_format.setTextIndent(50) + # Give options for alignment alignment_dict = { 'left': QtCore.Qt.AlignLeft, 'right': QtCore.Qt.AlignRight, 'center': QtCore.Qt.AlignCenter, 'justify': QtCore.Qt.AlignJustify} - block_format.setAlignment(alignment_dict[text_alignment]) + + current_index = self.window().bookToolBar.tocBox.currentIndex() + if current_index == 0: + block_format.setAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignHCenter) + else: + block_format.setAlignment(alignment_dict[text_alignment]) # Also for padding # Using setViewPortMargins for this disables scrolling in the margins