Shortcodes
What it is
This is a parser for the shortcode spec as explained in the Hugo docs and used in Hugo and Nikola. Approximately.
I am implementing this for Nicolino.
It probably won't be 100% identical, but I'll try to make it as close as practical.
- Implemented in Ragel + C for performance
- Allocates no memory, because all strings are references to pieces of input.
- Extensive testing, stable API
What works
- Detect shortcodes with names
- Standalone and matched shortcodes
- Capture data between tags in matched shortcodes
- Self-closing shortcodes
- Inline shortcodes
- Markdown and Verbatim shortcodes
- Capture arguments with and without names
- Capture values with and without single/double quotes (no support for quote escaping)
- UTF-8 strings AFAICT (arg and shortcode names only support ascii tho)
Nested shortcodes should be handled in a higher layer by recursive calls to the parser where it makes sense.
Building
You need Ragel and a C compiler.
Ragel is used to generate shortcodes.c
out of shortcodes.rl
.
As a convenience there is a generated shortcodes.c
in the repo,
Then:
make
To run tests:
crystal spec
Installation
-
Add the dependency to your
shard.yml
:dependencies: shortcodes: github: ralsina/shortcodes
-
Run
shards install
Usage in Crystal
require "shortcodes"
Usage in C
Just copy shortcodes.c
and shortcodes.h
into your project and have fun.