private void GenerateExample <TRoom>(IExampleGrid2D <TRoom> example) { AssetsFolder = Path.Combine(outputFolder, example.Options.DocsFileName); Directory.CreateDirectory(AssetsFolder); var className = example.GetType().Name; sourceCodeParser = new SourceCodeParser(Path.Combine(sourceFolder, $"{className}.cs")); var sourceCode = sourceCodeParser.GetMethod(example.Options.EntryPointMethod, false); var stringBuilder = new StringBuilder(); stringBuilder.AppendLine("---"); stringBuilder.AppendLine($"title: {example.Options.Name}"); stringBuilder.AppendLine("---"); stringBuilder.AppendLine(); stringBuilder.AppendLine("import { Gallery, GalleryImage } from \"@theme/Gallery\";"); stringBuilder.AppendLine(); // stringBuilder.AppendLine($"> The source code for this example can be found at the end of this page."); stringBuilder.AppendLine(); AddContent(sourceCode, stringBuilder); File.WriteAllText(Path.Combine(outputFolder, $"{example.Options.DocsFileName}.md"), stringBuilder.ToString()); if (example.Options.IncludeResults) { AddResults(stringBuilder, example); } if (example.Options.IncludeSourceCode) { AddSourceCode(stringBuilder); } Console.WriteLine(stringBuilder.ToString()); File.WriteAllText(Path.Combine(outputFolder, $"{example.Options.DocsFileName}.md"), stringBuilder.ToString()); example.Run(); }
private List <string> GetSourceCode(IExample example, string methodName) { var className = example.GetType().Name; var sourceCode = new List <string>(); var allLines = File.ReadAllLines(Path.Combine(sourceFolder, $"{className}.cs")); var insideMainMethod = false; var syntaxTree = CSharpSyntaxTree.ParseText(File.ReadAllText(Path.Combine(sourceFolder, $"{className}.cs"))); var root = syntaxTree.GetRoot() as CompilationUnitSyntax; var namespaceSyntax = root.Members.OfType <NamespaceDeclarationSyntax>().First(); var programClassSyntax = namespaceSyntax.Members.OfType <ClassDeclarationSyntax>().First(); var mainMethodSyntax = programClassSyntax.Members.OfType <MethodDeclarationSyntax>().First(); Console.WriteLine(mainMethodSyntax.ToString()); //var regionNodesList = new List<RegionNodes>(); //foreach (var regionDirective in root.DescendantTrivia() // .Where(i => i.Kind() == SyntaxKind.RegionDirectiveTrivia)) // regionNodesList.Add(new RegionNodes {RegionDirective = regionDirective}); //var count = regionNodesList.Count; //foreach (var endRegionDirective in root.DescendantTrivia() // .Where(j => j.Kind() == SyntaxKind.EndRegionDirectiveTrivia)) // regionNodesList[--count].EndRegionDirective = endRegionDirective; //foreach (var node in root.DescendantNodes() // .Where(i => i is MemberDeclarationSyntax || i is StatementSyntax)) //foreach (var regionNodes in regionNodesList) // regionNodes.AddNode(node); //foreach (var regionNode in regionNodesList) //{ // Console.WriteLine(); // var test = root.ToFullString(); // var test2 = root.GetText(); // var test3 = test2.GetSubText(regionNode.RegionSpan); // Console.WriteLine(test3.ToString()); //} Console.WriteLine(); // get the region trivia var regions = root .DescendantNodes(descendIntoTrivia: true) .OfType <RegionDirectiveTriviaSyntax>() .ToList(); foreach (var region in regions) { var regionText = region.ToString().Replace("#region ", ""); Console.WriteLine(regionText); Console.WriteLine(region.ToFullString()); var test = root.ToFullString(); var test2 = root.GetText(); var test3 = test2.GetSubText(region.Span); Console.WriteLine(); } var sourceCodeParser = new SourceCodeParser(Path.Combine(sourceFolder, $"{className}.cs")); for (var i = 0; i < allLines.Length; i++) { var line = allLines[i]; if (line.Contains(methodName) && line.Contains("public")) { insideMainMethod = true; if (!line.Contains("{")) { i++; } } else if (line.StartsWith(" }") || line.StartsWith(" return")) { insideMainMethod = false; } else if (insideMainMethod) { if (line.StartsWith(" ")) { line = line.Remove(0, " ".Length); } sourceCode.Add(line); } } return(sourceCode); }