Skip to main content

Ralsina.Me — Roberto Alsina's website

Posts about nicolino

New Project: Shortcodes

One thing leads to an­oth­er. I want­ed to learn a new lan­guage and chose Crys­tal. To prac­tice, I rewrote my stan­dard prac­tice pro­jec­t, Nico­let­ta, a stat­ic site gen­er­a­tor.

That brought to mind doit a task/­dataflow li­brary, which made me want to im­ple­ment some­thing sim­i­lar, so I wrote Croupi­er which went well, so I said "Why not write a more se­ri­ous SS­G, which is Nicol­i­no which I think would not be com­plete with­out a fea­ture from Niko­la which I stole from Hugo, called short­codes. So I had to write a short­code pars­er.

What are short­codes?

Sort of macros for markdown. You want to do a <figure> but markdown doesn't have it, so you do this:

{{% figure src="http://whatever" %}}

And you end up with a fig­ure tag in your doc­u­men­t.

That is done by pass­ing da­ta like that "s­r­c" ar­gu­ment and the con­text of the doc­u­ment you are writ­ing in­to a tem­plate, which ren­ders a piece of HTM­L, that's in­sert­ed where the short­code was and bob's your un­cle.

There are vari­ants, and de­tail­s, and sub­tleties, but that's the ba­sic idea.

The bad news was that I was not the au­thor of Niko­la's cur­rent short­code parser, that was some­one else, and I re­al­ly don't un­der­stant their code.

So I want­ed to write it from scratch.

I have been want­ing to learn ragel for a bit, let's say 10 years. It's a thing that takes a fi­nite state ma­chine de­scrip­tion in a DSL and turns it in­to a pars­er in the lan­guage you wan­t.

As long as you want one of the lan­guages it likes, which is not Crys­tal, so I used it to gen­er­ate it in C. And wrapped that in Crys­tal.

So, that's a very long way to say that I wrote a Short­code pars­er li­brary that can be used from Crys­tal and from C, and since it can be used from C, it can be used from pret­ty much any lan­guage.

It does­n't im­ple­ment the whole short­code "spec­i­fi­ca­tion" (which is not a re­al thing that ex­ist­s) but it im­ple­ments the ba­sic­s.

Now, I just need to use it in Nicol­i­no, and even­tu­al­ly make it bet­ter as need­ed.

BTW: Ragel rocks.


Contents © 2000-2023 Roberto Alsina