On Fri, May 25, 2012 at 1:36 PM, Kent Tenney <
kte...@gmail.com> wrote:
> I think I suggested just this ... AWKWARD ...
Don't panic. A templating script (or plugin) can do just about
anything as long as it doesn't monkey-patch leoFileCommands.py or
leoAtFile.py. But that's never going to be necessary.
Indeed, supporting templates is almost too easy, as I have just
verified. The problem isn't supporting templates, the problem is
choosing between all the various ways that could work.
Ville's jinjarender plugin is one way: it writes to files and uses the
valuespace plugin to get the Python dict that is passed to
Template.render.
But there are so many other ways. I just wrote a script that gets
key/value pairs from the children of an @jinja-data node and writes
the output to an @jinja-output node.
The way you suggest, namely passing an explicit dict of key/value
pairs is also perfectly feasible.
> Meaning the @x path machinery is maxed out?
Not at all. Let the wild rumpus start.
Edward
P.S. Here is a first draft of @button jinja-render. Simply
copy/paste into an outline. The code is dead easy, so it will be easy
to change it to make it do exactly what you want.
=====
<?xml version="1.0" encoding="utf-8"?>
<!-- Created by Leo (
http://webpages.charter.net/edreamleo/front.html) -->
<?xml-stylesheet ekr_test?>
<leo_file xmlns:leo="
http://www.leo-editor.org/2011/leo" >
<leo_header file_format="2"/>
<vnodes>
<v t="ekr.20120525134212.10696"><vh>@button jinja-render</vh>
<v t="ekr.20120525134212.10699"><vh><< imports >></vh></v>
<v t="ekr.20120525134212.10709"><vh>ctor</vh></v>
<v t="ekr.20120525134212.10714"><vh>error</vh></v>
<v t="ekr.20120525134212.10713"><vh>get_data</vh></v>
<v t="ekr.20120525134212.10720"><vh>put</vh></v>
<v t="ekr.20120525134212.10710"><vh>run</vh></v>
</v>
</vnodes>
<tnodes>
<t tx="ekr.20120525134212.10696">''' Render @jinja nodes. Requires
jinja2 module.
- Writes selected tree to the @jinja-output node in the selected tree.
- Takes arguments from the @jinja-data node.
'''
<< imports >>
class JinjaController:
@others
if Template:
JinjaController(c,p).run()
</t>
<t tx="ekr.20120525134212.10699">import leo.core.leoGlobals as g
try:
from jinja2 import Template
except ImportError:
Template = None
</t>
<t tx="ekr.20120525134212.10709">def __init__ (self,c,p):
self.c = c
self.p = p.copy()</t>
<t tx="ekr.20120525134212.10710">def run(self):
c,p = self.c,self.p
s = g.getScript(c,p,useSelectedText=False,useSentinels=False)
tmpl = Template(s)
d = self.get_data()
s = tmpl.render(d)
self.put(s)
</t>
<t tx="ekr.20120525134212.10713">def get_data (self):
c = self.c
p = g.findNodeAnywhere(c,'@jinja-data')
d = {}
if p:
for p in p.children():
key = p.h.strip()
val = p.b.strip() or None
d [key] = val
else:
self.error('no @jinja-data node')
return d
</t>
<t tx="ekr.20120525134212.10714">def error (self,s):
g.es_print(s,color='red')</t>
<t tx="ekr.20120525134212.10720">def put (self,s):
c,p = self.c,self.p
p2 = g.findNodeAnywhere(c,'@jinja-out')
if not p2:
p2 = p.insertAfter()
p2.h = '@jinja-out'
c.redraw()
p2.b = '@language rest\n\n' + s
c.selectPosition(p2)
</t>
</tnodes>
</leo_file>
==============
EKR