Changeset 278f31c1e627…
Parent 9627d2d6628a…
by Daniel Pupek
Changes to 2 files · Browse files at 278f31c1e627 Showing diff from parent 9627d2d6628a Diff from another changeset...
@@ -31,6 +31,9 @@ <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
+ <PropertyGroup>
+ <StartupObject>Brail2Razor.Brail2RazorProgram</StartupObject>
+ </PropertyGroup>
<ItemGroup>
<Reference Include="FluentCommandLineParser, Version=1.4.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\FluentCommandLineParser.1.4.3\lib\net35\FluentCommandLineParser.dll</HintPath>
|
|
|
@@ -139,6 +139,7 @@
private static SearchOption _allDirs;
private static bool _insideElement;
+ private static List<string> _imports;
static void Main(string[] args)
@@ -191,6 +192,8 @@
private static void BrailConvert(string brailPath, string outPath)
{
+ _imports = new List<string>();
+
Console.WriteLine("Converting file {0}", brailPath);
string code;
var razorOut = new StringBuilder("");
@@ -277,93 +280,122 @@ previosChar = currentChar;
}
- File.WriteAllText(outPath, razorOut.ToString());
+ //Add the imports
+ var imports = String.Join(Environment.NewLine, _imports.Select(i => "@" + i + ";").ToArray());
+ File.WriteAllText(outPath, imports + Environment.NewLine + razorOut.ToString());
}
private static string ConvertBrailBlockToRazor(string brail)
{
- var result = "";
+ var finalResult = "";
-
if (brail.Trim().Equals("end"))
{
- result += "}";
+ finalResult += "}";
if (_insideElement)
- result = "</text>" + result;
+ finalResult = "</text>" + finalResult;
}
else if (brail.Trim().Equals("else:"))
{
- result += "}else{";
+ finalResult += "}else{";
if (_insideElement)
- result = "</text>" + result + "<text>";
+ finalResult = "</text>" + finalResult + "<text>";
}
else
{
//All others here
+ var lines = brail.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
- result += TernaryBlockRegex.Replace(brail, "($2 ? $1 : $3)");
- result = AndNotRazorTernaryRegex.Replace(result, " && !($1)");
+ // One line at a time
+ foreach (var line in lines)
+ {
+ var lineResult = line.Trim();
- result = OutputSubViewRegex.Replace(result, "OutputSubView($1,new $2)");
-
- result = result.Replace("realSlimShady", "SiteRoot");
-
- if (IfBlockRegex.IsMatch(result) || ForBlockRegEx.IsMatch(result))
- {
-
- result = IfBlockRegex.Replace(result, "if($1){");
- result = ForBlockRegEx.Replace(result, "foreach(var $1 in $2){");
-
- while (FixHangingEndRegex.IsMatch(result))
+ if (lineResult.StartsWith("import"))
{
- result = FixHangingEndRegex.Replace(result, "$1 }");
+ _imports.Add("using " + lineResult.Substring(6));
+ continue;
}
- while (AndNotRazorIfRegex.IsMatch(result))
+ lineResult = TernaryBlockRegex.Replace(lineResult, "($2 ? $1 : $3)");
+ lineResult = AndNotRazorTernaryRegex.Replace(lineResult, " && !($1)");
+
+ lineResult = OutputSubViewRegex.Replace(lineResult, "Html.RenderPartial($1,new ViewDataDictionary( new $2 ))");
+
+ lineResult = lineResult.Replace("realSlimShady", "SiteRoot");
+
+ if (IfBlockRegex.IsMatch(lineResult) || ForBlockRegEx.IsMatch(lineResult))
{
- result = AndNotRazorIfRegex.Replace(result, " && !($1)");
+
+ lineResult = IfBlockRegex.Replace(lineResult, "if($1){");
+ lineResult = ForBlockRegEx.Replace(lineResult, "foreach(var $1 in $2){");
+
+ while (FixHangingEndRegex.IsMatch(lineResult))
+ {
+ lineResult = FixHangingEndRegex.Replace(lineResult, "$1 }");
+
+ }
+
+ while (AndNotRazorIfRegex.IsMatch(lineResult))
+ {
+ lineResult = AndNotRazorIfRegex.Replace(lineResult, " && !($1)");
+ }
+
+ while (AndInIfBlockRazorRegex.IsMatch(lineResult))
+ {
+ lineResult = AndInIfBlockRazorRegex.Replace(lineResult, " && ");
+
+ }
+ if (_insideElement)
+ lineResult = lineResult + "<text>";
+
+
}
+ //Order is VERY IMPORTANT HERE
+ lineResult = CleanAnonymousTypesRegex.Replace(lineResult, "$1 = ");
+ lineResult = EscapeQuotesInAposStringsreRegex.Replace(lineResult, "\\\"");
+ lineResult = ReplaceApostropheRegex.Replace(lineResult, "\"$1\"");
+ lineResult = NotIsNullRegex.Replace(lineResult, " $1 != null ");
+ lineResult = FixStandaloneNotRegex.Replace(lineResult, "!($1)");
+ lineResult = lineResult.Replace(" is null", " == null");
+
+ lineResult = lineResult.Replace("ViewHelper.TableStripe()", "new HtmlTableStripePainter()");
+
+ if(lineResult.Trim() == "end")
+ lineResult = lineResult.Replace("end", "}");
- while (AndInIfBlockRazorRegex.IsMatch(result))
- {
- result = AndInIfBlockRazorRegex.Replace(result, " && ");
+ finalResult += lineResult + Environment.NewLine;
+ }
- }
- if (_insideElement)
- result = result + "<text>";
+ finalResult = finalResult.Trim();
+
+ if (string.IsNullOrWhiteSpace(finalResult))
+ {
+ return "";
}
- //Order is VERY IMPORTANT HERE
- result = CleanAnonymousTypesRegex.Replace(result, "$1 = ");
- result = EscapeQuotesInAposStringsreRegex.Replace(result, "\\\"");
- result = ReplaceApostropheRegex.Replace(result, "\"$1\"");
- result = NotIsNullRegex.Replace(result, " $1 != null ");
- result = FixStandaloneNotRegex.Replace(result, "!($1)");
- result = result.Replace(" is null", " == null");
- result = result.Replace("import ", "using ");
- result = result.Replace("ViewHelper.TableStripe()", "new HtmlTableStripePainter()");
- if(NewLineRegex.IsMatch(result))
+ if (NewLineRegex.IsMatch(finalResult))
{
- result = result.Trim();
- if (result[0] == '@')
- result = result.Substring(1);
+ finalResult = finalResult.Trim();
+ if (finalResult[0] == '@')
+ finalResult = finalResult.Substring(1);
- result = AddSemiColonsRegex.Replace(result, "$1;\n");
+ finalResult = AddSemiColonsRegex.Replace(finalResult, "$1;\n");
- result = string.Format("@{{{0}}}", result);
+ finalResult = string.Format("@{{{0}}}", finalResult);
}
else
{
- result = "@" + result.Trim();
-
+ finalResult = "@" + finalResult.Trim();
+
}
}
- return result;
+ return finalResult;
}
}
|
Loading...