This post originated from an RSS feed registered with Ruby Buzz
by Christian Neukirchen.
Original Post: Thoughts on Vooly
Feed Title: chris blogs: Ruby stuff
Feed URL: http://chneukirchen.org/blog/category/ruby.atom
Feed Description: a weblog by christian neukirchen - Ruby stuff
Each of these get their own Vooly tag, for example links are structured like
this:
<<link << URL >>
<< INTRO >> DESCRIPTION >>
Or, as a real example:
<<link << http://www.picotux.com/ >>
<< picotux >>, the world's smallest Linux computer.>>
(IRC-)Quotes and Lyrics behave a bit differently, as there is special
non-Vooly syntax used. I do that because it’s easier to write and
feels more natural too:
<<quote God not only plays dice in physics but also in pure
mathematics. Mathematical truth is sometimes nothing more than a
perfect coin toss.
--- Gregory Chaitin, A Random Walk in Arithmetic>>
Everything after the --- will get converted to the source.
As you (hopefully) can see, writing repetitive HTML with “macros” like
this is much easier than doing it manually (or with XML/XSLT, even!).
Now, let’s see how this actually is implemented: Since Anarchaia is
powered by Nukumi2, it’s just a matter
of another plugin for it, called AugmentedHTML. This plugin is trivial:
class AugmentedHTML
def initialize(string)
@string = string
end
def to_html
v = Vooly::Slurp.new(@string, false)
data = v.slurp.with_mapping! Decorator::MAPPING
data.to_s
end
end
AugmentedHTML uses Vooly::Slurp, a tree-like interface (I prefer
this term over DOM) to Vooly documents. It provides a nice method
with_mapping! that calls constructors given in the mapping.
For example, for the <<link>>, this looks like:
class Link < Decorator
TEMPLATE = Kashmir.new(<<EOF)
<p class="link">
<a href="^href">^title</a>^description
</p>
EOF
def initialize(href, title, description='')
@href = href
@title = title
@description = description
end
def to_s
TEMPLATE.expand { |e|
e.href @href
e.title @title
e.description @description
}
end
end
You can see, I’m exercising all the cool technologies I built in the
last months. :-) The important part is the initializer, look how the
elements of the document get mapped onto parameters.
Then, we simply call to_srecursively on the Slurp, and voilà ,
there is our HTML!
Now, this works pretty good, but there are some issues with Vooly with
respect to using it that way. One, perhaps minor thing, are syntactic
constructs like
<<foo <em>cool</em>>>
This will not work as expected, since the parser will (and has to!)
match on the first >> to close the tag. Therefore, I decided
to make Vooly drop the very last space of tag content, so you simply
write
<<foo <em>cool</em> >>
And it’s not ambiguous anymore.
The more difficult thing is that I can’t use (my beloved) BlueCloth
anymore, since BlueCloth (and Markdown in general?) will interpret
<foo as unclosed HTML,
and ignore that block till it finds the matching <.
This is very unfortunate, but I can’t figure a way how to avoid it.
Also, AugmentedHTML must run before BlueCloth, since BlueCloth
will also try to escape the “superfluous” angle brackets! I wonder if
I should maybe change Vooly brackets to {{ and }}, but these are
icky to type (at least on German keyboards), and don’t look and feel
as near as cool as << and >> do…