public TextTransformation ProcessTemplate(
string sourceText,
IDictionary<string, string> parameters,
bool removeFileHeaders,
bool removeXmlComments,
bool removeThisPointer)
{
TraceService.WriteLine("SimpleTextTemplatingEngine::ProcessTemplate");
TextTransformation textTransformation = new TextTransformation();
//// first remove the this pointer if required
if (removeThisPointer)
{
sourceText = sourceText.Replace("this.", string.Empty);
}
//// now remove all the parameter definitions!
string[] lines = sourceText.Split('\n');
//// by default make it a standard csharp source file!
textTransformation.FileExtension = "cs";
string extensionLine = lines.FirstOrDefault(x => x.StartsWith("<#@ Output Extension="));
if (string.IsNullOrEmpty(extensionLine) == false)
{
///// <#@ Output Extension="cs" #>
string[] parts = extensionLine.Split('"');
if (parts.Length > 1)
{
textTransformation.FileExtension = parts[1];
}
}
IEnumerable<string> newLines = lines.Where(x => x.StartsWith("<#@ ") == false);
if (removeFileHeaders)
{
newLines = newLines.Where(x => x.TrimStart().StartsWith("//") == false);
}
if (removeXmlComments)
{
newLines = newLines.Where(x => x.TrimStart().StartsWith("///") == false);
}
string output = string.Empty;
foreach (string newLine in newLines)
{
output += newLine + "\n";
}
if (parameters != null)
{
foreach (KeyValuePair<string, string> parameter in parameters)
{
string t4Parameter = $"<#= {parameter.Key} #>";
output = output.Replace(t4Parameter, parameter.Value);
}
}
//// sort out single LF and replace with CR LF!
textTransformation.Output = Regex.Replace(output, @"\r\n|\r|\n", "\r\n");
return textTransformation;
}