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.
9 from librarian.dcparser import Person
10 from librarian import get_resource
13 def _register_function(f):
14 """ Register extension function with lxml """
15 ns = etree.FunctionNamespace('http://wolnelektury.pl/functions')
19 def reg_substitute_entities():
20 entity_substitutions = [
28 def substitute_entities(context, text):
29 """XPath extension function converting all entites in passed text."""
30 if isinstance(text, list):
32 for entity, substitutution in entity_substitutions:
33 text = text.replace(entity, substitutution)
36 _register_function(substitute_entities)
40 def strip(context, text):
41 """Remove unneeded whitespace from beginning and end"""
42 if isinstance(text, list):
44 return re.sub(r'\s+', ' ', text).strip()
45 _register_function(strip)
48 def reg_starts_white():
49 def starts_white(context, text):
50 if isinstance(text, list):
54 return text[0].isspace()
55 _register_function(starts_white)
59 def ends_white(context, text):
60 if isinstance(text, list):
64 return text[-1].isspace()
65 _register_function(ends_white)
69 def wrap_words(context, text, wrapping):
70 """XPath extension function automatically wrapping words in passed text"""
71 if isinstance(text, list):
76 words = re.split(r'\s', text)
81 line_length += len(word) + 1
82 if line_length > wrapping:
83 # Max line length was exceeded. We create new line
85 line_length = len(word)
86 lines[-1].append(word)
87 return '\n'.join(' '.join(line) for line in lines)
88 _register_function(wrap_words)
91 def reg_person_name():
92 def person_name(context, text):
93 """ Converts "Name, Forename" to "Forename Name" """
94 if isinstance(text, list):
96 return Person.from_text(text).readable()
97 _register_function(person_name)
100 def reg_texcommand():
101 def texcommand(context, text):
102 """Remove non-letters"""
103 if isinstance(text, list):
105 return re.sub(r'[^a-zA-Z]', '', text).strip()
106 _register_function(texcommand)
109 def reg_lang_code_3to2():
110 def lang_code_3to2(context, text):
111 """Convert 3-letter language code to 2-letter code"""
114 with open(get_resource('res/ISO-639-2_8859-1.txt'), 'rb') as f:
116 list = line.strip().split('|')
123 _register_function(lang_code_3to2)
126 def mathml_latex(context, trees):
127 from librarian.embeds.mathml import MathML
128 text = MathML(trees[0]).to_latex().data
129 # Remove invisible multiplications, they produce unwanted spaces.
130 text = text.replace(u'\u2062', '')
134 def reg_mathml_latex():
135 _register_function(mathml_latex)
138 def reg_mathml_epub(zipf):
139 from librarian.embeds.mathml import MathML
141 def mathml(context, trees):
142 data = MathML(trees[0]).to_latex().to_png().data
143 name = "math%d.png" % mathml.count
145 zipf.writestr('OPS/' + name, data)
148 _register_function(mathml)