forked from RickStrahl/Westwind.Scripting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptHelper.cs
More file actions
138 lines (120 loc) · 4.58 KB
/
Copy pathScriptHelper.cs
File metadata and controls
138 lines (120 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace Westwind.Scripting
{
/// <summary>
/// Script Helper that is injected into the script as a global `Script` variable
///
/// To use:
///
/// {{ Script.RenderPartial("./test.template") }}
/// </summary>
public class ScriptHelper
{
/// <summary>
/// This the base path that's used for ~/ or / paths when using RenderTemplate
///
/// This value is null by default and if not set the current working directory
/// is used instead.
/// </summary>
public string BasePath { get; set; }
ScriptParser _parser = new ScriptParser();
/// <summary>
/// Renders a partial file into the template
/// </summary>
/// <param name="scriptPath">Path to script file to execute</param>
/// <param name="model">optional model to pass in</param>
/// <returns></returns>
public string RenderPartial(string scriptPath, object model = null)
{
var script = File.ReadAllText(scriptPath);
string result = _parser.ExecuteScript(script, model);
if (_parser.Error)
{
result = $"{{! Template error ({scriptPath}): " + _parser.ErrorMessage?.Trim() + " !}";
}
return result;
}
/// <summary>
/// Renders a partial file into the template
/// </summary>
/// <param name="scriptPath">Path to script file to execute</param>
/// <param name="model">optional model to pass in</param>
/// <returns></returns>
public async Task<string> RenderPartialAsync(string scriptPath, object model = null)
{
var script = await ReadFileAsync(scriptPath);
string result = await _parser.ExecuteScriptAsync(script, model);
if (_parser.Error)
{
result = "!! " + _parser.ErrorMessage + " !!";
}
return result;
}
/// <summary>
/// Renders a string of script to effectively allow recursive
/// rendering of content into a fixed template
/// </summary>
/// <param name="scriptPath">text or script to render</param>
/// <param name="model">optional model to pass in</param>
/// <returns></returns>
public string RenderScript(string script, object model = null)
{
//var parser = new ScriptParser()
//{
// ScriptEngine = _parser.ScriptEngine,
//};
string result = _parser.ExecuteScript(script, model);
return result;
}
/// <summary>
/// Renders a string of script to effectively allow recursive
/// rendering of content into a fixed template
/// </summary>
/// <param name="scriptPath">text or script to render</param>
/// <param name="model">optional model to pass in</param>
/// <returns></returns>
public async Task<string> RenderScriptAsync(string script, object model = null)
{
return await _parser.ExecuteScriptAsync(script, model);
}
/// <summary>
/// Reads the entire content of a file asynchronously
/// </summary>
/// <param name="filePath"></param>
/// <param name="encoding"></param>
/// <returns></returns>
private async Task<string> ReadFileAsync(string filePath, Encoding encoding = null)
{
filePath = FixBasePath(filePath);
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var reader = new StreamReader(fs))
{
return await reader.ReadToEndAsync();
}
}
/// <summary>
/// Reads the entire content of a file asynchronously
/// </summary>
/// <param name="filePath"></param>
/// <param name="encoding"></param>
/// <returns></returns>
private string ReadFile(string filePath, Encoding encoding = null)
{
filePath = FixBasePath(filePath);
return File.ReadAllText(filePath);
}
private string FixBasePath(string filePath)
{
if (!string.IsNullOrEmpty(BasePath))
{
if (filePath.StartsWith("~") || filePath.StartsWith("/") || filePath.StartsWith("\\"))
{
filePath = Path.Combine(BasePath, filePath.TrimStart('~', '/', '\\'));
}
}
return filePath;
}
}
}