Variables work. Added trimline support.

This commit is contained in:
Brad Arant 2024-09-24 12:13:06 -07:00
parent 3679644f64
commit 933c400922
5 changed files with 26 additions and 25 deletions

View File

@ -33,14 +33,15 @@ namespace jet {
coreutils::ZString Global::getVariable(coreutils::ZString &variable) {
if(variable.ifNext("$[")) {
coreutils::MString name;
if(variable.ifNext("!")) {
return variables[renderVariableName(variable)];
return variables[renderVariableName(name, variable)];
} if(variable.ifNext(":")) {
// TODO: should only allow CGI variable name. Allow variable substitution.
} if(variable.ifNext("@")) {
// TODO: should only allow environment variables. Allow substitution.
} else {
return variables[renderVariableName(variable)];
return variables[renderVariableName(name, variable)];
}
throw coreutils::Exception("expected variable name or type designator.");
} if(variable.ifNext("#[")) {
@ -51,15 +52,17 @@ namespace jet {
throw coreutils::Exception("Expecting a variable initializer ('$[' or '#[').");
}
coreutils::MString Global::renderVariableName(coreutils::ZString &variable) {
coreutils::MString name = variable.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-");
coreutils::MString Global::renderVariableName(coreutils::MString &name, coreutils::ZString &variable) {
name << variable.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-");
if(variable.ifNext("]"))
return name;
if(variable.startsWith("$[")) {
name << getVariable(variable);
return name;
}
return coreutils::MString("");
if(variable.ifNext("]"))
return name;
}
renderVariableName(name, variable);
return name;
}
}

View File

@ -19,12 +19,11 @@ namespace jet {
void addSession(coreutils::MString sessionId, __mysql *mysql);
void removeSession(coreutils::MString sessionId);
coreutils::ZString getVariable(coreutils::ZString &variable);
coreutils::MString renderVariableName(coreutils::MString &name, coreutils::ZString &variable);
std::map<coreutils::MString, coreutils::MString> variables;
std::map<coreutils::MString, __mysql *> sessions;
private:
coreutils::MString renderVariableName(coreutils::ZString &variable);
};

View File

@ -5,10 +5,8 @@ namespace jet {
KeywordValue::KeywordValue(coreutils::ZString data, Global &global) : MString() {
while(!data.eod()) {
if(data.ifNext("$[")) {
coreutils::ZString varName(data.getTokenExclude("]"));
data.ifNext("]");
write(global.variables[varName]);
if(data.startsWith("$[")) {
write(global.getVariable(data));
} else {
write(data.charAt(0));
data.nextChar();

View File

@ -50,6 +50,9 @@ namespace jet {
if(variableDefined("filterblanklines")) {
filterBlankLines = variables["filterblanklines"] == "true" ? true: false;
}
if(variableDefined("trimlines")) {
trimLines = variables["trimlines"] == "true" ? true: false;
}
if(hasContainer) {
coreutils::Log(coreutils::LOG_DEBUG_2) << "has Container: " << hasContainer;
bool hasSplitTag = splitTagName == "" ? false: true;
@ -239,7 +242,10 @@ namespace jet {
while(!in.eod()) {
if(filterBlankLines) {
if(!in.lineIsWhitespace()) {
out.write(in.goeol());
if(trimLines)
out.write(in.goeol().trim());
else
out.write(in.goeol());
out.write('\n');
}
else {

View File

@ -6,7 +6,7 @@
int main(int argc, char **argv) {
coreutils::ZString data2("<jet name1=\"localname\" filterblanklines=\"true\">\n"
coreutils::ZString data("<jet name1=\"localname\" filterblanklines=\"true\" trimlines=\"true\">\n"
" <comment>This is a comment and should not show up in the output.</comment>\n"
" <html>\n"
" <set name=\"ix\" value=\"1\" />\n"
@ -20,7 +20,10 @@ int main(int argc, char **argv) {
" <set name=\"newname\" scope=\"global\">another container value</set>\n"
" >>>$[noeval]<<<\n"
" >>>$[thename]<<<\n"
" local: >>>#[name$[ix]]<<<\n"
" <set name=\"iz\" value=\"data\" />\n"
" <set name=\"ix1\" value=\"1\" />\n"
" <set name=\"test$[ix$[ix1]]$[iz]\" value=\"0123456789\" />\n"
" $[test$[ix$[ix1]]$[iz]]\n"
" <mysql key=\"uu\">\n"
" <if value1=\"X\" value2=\"Y\" type=\"ne\">\n"
" 789\n"
@ -41,14 +44,6 @@ int main(int argc, char **argv) {
" </html>\n"
"</jet>\n");
coreutils::ZString data("<jet filterblanklines=\"true\">\n"
" <set name=\"test1\" value=\"0123456789\" />\n"
" <set name=\"ix\" value=\"1\" />\n"
" $[test$[ix]]\n"
"</jet>\n");
// coreutils::ZString data("<jet>ABC<jet>HIJ</jet>XYZ</jet>\n");
std::cout << "---------\n" << data << "----------\n" << std::endl;
try {
@ -56,7 +51,7 @@ int main(int argc, char **argv) {
coreutils::MString out;
jet::__jet *jet = new jet::__jet(data, out, global);
delete jet;
std::cout << ">>-------" << std::endl << out << std::endl << "<<------" << std::endl;
std::cout << ">>-------" << std::endl << out << "<<------" << std::endl;
// global.dump();
}
catch(coreutils::Exception e) {