Skip to content
Snippets Groups Projects
Commit 78817caa authored by Johannes Hörmann's avatar Johannes Hörmann
Browse files

bugfixes in replacement logic

parent 757a42f1
No related branches found
No related tags found
No related merge requests found
......@@ -59,10 +59,12 @@ DocumentTemplate.prototype.toString = function ()
{
if (this._stringCache == null)
{
var content = this._getTemplatedContent();
if (this.type == DocumentTemplate.types.PLAIN)
this._stringCache = this.content;
this._stringCache = content;
else
this._stringCache = text.parseDocument(this.content);
this._stringCache = text.parseDocument(content);
}
return this._stringCache;
}
......@@ -83,7 +85,8 @@ DocumentTemplate.prototype._resolveEmbeddedTemplate = function ()
var templates = [];
// then load the possible replacement names
if (this.type == DocumentTemplate.types.PLAIN || this.type == DocumentTemplate.types.TXT)
// currently we support only txt and html as others would need special caution.
if (this.type == DocumentTemplate.types.TXT || this.type == DocumentTemplate.types.HTML)
{
templates = db.table(SqlCondition.begin()
.andPrepare("DOCUMENTTEMPLATE.KIND", $KeywordRegistry.documentTemplateType$textModular())
......@@ -91,12 +94,13 @@ DocumentTemplate.prototype._resolveEmbeddedTemplate = function ()
.buildSql("select DOCUMENTTEMPLATEID, REPLACEMENTNAME from DOCUMENTTEMPLATE"));
}
else if (this.type == DocumentTemplate.types.HTML)
if (this.type == DocumentTemplate.types.HTML)
{
templates = db.table(SqlCondition.begin()
templates.concat(db.table(SqlCondition.begin()
.andPrepare("DOCUMENTTEMPLATE.KIND", $KeywordRegistry.documentTemplateType$textModular())
.andPrepare("DOCUMENTTEMPLATE.CLASSIFICATION", $KeywordRegistry.documentTemplateTypeCategory$htmlTemplate())
.buildSql("select DOCUMENTTEMPLATEID, REPLACEMENTNAME from DOCUMENTTEMPLATE"));
.buildSql("select DOCUMENTTEMPLATEID, REPLACEMENTNAME from DOCUMENTTEMPLATE")));
}
var alias = SqlUtils.getSystemAlias();
......@@ -116,10 +120,28 @@ DocumentTemplate.prototype._resolveEmbeddedTemplate = function ()
replacedContent = replacedContent.replace("{@" + pPlaceholder[0] + "@}", pPlaceholder[1], "g")
}, this);
this._subtemplatedContent = util.encodeBase64String(replacedContent);
if (this.type == DocumentTemplate.types.PLAIN)
this._subtemplatedContent = replacedContent;
else
this._subtemplatedContent = util.encodeBase64String(replacedContent);
}
}
DocumentTemplate.prototype._getTemplatedContent = function () {
var content;
if (this._subtemplatedContent != null && pWithSubtemplates)
content = this._subtemplatedContent;
else
content = this.content;
if (this.type == DocumentTemplate.types.PLAIN)
return content;
else
return util.decodeBase64String(content);
}
/**
* The types a DocumentTemplate can have. Depending on the type,
* the correct method for replacing the placeholders can be chosen
......@@ -442,37 +464,45 @@ TemplateHelper._replaceText = function (pText, pReplacements, pSpecialCharFilter
}
/**
* @param {DocumentTemplate} pTemplate
* @return {Object[]} all placeholders needed in this template
* @private
*/
TemplateHelper._getRequiredPlaceholders = function (pTemplate)
{
// if there exists a _subtemplatedContent we use it because then I assume that the replacements are already based on content + subtemplates
var content;
if (pTemplate._subtemplatedContent == null)
content = pTemplate.content;
var content = "";
// for eml search the whole file not just the body text as placeholders could be anywhere (e.g. subject)
if (pTemplate.type == DocumentTemplate.types.EML)
content = pTemplate._getTemplatedContent();
else
content = pTemplate._subtemplatedContent;
if (pTemplate.type != DocumentTemplate.types.PLAIN)
{
content = util.decodeBase64String(content);
}
content = pTemplate.toString();
// get special regexp (e.g. to filter '=' in emls)
var filterRegexpPart = TemplateHelper._getSpecialRegexp(pTemplate);
var placeholders = PlaceholderUtils.getPlaceholders();
var placeholderCleanRegexp = new RegExp(filterRegexpPart, "gi");
var foundPlaceholders = content.match(new RegExp(PlaceholderUtils.getRegexpMatchAll(TemplateHelper._getSpecialRegexp(pTemplate)), "gi")).map(function(pFound) {
return pFound.replace(placeholderCleanRegexp,"");
});
placeholders = placeholders.filter(function(pPlaceholder)
// get all placeholders which matches the placeholder pattern
var foundPlaceholders = content.match(new RegExp(PlaceholderUtils.getRegexpMatchAll(TemplateHelper._getSpecialRegexp(pTemplate)), "gi"));
if (foundPlaceholders != null)
{
return foundPlaceholders.indexOf(pPlaceholder.placeholderName) != -1;
});
return placeholders;
// clean placeholder from the spechial strings (e.g. to filter '=' in emls)
foundPlaceholders = foundPlaceholders.map(function(pFound) {
return pFound.replace(placeholderCleanRegexp,"");
});
// filter the possible placeholders by all placeholders found
placeholders = placeholders.filter(function(pPlaceholder)
{
return foundPlaceholders.indexOf(pPlaceholder.placeholderName) != -1;
});
logging.log(JSON.stringify([placeholders], null, "\t"))
return placeholders;
}
return [];
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment