To simplify I refer to HTML code though it is something different.
I use decorators to generate such code.
I must format the output by indenting it. I think I need to keep track of the nesting level to adapt the indentation but I don't know if and how I can keep track of the nesting level. As an example (https://www.thecodeship.com/patterns/guide-to-python-function-decorators/)
def p_decorate(func):
def func_wrapper(name):
return "<p>{0}</p>".format(func(name))
return func_wrapper
def strong_decorate(func):
def func_wrapper(name):
return "<strong>{0}</strong>".format(func(name))
return func_wrapper
def div_decorate(func):
def func_wrapper(name):
return "<div>{0}</div>".format(func(name))
return func_wrapper
get_text = div_decorate(p_decorate(strong_decorate(get_text)))
@div_decorate
@p_decorate
@strong_decorate
def get_text(name):
return "lorem ipsum, {0} dolor sit amet".format(name)
print(get_text("John"))
# Outputs <div><p><strong>lorem ipsum, John dolor sit amet</strong></p></div>
I would like to obtain, using a general algorithm,
<div>
<p>
<strong>lorem ipsum, John dolor sit amet</strong>
</p>
</div>
Is it possible? and how?
I have a code but it is quite long.
EDit: since people asked for the real example here you have the output I aim at getting
config vdom
edit <vdom>
config router static
edit <number>
set dst <ip> <netmask>
set gateway <ip>
set device <intf>
next
end
end
Thanks,