Hello all,
I implemented something in a Leo outline yesterday, but I feel my code
is absolutely atrocious.
One of the ways I use Leo is as an information management/database for
when I'm running tabletop RPGs. Such games involve a fair amount of
similarly structure data. I set out to make a way of defining a
template, and providing macro expansions within the template, so that
the user could click a script button, be prompted for data, and be
rewarded with a fully-populated copy of the template. I've managed to
accomplish this with the code below:
https://gist.github.com/gatesphere/2be5030506a364ee6ec1
Now, that seems like an atrocious amount of code, and a lot of it feels
like too much work... but it was a hack, and it works.
How it works is like this:
- Create a `@template` node. This is the parent node for your template.
- Create a `@destination` node as a child of the @template node. This
node's headline is the name of a top level node where your completed
template will be placed. `@destination Sessions` will place the
finished product under the top-level "Sessions" node.
- Populate the @template node with various `@item` nodes - these keep
their structure in the filled template. Anything that needs to be a
part of the final product has to be an @item node.
All of that is fine and dandy if you just want to copy and paste... but
that's already baked into Leo. So I added macro expansion. The script
gathers up all instances of anything that fits the regex "<\$\w+>" is a
macro variable name. Something like `<$name>` or `<$date>`. The script
gathers up all of these variable names, and prompts the user (ala
todo.py's "Redistribute Priorities" function) for values for each of
them. They are global with respect to the template, so using the same
macro variable in multiple places in the same template will result in
all of them being filled in with the same value.
In addition, there is one more type of node that can go under @template
nodes - @default. @default nodes provide a default value for a macro
variable. The node with a headline of `@default <$name>` and a body of
"This is my body" gives the `<$name>` macro a value of "This is my body"
everywhere within the template, and that macro will not be prompted for
a value. This is handy if you're copying templates between Leo
worksheets, and want them flexible, but don't want them to fill in the
same value for every single invocation of copy-template.
Here's an example template:
@template <$campaign> Session #<$num> ($date)
@destination Sessions
@default <$campaign>
@item Who's Coming?
@item In-World Start Date
@item Log
@item Threads
@item High Priority
@item Low Priority
@item News
@item Out of Character
@item In Character
The body of `@default <$campaign>` is "World of Ka'rim". The body of
`@item Who's Coming?` is "John, Jane, Jack, Jenny, Jeremy".
Running copy-template on this template, and filling the values prompted
for (<$num> and <$date>) gives the following under the top-level node
Sessions:
World of Ka'rim Session #1 (01 May 2013)
Who's Coming?
In-World Start Date
Log
Threads
High Priority
Low Priority
News
Out of Character
In Character
The headlines bodies are correctly filled in with macro expansions, and
other text is verbatim copied from the respective headlines and bodies.
So... two questions:
1.) Did I re-implement something Leo already does?
2.) Anyone have any suggestions on reducing the amount of code/any fixes?
Also, hope someone else finds this useful. Once it's cleaned up, I
might do a leo-editor blog post about it.
-->Jake
--
You received this message because you are subscribed to the Google Groups
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at http://groups.google.com/group/leo-editor?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.