While setting up this blog, I came upon my first challenge: How can you display source code in a WordPress post?
Being the perfectionist that I am, I wanted to see if I could get the code to look closer to the default fonts and colors used in Visual Studio. Thanks to this posting, I discovered pastebin.com. It does a nice job of converting source code into html. And it has syntax highlighting for most languages, including C#. To streamline the process, I wrote a parser. This code will parse the full source of the pastebin output, yank out the relevent html, and add style to make it look close to Visual Studio. The following source code is a solution to the problem and also a demonstration. Which method do you think looks better?
using System.Text.RegularExpressions;
namespace PasteBinCodeScrubber
static void Main(string[] args)
FileInfo[] allHtmlFiles = new DirectoryInfo(“.”).GetFiles(“*.htm*”);
foreach (FileInfo htmlFile in allHtmlFiles)
string allHtml = File.ReadAllText(htmlFile.FullName);
// capture all relevent html
Regex sourceCodeRegex = new Regex(“<div class=“csharp“ style=“font-family: monospace;“><ol>[\s\S]*?</ol></div>”);
string sourceCodeHtml = sourceCodeRegex.Match(allHtml).Value;
sourceCodeHtml = ScrubHtml(sourceCodeHtml);
sourceCodeHtml = ReplaceCssClassWithStyle(sourceCodeHtml);
File.WriteAllText(htmlFile.FullName + “.out”, sourceCodeHtml);
private static string ReplaceCssClassWithStyle(string sourceCodeHtml)
sourceCodeHtml = sourceCodeHtml.Replace(“class=“de1““, string.Empty);
sourceCodeHtml = sourceCodeHtml.Replace(“class=“de2““, string.Empty);
sourceCodeHtml = sourceCodeHtml.Replace(“class=“inp““, “style=“color: red;““);
sourceCodeHtml = sourceCodeHtml.Replace(“class=“li1““, “style=“background: #ffffff;““);
sourceCodeHtml = sourceCodeHtml.Replace(“class=“li2““, “style=“background: #f4f4f4;““);
sourceCodeHtml = sourceCodeHtml.Replace(“class=“kw1““, “style=“color: #0600FF;““);
sourceCodeHtml = sourceCodeHtml.Replace(“class=“kw2““, “style=“color: #FF8000;““);
sourceCodeHtml = sourceCodeHtml.Replace(“class=“kw3““, “style=“color: #0600FF;““);
sourceCodeHtml = sourceCodeHtml.Replace(“class=“kw4““, “style=“color: #0600FF;““);
sourceCodeHtml = sourceCodeHtml.Replace(“class=“kw5““, “style=“color: #000000;““);
sourceCodeHtml = sourceCodeHtml.Replace(“class=“co1““, “style=“color: green;““);
sourceCodeHtml = sourceCodeHtml.Replace(“class=“co2““, “style=“color: green;““);
sourceCodeHtml = sourceCodeHtml.Replace(“class=“coMULTI““, “style=“color: green;““);
sourceCodeHtml = sourceCodeHtml.Replace(“class=“es0““, “style=“color: #008080;““);
sourceCodeHtml = sourceCodeHtml.Replace(“class=“br0““, “style=“color: #008000;““);
sourceCodeHtml = sourceCodeHtml.Replace(“class=“st0““, “style=“color: maroon;““);
sourceCodeHtml = sourceCodeHtml.Replace(“class=“nu0““, “style=“color: #FF0000;““);
sourceCodeHtml = sourceCodeHtml.Replace(“class=“me1““, “style=“color: #0000FF;““);
sourceCodeHtml = sourceCodeHtml.Replace(“class=“me2““, “style=“color: #0000FF;““);
private static string ScrubHtml(string sourceCodeHtml)
sourceCodeHtml = sourceCodeHtml.Replace(“<div class=“csharp“ style=“font-family: monospace;“>”,
“<div style=“font-family:monospace; font-size:1.3em;“>”);
sourceCodeHtml = sourceCodeHtml.Replace(“<ol>”, string.Empty);
sourceCodeHtml = sourceCodeHtml.Replace(“</ol>”, string.Empty);
sourceCodeHtml = sourceCodeHtml.Replace(“<li “, “<div “);
sourceCodeHtml = sourceCodeHtml.Replace(“</li>”, “</div>”);
Regex hyperLinkRegex = new Regex(“<a href=.*?>(?’linkWrappedHtml'[\s\S]*?)</a>”, RegexOptions.ExplicitCapture);
MatchCollection hyperLinkMatches = hyperLinkRegex.Matches(sourceCodeHtml);
foreach (Match hyperLinkMatch in hyperLinkMatches)
string sHtmlInsideHyperLink = hyperLinkMatch.Groups[“linkWrappedHtml”].Value;
string sFullHyperLink = hyperLinkMatch.Value;
sourceCodeHtml = sourceCodeHtml.Replace(sFullHyperLink, sHtmlInsideHyperLink);