shortcode/shortcodes.rl

137 lines
3.3 KiB
Plaintext
Raw Normal View History

2023-07-11 12:58:58 +00:00
#include <string.h>
2023-07-12 19:22:54 +00:00
#include "shortcodes.h"
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 23:36:50 +00:00
path = (alnum | '/' )+;
2023-07-10 21:11:54 +00:00
2023-07-11 00:55:45 +00:00
name = (alpha+ path?)
2023-07-10 21:11:54 +00:00
> mark
2023-07-12 01:20:26 +00:00
%{ sc_list[c_sc].name.start = mark-start;
sc_list[c_sc].name.len = p-mark;
sc_list[c_sc].matching = 0;
};
2023-07-11 00:55:45 +00:00
argname = alpha+
2023-07-10 21:17:17 +00:00
> mark
2023-07-12 00:09:42 +00:00
% {
2023-07-12 01:20:26 +00:00
sc_list[c_sc].argnames[sc_list[c_sc].argcount].start = mark-start;
sc_list[c_sc].argnames[sc_list[c_sc].argcount].len = p-mark;
2023-07-12 00:09:42 +00:00
};
qvalue = ('"' [^"]* '"')
2023-07-10 21:11:54 +00:00
> mark
2023-07-12 00:09:42 +00:00
% {
2023-07-12 01:20:26 +00:00
sc_list[c_sc].argvals[sc_list[c_sc].argcount].start = mark-start+1;
sc_list[c_sc].argvals[sc_list[c_sc].argcount].len = p-mark-2;
sc_list[c_sc].argcount++;
2023-07-12 00:09:42 +00:00
};
value = alnum+
> mark
2023-07-12 00:09:42 +00:00
% {
2023-07-12 01:20:26 +00:00
sc_list[c_sc].argvals[sc_list[c_sc].argcount].start = mark-start;
sc_list[c_sc].argvals[sc_list[c_sc].argcount].len = p-mark;
sc_list[c_sc].argcount++;
2023-07-12 00:09:42 +00:00
};
arg = ((argname '=')? (value|qvalue));
2023-07-10 19:39:01 +00:00
2023-07-12 23:33:37 +00:00
start_p = ('{{%');
end_p = '%}}';
2023-07-10 19:39:01 +00:00
2023-07-12 23:33:37 +00:00
start_b = ('{{<');
end_b = '>}}';
2023-07-12 22:40:25 +00:00
content = spc name (sep arg)* spc;
start = start_p | start_b ;
2023-07-11 19:51:11 +00:00
end = end_p | end_b ;
2023-07-12 22:40:25 +00:00
mismatched = ((start_p content end_b) | (start_b content end_p))
@{
// Since it's mismatched, remove the name
sc_list[c_sc].name.start = 0;
sc_list[c_sc].name.len=0;
2023-07-14 00:24:37 +00:00
result.errors[result.errcount].position = p-start-2;
result.errors[result.errcount].code = ERR_MISMATCHED_BRACKET;
result.errcount++;
2023-07-12 22:40:25 +00:00
};
shortcode = ((start_p content end_p) | (start_b content end_b))
2023-07-11 23:18:05 +00:00
> {
2023-07-12 20:10:55 +00:00
sc_list[c_sc].whole.start = p-start-1;
2023-07-11 23:18:05 +00:00
}
2023-07-11 01:11:43 +00:00
@ {
2023-07-12 20:10:55 +00:00
sc_list[c_sc].whole.len = p-start-sc_list[c_sc].whole.start+1;
2023-07-11 23:18:05 +00:00
data_mark = p+1;
c_sc++;
2023-07-11 01:11:43 +00:00
};
2023-07-11 13:22:18 +00:00
closing_shortcode = (start spc '/' name spc end)
> {
2023-07-12 23:33:37 +00:00
// Starting a "closing" shortcode ( {{ /foo }}),
// close data (if previous shortcode is opening)
// If previous shortcode was closing, then previous
// text can't be data.
2023-07-12 01:20:26 +00:00
if (sc_list[c_sc-1].matching == 0) {
sc_list[c_sc-1].data.start = data_mark-start;
sc_list[c_sc-1].data.len = p-data_mark-1;
2023-07-11 23:49:33 +00:00
}
2023-07-11 13:22:18 +00:00
};
2023-07-11 01:29:53 +00:00
matched_shortcode = (shortcode any* closing_shortcode)
2023-07-11 01:11:43 +00:00
@ {
2023-07-11 23:49:33 +00:00
if (
2023-07-12 01:20:26 +00:00
sc_list[c_sc-1].name.len != sc_list[c_sc].name.len ||
2023-07-11 23:49:33 +00:00
strncmp(
2023-07-12 01:20:26 +00:00
start + sc_list[c_sc-1].name.start,
start + sc_list[c_sc].name.start,
sc_list[c_sc-1].name.len) !=0)
{
2023-07-14 00:57:47 +00:00
result.errors[result.errcount].position =
sc_list[c_sc].whole.start;
result.errors[result.errcount].code = ERR_MISMATCHED_CLOSING_TAG;
result.errcount++;
2023-07-14 00:57:47 +00:00
} else {
// The previous shortcode is matching (mark it)
sc_list[c_sc-1].matching = 1;
sc_list[c_sc-1].whole.len = p-start-sc_list[c_sc-1].whole.start + 1;
2023-07-11 01:26:19 +00:00
}
2023-07-14 00:57:47 +00:00
// Reuse the closing shortcode tag entry for next one
2023-07-12 01:20:26 +00:00
sc_list[c_sc].name.start = 0;
2023-07-12 20:10:55 +00:00
sc_list[c_sc].name.len=0;
};
2023-07-10 23:36:50 +00:00
2023-07-12 22:40:25 +00:00
main := (any* (shortcode | matched_shortcode | mismatched))*;
2023-07-10 19:39:01 +00:00
}%%
2023-07-11 23:18:05 +00:00
sc_result parse(char *input) {
2023-07-11 23:18:05 +00:00
2023-07-12 00:09:42 +00:00
%%write data;
char *eof, *ts, *te = 0;
int cs, act = 0;
char *start = input;
char *p = input;
char *pe = p + strlen(input);
sc_result result;
shortcode *sc_list = result.sc;
2023-07-12 00:09:42 +00:00
int c_sc = 0;
char *mark = p;
char *data_mark = p;
%% write init;
%% write exec;
2023-07-13 01:22:22 +00:00
return result;
2023-07-11 23:18:05 +00:00
}