From e41611dc9817402df4b384e53914f9c3662d73b0 Mon Sep 17 00:00:00 2001 From: Rene Zeldenthuis Date: Sun, 5 Feb 2023 13:07:10 +0100 Subject: [PATCH 1/2] Use micro-moustache --- lib/template_render/library.json | 5 --- lib/template_render/template_render.h | 53 --------------------------- platformio.ini | 3 +- src/main.cpp | 10 ++--- 4 files changed, 7 insertions(+), 64 deletions(-) delete mode 100644 lib/template_render/library.json delete mode 100644 lib/template_render/template_render.h diff --git a/lib/template_render/library.json b/lib/template_render/library.json deleted file mode 100644 index f8946d4..0000000 --- a/lib/template_render/library.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "name": "template_render", - "version": "1.0.0", - "description": "A mini template renderer" -} \ No newline at end of file diff --git a/lib/template_render/template_render.h b/lib/template_render/template_render.h deleted file mode 100644 index 1166efe..0000000 --- a/lib/template_render/template_render.h +++ /dev/null @@ -1,53 +0,0 @@ -#pragma once - -#include - -typedef struct -{ - const char *key; - const String value; -} template_variable_t; - -template -inline String template_render(const String& format, T (&values)[n]) -{ - auto s = String(format); - // Conditional sections - for (size_t i = 0; i < n; i++) - { - // Include Section {{#expr}} - auto match_section_begin = "{{#" + String(values[i].key) + "}}"; - // Inverted section {{^expr}} - auto match_section_inverted_begin = "{{^" + String(values[i].key) + "}}"; - // End section {{/expr}} - auto match_section_end = "{{/" + String(values[i].key) + "}}"; - while (true) - { - bool inverted = false; - auto first = s.indexOf(match_section_begin); - if (first < 0) - { - inverted = true; - first = s.indexOf(match_section_inverted_begin); - if (first < 0) - break; - } - - auto second = s.indexOf(match_section_end, first + match_section_begin.length()); - if (second < 0) - break; - - // Arduino returns 0 and 1 for bool.toString() - if ((!inverted && (values[i].value == "1")) || (inverted && (values[i].value == "0"))) - s = s.substring(0, first) + s.substring(first + match_section_begin.length(), second) + s.substring(second + match_section_end.length()); - else - s = s.substring(0, first) + s.substring(second + match_section_end.length()); - } - } - - // Replace variables {{variable}} - for (size_t i = 0; i < n; i++) - s.replace("{{" + String(values[i].key) + "}}", values[i].value); - - return s; -} \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 904c733..7710af9 100644 --- a/platformio.ini +++ b/platformio.ini @@ -27,4 +27,5 @@ build_flags = lib_deps = prampec/IotWebConf @ ^3.2.1 - geeksville/Micro-RTSP @ ^0.1.6 \ No newline at end of file + geeksville/Micro-RTSP @ ^0.1.6 + rzeldent/micro-moustache@^1.0.1 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index ade5581..db085b9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include @@ -58,7 +58,7 @@ void handle_root() const char *wifi_modes[] = {"NULL", "STA", "AP", "STA+AP"}; - const template_variable_t substitutions[] = { + moustache_variable_t substitutions[] = { // Config Changed? {"ConfigChanged", String(config_changed)}, // Version / CPU @@ -101,7 +101,7 @@ void handle_root() {"RtspPort", String(RTSP_PORT)}}; web_server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); - auto html = template_render(file_data_index_html, substitutions); + auto html = moustache_render(file_data_index_html, substitutions); web_server.send(200, "text/html", html); } @@ -117,12 +117,12 @@ void handle_restart() return; } - const template_variable_t substitutions[] = { + moustache_variable_t substitutions[] = { {"AppTitle", APP_TITLE}, {"AppVersion", APP_VERSION}, {"ThingName", iotWebConf.getThingName()}}; - auto html = template_render(file_data_restart_html, substitutions); + auto html = moustache_render(file_data_restart_html, substitutions); web_server.send(200, "text/html", html); log_v("Restarting... Press refresh to connect again"); sleep(100); From 0f1e47c32e373925ee5c02466b70898fac1cfa76 Mon Sep 17 00:00:00 2001 From: rzeldent Date: Sun, 5 Feb 2023 13:18:53 +0100 Subject: [PATCH 2/2] Update main.yml Fix: Node.js 12 actions are deprecated. --- .github/workflows/main.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e819dc4..9b38fc0 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -5,9 +5,9 @@ jobs: name: Build runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Set up python - uses: actions/setup-python@v2 + uses: actions/setup-python@v3 with: python-version: '3.x' architecture: 'x64' @@ -16,7 +16,7 @@ jobs: - name: Build firmware run: platformio run - name: Archive - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v3 with: name: firmware.bin path: .pio/build/*/firmware.bin