X-Git-Url: https://git.mdrn.pl/librarian.git/blobdiff_plain/f492e325efb42a3348b2479a0fd0ffc3c484657b..12b5230d8fdb3ad995e867fb5d58a69e8a627e68:/librarian/text.py?ds=sidebyside diff --git a/librarian/text.py b/librarian/text.py index c23bcd6..0eb7b59 100644 --- a/librarian/text.py +++ b/librarian/text.py @@ -3,7 +3,8 @@ # This file is part of Librarian, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # -from librarian import dcparser, parser, functions +import copy +from librarian import functions, OutputFile from lxml import etree import os @@ -25,10 +26,11 @@ Utwór opracowany został w ramach projektu Wolne Lektury przez fundację Nowocz %(license_description)s.%(source)s -%(description)s%(contributors)s +%(description)s%(contributors)s%(funders)s """ -def transform(input_file, output_file, parse_dublincore=True, flags=None, **options): + +def transform(wldoc, flags=None, **options): """ Transforms input_file in XML to output_file in TXT. possible flags: raw-text, @@ -37,27 +39,38 @@ def transform(input_file, output_file, parse_dublincore=True, flags=None, **opti style_filename = os.path.join(os.path.dirname(__file__), 'xslt/book2txt.xslt') style = etree.parse(style_filename) - document = parser.WLDocument.from_file(input_file, True, parse_dublincore=parse_dublincore) + 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 parse_dublincore: - parsed_dc = dcparser.BookInfo.from_element(document.edoc) + if document.book_info: + parsed_dc = document.book_info description = parsed_dc.description - url = parsed_dc.url + url = document.book_info.url license_description = parsed_dc.license_description license = parsed_dc.license if license: - license_description = u"Ten utwór jest udostepniony na licencji %s: \n%s" % (license_description, license) + license_description = u"Ten utwór jest udostepniony na licencji %s: \n%s" % ( + license_description, license) else: - license_description = u"Ten utwór nie jest chroniony prawem autorskim i znajduje się w domenie publicznej, co oznacza że możesz go swobodnie wykorzystywać, publikować i rozpowszechniać. Jeśli utwór opatrzony jest dodatkowymi materiałami (przypisy, motywy literackie etc.), które podlegają prawu autorskiemu, to te dodatkowe materiały udostępnione są na licencji Creative Commons Uznanie Autorstwa – Na Tych Samych Warunkach 3.0 PL (http://creativecommons.org/licenses/by-sa/3.0/)" - + license_description = u"Ten utwór nie jest objęty majątkowym prawem autorskim i znajduje się " \ + u"w domenie publicznej, co oznacza że możesz go swobodnie wykorzystywać, " \ + u"publikować i rozpowszechniać. Jeśli utwór opatrzony jest dodatkowymi " \ + u"materiałami (przypisy, motywy literackie etc.), które podlegają prawu " \ + u"autorskiemu, to te dodatkowe materiały udostępnione są na licencji " \ + u"Creative Commons Uznanie Autorstwa – Na Tych Samych Warunkach 3.0 PL " \ + u"(http://creativecommons.org/licenses/by-sa/3.0/)" + source = parsed_dc.source_name if source: source = "\n\nTekst opracowany na podstawie: " + source @@ -67,22 +80,26 @@ def transform(input_file, output_file, parse_dublincore=True, flags=None, **opti 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 + contributors = "\n\nOpracowanie redakcyjne i przypisy: %s." % contributors + funders = ', '.join(parsed_dc.funders) + if funders: + funders = u"\n\nPublikację ufundowali i ufundowały: %s." % funders else: description = 'Publikacja zrealizowana w ramach projektu Wolne Lektury (http://wolnelektury.pl).' url = '*' * 10 - license = "" license_description = "" source = "" contributors = "" - output_file.write((TEMPLATE % { + funders = "" + result = (TEMPLATE % { 'description': description, 'url': url, 'license_description': license_description, 'text': unicode(result), 'source': source, 'contributors': contributors, - }).encode('utf-8')) + 'funders': funders, + }).encode('utf-8') else: - output_file.write(unicode(result).encode('utf-8')) - + result = unicode(result).encode('utf-8') + return OutputFile.from_string("\r\n".join(result.splitlines()) + "\r\n")