shortcode/shortcodes.rl

133 lines
2.6 KiB
Plaintext
Raw Normal View History

2023-07-10 19:39:01 +00:00
#include <stdio.h>
2023-07-11 12:58:58 +00:00
#include <string.h>
#include "bglibs/str.h"
2023-07-11 13:22:18 +00:00
#include "bglibs/gstack.h"
2023-07-10 21:11:54 +00:00
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-11 12:58:58 +00:00
% {str_cats(&output, "N ");
str_catb(&output, mark, p-mark);
str_cats(&output, "\n");
str_copyb(&new_name, mark, p-mark);
};
2023-07-11 00:55:45 +00:00
argname = alpha+
2023-07-10 21:17:17 +00:00
> mark
2023-07-11 12:58:58 +00:00
% {str_cats(&output, "A ");
str_catb(&output, mark, p-mark);
str_cats(&output, "\n");
};
qvalue = ('"' [^"]* '"')
2023-07-10 21:11:54 +00:00
> mark
2023-07-11 12:58:58 +00:00
% {str_cats(&output, "V ");
str_catb(&output, mark+1, p-mark-2);
str_cats(&output, "\n");
};
value = alnum+
> mark
2023-07-11 12:58:58 +00:00
% {str_cats(&output, "V ");
str_catb(&output, mark, p-mark);
str_cats(&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 ;
2023-07-11 19:51:11 +00:00
end = end_p | end_b ;
shortcode = (start spc name (sep arg)* spc end)
2023-07-11 19:51:11 +00:00
> {sc_start = p-start;}
2023-07-11 01:11:43 +00:00
@ {
2023-07-11 12:58:58 +00:00
str_cats(&output, "+++ opening\n");
str_copy(&open_name, &new_name);
2023-07-11 13:22:18 +00:00
data_mark = p;
2023-07-11 01:11:43 +00:00
};
2023-07-11 13:22:18 +00:00
closing_shortcode = (start spc '/' name spc end)
> {
// Starting a shortcode, close data
str_copyb(&data, data_mark+1, p-data_mark-2);
};
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 12:58:58 +00:00
str_cats(&output, "--- closing\n");
printf("closing from %s to %s\n", open_name.s, new_name.s);
2023-07-11 13:22:18 +00:00
printf("data: %s\n", data.s);
2023-07-11 12:58:58 +00:00
if (str_cmp(&open_name, 0, &new_name, 0) != 0) {
2023-07-11 01:26:19 +00:00
// Closing the wrong code
2023-07-11 12:58:58 +00:00
// TODO error reporting
return output;
2023-07-11 01:26:19 +00:00
}
2023-07-11 12:58:58 +00:00
str_truncate(&open_name,0);
2023-07-11 01:11:43 +00:00
};
2023-07-10 23:36:50 +00:00
2023-07-11 01:29:53 +00:00
main := (any* (shortcode | matched_shortcode))*;
2023-07-10 19:39:01 +00:00
}%%
2023-07-11 12:58:58 +00:00
str parse(char *input) {
2023-07-10 19:39:01 +00:00
%%write data;
char *eof, *ts, *te = 0;
int cs, act = 0;
2023-07-11 19:48:02 +00:00
char *start = input;
char *p = input;
char *pe = p + strlen(input);
2023-07-11 13:22:18 +00:00
str open_name, new_name, output, data;
2023-07-11 12:58:58 +00:00
str_init(&open_name);
str_init(&new_name);
str_init(&output);
2023-07-11 13:22:18 +00:00
str_init(&data);
2023-07-11 19:48:02 +00:00
int sc_start = 0;
int sc_end = 0;
2023-07-11 13:22:18 +00:00
char *mark = p;
char *data_mark = p;
2023-07-10 19:39:01 +00:00
%% write init;
%% write exec;
return output;
}
2023-07-11 13:22:18 +00:00
struct shortcode {
str name;
2023-07-11 19:48:02 +00:00
int start;
int end;
2023-07-11 13:22:18 +00:00
};
2023-07-10 19:39:01 +00:00
int main(int argc, char **argv) {
2023-07-11 12:58:58 +00:00
str output = parse("
2023-07-11 01:29:53 +00:00
sarasa
2023-07-11 00:55:45 +00:00
{{< o1 arg1 >}}
2023-07-11 01:29:53 +00:00
stuff
2023-07-11 13:22:18 +00:00
{{< c1 arg2 >}}foobar{{% /c1%}}
2023-07-11 01:29:53 +00:00
{{< o2 arg1 >}}
{{< o3 arg1 >}}
more stuff
");
2023-07-11 12:58:58 +00:00
// if (output == 0) {
// printf("parse error\n");
// return 1;
// }
printf("\n%s\n", output.s);
2023-07-10 19:39:01 +00:00
return 0;
}