From 56f15528c222ac4e319c82b8398a02cf7e523786 Mon Sep 17 00:00:00 2001 From: BasioMeusPuga Date: Sat, 16 Mar 2019 12:02:30 -0400 Subject: [PATCH] Markdown support --- lector/__main__.py | 2 +- lector/parsers/markdown.py | 54 ++++++++++++++++++++++++++++++++++++++ lector/sorter.py | 10 +++++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 lector/parsers/markdown.py diff --git a/lector/__main__.py b/lector/__main__.py index b515556..8ffca05 100755 --- a/lector/__main__.py +++ b/lector/__main__.py @@ -265,7 +265,7 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): # Get list of available parsers self.available_parsers = '*.' + ' *.'.join(sorter.available_parsers) - logger.info('Available parsers: ' + self.available_parsers) + logger.log(60, 'Available parsers: ' + self.available_parsers) # The Library tab gets no button self.tabWidget.tabBar().setTabButton( diff --git a/lector/parsers/markdown.py b/lector/parsers/markdown.py new file mode 100644 index 0000000..1ee2ba2 --- /dev/null +++ b/lector/parsers/markdown.py @@ -0,0 +1,54 @@ +# 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 os +import logging +import collections + +import markdown + +logger = logging.getLogger(__name__) + + +class ParseMD: + def __init__(self, filename, *args): + self.book = None + self.filename = filename + + def read_book(self): + self.book = None + + def generate_metadata(self): + 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): + with open(self.filename, 'r') as book: + text = book.read() + content = [markdown.markdown(text)] + + toc = [(1, 'Markdown', 1)] + + # Return toc, content, images_only + return toc, content, False diff --git a/lector/sorter.py b/lector/sorter.py index 0b4cf44..e229ef8 100644 --- a/lector/sorter.py +++ b/lector/sorter.py @@ -72,6 +72,16 @@ else: print(error_string) logger.error(error_string) +# markdown - Optional +markdown_check = importlib.util.find_spec('markdown') +if markdown_check: + from lector.parsers.markdown import ParseMD + sorter['md'] = ParseMD +else: + error_string = 'markdown is not installed. Will be unable to load Markdown files.' + print(error_string) + logger.error(error_string) + # python-lxml - Required for everything except comics lxml_check = importlib.util.find_spec('lxml') xmltodict_check = importlib.util.find_spec('xmltodict')