Commit 9c9c89bc by josé bollo

implements escaping using {{{...}}}

Change-Id: Iebcde2332ed60cc8754564c5a563da504fc1e1c8
Signed-off-by: José Bollo <jobol@nonadev.net>
parent 1e83e14d
......@@ -7,10 +7,12 @@ mustach: mustach-tool.c mustach.c mustach.h mustach-json-c.c mustach-json-c.
test: mustach
@make -C test1 test
@make -C test2 test
@make -C test3 test
clean:
rm -f mustach
@make -C test1 clean
@make -C test2 clean
@make -C test3 clean
......@@ -72,12 +72,27 @@ static int start(void *closure)
return 0;
}
static void print(FILE *file, const char *string, int escape)
{
if (!escape)
fprintf(file, "%s", string);
else if (*string)
do {
switch(*string) {
case '<': fputs("&lt;", file); break;
case '>': fputs("&gt;", file); break;
case '&': fputs("&amp;", file); break;
default: putc(*string, file); break;
}
} while(*++string);
}
static int put(void *closure, const char *name, int escape, FILE *file)
{
struct expl *e = closure;
struct json_object *o = find(e, name);
if (o)
fprintf(file, "%s", json_object_get_string(o));
print(file, json_object_get_string(o), escape);
return 0;
}
......
......@@ -24,11 +24,18 @@
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <json-c/json.h>
#include "mustach-json-c.h"
static void help(char *prog)
{
printf("usage: %s json-file mustach-templates...\n", basename(prog));
exit(0);
}
static char *readfile(const char *filename)
{
struct stat s;
......@@ -48,8 +55,11 @@ int main(int ac, char **av)
{
struct json_object *o;
char *t;
char *prog = *av;
if (*++av) {
if (!strcmp(*av, "-h") || !strcmp(*av, "--help"))
help(prog);
o = json_object_from_file(*av++);
while(o && *av) {
t = readfile(*av++);
......
......@@ -63,6 +63,18 @@ static int process(const char *template, struct mustach_itf *itf, void *closure,
case '!':
case '=':
break;
case '{':
for (l = 0 ; clstr[l] == '}' ; l++);
if (clstr[l]) {
if (!len || beg[len-1] != '}')
return MUSTACH_ERROR_BAD_UNESCAPE_TAG;
len--;
} else {
if (term[l] != '}')
return MUSTACH_ERROR_BAD_UNESCAPE_TAG;
template++;
}
c = '&';
case '^':
case '#':
case '/':
......
......@@ -62,14 +62,15 @@ struct mustach_itf {
int (*partial)(void *closure, const char *name, char **result);
};
#define MUSTACH_OK 0
#define MUSTACH_ERROR_SYSTEM -1
#define MUSTACH_ERROR_UNEXPECTED_END -2
#define MUSTACH_ERROR_EMPTY_TAG -3
#define MUSTACH_ERROR_TAG_TOO_LONG -4
#define MUSTACH_ERROR_BAD_SEPARATORS -5
#define MUSTACH_ERROR_TOO_DEPTH -6
#define MUSTACH_ERROR_CLOSING -7
#define MUSTACH_OK 0
#define MUSTACH_ERROR_SYSTEM -1
#define MUSTACH_ERROR_UNEXPECTED_END -2
#define MUSTACH_ERROR_EMPTY_TAG -3
#define MUSTACH_ERROR_TAG_TOO_LONG -4
#define MUSTACH_ERROR_BAD_SEPARATORS -5
#define MUSTACH_ERROR_TOO_DEPTH -6
#define MUSTACH_ERROR_CLOSING -7
#define MUSTACH_ERROR_BAD_UNESCAPE_TAG -8
/**
* fmustach - Renders the mustache 'template' in 'file' for 'itf' and 'closure'.
......
Memcheck, a memory error detector
Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
Command: ../mustach json must
......
Memcheck, a memory error detector
Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
Command: ../mustach json must
......
resu.last
vg.last
.PHONY: test clean
test: ../mustach
@echo starting test
@valgrind ../mustach json must > resu.last 2> vg.last
@sed -i 's:^==[0-9]*== ::' vg.last
@diff resu.ref resu.last && echo "result ok" || echo "ERROR! Result differs"
@diff vg.ref vg.last && echo "memory ok" || echo "ERROR! Memory differs"
clean:
rm -f resu.last vg.last
{
"name": "Chris",
"company": "<b>GitHub & Co</b>"
}
* {{name}}
* {{age}}
* {{company}}
* {{&company}}
* {{{company}}}
{{=<% %>=}}
* <%company%>
* <%&company%>
* <%{company}%>
* Chris
*
* &lt;b&gt;GitHub &amp; Co&lt;/b&gt;
* <b>GitHub & Co</b>
* <b>GitHub & Co</b>
* &lt;b&gt;GitHub &amp; Co&lt;/b&gt;
* <b>GitHub & Co</b>
* <b>GitHub & Co</b>
Memcheck, a memory error detector
Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
Command: ../mustach json must
HEAP SUMMARY:
in use at exit: 0 bytes in 0 blocks
total heap usage: 21 allocs, 21 frees, 6,339 bytes allocated
All heap blocks were freed -- no leaks are possible
For counts of detected and suppressed errors, rerun with: -v
ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment