Generalized the handlebars code.

This commit is contained in:
2021-08-05 10:03:47 -07:00
parent def6df8865
commit 2449c05fb0
7 changed files with 151 additions and 97 deletions

View File

@ -43,97 +43,11 @@ char * languageGetText(language_t *language, languagestring_t *string) {
int32_t languageGetTextWithVariables(
language_t *language, languagestring_t *string,
languagevariable_t *variables, int32_t variableCount,
stringhandlebarvariable_t *variables, int32_t variableCount,
char *buffer
) {
int32_t i, l, n;
char *text;
char c;
char keyBuffer[32];
languagevariable_t *variable;
text = languageGetText(language, string);
if(text == NULL) return 0;
// Start two counters. I holds source index, L holds target index.
i = 0;
l = 0;
while(true) {
c = text[i];
if(c == '\0') break;// Break on end of string.
// Look for {{, if not {{ then just treat as normal string.
if(c != '{' || text[i + 1] != '{') {
buffer[l] = c;
i++;
l++;
continue;
}
//Ignore those two chars
i += 2;
// Skip space(s)
while(true) {
c = text[i];
if(c != ' ') break;
i++;
}
// Get the key name
n = 0;// Will hold the index within keyBuffer to copy chars into.
while(true) {
c = text[i];
// Don't overflow
if(c == '\0') break;
// Skip spaces
if(c == ' ') {
i++;
continue;
}
// Handle end of key
if(c == '}') {
i++;
if(text[i] == '}') i++;// For }} then skip the second }
break;
}
// Add to buffer.
keyBuffer[n] = c;
n++;
i++;
}
// Seal the keyBuffer string
keyBuffer[n] = '\0';
// Now check each variable for a match.
for(n = 0; n < variableCount; n++) {
variable = variables + n;
if(strcmp(keyBuffer, variable->name) != 0) continue;// Does the key match?
// Begin copying the variables' value into the string.
n = 0;
while(true) {
if(variable->value[n] == '\0') break;// Handle end of variable value
buffer[l] = variable->value[n];
l++;
n++;
}
// We found the value, so break.
break;
}
// Continue looking for next {{ variable }}...
}
// Buffer has been fully cloned, seal the string.
buffer[l] = '\0';
// Return the count of chars we wrote to the buffer. -1 due to null term.
return l - 1;
char *source;
source = languageGetText(language, string);
if(source == NULL) return 0;
return stringHandlebarsBuffer(source, variables, variableCount, buffer);
}

View File

@ -7,6 +7,7 @@
#pragma once
#include <dawn/dawn.h>
#include "../util/string.h"
/**
* Initializes a language.
@ -54,6 +55,6 @@ char * languageGetText(language_t *language, languagestring_t *string);
*/
int32_t languageGetTextWithVariables(
language_t *language, languagestring_t *string,
languagevariable_t *variables, int32_t variableCount,
stringhandlebarvariable_t *variables, int32_t variableCount,
char *buffer
);