Coverage for lino/utils/memo.py : 87%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# -*- coding: UTF-8 -*- # Copyright 2006-2016 Luc Saffre # License: BSD (see file COPYING for details)
string to produce a resulting output string. Commands are in the form ``[KEYWORD ARGS]``. The caller defines itself all commands, there are no predefined commands.
.. This document is part of the Lino test suite. You can test only this document with::
$ python setup.py test -s tests.UtilsTests.test_memo
Usage example -------------
Instantiate a parser:
>>> from lino.utils.memo import Parser >>> p = Parser()
We declare a "command handler" function `url2html` and register it:
>>> def url2html(parser, s): ... print("[DEBUG] url2html() got %r" % s) ... if not s: return "XXX" ... url, text = s.split(None,1) ... return '<a href="%s">%s</a>' % (url,text) >>> p.register_command('url', url2html)
The intended usage of our example handler is ``[url URL TEXT]``, where URL is the URL to link to, and TEXT is the label of the link:
>>> print(p.parse('This is a [url http://xyz.com test].')) [DEBUG] url2html() got 'http://xyz.com test' This is a <a href="http://xyz.com">test</a>.
A command handler will be called with one parameter: the portion of text between the KEYWORD and the closing square bracket. Not including the whitespace after the keyword. It must return the text which is to replace the ``[KEYWORD ARGS]`` fragment. It is responsible for parsing the text that it receives as parameter.
If an exception occurs during the command handler, the final exception message is inserted into the result. The whole traceback is being logged to the lino logger.
To demonstrate this, our example implementation has a bug, it doesn't support the case of having only an URL without TEXT:
>>> print(p.parse('This is a [url http://xyz.com].')) [DEBUG] url2html() got 'http://xyz.com' This is a [ERROR need more than 1 value to unpack in '[url http://xyz.com]' at position 10-30].
Newlines preceded by a backslash will be removed before the command handler is called:
>>> print(p.parse('''This is [url http://xy\ ... z.com another test].''')) [DEBUG] url2html() got 'http://xyz.com another test' This is <a href="http://xyz.com">another test</a>.
The whitespace between the KEYWORD and ARGS can be any whitespace, including newlines:
>>> print(p.parse('''This is a [url ... http://xyz.com test].''')) [DEBUG] url2html() got 'http://xyz.com test' This is a <a href="http://xyz.com">test</a>.
The ARGS part is optional (it's up to the command handler to react accordingly, our handler function returns XXX in that case):
>>> print(p.parse('''This is a [url] test.''')) [DEBUG] url2html() got '' This is a XXX test.
The ARGS part may contain pairs of square brackets:
>>> print(p.parse('''This is a [url ... http://xyz.com test with [more] brackets].''')) [DEBUG] url2html() got 'http://xyz.com test with [more] brackets' This is a <a href="http://xyz.com">test with [more] brackets</a>.
Fragments of text between brackets that do not match any registered command will be left unchanged:
>>> print(p.parse('''This is a [1] test.''')) This is a [1] test.
>>> print(p.parse('''This is a [foo bar] test.''')) This is a [foo bar] test.
>>> print(p.parse('''Text with only [opening square bracket.''')) Text with only [opening square bracket.
Limits ------
A single closing square bracket as part of ARGS will not produce the desired result:
>>> print(p.parse('''This is a [url ... http://xyz.com The character "\]"].''')) [DEBUG] url2html() got 'http://xyz.com The character "\\' This is a <a href="http://xyz.com">The character "\</a>"].
Execution flow statements like `[if ...]` and `[endif ...]` or ``[for ...]`` and ``[endfor ...]`` would be nice.
The ``[=expression]`` form --------------------------
Instantiate a new parser with and without a context:
>>> print(p.parse('''\ ... The answer is [=a*a*5-a].''', a=3)) The answer is 42.
>>> print(p.parse('''<ul>[="".join(['<li>%s</li>' % (i+1) for i in range(5)])]</ul>''')) <ul><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul>
"""
# ===...... .......=
except Exception as e: logger.exception(e) return self.handle_error(matchobj, e)
return str(etree.tostring(v))
#~ return mo.group(0) e, mo.group(0), mo.start(), mo.end())
#~ self.context = context
import doctest doctest.testmod()
_test() |