|
| 1 | +using System.Text.RegularExpressions; |
| 2 | +using BlazorWebFormsComponents.Cli.Pipeline; |
| 3 | + |
| 4 | +namespace BlazorWebFormsComponents.Cli.Transforms.Markup; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Converts ASP.NET Web Forms server-side code blocks to Razor control-flow syntax. |
| 8 | +/// Handles if/else/foreach/for/while statement blocks and bare code blocks. |
| 9 | +/// Also sanitizes any residual <% %> delimiters found inside @* *@ Razor comments |
| 10 | +/// (which ExpressionTransform produced from <%-- --%> comments). |
| 11 | +/// Must run after ExpressionTransform (500) and before AspPrefixTransform (610). |
| 12 | +/// </summary> |
| 13 | +public class ServerCodeBlockTransform : IMarkupTransform |
| 14 | +{ |
| 15 | + public string Name => "ServerCodeBlock"; |
| 16 | + public int Order => 510; |
| 17 | + |
| 18 | + // Match @* ... *@ Razor comments so we can sanitize inner <% %> remnants |
| 19 | + private static readonly Regex RazorCommentRegex = new( |
| 20 | + @"(?s)@\*(.*?)\*@", |
| 21 | + RegexOptions.Compiled); |
| 22 | + |
| 23 | + // Match <% ... %> statement blocks — excludes <%#, <%:, <%=, <%-- |
| 24 | + private static readonly Regex StatementBlockRegex = new( |
| 25 | + @"(?s)<%(?![#:=\-])(.*?)%>", |
| 26 | + RegexOptions.Compiled); |
| 27 | + |
| 28 | + public string Apply(string content, FileMetadata metadata) |
| 29 | + { |
| 30 | + // Step 1: neutralize <% %> inside @* *@ Razor comments so Razor doesn't choke. |
| 31 | + // ExpressionTransform already converted <%-- --%> to @* *@, but the inner |
| 32 | + // <% %> fragments survived as raw text. |
| 33 | + content = RazorCommentRegex.Replace(content, m => |
| 34 | + { |
| 35 | + var inner = m.Groups[1].Value |
| 36 | + .Replace("<%", "[%") |
| 37 | + .Replace("%>", "%]"); |
| 38 | + return $"@*{inner}*@"; |
| 39 | + }); |
| 40 | + |
| 41 | + // Step 2: convert remaining statement blocks to Razor control-flow. |
| 42 | + content = StatementBlockRegex.Replace(content, m => |
| 43 | + TransformStatementBlock(m.Groups[1].Value)); |
| 44 | + |
| 45 | + return content; |
| 46 | + } |
| 47 | + |
| 48 | + private static string TransformStatementBlock(string rawInner) |
| 49 | + { |
| 50 | + // Collapse all whitespace (including newlines from multi-line blocks) to single spaces. |
| 51 | + var normalized = Regex.Replace(rawInner.Trim(), @"\s+", " ").Trim(); |
| 52 | + |
| 53 | + // <% } %> — closing brace only |
| 54 | + if (Regex.IsMatch(normalized, @"^\}\s*$")) |
| 55 | + return "}"; |
| 56 | + |
| 57 | + // <% } else if (condition) { %> |
| 58 | + var elseIfMatch = Regex.Match(normalized, |
| 59 | + @"^\}\s*else\s+if\s*(\(.*\))\s*\{?\s*$", RegexOptions.IgnoreCase); |
| 60 | + if (elseIfMatch.Success) |
| 61 | + return "}\nelse if " + elseIfMatch.Groups[1].Value + "\n{"; |
| 62 | + |
| 63 | + // <% } else { %> |
| 64 | + if (Regex.IsMatch(normalized, @"^\}\s*else\s*\{?\s*$", RegexOptions.IgnoreCase)) |
| 65 | + return "}\nelse\n{"; |
| 66 | + |
| 67 | + // <% foreach (...) { %> |
| 68 | + var foreachCond = TryExtractCondition(normalized, "foreach"); |
| 69 | + if (foreachCond != null) |
| 70 | + return "@foreach " + foreachCond + "\n{"; |
| 71 | + |
| 72 | + // <% for (...) { %> |
| 73 | + var forCond = TryExtractCondition(normalized, "for"); |
| 74 | + if (forCond != null) |
| 75 | + return "@for " + forCond + "\n{"; |
| 76 | + |
| 77 | + // <% while (...) { %> |
| 78 | + var whileCond = TryExtractCondition(normalized, "while"); |
| 79 | + if (whileCond != null) |
| 80 | + return "@while " + whileCond + "\n{"; |
| 81 | + |
| 82 | + // <% if (...) { %> — checked after foreach/for/while to avoid false matches |
| 83 | + var ifCond = TryExtractCondition(normalized, "if"); |
| 84 | + if (ifCond != null) |
| 85 | + return "@if " + ifCond + "\n{"; |
| 86 | + |
| 87 | + // Bare statement block: <% code; %> |
| 88 | + return "@{ " + normalized + " }"; |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Tries to extract the parenthesised condition after a control-flow keyword. |
| 93 | + /// Returns the condition string (including outer parens) or null if no match. |
| 94 | + /// </summary> |
| 95 | + private static string? TryExtractCondition(string normalized, string keyword) |
| 96 | + { |
| 97 | + if (!normalized.StartsWith(keyword, StringComparison.OrdinalIgnoreCase)) |
| 98 | + return null; |
| 99 | + |
| 100 | + // Ensure the keyword is not a prefix of a longer identifier |
| 101 | + if (normalized.Length > keyword.Length) |
| 102 | + { |
| 103 | + var next = normalized[keyword.Length]; |
| 104 | + if (char.IsLetterOrDigit(next) || next == '_') |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + var rest = normalized.Substring(keyword.Length).TrimStart(); |
| 109 | + |
| 110 | + // Condition must start with '(' |
| 111 | + if (!rest.StartsWith("(")) |
| 112 | + return null; |
| 113 | + |
| 114 | + // Strip trailing ' {' or '{' when the opening brace was on the same line |
| 115 | + rest = rest.TrimEnd(); |
| 116 | + if (rest.EndsWith("{")) |
| 117 | + rest = rest[..^1].TrimEnd(); |
| 118 | + |
| 119 | + return rest.Trim(); |
| 120 | + } |
| 121 | +} |
0 commit comments