forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptPreprocessors.cs
More file actions
82 lines (73 loc) · 2.69 KB
/
ScriptPreprocessors.cs
File metadata and controls
82 lines (73 loc) · 2.69 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
using ServiceStack.Text;
namespace ServiceStack.Script
{
public static class ScriptPreprocessors
{
public static string TransformCodeBlocks(string script)
{
var hadCodeBlocks = false;
var processed = StringBuilderCache.Allocate();
var inCodeBlock = false;
var inMultiLineBlock = false;
foreach (var line in script.ReadLines())
{
if (line == "```code")
{
hadCodeBlocks = true;
inCodeBlock = true;
continue;
}
if (inCodeBlock)
{
if (line == "```")
{
inCodeBlock = false;
continue;
}
var codeOnly = line.Trim();
if (string.IsNullOrEmpty(codeOnly))
continue;
if (codeOnly.StartsWith("{{"))
{
inMultiLineBlock = !codeOnly.EndsWith("}}");
processed.AppendLine(codeOnly);
continue;
}
if (inMultiLineBlock)
{
if (codeOnly.EndsWith("}}"))
{
inMultiLineBlock = false;
}
processed.AppendLine(codeOnly);
continue;
}
if (codeOnly.StartsWith("*") && !codeOnly.EndsWith("*"))
{
processed.AppendLine(codeOnly.Substring(1));
continue;
}
processed
.Append("{{")
.Append(codeOnly)
.AppendLine("}}");
continue;
}
processed.AppendLine(line);
}
if (inMultiLineBlock)
throw new SyntaxErrorException("Unterminated '}}' multi-line block not found");
if (inCodeBlock)
throw new SyntaxErrorException("Unterminated '```code' block, line with '```' not found");
if (hadCodeBlocks)
return StringBuilderCache.ReturnAndFree(processed);
// return original script if there were no code blocks
StringBuilderCache.Free(processed);
return script;
}
public static string TransformStatementBody(string body)
{
return TransformCodeBlocks("```code\n" + body + "\n```");
}
}
}