From 817eea895ad2b2dc2c77e814a8d719b69be60bf4 Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Fri, 14 Jul 2023 12:08:11 -0300 Subject: [PATCH] Cleanup --- README.md | 44 ++++++++++++++++++++++++++++++++++++++--- spec/shortcodes_spec.cr | 8 ++++---- src/Makefile | 1 - src/shortcodes.cr | 7 +++---- 4 files changed, 48 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 027a20e..1743ce1 100644 --- a/README.md +++ b/README.md @@ -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. @@ -9,14 +11,50 @@ 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 +* 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" +``` diff --git a/spec/shortcodes_spec.cr b/spec/shortcodes_spec.cr index e2389be..4d9f778 100644 --- a/spec/shortcodes_spec.cr +++ b/spec/shortcodes_spec.cr @@ -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 @@ -237,4 +237,4 @@ end # // result = parse(input); # // // No shortcodes # // assert_that(result.sc[0].name.len, is_equal_to(0)); -# // } \ No newline at end of file +# // } diff --git a/src/Makefile b/src/Makefile index 99d68f2..a22d229 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,4 +1,3 @@ -CC=gcc shortcodes.c: shortcodes.rl ragel -G2 shortcodes.rl -o shortcodes.c clean: diff --git a/src/shortcodes.cr b/src/shortcodes.cr index 08a4d31..9f70028 100644 --- a/src/shortcodes.cr +++ b/src/shortcodes.cr @@ -1,13 +1,12 @@ @[Link(ldflags: "#{__DIR__}/shortcodes.o")] lib Shortcodes - struct Chunk start : UInt32 len : UInt32 end struct ScError - position: UInt32 + position : UInt32 code : UInt32 end @@ -28,5 +27,5 @@ lib Shortcodes errcount : UInt32 end - fun parse(input : Pointer(LibC::Char)) : ScResult; -end \ No newline at end of file + fun parse(input : Pointer(LibC::Char)) : ScResult +end