From 24e45ac2b78cf8fad1888ad9a258048470fe56e7 Mon Sep 17 00:00:00 2001 From: Dmitrii Petukhov Date: Sat, 6 Jul 2019 00:09:50 +0100 Subject: [PATCH 1/3] Support for opening txt files --- lector/parsers/txt.py | 55 +++++++++++++++++++++++++++++++++++++++++++ lector/sorter.py | 7 +++++- 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 lector/parsers/txt.py diff --git a/lector/parsers/txt.py b/lector/parsers/txt.py new file mode 100644 index 0000000..9994ecf --- /dev/null +++ b/lector/parsers/txt.py @@ -0,0 +1,55 @@ +# This file is a part of Lector, a Qt based ebook reader +# Copyright (C) 2017-2019 BasioMeusPuga + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import collections +import os + +import textile + + +class ParseTXT: + """Parser for TXT files.""" + + def __init__(self, filename, *args): + """Initialize new instance of the TXT parser.""" + self.filename = filename + + def read_book(self): + """Prepare the parser to read book.""" + pass + + def generate_metadata(self): + """Generate metadata for the book.""" + title = os.path.basename(self.filename) + author = 'Unknown' + year = 9999 + isbn = None + tags = [] + cover = None + + Metadata = collections.namedtuple( + 'Metadata', ['title', 'author', 'year', 'isbn', 'tags', 'cover']) + return Metadata(title, author, year, isbn, tags, cover) + + def generate_content(self): + """Generate content of the book.""" + with open(self.filename, 'rt') as txt: + text = txt.read() + content = [textile.textile(text)] + + toc = [(1, 'Text', 1)] + + return toc, content, False diff --git a/lector/sorter.py b/lector/sorter.py index fca919d..c30c1bd 100644 --- a/lector/sorter.py +++ b/lector/sorter.py @@ -105,6 +105,11 @@ else: print(critical_sting) logger.critical(critical_sting) +# Text file parser +# TODO Check if textile is installed +from lector.parsers.txt import ParseTXT +sorter['txt'] = ParseTXT + available_parsers = [i for i in sorter] progressbar = None # This is populated by __main__ _progress_emitter = None # This is to be made into a global variable @@ -184,7 +189,7 @@ class BookSorter: return book_data def read_book(self, filename): - # filename is expected as a string containg the + # filename is expected as a string containing the # full path of the ebook file with open(filename, 'rb') as current_book: From 5e74b6f261ab79ebfc68c2fee4392ede55db663b Mon Sep 17 00:00:00 2001 From: Dmitrii Petukhov Date: Sat, 6 Jul 2019 07:30:40 +0100 Subject: [PATCH 2/3] Fix for bookmarks in books without cover --- lector/widgets.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lector/widgets.py b/lector/widgets.py index 8b47bd5..7aa1788 100644 --- a/lector/widgets.py +++ b/lector/widgets.py @@ -272,7 +272,7 @@ class Tab(QtWidgets.QWidget): # Finally, to make sure the cover image isn't # scrolled halfway through on first open, - if self.metadata['position']['current_chapter'] == 1: + if self.metadata['cover'] and self.metadata['position']['current_chapter'] == 1: self.contentView.verticalScrollBar().setValue(0) def generate_position(self, is_read=False): From d0cdd531a9de68f1d25d4ae63de9d438618008bb Mon Sep 17 00:00:00 2001 From: Dmitrii Petukhov Date: Sat, 6 Jul 2019 07:37:32 +0100 Subject: [PATCH 3/3] Check for textile installation --- README.md | 1 + lector/sorter.py | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 73655cb..9f24409 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ Bitcoin: 17jaxj26vFJNqQ2hEVerbBV5fpTusfqFro | python-pymupdf | 1.14.5 | PDF support | | python-djvulibre | 0.8.4 | DjVu support | | python-markdown | 3.0.1 | Markdown support | +| textile | 3.0.4 | TXT support | ## Support When reporting issues: diff --git a/lector/sorter.py b/lector/sorter.py index c30c1bd..b836f9a 100644 --- a/lector/sorter.py +++ b/lector/sorter.py @@ -105,10 +105,15 @@ else: print(critical_sting) logger.critical(critical_sting) -# Text file parser -# TODO Check if textile is installed -from lector.parsers.txt import ParseTXT -sorter['txt'] = ParseTXT +# txt - Optional +textile_check = importlib.util.find_spec('textile') +if textile_check: + from lector.parsers.txt import ParseTXT + sorter['txt'] = ParseTXT +else: + error_string = 'textile is not installed. Will be unable to load TXT files.' + print(error_string) + logger.error(error_string) available_parsers = [i for i in sorter] progressbar = None # This is populated by __main__