Skip to content

Showing Code In A Wordpress Blog

Updated: at 06:34 PM

For years, I’ve wrestled (and failed) at using code formatting plugins. I use Windows Live Writer to do my blog posts on WordPress and though there are several plugins available for Live Writer, I really don’t like any of them (please correct me if you know of a great one and I’ll post that).

Today, I’m trying a new plugin called Crayon that does a great job but is a little cumbersome to use.  Let me describe my workflow now. If you have a better suggestion for how to use it, please let me know.

The first thing to do is install the plugin. You can find it at the URL: https://github.com/aramk/crayon-syntax-highlighter or simply search for the plugin “crayon” and have wordpress install it for you automatically (like I did).

Once crayon is activated, create a dummy post (not published) and in visual mode add the lines C# Start and C# End with some empty space between them (as follows):

image

Next, press the “Insert Code” button and in that dialog put in some code as well as tell the dialog the type and anything else you want (you can see what I did highlighted in yellow).

image

Then of course, press the “Add” button in the upper right.

How, since you want to put this into Live Writer, you’ll need to cut and paste from the “text” view in the wordpress editor the code between the “pre” tags as follows:

image

and put that into Live Writer in the Source tab:

image

And, as you can see below, in Live Writer, you just see the source (in edit tab), but when you preview it, well, that becomes a very nice story.

image

And, as you can see the result below, quite nice!

It would be nice if this where a plugin to live writer!

// look for first good price (effective now price). If none found, then assume retail price
for (int index = 0; index < sessionPriceList.Count; index++)
{
    var sessionPrice = sessionPriceList[index];
    if (sessionPrice.PriceGoodThroughDate.CompareTo(DateTime.Now) > 0)
    {
        session.CurrentPrice = sessionPrice.PriceOfSession.ToString("F2");
        if (index < sessionPriceList.Count)
        {
            var nextOne = sessionPriceList[index + 1];
            session.NextPriceDate = nextOne.PriceGoodThroughDate.ToString("d");
            session.NextPrice = nextOne.PriceOfSession.ToString("F2");
            session.DaysUntilNextPrice =
                sessionPrice.PriceGoodThroughDate.Subtract(DateTime.Now).Days;
        }
    // confirm that we have number at this slot (if null, then always stop at this price)
    if (!sessionPrice.MaxAtThisPrice.HasValue || session.CurrentAttendance &lt; sessionPrice.MaxAtThisPrice) {
            break;
    }

}

}