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):
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)
90 _register_function(wrap_words)
93 def reg_person_name():
94 def person_name(context, text):
95 """ Converts "Name, Forename" to "Forename Name" """
96 if isinstance(text, list):
98 return Person.from_text(text).readable()
99 _register_function(person_name)
102 def reg_texcommand():
103 def texcommand(context, text):
104 """Remove non-letters"""
105 if isinstance(text, list):
107 return re.sub(r'[^a-zA-Z]', '', text).strip()
108 _register_function(texcommand)
111 def reg_lang_code_3to2():
112 def lang_code_3to2(context, text):
113 """Convert 3-letter language code to 2-letter code"""
116 with open(get_resource('res/ISO-639-2_8859-1.txt'), 'rb') as f:
117 for line in f.read().decode('latin1').split('\n'):
118 list = line.strip().split('|')
125 _register_function(lang_code_3to2)
128 def mathml_latex(context, trees):
129 from librarian.embeds.mathml import MathML
130 text = MathML(trees[0]).to_latex().data
131 # Remove invisible multiplications, they produce unwanted spaces.
132 text = text.replace(u'\u2062', '')
136 def reg_mathml_latex():
137 _register_function(mathml_latex)
140 def reg_mathml_epub(zipf):
141 from librarian.embeds.mathml import MathML
143 def mathml(context, trees):
144 data = MathML(trees[0]).to_latex().to_png().data
145 name = "math%d.png" % mathml.count
147 zipf.writestr('OPS/' + name, data)
150 _register_function(mathml)