OpenSource » SampleCode » Brail2Razor
Clone URL:  
Pushed to one repository · View In Graph Contained in tip

Uses a line by line parsing now

Changeset 278f31c1e627

Parent 9627d2d6628a

by Profile picture of Daniel PupekDaniel Pupek

Changes to 2 files · Browse files at 278f31c1e627 Showing diff from parent 9627d2d6628a Diff from another changeset...

 
31
32
33
 
 
 
34
35
36
 
31
32
33
34
35
36
37
38
39
@@ -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
140
141
 
142
143
144
 
191
192
193
 
 
194
195
196
 
277
278
279
280
 
 
 
281
282
283
284
285
286
 
287
288
289
290
291
 
292
293
294
 
295
296
297
298
299
 
300
301
 
302
303
304
305
 
306
307
308
 
 
 
 
309
310
311
312
313
314
315
316
317
318
319
320
 
321
322
 
 
323
324
325
 
 
 
 
 
 
 
 
326
327
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328
 
 
 
 
 
 
 
 
 
 
 
 
329
330
331
332
 
 
333
334
335
336
 
 
 
 
 
337
338
339
340
341
342
343
344
345
346
347
348
 
349
350
351
352
 
 
 
353
354
 
355
356
 
357
358
359
360
361
 
 
362
363
364
365
366
 
367
368
369
 
139
140
141
142
143
144
145
 
192
193
194
195
196
197
198
199
 
280
281
282
 
283
284
285
286
287
288
289
290
 
291
292
 
293
294
 
295
296
297
 
298
299
300
301
302
 
303
304
 
305
306
307
308
309
310
311
 
 
312
313
314
315
316
 
 
 
 
 
 
 
 
 
 
 
317
318
 
319
320
321
322
 
323
324
325
326
327
328
329
330
331
 
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
 
 
 
370
371
372
 
 
 
373
374
375
376
377
378
 
 
 
 
 
 
 
 
 
379
 
380
381
 
 
 
382
383
384
385
 
386
387
 
388
389
390
391
 
 
392
393
394
395
396
397
 
398
399
400
401
@@ -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;   }   }