X-Git-Url: https://git.mdrn.pl/librarian.git/blobdiff_plain/432b6175940bcddf371c80c46e429f37ada6559f..6c528bcbd5a184387bbb30500b67caef8a21889d:/librarian/functions.py diff --git a/librarian/functions.py b/librarian/functions.py index e91d7e1..7eb9d56 100644 --- a/librarian/functions.py +++ b/librarian/functions.py @@ -8,21 +8,23 @@ import re 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) @@ -35,7 +37,7 @@ def reg_substitute_entities(): _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) @@ -46,7 +48,7 @@ def reg_strip(): _register_function(strip) -def starts_white(context, text): +def starts_white(text): if isinstance(text, list): text = ''.join(text) if not text: @@ -59,7 +61,7 @@ def reg_starts_white(): def reg_ends_white(): - def ends_white(context, text): + def ends_white(text): if isinstance(text, list): text = ''.join(text) if not text: @@ -68,7 +70,7 @@ def reg_ends_white(): _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) @@ -93,7 +95,7 @@ def reg_wrap_words(): _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) @@ -104,7 +106,7 @@ def reg_person_name(): _register_function(person_name) -def texcommand(context, text): +def texcommand(text): """Remove non-letters""" if isinstance(text, list): text = ''.join(text) @@ -115,3 +117,17 @@ def reg_texcommand(): _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)