1 # -*- coding: utf-8 -*-
3 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 from __future__ import unicode_literals
11 from librarian.dcparser import Person
12 from librarian import get_resource
15 def _register_function(f):
16 """ Register extension function with lxml """
17 ns = etree.FunctionNamespace('http://wolnelektury.pl/functions')
21 def reg_substitute_entities():
22 entity_substitutions = [
30 def substitute_entities(context, text):
31 """XPath extension function converting all entites in passed text."""
32 if isinstance(text, list):
34 for entity, substitutution in entity_substitutions:
35 text = text.replace(entity, substitutution)
38 _register_function(substitute_entities)
42 def strip(context, text):
43 """Remove unneeded whitespace from beginning and end"""
44 if isinstance(text, list):
46 return re.sub(r'\s+', ' ', text).strip()
47 _register_function(strip)
50 def reg_starts_white():
51 def starts_white(context, text):
52 if isinstance(text, list):
56 return text[0].isspace()
57 _register_function(starts_white)
61 def ends_white(context, text):
62 if isinstance(text, list):
66 return text[-1].isspace()
67 _register_function(ends_white)
71 def wrap_words(context, text, wrapping):
73 XPath extension function automatically wrapping words
76 if isinstance(text, list):
81 words = re.split(r'\s', text)
86 line_length += len(word) + 1
87 if line_length > wrapping:
88 # Max line length was exceeded. We create new line
90 line_length = len(word)
91 lines[-1].append(word)
92 return '\n'.join(' '.join(line) for line in lines)
93 _register_function(wrap_words)
96 def reg_person_name():
97 def person_name(context, text):
98 """ Converts "Name, Forename" to "Forename Name" """
99 if isinstance(text, list):
101 return Person.from_text(text).readable()
102 _register_function(person_name)
105 def reg_texcommand():
106 def texcommand(context, text):
107 """Remove non-letters"""
108 if isinstance(text, list):
110 return re.sub(r'[^a-zA-Z]', '', text).strip()
111 _register_function(texcommand)
114 def reg_lang_code_3to2():
115 def lang_code_3to2(context, text):
116 """Convert 3-letter language code to 2-letter code"""
119 with open(get_resource('res/ISO-639-2_8859-1.txt'), 'rb') as f:
120 for line in f.read().decode('latin1').split('\n'):
121 list = line.strip().split('|')
128 _register_function(lang_code_3to2)
131 def mathml_latex(context, trees):
132 from librarian.embeds.mathml import MathML
133 text = MathML(trees[0]).to_latex().data
134 # Remove invisible multiplications, they produce unwanted spaces.
135 text = text.replace(u'\u2062', '')
139 def reg_mathml_latex():
140 _register_function(mathml_latex)
143 def reg_mathml_epub(zipf):
144 from librarian.embeds.mathml import MathML
146 def mathml(context, trees):
147 data = MathML(trees[0]).to_latex().to_png().data
148 name = "math%d.png" % mathml.count
150 zipf.writestr('OPS/' + name, data)
153 _register_function(mathml)