Base renderer#
BaseRenderer is the renderer every other renderer inherits from. Used directly, it
returns the render tree: a nested Python list, before any joining into text.
from handcalcs.renderers import BaseRenderer
## Check
a = 2
b = a * 3 # note
if b > 5:
c = b - 5
['# Check',
['', [['a', '=', '2'], ['b', '=', 'a * 3', '=', '2 * 3', '=', '6', '(note)']]],
['Since (b>5) -> (6>5) is True:',
[['', [['c', '=', 'b - 5', '=', '6 - 5', '=', '1']]]]]]
Pass it explicitly, HandCalcs(BaseRenderer()), to get the render tree; a bare
HandCalcs() uses the ASCII renderer unless you set default_renderer.
When you would use it#
To see the structure a renderer works with before you write one. Each string is a finished line; each list of strings is a line’s components; a two-item
[header, body]list is a block. The render tree explains the shapes.To post-process the rendered pieces yourself instead of using a renderer’s
complete().
BaseRenderer also has a join() method that turns the tree into plain text, the first
step most text renderers take in their own complete():
from handcalcs import HandCalcs
from handcalcs.renderers import BaseRenderer
renderer = BaseRenderer()
hc = HandCalcs(renderer)
tree = hc("a = 2\nb = a * 3\n")
print(renderer.join(tree))