[Test] public void TestExpandHtml() { var filepath = Path.GetTempFileName(); var data = Encoding.UTF8.GetBytes("include file\r\ntext data"); // Shared read access doesn't work for DeleteOnClose files so might as well just use a normal file and delete on clean up using (var file = new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.Read)) file.Write(data, 0, data.Length); using (Scope.Create(null, () => Path_.DelFile(filepath))) { var template = $"<root>\r\n" + $"\t\t <!--#include file=\"{filepath}\"-->\r\n" + $"<!--#var name=\"MyVar\" file=\"{filepath}\" value=\"text\\s+(?<value>\\w+)\"-->\r\n" + $" <!--#value name=\"MyVar\"-->\r\n" + $" <!--#value name=\"MyVar\"-->\r\n" + $"</root>\r\n"; const string result = "<root>\r\n" + "\t\t include file\r\n" + "\t\t text data\r\n" + "\r\n" + " data\r\n" + " data\r\n" + "</root>\r\n"; var r = Expand.Html(new StringSrc(template), Environment.CurrentDirectory); Assert.Equal(result, r); } }
/// <summary>Generate the code-behind</summary> protected override byte[] GenerateCode(string src_filepath, string src) { try { var current_dir = Path.GetDirectoryName(src_filepath) ?? string.Empty; var result = Expand.Html(new StringSrc(src), current_dir); return(Encoding.UTF8.GetBytes(result)); } catch (Exception ex) { var msg = "HTML Expansion failed" + Environment.NewLine + "Exception:" + Environment.NewLine + ex.Message; return(Encoding.UTF8.GetBytes(msg)); } }