From ddeba8a8483dc73d0103030afe46fc5bce51d8a0 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Tue, 17 Sep 2024 14:17:16 +0200 Subject: [PATCH 1/1] Remove legacy code for txt. --- MANIFEST.in | 1 - scripts/book2txt | 32 --- setup.py | 1 - src/librarian/functions.py | 26 -- src/librarian/parser.py | 8 - src/librarian/text.py | 133 ----------- src/librarian/xslt/book2txt.xslt | 391 ------------------------------- 7 files changed, 592 deletions(-) delete mode 100755 scripts/book2txt delete mode 100644 src/librarian/text.py delete mode 100644 src/librarian/xslt/book2txt.xslt diff --git a/MANIFEST.in b/MANIFEST.in index 40f0f90..cdd48f6 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -4,7 +4,6 @@ include NOTICE include tox.ini recursive-include scripts *.py *.css recursive-include tests *.py *.xml *.html *.out *.txt *.jpeg -include src/librarian/xslt/*.xslt include src/librarian/pdf/* include src/librarian/fb2/* include src/librarian/fonts/* diff --git a/scripts/book2txt b/scripts/book2txt deleted file mode 100755 index 8b75f98..0000000 --- a/scripts/book2txt +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env python -# This file is part of Librarian, licensed under GNU Affero GPLv3 or later. -# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. -# -from librarian.book2anything import Book2Anything, Option -from librarian.parser import WLDocument - - -class Book2Txt(Book2Anything): - format_name = "TXT" - ext = "txt" - uses_cover = False - uses_provider = False - parser_options = [ - Option('-i', '--ignore-dublin-core', dest='parse_dublincore', - action='store_false', default=True, - help='don\'t try to parse dublin core metadata') - ] - transform_options = [ - Option('-w', '--wrap', action='store', type='int', dest='wrapping', default=0, - help='set line wrap column') - ] - transform_flags = [ - Option('-r', '--raw', dest='raw-text', - action='store_true', default=False, - help='Produce raw text, without any surrounding info.') - ] - transform = WLDocument.as_text - - -if __name__ == '__main__': - Book2Txt.run() diff --git a/setup.py b/setup.py index 2af0a05..afca003 100755 --- a/setup.py +++ b/setup.py @@ -51,7 +51,6 @@ setup( ] }, scripts=[ - 'scripts/book2txt', 'scripts/book2pdf', 'scripts/book2fb2', 'scripts/book2cover', diff --git a/src/librarian/functions.py b/src/librarian/functions.py index bcaa784..cde6494 100644 --- a/src/librarian/functions.py +++ b/src/librarian/functions.py @@ -64,32 +64,6 @@ def reg_ends_white(): _register_function(ends_white) -def reg_wrap_words(): - def wrap_words(context, text, wrapping): - """ - XPath extension function automatically wrapping words - in passed text. - """ - if isinstance(text, list): - text = ''.join(text) - if not wrapping: - return text - - words = re.split(r'\s', text) - - line_length = 0 - lines = [[]] - for word in words: - line_length += len(word) + 1 - if line_length > wrapping: - # Max line length was exceeded. We create new line - lines.append([]) - line_length = len(word) - lines[-1].append(word) - return '\n'.join(' '.join(line) for line in lines) - _register_function(wrap_words) - - def reg_person_name(): def person_name(context, text): """ Converts "Name, Forename" to "Forename Name" """ diff --git a/src/librarian/parser.py b/src/librarian/parser.py index b4e4c5c..6e0a9c9 100644 --- a/src/librarian/parser.py +++ b/src/librarian/parser.py @@ -222,14 +222,6 @@ class WLDocument: # Converters - def as_html(self, *args, **kwargs): - from librarian import html - return html.transform(self, *args, **kwargs) - - def as_text(self, *args, **kwargs): - from librarian import text - return text.transform(self, *args, **kwargs) - def as_pdf(self, *args, **kwargs): from librarian import pdf return pdf.transform(self, *args, **kwargs) diff --git a/src/librarian/text.py b/src/librarian/text.py deleted file mode 100644 index d3df5fe..0000000 --- a/src/librarian/text.py +++ /dev/null @@ -1,133 +0,0 @@ -# This file is part of Librarian, licensed under GNU Affero GPLv3 or later. -# Copyright © Fundacja Wolne Lektury. See NOTICE for more information. -# -import copy -from librarian import functions, OutputFile, get_resource -from lxml import etree -import io -import os - - -functions.reg_substitute_entities() -functions.reg_wrap_words() -functions.reg_strip() -functions.reg_person_name() - - -with io.open(get_resource("res/text/template.txt")) as f: - TEMPLATE = f.read() - - -def transform(wldoc, flags=None, **options): - """ - Transforms input_file in XML to output_file in TXT. - possible flags: raw-text, - """ - # Parse XSLT - style_filename = os.path.join(os.path.dirname(__file__), - 'xslt/book2txt.xslt') - style = etree.parse(style_filename) - - document = copy.deepcopy(wldoc) - del wldoc - document.swap_endlines() - - if flags: - for flag in flags: - document.edoc.getroot().set(flag, 'yes') - if 'wrapping' in options: - options['wrapping'] = str(options['wrapping']) - - result = document.transform(style, **options) - - if not flags or 'raw-text' not in flags: - if document.book_info: - parsed_dc = document.book_info - description = parsed_dc.description - url = document.book_info.url - - license_name = parsed_dc.license_description - license = parsed_dc.license - license_description = [ - ( - "Wszystkie zasoby Wolnych Lektur możesz swobodnie wykorzystywać, " - "publikować i rozpowszechniać pod warunkiem zachowania warunków " - "licencji i zgodnie z Zasadami wykorzystania Wolnych Lektur." - ) - ] - - if license: - license_description.append( - "Ten utwór jest udostępniony na licencji %s: %s" % ( - license_name, license - ) - ) - else: - license_description.append( - "Ten utwór jest w domenie publicznej." - ) - license_description.append( - "Wszystkie materiały dodatkowe (przypisy, motywy literackie) są " - "udostępnione na Licencji Wolnej Sztuki 1.3: " - "https://artlibre.org/licence/lal/pl/\n" - "Fundacja Wolne Lektury zastrzega sobie prawa do wydania " - "krytycznego zgodnie z art. Art.99(2) Ustawy o prawach autorskich " - "i prawach pokrewnych.\nWykorzystując zasoby z Wolnych Lektur, " - "należy pamiętać o zapisach licencji oraz zasadach, które " - "spisaliśmy w Zasadach wykorzystania Wolnych Lektur: " - "https://wolnelektury.pl/info/zasady-wykorzystania/\nZapoznaj " - "się z nimi, zanim udostępnisz dalej nasze książki." - ) - license_description = "\n".join(license_description) - - source = parsed_dc.source_name - if source: - source = "\n\nTekst opracowany na podstawie: " + source - else: - source = '' - - contributors = ', '.join( - person.readable() - for person in sorted(set( - p for p in ( - parsed_dc.technical_editors + parsed_dc.editors - ) if p)) - ) - if contributors: - contributors = ( - "\n\nOpracowanie redakcyjne i przypisy: %s." - % contributors - ) - funders = ', '.join(parsed_dc.funders) - if funders: - funders = "\n\nPublikację wsparli i wsparły: %s." % funders - publisher = '\n\nWydawca: ' + ', '.join(parsed_dc.publisher) - isbn = getattr(parsed_dc, 'isbn_txt', None) - if isbn: - isbn = '\n\n' + isbn - else: - isbn = '' - else: - description = ("Publikacja zrealizowana w ramach projektu " - "Wolne Lektury (http://wolnelektury.pl).") - url = '*' * 10 - license_description = "" - source = "" - contributors = "" - funders = "" - publisher = "" - isbn = "" - result = (TEMPLATE % { - 'description': description, - 'url': url, - 'license_description': license_description, - 'text': str(result), - 'source': source, - 'contributors': contributors, - 'funders': funders, - 'publisher': publisher, - 'isbn': isbn, - }).encode('utf-8') - else: - result = str(result).encode('utf-8') - return OutputFile.from_bytes(b"\r\n".join(result.splitlines()) + b"\r\n") diff --git a/src/librarian/xslt/book2txt.xslt b/src/librarian/xslt/book2txt.xslt deleted file mode 100644 index d43c7d4..0000000 --- a/src/librarian/xslt/book2txt.xslt +++ /dev/null @@ -1,391 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/ / - - - - - * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -„” - - - -** - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -* - - - - - - - ------------------------------------------------- - - - - - - - - - - - - - - - - - - - - tłum. - - - , - - - - - - - - - - - - -ISBN - - - - - - - - - - - - - - - - - - - - - - - - -- 2.20.1