minor fixes
[librarian.git] / librarian / functions.py
1 # -*- coding: utf-8 -*-
2 #
3 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 #
6 from lxml import etree
7 import re
8
9 def _register_function(f):
10     """ Register extension function with lxml """
11     ns = etree.FunctionNamespace('http://wolnelektury.pl/functions')
12     ns[f.__name__] = f
13
14
15 def reg_substitute_entities(): 
16     ENTITY_SUBSTITUTIONS = [
17         (u'---', u'—'),
18         (u'--', u'–'),
19         (u'...', u'…'),
20         (u',,', u'„'),
21         (u'"', u'”'),
22     ]
23
24     def substitute_entities(context, text):
25         """XPath extension function converting all entites in passed text."""
26         if isinstance(text, list):
27             text = ''.join(text)
28         for entity, substitutution in ENTITY_SUBSTITUTIONS:
29             text = text.replace(entity, substitutution)
30         return text
31
32     _register_function(substitute_entities)
33
34
35 def reg_strip():
36     def strip(context, text):
37         """Remove unneeded whitespace from beginning and end"""
38         if isinstance(text, list):
39             text = ''.join(text)
40         return re.sub(r'\s+', ' ', text).strip()
41     _register_function(strip)
42
43
44 def reg_starts_white():
45     def starts_white(context, text):
46         if isinstance(text, list):
47             text = ''.join(text)
48         if not text:
49             return False
50         return text[0].isspace()
51     _register_function(starts_white)
52
53
54 def reg_ends_white():
55     def ends_white(context, text):
56         if isinstance(text, list):
57             text = ''.join(text)
58         if not text:
59             return False
60         return text[-1].isspace()
61     _register_function(ends_white)
62
63
64 def reg_wrap_words():
65     def wrap_words(context, text, wrapping):
66         """XPath extension function automatically wrapping words in passed text"""
67         if isinstance(text, list):
68             text = ''.join(text)
69         if not wrapping:
70             return text
71     
72         words = re.split(r'\s', text)
73     
74         line_length = 0
75         lines = [[]]
76         for word in words:
77             line_length += len(word) + 1
78             if line_length > wrapping:
79                 # Max line length was exceeded. We create new line
80                 lines.append([])
81                 line_length = len(word)
82             lines[-1].append(word)
83         return '\n'.join(' '.join(line) for line in lines)
84     _register_function(wrap_words)
85
86
87 def reg_person_name():
88     def person_name(context, text):
89         """ Converts "Name, Forename" to "Forename Name" """
90         if isinstance(text, list):
91             text = ''.join(text)
92         return ' '.join([t.strip() for t in text.split(',', 1)[::-1]])
93     _register_function(person_name)
94
95