from librarian.dcparser import Person
+
def _register_function(f):
""" Register extension function with lxml """
ns = etree.FunctionNamespace('http://wolnelektury.pl/functions')
- ns[f.__name__] = f
+ ns[f.__name__] = lambda context, *args: f(*args)
ENTITY_SUBSTITUTIONS = [
- (u'---', u'—'),
- (u'--', u'–'),
- (u'...', u'…'),
- (u',,', u'„'),
- (u'"', u'”'),
+ (u'---', u'—'),
+ (u'--', u'–'),
+ (u'...', u'…'),
+ (u',,', u'„'),
+ (u'"', u'”'),
]
-def substitute_entities(context, text):
+
+def substitute_entities(text):
"""XPath extension function converting all entites in passed text."""
if isinstance(text, list):
text = ''.join(text)
_register_function(substitute_entities)
-def strip(context, text):
+def strip(text):
"""Remove unneeded whitespace from beginning and end"""
if isinstance(text, list):
text = ''.join(text)
_register_function(strip)
-def starts_white(context, text):
+def starts_white(text):
if isinstance(text, list):
text = ''.join(text)
if not text:
def reg_ends_white():
- def ends_white(context, text):
+ def ends_white(text):
if isinstance(text, list):
text = ''.join(text)
if not text:
_register_function(ends_white)
-def wrap_words(context, text, wrapping):
+def wrap_words(text, wrapping):
"""XPath extension function automatically wrapping words in passed text"""
if isinstance(text, list):
text = ''.join(text)
_register_function(wrap_words)
-def person_name(context, text):
+def person_name(text):
""" Converts "Name, Forename" to "Forename Name" """
if isinstance(text, list):
text = ''.join(text)
_register_function(person_name)
-def texcommand(context, text):
+def texcommand(text):
"""Remove non-letters"""
if isinstance(text, list):
text = ''.join(text)
_register_function(texcommand)
+def reg_get(format_):
+ def get(*args):
+ obj = format_
+ for arg in args:
+ if hasattr(obj, arg):
+ obj = getattr(obj, arg)
+ else:
+ try:
+ obj = obj[arg]
+ except (TypeError, KeyError), e:
+ # Just raise proper AttributeError.
+ getattr(obj, arg)
+ return obj
+ _register_function(get)