From 429b39cf08ecc6dcf45909392b3197aad8d65a7c Mon Sep 17 00:00:00 2001 From: Brett Date: Wed, 23 Aug 2023 15:27:27 -0400 Subject: [PATCH] dynamic logic based control inside html files using mustash syntax {{%bool1 && bool2 || (bool3 && !bool4)}} must be closed exactly as stated!! {{/bool1 && bool2 || (bool3 && !bool4)}} --- crow_test/webcontent/index.html | 12 ++++++-- libs/BLT | 2 +- src/crowsite/site/cache.cpp | 52 +++++++++++++++++++++++---------- src/main.cpp | 4 +++ 4 files changed, 52 insertions(+), 18 deletions(-) diff --git a/crow_test/webcontent/index.html b/crow_test/webcontent/index.html index b532ad2..b43ebf9 100644 --- a/crow_test/webcontent/index.html +++ b/crow_test/webcontent/index.html @@ -18,8 +18,16 @@
HAXsdsad - {{#_admin}} - Admin detected + {{%_admin}} +

Admin detected

+ {{/_admin}} + {{%_admin && _create_posts}} +

Admin and can read!

+ {{/_admin && _create_posts}} + {{%_admin}} + {{%_read_files}} + Nested time! + {{/_read_files}} {{/_admin}}
diff --git a/libs/BLT b/libs/BLT index 34536e2..1b4e364 160000 --- a/libs/BLT +++ b/libs/BLT @@ -1 +1 @@ -Subproject commit 34536e2a633050ed241b951c292aca58c55435b9 +Subproject commit 1b4e36416ae67d3a3789333ff965866ae45bb98b diff --git a/src/crowsite/site/cache.cpp b/src/crowsite/site/cache.cpp index 4334f51..79b08ba 100644 --- a/src/crowsite/site/cache.cpp +++ b/src/crowsite/site/cache.cpp @@ -9,6 +9,7 @@ #include #include #include +#include namespace cs { @@ -65,9 +66,7 @@ namespace cs while (!hasTemplateSuffix()) { if (!hasNext()) - { - throw LexerSyntaxError(); - } + blt_throw(LexerSyntaxError()); token += consume(); } consumeTemplateSuffix(); @@ -195,12 +194,12 @@ namespace cs { case '&': if (consume() != '&') - throw LexerSyntaxError("Unable to parse logical expression. Found single '&' missing second '&'"); + blt_throw(LexerSyntaxError("Unable to parse logical expression. Found single '&' missing second '&'")); tokens.emplace_back(TokenType::AND); break; case '|': if (consume() != '|') - throw LexerSyntaxError("Unable to parse logical expression. Found single '|' missing second '|'"); + blt_throw(LexerSyntaxError("Unable to parse logical expression. Found single '|' missing second '|'")); tokens.emplace_back(TokenType::OR); break; case '!': @@ -225,19 +224,19 @@ namespace cs static inline bool isTrue(const RuntimeContext& context, const std::string& token) { - BLT_DEBUG("isTrue for token '%s' contains? %s", token.c_str(), context.contains(token) ? "True" : "False"); + //BLT_DEBUG("isTrue for token '%s' contains? %s", token.c_str(), context.contains(token) ? "True" : "False"); return context.contains(token) && !context.at(token).empty(); } // http://www.cs.unb.ca/~wdu/cs4613/a2ans.htm bool factor(const RuntimeContext& context) { if (!hasNextToken()) - throw LexerSyntaxError("Processing boolean factor but no token was found!"); + blt_throw(LexerSyntaxError("Processing boolean factor but no token was found!")); auto next = consumeToken(); switch (next.type){ case TokenType::IDENT: if (!next.value.has_value()) - throw LexerSyntaxError("Token identifier does not have a value!"); + blt_throw(LexerSyntaxError("Token identifier does not have a value!")); return isTrue(context, next.value.value()); case TokenType::NOT: return !factor(context); @@ -245,11 +244,11 @@ namespace cs { // auto ret = ; // if (consume() != ')') -// throw LexerSyntaxError("Found token '(', parsed expression, but not ')' was found!"); +// blt_throw( LexerSyntaxError("Found token '(', parsed expression, but not ')' was found!"); return expr(context); } default: - throw LexerSyntaxError("Weird token found while parsing tokens, type: " + decodeName(next.type)); + blt_throw(LexerSyntaxError("Weird token found while parsing tokens, type: " + decodeName(next.type))); } } @@ -299,9 +298,7 @@ namespace cs } - static size_t findLastTagLocation(const std::string& tag, const std::string& data) - { - std::vector tagLocations{}; + static void getTagLocations(std::vector& tagLocations, const std::string& tag, const std::string& data){ RuntimeLexer lexer(data); while (lexer.hasNext()) { @@ -315,10 +312,22 @@ namespace cs lexer.consume(); } if (tagLocations.empty()) - throw LexerSearchFailure(tag); + blt_throw( LexerSearchFailure(tag)); + } + + static size_t findLastTagLocation(const std::string& tag, const std::string& data) + { + std::vector tagLocations{}; + getTagLocations(tagLocations, tag, data); return tagLocations[tagLocations.size() - 1]; } + static size_t findNextTagLocation(const std::string& tag, const std::string& data) { + std::vector tagLocations{}; + getTagLocations(tagLocations, tag, data); + return tagLocations[0]; + } + static std::string searchAndReplace(const std::string& data, const RuntimeContext& context) { RuntimeLexer lexer(data); @@ -329,9 +338,22 @@ namespace cs { auto token = lexer.consumeToken(); auto searchField = lexer.str.substr(lexer.index); - auto endTokenLoc = RuntimeLexer::findLastTagLocation(token, searchField); + auto endTokenLoc = RuntimeLexer::findNextTagLocation(token, searchField); + auto internalData = searchField.substr(0, endTokenLoc); + LogicalEval eval(token); + if (eval.eval(context)) { + results += searchAndReplace(internalData, context); + } + + lexer.index += endTokenLoc; + if (lexer.hasTemplatePrefix('/')) + lexer.consumeToken(); + else { + blt_throw(LexerSyntaxError("Ending token not found!")); + } + } else results += lexer.consume(); } diff --git a/src/main.cpp b/src/main.cpp index 3d94a0f..c067148 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,6 +10,7 @@ #include #include #include +#include "blt/std/assert.h" using Session = crow::SessionMiddleware; using CrowApp = crow::App; @@ -152,6 +153,8 @@ crow::response handle_root_page(const site_params& params) crow::mustache::context ctx; cs::RuntimeContext context; + generateRuntimeContext(params, context); + // pass perms in if (user_perms & cs::PERM_ADMIN) ctx["_admin"] = true; @@ -189,6 +192,7 @@ int main(int argc, const char** argv) blt::logging::setLogOutputFormat( "\033[94m[${{FULL_TIME}}]${{RC}} ${{LF}}[${{LOG_LEVEL}}]${{RC}} \033[35m(${{FILE}}:${{LINE}})${{RC}} ${{CNR}}${{STR}}${{RC}}\n" ); + cs::requests::init(); blt::arg_parse parser;