shortcode/shortcodes.rl

82 lines
1.5 KiB
Plaintext
Raw Normal View History

2023-07-10 19:39:01 +00:00
#include <stdio.h>
#include "bstrlib/bstrlib.h"
2023-07-10 21:11:54 +00:00
bstring grab_chunk(char *start, char *end) {
blk2bstr(start, end-start);
}
2023-07-10 19:39:01 +00:00
%%{
machine shortcode;
2023-07-10 21:11:54 +00:00
action mark {
mark = p;
}
spc = space*;
sep = space+;
2023-07-10 21:17:17 +00:00
name = alpha+
2023-07-10 21:11:54 +00:00
> mark
% {bcatcstr(output, "N");
bcatblk(output, mark, p-mark);
bcatcstr(output, "\n");
};
2023-07-10 21:17:17 +00:00
argname = alpha+
> mark
% {bcatcstr(output, "A");
bcatblk(output, mark, p-mark);
bcatcstr(output, "\n");
};
qvalue = ('"' [^"]* '"')
2023-07-10 21:11:54 +00:00
> mark
% {bcatcstr(output, "V");
bcatblk(output, mark+1, p-mark-2);
bcatcstr(output, "\n");
};
value = alnum+
> mark
% {bcatcstr(output, "V");
bcatblk(output, mark, p-mark);
bcatcstr(output, "\n");
};
arg = ((argname '=')? (value|qvalue));
2023-07-10 19:39:01 +00:00
start_p = '{{%';
end_p = '%}}';
2023-07-10 19:39:01 +00:00
start_b = '{{<';
end_b = '>}}';
start = start_p | start_b ;
end = end_p | end_b ;
shortcode = (start spc name (sep arg)* spc end)
> {bcatcstr(output, "---\n");}
% {bcatcstr(output, "\n");};
main := (any* shortcode)*;
2023-07-10 19:39:01 +00:00
}%%
bstring parse(char *input) {
2023-07-10 19:39:01 +00:00
%%write data;
char *eof, *ts, *te = 0;
int cs, act = 0;
char *p = input;
char *pe = p + strlen(input);
2023-07-10 19:39:01 +00:00
2023-07-10 21:11:54 +00:00
char *mark = 0;
bstring output = bfromcstr("");
2023-07-10 19:39:01 +00:00
%% write init;
%% write exec;
return output;
}
int main(int argc, char **argv) {
bstring output = parse("{{< thename \"onearg\">}} {{% another argname=\"val3\" %}}");
2023-07-10 19:39:01 +00:00
printf("\n%s\n", output->data);
return 0;
}