I've made a email generator, that uses a HTML template and replaces keywords in the template with information taken from a database. I'm looking to see if there is a way to improve the coding.
public class EmailGenerator : IEmailGenerator
{
private string mergeTemplate(string template, object obj)
{
Regex operationParser = new Regex(@"\$(?:(?<operation>[\w\-\,\.]+)\x20)(?<value>[\w\-\,\.]+)\$", RegexOptions.Compiled);
Regex valueParser = new Regex(@"\$(?<value>[\w\-\,\.]+)\$", RegexOptions.Compiled);
var operationMatches = operationParser.Matches(template).Cast<Match>().Reverse().ToList();
foreach (var match in operationMatches)
{
string operation = match.Groups["operation"].Value;
string value = match.Groups["value"].Value;
var propertyInfo = obj.GetType().GetProperty(value);
object dataValue = propertyInfo.GetValue(obj, null);
if (operation == "endforeach")
{
string foreachToken = "$foreach " + value + "$";
var startIndex = template.LastIndexOf(foreachToken, match.Index);
var templateBlock = template.Substring(startIndex + foreachToken.Length, match.Index - startIndex - foreachToken.Length);
var items = (IEnumerable) dataValue;
string blockResult = "";
foreach (object item in items)
{
blockResult += this.mergeTemplate(templateBlock, item);
}
template = template.Remove(startIndex, match.Index - startIndex + match.Length).Insert(startIndex, blockResult);
}
}
var valueMatches = valueParser.Matches(template).Cast<Match>().Reverse().ToList();
foreach (var match in valueMatches)
{
string value = match.Groups["value"].Value;
var propertyInfo = obj.GetType().GetProperty(value);
object dataValue = propertyInfo.GetValue(obj, null);
template = template.Remove(match.Index, match.Length).Insert(match.Index, dataValue.ToString());
}
return template;
}
}
An idea I have, but haven't been able to implement is to use a while statement instead of the if statement, since the generator needs to start at the end of the template block, so that it replaces the correct information.
Email Template Example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/DTD/xhtml1">
<html xmlns="http://www.w3.org/1993/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charste=UTF-8" />
<title>Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body style="margin: 0; padding: 0;">
<!--[if (gte mso 9)|(IE)]>
<table border="0" cellpadding="0" cellspacing="0" width="600">
<tr>
<td>
<![endif]-->
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="padding: 20px 0 30px 0;">
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse; max-width: 600px;" width="100%">
<tr>
<td align="center" bgcolor="f7f8f6">
<img src="#" alt="alttext" height="auto" style="display: block;" width="100%" />
</td>
</tr>
<tr>
<td bgcolor="f7f8f6" style="padding: 20px 30px 40px 30px;">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td style="border-bottom: 1px solid #13161c; color: #13161c; font-family: Helvetica, Arial, sans-serif; font-size: 18px;">
<b>ID - $ID$</b>
</td>
</tr>
<tr>
<td style="padding: 5px 0 0 0;">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td style="color: #13161c; font-family: Helvetica, Arial, sans-serif; font-size: 12px; line-height: 20px;" valign="top" width="25%">To:</td>
<td style="font-size: 0; line-height: 0;" width="5%"></td>
<td style="color: #13161c; font-family: Helvetica, Arial, sans-serif; font-size: 12px; line-height: 20px;" valign="top" width="70%">$Company$</td>
</tr>
<tr>
<td style="color: #13161c; font-family: Helvetica, Arial, sans-serif; font-size: 12px; line-height: 20px;" valign="top" width="25%">Referance:</td>
<td style="font-size: 0; line-height: 0;" width="5%"></td>
<td style="color: #13161c; font-family: Helvetica, Arial, sans-serif; font-size: 12px; line-height: 20px;" valign="top" width="70%">$Reference$</td>
</tr>
<tr>
<td style="color: #13161c; font-family: Helvetica, Arial, sans-serif; font-size: 12px; line-height: 20px;" valign="top" width="25%">Date:</td>
<td style="font-size: 0; line-height: 0;" width="5%"></td>
<td style="color: #13161c; font-family: Helvetica, Arial, sans-serif; font-size: 12px; line-height: 20px;" valign="top" width="70%">$Date$</td>
</tr>
</table>
</td>
</tr>
<tr>
<td style="border-bottom: 1px solid #13161c; color: #13161c; font-family: Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px; padding: 20px 0 0 0;">We've recieved this information from you:</td>
</tr>
$foreach Items$<tr>
<td style="border-bottom: 1px dashed #13161c; color: #13161c; font-family: Helvetica, Arial, sans-serif; font-size: 12px; line-height: 20px; padding: 5px 0 0 0;" width="100%">
<b>$Name$</b>
</td>
</tr>
<tr>
<td style="padding: 0 0 20px 0;">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td style="color: #13161c; font-family: Helvetica, Arial, sans-serif; font-size: 12px; line-height: 20px;" valign="top" width="25%">Period:</td>
<td style="font-size: 0; line-height: 0" width="5%"></td>
<td style="color: #13161c; font-family: Helvetica, Arial, sans-serif; font-size: 12px; line-height: 20px;" valign="top" width="70%">$Period$</td>
</tr>
<tr>
<td style="color: #13161c; font-family: Helvetica, Arial, sans-serif; font-size: 12px; line-height: 20px;" valign="top" width="25%">Update:</td>
<td style="font-size: 0; line-height: 0" width="5%"></td>
<td style="color: #13161c; font-family: Helvetica, Arial, sans-serif; font-size: 12px; line-height: 20px;" valign="top" width="70%"><b>$Update$</b></td>
</tr>
</table>
</td>
</tr>
$endforeach Items$
<tr>
<td style="color: #13161c; font-family: Helvetica, Arial, sans-serif; font-size: 12px; line-height: 20px; padding: 10px 0 0 0;">
Sincerely,
<br/>
$ContactPerson$
<br/>
<em style="font-size: 11px; line-height: 11px;">For Nordic Trustee (NO)</em>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td bgcolor="13161c" style="padding: 30px 30px 30px 30px;">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td style="color: #f7f8f6; font-family: Helvetica, Arial, sans-serif; font-size: 10px; line-height: 12px; padding: 10px 0 10px 0;">
Priveleged/confidential information may be contained in this message. If you are not the addressee indicated in this message (or responsible for delivery of the message to such person), you may not copy or deliver this message to anyone. In such case, you should destroy this message and kindly notify the sender for example by reply e-mail.
</td>
</tr>
<tr>
<td style="color: #f7f8f6; font-family: Helvetica, Arial, sans-serif; font-size: 10px;">2019 © Nordic Trustee</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
<!--[if (gte mso 9)|(IE)]>
</td>
</tr>
</table>
<![endif]-->
</body>
</html>