gluon-web: add new <%| tag for escaped expressions

This commit is contained in:
Matthias Schiffer 2018-02-23 19:45:28 +01:00
parent 961c9db81f
commit dd23a805c2
No known key found for this signature in database
GPG Key ID: 16EF3F64CB201D9C

View File

@ -35,22 +35,24 @@
#include <unistd.h> #include <unistd.h>
/* code types */ typedef enum {
#define T_TYPE_INIT 0 T_TYPE_INIT,
#define T_TYPE_TEXT 1 T_TYPE_TEXT,
#define T_TYPE_COMMENT 2 T_TYPE_COMMENT,
#define T_TYPE_EXPR 3 T_TYPE_EXPR,
#define T_TYPE_INCLUDE 4 T_TYPE_EXPR_RAW,
#define T_TYPE_I18N 5 T_TYPE_INCLUDE,
#define T_TYPE_I18N_RAW 6 T_TYPE_I18N,
#define T_TYPE_CODE 7 T_TYPE_I18N_RAW,
#define T_TYPE_EOF 8 T_TYPE_CODE,
T_TYPE_EOF,
} t_type_t;
struct template_chunk { struct template_chunk {
const char *s; const char *s;
const char *e; const char *e;
int type; t_type_t type;
int line; int line;
}; };
@ -75,7 +77,8 @@ static const char *const gen_code[][2] = {
[T_TYPE_INIT] = {NULL, NULL}, [T_TYPE_INIT] = {NULL, NULL},
[T_TYPE_TEXT] = {"write('", "')"}, [T_TYPE_TEXT] = {"write('", "')"},
[T_TYPE_COMMENT] = {NULL, NULL}, [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_INCLUDE] = {"include('", "')"},
[T_TYPE_I18N] = {"write(pcdata(translate('", "')))"}, [T_TYPE_I18N] = {"write(pcdata(translate('", "')))"},
[T_TYPE_I18N_RAW] = {"write(translate('", "'))"}, [T_TYPE_I18N_RAW] = {"write(translate('", "'))"},
@ -226,11 +229,17 @@ static void template_code(struct template_parser *parser, const char *e)
break; break;
/* expr */ /* expr */
case '=': case '|':
s++; s++;
parser->cur_chunk.type = T_TYPE_EXPR; parser->cur_chunk.type = T_TYPE_EXPR;
break; break;
/* expr raw */
case '=':
s++;
parser->cur_chunk.type = T_TYPE_EXPR_RAW;
break;
/* code */ /* code */
default: default:
parser->cur_chunk.type = T_TYPE_CODE; parser->cur_chunk.type = T_TYPE_CODE;
@ -295,6 +304,7 @@ static struct template_buffer * template_format_chunk(struct template_parser *pa
break; break;
case T_TYPE_EXPR: case T_TYPE_EXPR:
case T_TYPE_EXPR_RAW:
buf_append(buf, c->s, c->e - c->s); buf_append(buf, c->s, c->e - c->s);
for (p = c->s; p < c->e; p++) for (p = c->s; p < c->e; p++)
parser->line += (*p == '\n'); 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++) for (p = c->s; p < c->e; p++)
parser->line += (*p == '\n'); parser->line += (*p == '\n');
break; break;
case T_TYPE_INIT:
case T_TYPE_COMMENT:
case T_TYPE_EOF:
break;
} }
if ((tail = gen_code[c->type][1]) != NULL) if ((tail = gen_code[c->type][1]) != NULL)