Support for opening txt files
This commit is contained in:
55
lector/parsers/txt.py
Normal file
55
lector/parsers/txt.py
Normal file
@@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
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
|
@@ -105,6 +105,11 @@ else:
|
|||||||
print(critical_sting)
|
print(critical_sting)
|
||||||
logger.critical(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]
|
available_parsers = [i for i in sorter]
|
||||||
progressbar = None # This is populated by __main__
|
progressbar = None # This is populated by __main__
|
||||||
_progress_emitter = None # This is to be made into a global variable
|
_progress_emitter = None # This is to be made into a global variable
|
||||||
@@ -184,7 +189,7 @@ class BookSorter:
|
|||||||
return book_data
|
return book_data
|
||||||
|
|
||||||
def read_book(self, filename):
|
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
|
# full path of the ebook file
|
||||||
|
|
||||||
with open(filename, 'rb') as current_book:
|
with open(filename, 'rb') as current_book:
|
||||||
|
Reference in New Issue
Block a user