Ken Kopczyk

Hurdles in .NET Development

Posts Tagged ‘pastebin’

Displaying Source Code in a WordPress Post

Posted by Ken on February 1, 2009

While setting up this blog, I came upon my first challenge:  How can you display source code in a WordPress post?

There are many solutions to this problem which include various WordPress.org plug-ins.  The problem with the plug-ins is that they don’t work with freebee WordPress.com blogs.  The simplest solution that I found for WordPress.com blogs was to simply wrap the code you want to post in “sourcecode” tags and let javascript take care of the rest. Implementing this is easy because you don’t have to worry about a big blob of HTML. All that is required is the code you want to paste and the javascript tags. Example:

static void Main(string[] args)
{
      Console.Out.WriteLine("Hello World!");
      return;
}

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;
using System.IO;
using System.Text.RegularExpressions;
 
namespace PasteBinCodeScrubber
{
    class Program
    {
        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;
 
                // doctor the html
                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;);
            return sourceCodeHtml;
        }
 
        private static string ScrubHtml(string sourceCodeHtml)
        {
            // add font size
            sourceCodeHtml = sourceCodeHtml.Replace(“<div class=csharp style=font-family: monospace;>”,
                                                    “<div style=font-family:monospace; font-size:1.3em;>”);
 
            // remove list tags
            sourceCodeHtml = sourceCodeHtml.Replace(“<ol>”, string.Empty);
            sourceCodeHtml = sourceCodeHtml.Replace(“</ol>”, string.Empty);
            sourceCodeHtml = sourceCodeHtml.Replace(“<li “, “<div “);
            sourceCodeHtml = sourceCodeHtml.Replace(“</li>”, “</div>”);
 
            // remove hyperlinks
            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);
            }
 
            return sourceCodeHtml;
        }
    }
}

Bookmark and Share

Posted in WordPress | Tagged: , | 4 Comments »