summaryrefslogtreecommitdiff
path: root/server/src/formattools.cc
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/formattools.cc')
-rw-r--r--server/src/formattools.cc109
1 files changed, 33 insertions, 76 deletions
diff --git a/server/src/formattools.cc b/server/src/formattools.cc
index 63cfec6..6a58d74 100644
--- a/server/src/formattools.cc
+++ b/server/src/formattools.cc
@@ -25,7 +25,7 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include "formattools.h"
-
+#include "mltokenizer.h"
#include "luaformatmapper.h"
/**
@@ -49,7 +49,8 @@ static std::string deescape_string(std::string str, char c)
std::string out;
for(size_t i = 0; i < str.length(); i++) {
if(i < str.length() - 1 && str[i] == c && str[i + 1] == c) {
- // We simply do nothing here, thus skipping the current character.
+ out += str[i];
+ i++; // Skip the next character
} else {
out += str[i];
}
@@ -67,82 +68,32 @@ std::string escape_resume_string(std::string str)
return escape_string(escape_string(str, '['), ']');
}
-// FIXME: This function doesn't work... reimplement using regexps.
-Fields get_multilist_values(std::string str)
+Fields get_multilist_values(std::string mlvalue)
{
std::map<std::string, std::string> values;
- // Replace ${foo|bar} with bar
- for(size_t i = 0; i < str.length(); i++) {
- if(str[i] == '$') {
- if(i < str.length() - 2 &&
- str[i+1] == '{' && str[i+2] != '{') {
- // We are in a key/value
-
- // Look for end marker, and |
- size_t j;
- for(j = i + 2; j < str.length(); j++) {
- if(str[j] == '}' && str[j + 1] != '}') {
- // We have an end marker
- break;
- }
- }
-
- std::string key_value = str.substr(i + 2, j - (i + 2));
- printf("Found [%s]\n", key_value.c_str());
-
- std::string value = key_value.substr(key_value.find('|') + 1);
- std::string key = key_value.substr(0, key_value.find('|') - 1);
-
- values[key] = value;
-
- i = j;
-
- }
- }
+ std::vector< mltoken_t > tokens = mltokenize(mlvalue);
+ std::vector< mltoken_t >::iterator i = tokens.begin();
+ while(i != tokens.end()) {
+ if(i->type == MLTT_VALUE) values[i->name] = i->value;
+ i++;
}
return values;
}
-
-// FIXME: This function doesn't work... reimplement using regexps.
-std::string render_multilist_string(std::string str)
+std::string render_multilist_string(std::string mlvalue)
{
- std::string out;
-
- // Replace ${foo|bar} with bar
- for(size_t i = 0; i < str.length(); i++) {
- if(str[i] == '$') {
- if(i < str.length() - 2 &&
- str[i+1] == '{' && str[i+2] != '{') {
- // We are in a key/value
-
- // Look for end marker, and |
- size_t j;
- for(j = i + 2; j < str.length(); j++) {
- if(str[j] == '}' && str[j + 1] != '}') {
- // We have an end marker
- break;
- }
- }
-
- std::string key_value = str.substr(i + 2, j - (i + 2));
- printf("Found [%s]\n", key_value.c_str());
-
- std::string value = key_value.substr(key_value.find('|') + 1);
+ std::string output;
- out += value;
-
- i = j;
-
- } else out += str[i];
- } else {
- out += str[i];
- }
+ std::vector< mltoken_t > tokens = mltokenize(mlvalue);
+ std::vector< mltoken_t >::iterator i = tokens.begin();
+ while(i != tokens.end()) {
+ output += i->value;
+ i++;
}
-
- return deescape_string(deescape_string(out, '{'), '}');
+
+ return output;
}
std::string render_resume_string(std::string str, Fields &fields)
@@ -158,7 +109,7 @@ std::string render_resume_string(std::string str, Fields &fields)
str[i+1] == '[' && str[i+2] != '[') {
// We are in a key/value
- // Look for end marker, and |
+ // Look for end marker
size_t j;
for(j = i + 2; j < str.length(); j++) {
if(str[j] == ']' && str[j + 1] != ']') {
@@ -168,11 +119,8 @@ std::string render_resume_string(std::string str, Fields &fields)
}
std::string luaprogram = str.substr(i + 2, j - (i + 2));
- printf("Found [%s]\n", luaprogram.c_str());
-
std::string value = mapper.map(luaprogram);
-
- out += value;
+ out += escape_resume_string(value);
i = j;
@@ -192,19 +140,28 @@ int main()
Fields fields;
fields["dingo"] = "[[meget dyr]]";
fields["fnuld"] = "Zimbabwe";
- fields["mlstring"] = "Ladidaa ${foo|bar} ${{dingo|dyt}} ${dims|dulle}.";
+ fields["mlstring"] =
+ "Ladidaa ${myname|bar} ${{dingo|dyt}} ${dims|{{dimmer}}}.\n"
+ "Ladidaa ${myname|bole} ${{dingo|dyt}} ${dims|[[fillerhejs]]}.\n"
+ "Ladidaa ${myname|daske} ${{dingo|dyt}} ${dims|buller}.\n";
+ /*
printf("%s\n", deescape_string(deescape_string("[[] []]", '['), ']').c_str());
if(escape_multilist_string("${} {{}}") != "${{}} {{{{}}}}") return 1;
if(escape_resume_string("$[] [[]]") != "$[[]] [[[[]]]]") return 1;
- std::string mlstring = "Ladidaa ${foo|bar} ${{dingo|dyt}} ${dims|dulle}.";
+ std::string mlstring = "Ladidaa ${myname|bar} ${{dingo|dyt}} ${dims|dulle}.";
printf("{%s}\n", render_multilist_string(mlstring).c_str());
+ */
- // std::string resumestring = "Ladidaa $[printval('dims' .. dingo)] $[[et eller andet]] $[printval('noget andet')].";
- std::string resumestring = "Ladidaa \n$[printfmlval('mlstring', ' * ', true, '<p>', 'myname', '</p>')] dalidaadoo.";
+ std::string resumestring = "Ladidaa \n$["
+ "printfmlval(mlstring, ' * ', true, '<p>', 'myname', '</p>', 'PRE', 'dims', 'POST')"
+ "] dalidaadoo.";
printf("{%s}\n", render_resume_string(resumestring, fields).c_str());
+
+ // std::string resumestring2 = "Ladidaa \n$[printfmlval(mlstring, ' * ', false, '<p>', 'myname', '</p>', 'PRE', 'dims', 'POST')] dalidaadoo.";
+ // printf("{%s}\n", render_resume_string(resumestring2, fields).c_str());
return 0;
}