From dd23a805c2a1edf7f1df293dd03a542f3b2b3d9e Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 23 Feb 2018 19:45:28 +0100 Subject: [PATCH] gluon-web: add new <%| tag for escaped expressions --- package/gluon-web/src/template_parser.c | 41 +++++++++++++++++-------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/package/gluon-web/src/template_parser.c b/package/gluon-web/src/template_parser.c index 3ba45488..d71afe72 100644 --- a/package/gluon-web/src/template_parser.c +++ b/package/gluon-web/src/template_parser.c @@ -35,22 +35,24 @@ #include -/* code types */ -#define T_TYPE_INIT 0 -#define T_TYPE_TEXT 1 -#define T_TYPE_COMMENT 2 -#define T_TYPE_EXPR 3 -#define T_TYPE_INCLUDE 4 -#define T_TYPE_I18N 5 -#define T_TYPE_I18N_RAW 6 -#define T_TYPE_CODE 7 -#define T_TYPE_EOF 8 +typedef enum { + T_TYPE_INIT, + T_TYPE_TEXT, + T_TYPE_COMMENT, + T_TYPE_EXPR, + T_TYPE_EXPR_RAW, + T_TYPE_INCLUDE, + T_TYPE_I18N, + T_TYPE_I18N_RAW, + T_TYPE_CODE, + T_TYPE_EOF, +} t_type_t; struct template_chunk { const char *s; const char *e; - int type; + t_type_t type; int line; }; @@ -75,7 +77,8 @@ static const char *const gen_code[][2] = { [T_TYPE_INIT] = {NULL, NULL}, [T_TYPE_TEXT] = {"write('", "')"}, [T_TYPE_COMMENT] = {NULL, NULL}, - [T_TYPE_EXPR] = {"write(tostring(", " or ''))"}, + [T_TYPE_EXPR] = {"write(pcdata(tostring(", " or '')))"}, + [T_TYPE_EXPR_RAW] = {"write(tostring(", " or ''))"}, [T_TYPE_INCLUDE] = {"include('", "')"}, [T_TYPE_I18N] = {"write(pcdata(translate('", "')))"}, [T_TYPE_I18N_RAW] = {"write(translate('", "'))"}, @@ -226,11 +229,17 @@ static void template_code(struct template_parser *parser, const char *e) break; /* expr */ - case '=': + case '|': s++; parser->cur_chunk.type = T_TYPE_EXPR; break; + /* expr raw */ + case '=': + s++; + parser->cur_chunk.type = T_TYPE_EXPR_RAW; + break; + /* code */ default: parser->cur_chunk.type = T_TYPE_CODE; @@ -295,6 +304,7 @@ static struct template_buffer * template_format_chunk(struct template_parser *pa break; case T_TYPE_EXPR: + case T_TYPE_EXPR_RAW: buf_append(buf, c->s, c->e - c->s); for (p = c->s; p < c->e; p++) parser->line += (*p == '\n'); @@ -305,6 +315,11 @@ static struct template_buffer * template_format_chunk(struct template_parser *pa for (p = c->s; p < c->e; p++) parser->line += (*p == '\n'); break; + + case T_TYPE_INIT: + case T_TYPE_COMMENT: + case T_TYPE_EOF: + break; } if ((tail = gen_code[c->type][1]) != NULL)