From 9f7eddbb20e295624eca6289ca0454d9ca23c0d8 Mon Sep 17 00:00:00 2001
From: nishi <nishi@f982e544-4a7d-3444-ad1a-fde59a2a69f1>
Date: Sat, 20 Apr 2024 08:42:41 +0000
Subject: [PATCH] if/ifnot/endif works

git-svn-id: file:///raid/svn-main/nishi-mandshurica/trunk@53 f982e544-4a7d-3444-ad1a-fde59a2a69f1
---
 Mandshurica/template.c | 19 +++++++++++++++++--
 Module/http.c          |  2 +-
 Webroot/bar.html       |  2 +-
 3 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/Mandshurica/template.c b/Mandshurica/template.c
index e08c0d6..ce9ab96 100644
--- a/Mandshurica/template.c
+++ b/Mandshurica/template.c
@@ -53,6 +53,7 @@ char* mandshurica_parse_template(const char* data) {
 	int start = 0;
 	char* ret = malloc(1);
 	ret[0] = 0;
+	bool skip = false;
 	for(i = 0; data[i] != 0; i++) {
 		cbuf[0] = data[i];
 		if(data[i] == '@') {
@@ -75,7 +76,21 @@ char* mandshurica_parse_template(const char* data) {
 					}
 				}
 
-				if(strcmp(op, "include") == 0) {
+				if(strcmp(op, "endif") == 0) {
+					skip = false;
+				} else if(skip) {
+				} else if(strcmp(op, "if") == 0 || strcmp(op, "ifnot") == 0) {
+					bool result = false;
+					if(arg != NULL) {
+						if(mandshurica_strcaseequ(arg, "true")) {
+							result = true;
+						} else if(mandshurica_strcaseequ(arg, "false")) {
+							result = false;
+						}
+					}
+					if(strcmp(op, "ifnot") == 0) result = !result;
+					skip = !result;
+				} else if(strcmp(op, "include") == 0) {
 					if(arg != NULL) {
 						FILE* f = fopen(arg, "r");
 						if(f != NULL) {
@@ -112,7 +127,7 @@ char* mandshurica_parse_template(const char* data) {
 
 				free(tmpl);
 			}
-		} else if(!template) {
+		} else if(!template && !skip) {
 			char* tmp = ret;
 			ret = mandshurica_strcat(tmp, cbuf);
 			free(tmp);
diff --git a/Module/http.c b/Module/http.c
index ec40433..9550b6c 100644
--- a/Module/http.c
+++ b/Module/http.c
@@ -209,7 +209,7 @@ void http_handler(int sock) {
 														uint64_t len;
 														char* data = NULL;
 														char* s;
-				
+
 														struct stat st;
 														char* lastmod = NULL;
 														const char* pth = mandshurica_strcat(webroot, "/login-fail/");
diff --git a/Webroot/bar.html b/Webroot/bar.html
index 0eb3904..08c3f66 100644
--- a/Webroot/bar.html
+++ b/Webroot/bar.html
@@ -1,3 +1,3 @@
 <hr>
-<a href="/">Home</a> <a href="/login">Login</a>
+<a href="/">Home</a> @ifnot login@<a href="/login">Login</a>@endif@
 <hr>
-- 
2.43.0