protected override async Task OnInitializedAsync() { DocExamples.AddCodeSnippet(this); if (!string.IsNullOrWhiteSpace(ClassName) && string.IsNullOrEmpty(Code)) { var formatter = new HtmlClassFormatter(); var html = (await CodeSnippetService.GetCodeSnippet(ClassName)).Trim(); var cSharp = ""; var index = html.IndexOf("@code {"); if (index > 0) { cSharp = html.Substring(index); html = html.Substring(0, index); } var code = formatter.GetHtmlString(html, Languages.Html); if (!string.IsNullOrWhiteSpace(cSharp)) { code = code + @" <div class=""mb-1""> </div> " + formatter.GetHtmlString(cSharp, Languages.CSharp); } code = HighlightRazor(code); Code = code; } }
private static void CreateHilitedCode(string doc_path) { var formatter = new HtmlClassFormatter(); foreach (var entry in Directory.EnumerateFiles(doc_path, "*.razor", SearchOption.AllDirectories) .OrderBy(e => e.Replace("\\", "/"), StringComparer.Ordinal)) { if (entry.EndsWith("Code.razor")) { continue; } var filename = Path.GetFileName(entry); if (!filename.Contains(ExampleDiscriminator)) { continue; } //var component_name = Path.GetFileNameWithoutExtension(filename); var markup_path = entry.Replace("Examples", "Code").Replace(".razor", "Code.razor"); var markup_dir = Path.GetDirectoryName(markup_path); if (!Directory.Exists(markup_dir)) { Directory.CreateDirectory(markup_dir); } //Console.WriteLine("Found code snippet: " + component_name); var src = StripComponentSource(entry); var blocks = src.Split("@code"); var blocks0 = Regex.Replace(blocks[0], @"</?DocsFrame>", "") .Replace("@", "PlaceholdeR") .Trim(); // Note: the @ creates problems and thus we replace it with an unlikely placeholder and in the markup replace back. var html = formatter.GetHtmlString(blocks0, Languages.Html).Replace("PlaceholdeR", "@"); html = AttributePostprocessing(html).Replace("@", "<span class=\"atSign\">@</span>"); using (var f = File.Open(markup_path, FileMode.Create)) using (var w = new StreamWriter(f) { NewLine = "\n" }) { w.WriteLine("@* Auto-generated markup. Any changes will be overwritten *@"); w.WriteLine("@namespace MudBlazor.Docs.Examples.Markup"); w.WriteLine("<div class=\"mud-codeblock\">"); w.WriteLine(html.ToLfLineEndings()); if (blocks.Length == 2) { w.WriteLine( formatter.GetHtmlString("@code" + blocks[1], Languages.CSharp) .Replace("@", "<span class=\"atSign\">@</span>") .ToLfLineEndings() ); } w.WriteLine("</div>"); w.Flush(); } } }
private static void Main(string[] args) { if (args.Contains("--concurrent")) { var tasks = new Task[100]; for (int i = 0; i < 100; i++) { tasks[i] = Task.Run(() => { var code = "public void Method()\n{\n}"; var formatter = new HtmlFormatter(); var html = formatter.GetHtmlString(code, Languages.CSharp); Console.WriteLine(html); }); } Task.WaitAll(tasks); } else { var csharpstring = "public void Method()\n{\n}"; var formatter = new HtmlClassFormatter(); var html = formatter.GetHtmlString(csharpstring, Languages.CSharp); var css = formatter.GetCSSString(); Console.WriteLine("Original:"); Console.WriteLine(csharpstring); Console.WriteLine("HTML:"); Console.WriteLine(html); Console.ReadKey(); } }
protected override void Write(HtmlRenderer renderer, CodeBlock obj) { FencedCodeBlock fencedCodeBlock = obj as FencedCodeBlock; FencedCodeBlockParser parser = obj.Parser as FencedCodeBlockParser; if (fencedCodeBlock == null || parser == null) { _underlyingRenderer.Write(renderer, obj); return; } var languageMoniker = fencedCodeBlock.Info.Replace(parser.InfoPrefix, string.Empty); if (string.IsNullOrEmpty(languageMoniker)) { _underlyingRenderer.Write(renderer, obj); return; } string firstLine; var code = GetCode(obj, out firstLine); Console.WriteLine(formatter.GetCSSString()); renderer.WriteLine(formatter.GetHtmlString(code, Languages.FindById(languageMoniker))); }
private static void Main(string[] args) { var csharpstring = "public void Method()\n{\n}"; var formatter = new HtmlClassFormatter(); var html = formatter.GetHtmlString(csharpstring, Languages.CSharp); var css = formatter.GetCSSString(); Console.WriteLine("Original:"); Console.WriteLine(csharpstring); Console.WriteLine("HTML:"); Console.WriteLine(html); Console.ReadKey(); }
protected override void Write(HtmlRenderer renderer, CodeInline obj) { if (obj is CodeInline) { var text = obj.Content; var formatter = new HtmlClassFormatter(); var html = formatter.GetHtmlString(text, Languages.CSharp); html = html. Replace("<pre>", "<code>"). Replace("</pre>", "</code>"). Replace("div style=\"color:#000000;background-color:#FFFFFF;\"", "pre class=\"csharp\""). Replace("</div>", "</pre>"). Replace("<div class=\"csharp\"", "<pre class=\"csharp\""); _ = renderer.Write(html); } }
protected override void Write(HtmlRenderer renderer, CodeBlock obj) { if (obj is FencedCodeBlock) { var text = GetText(obj); var formatter = new HtmlClassFormatter(); var lang = Languages.CSharp; var html = formatter.GetHtmlString(text, lang); html = html. Replace("<pre>", "<code class=\"codeblock\">"). Replace("</pre>", "</code>"). Replace("div style=\"color:#000000;background-color:#FFFFFF;\"", "pre class=\"csharp\""). Replace("</div>", "</pre>"). Replace("<div class=\"csharp\"", "<pre class=\"csharp\""). Replace(" ", "<span style=\"display:inline-block;margin-left:2.5em;\"></span>").Replace("\r\n", "<br>"); _ = renderer.Write(html); } else if (obj is CodeBlock) { var text = GetText(obj); var formatter = new HtmlClassFormatter(); var html = formatter.GetHtmlString(text, Languages.CSharp); html = html. Replace("<pre>", "<code class=\"codeblock\">"). Replace("</pre>", "</code>"). Replace("div style=\"color:#000000;background-color:#FFFFFF;\"", "pre class=\"csharp\""). Replace("</div>", "</pre>"). Replace("<div class=\"csharp\"", "<pre class=\"csharp\""). Replace(" ", "<span style=\"display:inline-block;margin-left:2.5em;\"></span>"); _ = renderer.Write(html); } }
public bool Execute() { var paths = new Paths(); var newFiles = new StringBuilder(); var success = true; var noOfFilesUpdated = 0; var noOfFilesCreated = 0; try { var formatter = new HtmlClassFormatter(); var lastCheckedTime = new DateTime(); if (File.Exists(paths.NewFilesToBuildPath)) { var lastNewFilesToBuild = new FileInfo(paths.NewFilesToBuildPath); lastCheckedTime = lastNewFilesToBuild.LastWriteTime; } var directoryInfo = new DirectoryInfo(paths.DocsDirPath); foreach (var entry in directoryInfo.GetFiles("*.razor", SearchOption.AllDirectories)) { if (entry.Name.EndsWith("Code.razor")) { continue; } if (!entry.Name.Contains(Paths.ExampleDiscriminator)) { continue; } var markupPath = entry.FullName.Replace("Examples", "Code").Replace(".razor", "Code.html"); if (entry.LastWriteTime < lastCheckedTime && File.Exists(markupPath)) { continue; } var markupDir = Path.GetDirectoryName(markupPath); if (!Directory.Exists(markupDir)) { Directory.CreateDirectory(markupDir); } var src = StripComponentSource(entry.FullName); var blocks = src.Split("@code"); var blocks0 = Regex.Replace(blocks[0], @"</?DocsFrame>", string.Empty) .Replace("@", "PlaceholdeR") .Trim(); // Note: the @ creates problems and thus we replace it with an unlikely placeholder and in the markup replace back. var html = formatter.GetHtmlString(blocks0, Languages.Html).Replace("PlaceholdeR", "@"); html = AttributePostprocessing(html).Replace("@", "<span class=\"atSign\">@</span>"); var currentCode = string.Empty; if (File.Exists(markupPath)) { currentCode = File.ReadAllText(markupPath); } var cb = new CodeBuilder(); // cb.AddLine("@* Auto-generated markup. Any changes will be overwritten *@"); // cb.AddLine("@namespace MudBlazor.Docs.Examples.Markup"); cb.AddLine("<div class=\"mud-codeblock\">"); cb.AddLine(html.ToLfLineEndings()); if (blocks.Length == 2) { cb.AddLine( formatter.GetHtmlString("@code" + blocks[1], Languages.CSharp) .Replace("@", "<span class=\"atSign\">@</span>") .ToLfLineEndings()); } cb.AddLine("</div>"); if (currentCode != cb.ToString()) { File.WriteAllText(markupPath, cb.ToString()); if (currentCode == string.Empty) { newFiles.AppendLine(markupPath); noOfFilesCreated++; } else { noOfFilesUpdated++; } } } File.WriteAllText(paths.NewFilesToBuildPath, newFiles.ToString()); } catch (Exception e) { Console.WriteLine($"Error generating examples markup : {e.Message}"); success = false; } Console.WriteLine($"Docs.Compiler updated {noOfFilesUpdated} generated files"); Console.WriteLine($"Docs.Compiler generated {noOfFilesCreated} new files"); return(success); }
public bool Execute() { var paths = new Paths(); bool success = true; try { var formatter = new HtmlClassFormatter(); foreach (var entry in Directory.EnumerateFiles(paths.DocsDirPath, "*.razor", SearchOption.AllDirectories) .OrderBy(e => e.Replace("\\", "/"), StringComparer.Ordinal)) { if (entry.EndsWith("Code.razor")) { continue; } var filename = Path.GetFileName(entry); if (!filename.Contains(Paths.ExampleDiscriminator)) { continue; } var markupPath = entry.Replace("Examples", "Code").Replace(".razor", "Code.razor"); var markupDir = Path.GetDirectoryName(markupPath); if (!Directory.Exists(markupDir)) { Directory.CreateDirectory(markupDir); } var src = StripComponentSource(entry); var blocks = src.Split("@code"); var blocks0 = Regex.Replace(blocks[0], @"</?DocsFrame>", string.Empty) .Replace("@", "PlaceholdeR") .Trim(); // Note: the @ creates problems and thus we replace it with an unlikely placeholder and in the markup replace back. var html = formatter.GetHtmlString(blocks0, Languages.Html).Replace("PlaceholdeR", "@"); html = AttributePostprocessing(html).Replace("@", "<span class=\"atSign\">@</span>"); string currentCode = string.Empty; if (File.Exists(markupPath)) { currentCode = File.ReadAllText(markupPath); } var cb = new CodeBuilder(); cb.AddLine("@* Auto-generated markup. Any changes will be overwritten *@"); cb.AddLine("@namespace MudBlazor.Docs.Examples.Markup"); cb.AddLine("<div class=\"mud-codeblock\">"); cb.AddLine(html.ToLfLineEndings()); if (blocks.Length == 2) { cb.AddLine( formatter.GetHtmlString("@code" + blocks[1], Languages.CSharp) .Replace("@", "<span class=\"atSign\">@</span>") .ToLfLineEndings()); } cb.AddLine("</div>"); if (currentCode != cb.ToString()) { File.WriteAllText(markupPath, cb.ToString()); } } } catch (Exception e) { Console.WriteLine($"Error generating examples markup : {e.Message}"); success = false; } return(success); }