1 # -*- coding: utf-8 -*-
3 # This file is part of Librarian.
5 # Copyright © 2008,2009,2010 Fundacja Nowoczesna Polska <fundacja@nowoczesnapolska.org.pl>
7 # For full list of contributors see AUTHORS file.
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU Affero General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU Affero General Public License for more details.
19 # You should have received a copy of the GNU Affero General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 from librarian import dcparser, parser
23 from lxml import etree
30 ENTITY_SUBSTITUTIONS = [
40 Kodowanie znaków w dokumencie: UTF-8.
42 Publikacja zrealizowana w ramach projektu Wolne Lektury (http://wolnelektury.pl/). Reprodukcja cyfrowa wykonana przez
43 Bibliotekę Narodową z egzemplarza pochodzącego ze zbiorów BN. Ten utwór nie jest chroniony prawem autorskim i znajduje
44 się w domenie publicznej, co oznacza, że możesz go swobodnie wykorzystywać, publikować i rozpowszechniać.
46 Wersja lektury w opracowaniu merytorycznym i krytycznym (przypisy i motywy) dostępna jest na stronie %(url)s.
55 def strip(context, text):
56 """Remove unneeded whitespace from beginning and end"""
57 if isinstance(text, list):
59 return re.sub(r'\s+', ' ', text).strip()
62 def substitute_entities(context, text):
63 """XPath extension function converting all entites in passed text."""
64 if isinstance(text, list):
66 for entity, substitutution in ENTITY_SUBSTITUTIONS:
67 text = text.replace(entity, substitutution)
71 def wrap_words(context, text, wrapping):
72 """XPath extension function automatically wrapping words in passed text"""
73 if isinstance(text, list):
78 words = re.split(r'\s', text)
83 line_length += len(word) + 1
84 if line_length > wrapping:
85 # Max line length was exceeded. We create new line
87 line_length = len(word)
88 lines[-1].append(word)
89 return '\n'.join(' '.join(line) for line in lines)
92 # Register substitute_entities function with lxml
93 ns = etree.FunctionNamespace('http://wolnelektury.pl/functions')
95 ns['substitute_entities'] = substitute_entities
96 ns['wrap_words'] = wrap_words
99 def transform(input_filename, output_filename, is_file=True, parse_dublincore=True, **options):
100 """Transforms file input_filename in XML to output_filename in TXT."""
102 style_filename = os.path.join(os.path.dirname(__file__), 'xslt/book2txt.xslt')
103 style = etree.parse(style_filename)
106 document = parser.WLDocument.from_file(input_filename, True, parse_dublincore=parse_dublincore)
108 document = parser.WLDocument.from_string(input_filename, True, parse_dublincore=parse_dublincore)
110 result = document.transform(style, **options)
112 output_file = codecs.open(output_filename, 'wb', encoding='utf-8')
115 url = dcparser.parse(input_filename).url
118 output_file.write(TEMPLATE % {
120 'text': unicode(result),