This commit is contained in:
Roberto Alsina 2023-07-14 12:08:11 -03:00
parent df2c0c147a
commit 817eea895a
4 changed files with 48 additions and 12 deletions

View File

@ -1,5 +1,7 @@
# 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.
@ -12,11 +14,47 @@ as close as practical.
* Allocates no memory, because all strings are references to
pieces of input.
What works:
## What works
* Detect shortcodes with names
* Standalone and matched shortcodes
* Capture data between tags in matched shortcodes
* Capture arguments with and without names
* Capture values with and without quotes (with details, see TODO above)
* Capture values with and without quotes (with details, see [TODO](TODO.md))
## Building
You need [Ragel](http://www.colm.net/open-source/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:
```shell
cd src && make
```
To run tests:
```shell
crystal spec
```
## Installation
1. Add the dependency to your `shard.yml`:
```yaml
dependencies:
cr-discount:
github: ralsina/shortcodes
```
2. Run `shards install`
## Usage
```crystal
require "shortcodes"
```

View File

@ -4,9 +4,9 @@ describe "Shortcodes" do
# TODO: Write tests
it "works" do
result = Shortcodes.parse("foo{{% bar %}}baz{{% /bar %}}qux");
p! result.sccount;
p! result.shortcodes[0].matching;
result = Shortcodes.parse("foo{{% bar %}}baz{{% /bar %}}qux")
result.sccount.should eq 1
result.shortcodes[0].matching.should eq 1
end
end

View File

@ -1,4 +1,3 @@
CC=gcc
shortcodes.c: shortcodes.rl
ragel -G2 shortcodes.rl -o shortcodes.c
clean:

View File

@ -1,6 +1,5 @@
@[Link(ldflags: "#{__DIR__}/shortcodes.o")]
lib Shortcodes
struct Chunk
start : UInt32
len : UInt32
@ -28,5 +27,5 @@ lib Shortcodes
errcount : UInt32
end
fun parse(input : Pointer(LibC::Char)) : ScResult;
fun parse(input : Pointer(LibC::Char)) : ScResult
end