# Stencil 1.0 by Dan Rahmel # Stencil button is a form of macro replace where template body text can be inserted into # one or more nodes that point to a text stencil. # # There is an example provided in the sample file. Examine the outline and then click # on the Stencil button on the toolbar to see it work. # # For example: a node with the HEADLINE of '@stencil tasklist' has the stencil name # of 'tasklist'. The body text of that node is stored in the dictionary. # When any subsequent nodes have BODY text beginning with '@stencil tasklist', # Stencil will replace that body text with the body text of the stencil from # the dictionary. # # The Stencil button first scans nodes (from the currently selected node) for headlines # that begin with @stencil and stores the body text for those nodes # in a dictionary under the headline names. # # After a dictionary is built, the tree is re-scanned, this time looking for nodes with # body text that begins with @template. If it finds such a node, it looks for a matching # name in the dictionary. It will replace the node's body text with the body text of the # stencil that was stored in the dictionary. If no match of stencil name with dictionary # name is found, the body text is left as-is.

def stencil():

stencilDict = {} g.es("Begin stencil scan___") for myP in p.subtree_iter():

myHead = myP.headString() if myHead[:8]? == "@stencil":

stencilTemp = myHead.split()[1]? g.es("Stencil found:" + stencilTemp) stencilDict[stencilTemp]? = myP.bodyString()
if len(stencilDict):
for myP in p.subtree_iter():

myBody = myP.bodyString() myHead = myP.headString() if myBody[:8]? == "@stencil":

stencilTemp = myBody.split()[1]? if stencilTemp in stencilDict:

g.es("Found a match with: " + stencilTemp) myP.setBodyStringOrPane(stencilDict[stencilTemp]?)
else:
g.es("No match with: " + stencilTemp)
else:
g.es("No stencil found.")

g.es("End stencil scan___")

stencil()

file: http://leo.zwiki.org/StencilButton/stencil.leo