Changeset 1bf50d1b9bfd…
by Daniel Pupek
Changes to 14 files · Browse files at 1bf50d1b9bfd Diff from another changeset...
|
@@ -0,0 +1,22 @@ +
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio 2013
+VisualStudioVersion = 12.0.40629.0
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Brail2Razor", "Brail2Razor\Brail2Razor.csproj", "{BC253A2F-F52C-4C4D-8952-FC3DCFFBBCD2}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {BC253A2F-F52C-4C4D-8952-FC3DCFFBBCD2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {BC253A2F-F52C-4C4D-8952-FC3DCFFBBCD2}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {BC253A2F-F52C-4C4D-8952-FC3DCFFBBCD2}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {BC253A2F-F52C-4C4D-8952-FC3DCFFBBCD2}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+EndGlobal
|
|
@@ -0,0 +1,6 @@ + <?xml version="1.0" encoding="utf-8" ?>
+<configuration>
+ <startup>
+ <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
+ </startup>
+</configuration>
\ No newline at end of file |
|
@@ -0,0 +1,63 @@ + <?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProjectGuid>{BC253A2F-F52C-4C4D-8952-FC3DCFFBBCD2}</ProjectGuid>
+ <OutputType>Exe</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>Brail2Razor</RootNamespace>
+ <AssemblyName>Brail2Razor</AssemblyName>
+ <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <PlatformTarget>AnyCPU</PlatformTarget>
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <PlatformTarget>AnyCPU</PlatformTarget>
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </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>
+ <Private>True</Private>
+ </Reference>
+ <Reference Include="System" />
+ <Reference Include="System.Core" />
+ <Reference Include="System.Xml.Linq" />
+ <Reference Include="System.Data.DataSetExtensions" />
+ <Reference Include="Microsoft.CSharp" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="Brail2RazorProgram.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="App.config" />
+ <None Include="packages.config" />
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>
\ No newline at end of file |
|
|
@@ -0,0 +1,376 @@ + using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Text.RegularExpressions;
+using System.Threading.Tasks;
+using Fclp;
+
+namespace Brail2Razor
+{
+ class Brail2RazorProgram
+ {
+ private static string _directory = null;
+ private static string _extension = "brail";
+
+ public static Regex IfBlockRegex = new Regex(
+ "if\\s([^:{}@%]+)\\s*\\:",
+ RegexOptions.Singleline
+ | RegexOptions.CultureInvariant
+ | RegexOptions.Compiled
+ );
+
+ public static Regex ForBlockRegEx = new Regex(
+ "for\\s([^:]+)\\s+in\\s+([^:]+)\\:",
+ RegexOptions.Singleline
+ | RegexOptions.CultureInvariant
+ | RegexOptions.Compiled
+ );
+
+ public static Regex TernaryBlockRegex = new Regex(
+ "\\(\\s*((?:\\w|[().])+)\\s+if\\s+(.+)\\s+else\\s+(.+)\\s*\\)",
+ RegexOptions.Singleline
+ | RegexOptions.CultureInvariant
+ | RegexOptions.Compiled
+ );
+
+ public static Regex OutputSubViewRegex = new Regex(
+ "OutputSubView\\s*\\(\\s*(\\\"[^\"]*\\\"\\s*)\\,\\s*(\\{[^}]*" +
+ "\\})\\s*\\)",
+ RegexOptions.Singleline
+ | RegexOptions.CultureInvariant
+ | RegexOptions.Compiled
+ );
+
+ /// <summary>
+ /// Finds "and not XXXX' in a ternary
+ /// </summary>
+ public static Regex AndNotRazorTernaryRegex = new Regex(
+ "\\s+and\\s+not\\s+([^?]*)(?=\\s+\\?)",
+ RegexOptions.Singleline
+ | RegexOptions.CultureInvariant
+ | RegexOptions.Compiled
+ );
+ /// <summary>
+ /// Finds "and not" statements in razor IF blocks and tries to fix it
+ /// </summary>
+ public static Regex AndNotRazorIfRegex = new Regex(
+ "\\s+and\\s+not\\s+([^{]*)(?=\\s+(?:\\)\\s*)+{)",
+ RegexOptions.Singleline
+ | RegexOptions.CultureInvariant
+ | RegexOptions.Compiled
+ );
+
+ public static Regex AndInIfBlockRazorRegex = new Regex(
+ "\\s+(and)\\s+(?=[^{]*(?:\\)\\s*)+\\{)",
+ RegexOptions.Multiline
+ | RegexOptions.Singleline
+ | RegexOptions.CultureInvariant
+ | RegexOptions.Compiled
+ );
+
+ public static Regex NewLineRegex = new Regex(
+ "\\n", RegexOptions.Multiline |
+ RegexOptions.Singleline
+ | RegexOptions.CultureInvariant
+ | RegexOptions.Compiled
+ );
+
+ public static Regex FixHangingEndRegex = new Regex(
+ "((?:if|foreach)\\s*\\([^{]+\\)\\s*\\{.*)end",
+ RegexOptions.Multiline
+ | RegexOptions.Singleline
+ | RegexOptions.CultureInvariant
+ | RegexOptions.Compiled
+ );
+
+ public static Regex AddSemiColonsRegex = new Regex(
+ @"([^{};\r]+)(?=\r)",
+ RegexOptions.Multiline
+ | RegexOptions.Singleline
+ | RegexOptions.CultureInvariant
+ | RegexOptions.Compiled
+ );
+
+ public static Regex CleanAnonymousTypesRegex = new Regex(
+ "(?<=new\\s*{[^}]*)(?:\\'(?<1>[^']*)\\'|\\\"(?<1>[^']*)\\\")\\s" +
+ "*\\:",
+ RegexOptions.Multiline
+ | RegexOptions.Singleline
+ | RegexOptions.CultureInvariant
+ | RegexOptions.Compiled
+ );
+
+ public static Regex ReplaceApostropheRegex = new Regex(
+ "\\'([^\\r']*)\\'",
+ RegexOptions.Multiline
+ | RegexOptions.Singleline
+ | RegexOptions.CultureInvariant
+ | RegexOptions.Compiled
+ );
+
+ public static Regex EscapeQuotesInAposStringsreRegex = new Regex(
+ "(?<=\\'[^\\']*)[\"]+(?=[^'\\r]*\\')",
+ RegexOptions.Multiline
+ | RegexOptions.Singleline
+ | RegexOptions.CultureInvariant
+ | RegexOptions.Compiled
+ );
+
+ public static Regex NotIsNullRegex = new Regex(
+ "not\\s+([^{&|\\r]*)\\s+is\\s+null",
+ RegexOptions.Multiline
+ | RegexOptions.Singleline
+ | RegexOptions.CultureInvariant
+ | RegexOptions.Compiled
+ );
+
+ public static Regex FixStandaloneNotRegex = new Regex(
+ "(?<=(?:if|\\()\\s*[^}]*)not\\s+([^&!|{:]+)",
+ RegexOptions.Multiline
+ | RegexOptions.Singleline
+ | RegexOptions.CultureInvariant
+ | RegexOptions.Compiled
+ );
+
+
+
+
+ private static SearchOption _allDirs;
+ private static bool _insideElement;
+
+
+ static void Main(string[] args)
+ {
+ var p = new FluentCommandLineParser();
+
+ p.SetupHelp("?", "help")
+ .Callback(text => Console.WriteLine(text));
+
+ p.Setup<string>('d', "directory")
+ .WithDescription("The directory to scan for brail files.")
+ .Callback(dir => _directory = dir)
+ .Required();
+
+ p.Setup<string>('e', "extension")
+ .WithDescription("The brail file extension. The default is .brail")
+ .Callback(ext => _extension = ext.StartsWith(".") ? ext.Substring(1) : ext)
+ .SetDefault("brail");
+
+ p.Setup<bool>('s', "subdirs")
+ .WithDescription("If set then the converter will include sub directories.")
+ .Callback(include => _allDirs = include ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
+
+
+ var result = p.Parse(args);
+
+ if (result.HasErrors)
+ {
+ Console.WriteLine(result.ErrorText);
+
+ Console.WriteLine();
+ p.HelpOption.ShowHelp(p.Options);
+
+ return;
+ }
+ if (result.HelpCalled)
+ {
+ return;
+ }
+
+ Console.WriteLine("Starting brail to razor conversion of directory {0}", _directory);
+
+ var files = System.IO.Directory.GetFiles(_directory, "*." + _extension, _allDirs);
+
+ foreach (var file in files)
+ {
+ BrailConvert(file, Path.ChangeExtension(file, "cshtml"));
+ }
+ }
+
+ private static void BrailConvert(string brailPath, string outPath)
+ {
+ Console.WriteLine("Converting file {0}", brailPath);
+ string code;
+ var razorOut = new StringBuilder("");
+ using (var brailStream = File.OpenRead(brailPath))
+ {
+ var brailReader = new StreamReader(brailStream);
+ code = brailReader.ReadToEnd();
+ }
+
+ char? previosChar = null;
+ CodeLocationEnum codeLocation = CodeLocationEnum.NormalText;
+ var currentCodeBlock = new StringBuilder("");
+
+
+ for (int i = 0; i < code.Length; i++)
+ {
+ char currentChar = code[i];
+
+ //Write Output
+ switch (codeLocation)
+ {
+ case CodeLocationEnum.NormalText:
+ razorOut.Append(currentChar);
+ if ( _insideElement && currentChar == '>')
+ {
+ //left the element
+ _insideElement = false;
+ }
+ break;
+ case CodeLocationEnum.BrailBlock:
+ currentCodeBlock.Append(currentChar);
+ break;
+ case CodeLocationEnum.PropertyBlock:
+ currentCodeBlock.Append(currentChar);
+ break;
+ default:
+ throw new ArgumentOutOfRangeException();
+ }
+
+ if (previosChar.HasValue)
+ {
+ var testString = previosChar.Value.ToString() + currentChar.ToString();
+ switch (testString)
+ {
+ case "<%":
+ //Remove the <% from the normal output
+ razorOut.Remove(razorOut.Length - 2, 2);
+ codeLocation = CodeLocationEnum.BrailBlock;
+ break;
+ case "%>":
+ //Get rid of the extra stuff
+ currentCodeBlock.Remove(currentCodeBlock.Length - 2,2);
+ string converted = ConvertBrailBlockToRazor(currentCodeBlock.ToString());
+ razorOut.Append(converted);
+ currentCodeBlock.Clear();
+ codeLocation = CodeLocationEnum.NormalText;
+ break;
+ case "${":
+ case "!{":
+ //Remove the !{ from the normal output
+ razorOut.Remove(razorOut.Length - 2, 2);
+ codeLocation= CodeLocationEnum.PropertyBlock;
+ break;
+
+ }
+ if (codeLocation == CodeLocationEnum.PropertyBlock && currentChar == '}')
+ {
+ //Get rid of the extra stuff
+ currentCodeBlock.Remove(currentCodeBlock.Length - 1, 1);
+ string converted = "@" + currentCodeBlock.ToString().Trim();
+ razorOut.Append(converted);
+ currentCodeBlock.Clear();
+ codeLocation = CodeLocationEnum.NormalText;
+ }
+
+ if (previosChar == '<' && codeLocation == CodeLocationEnum.NormalText)
+ {
+ // INSIDE AN ELEMENT
+ _insideElement = true;
+ }
+
+ }
+
+ previosChar = currentChar;
+ }
+
+ File.WriteAllText(outPath, razorOut.ToString());
+
+ }
+
+ private static string ConvertBrailBlockToRazor(string brail)
+ {
+ var result = "";
+
+
+ if (brail.Trim().Equals("end"))
+ {
+ result += "}";
+
+ if (_insideElement)
+ result = "</text>" + result;
+
+ }
+ else if (brail.Trim().Equals("else:"))
+ {
+ result += "}else{";
+ if (_insideElement)
+ result = "</text>" + result + "<text>";
+ }
+ else
+ {
+ //All others here
+
+ result += TernaryBlockRegex.Replace(brail, "($2 ? $1 : $3)");
+ result = AndNotRazorTernaryRegex.Replace(result, " && !($1)");
+
+ 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))
+ {
+ result = FixHangingEndRegex.Replace(result, "$1 }");
+ }
+
+ while (AndNotRazorIfRegex.IsMatch(result))
+ {
+ result = AndNotRazorIfRegex.Replace(result, " && !($1)");
+ }
+
+ while (AndInIfBlockRazorRegex.IsMatch(result))
+ {
+ result = AndInIfBlockRazorRegex.Replace(result, " && ");
+
+ }
+ if (_insideElement)
+ result = result + "<text>";
+ }
+ //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))
+ {
+ result = result.Trim();
+ if (result[0] == '@')
+ result = result.Substring(1);
+
+ result = AddSemiColonsRegex.Replace(result, "$1;\n");
+
+ result = string.Format("@{{{0}}}", result);
+ }
+ else
+ {
+ result = "@" + result.Trim();
+
+ }
+
+ }
+
+ return result;
+ }
+ }
+
+ internal enum CodeLocationEnum
+ {
+ NormalText,
+ BrailBlock,
+ PropertyBlock
+ }
+}
|
|
@@ -0,0 +1,36 @@ + using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("Brail2Razor")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("Brail2Razor")]
+[assembly: AssemblyCopyright("Copyright © 2015")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible
+// to COM components. If you need to access a type in this assembly from
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("84108f95-fb53-45d5-bd7c-863222ddb650")]
+
+// Version information for an assembly consists of the following four values:
+//
+// Major Version
+// Minor Version
+// Build Number
+// Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
|
|
@@ -0,0 +1,4 @@ + <?xml version="1.0" encoding="utf-8"?>
+<packages>
+ <package id="FluentCommandLineParser" version="1.4.3" targetFramework="net45" />
+</packages>
\ No newline at end of file |
|
|
|
|
This file's diff was not loaded because this changeset is very large. Load changes Loading... |
|
||
---|---|---|
This file's diff was not loaded because this changeset is very large. Load changes Loading... |
|
|
|
|
|
||
---|---|---|
This file's diff was not loaded because this changeset is very large. Load changes Loading... |
|
@@ -0,0 +1,4 @@ + <?xml version="1.0" encoding="utf-8"?>
+<repositories>
+ <repository path="..\Brail2Razor\packages.config" />
+</repositories>
\ No newline at end of file |
Loading...