Code highlighting integration for BlogEngine.NET 1.5.x

by William 16. August 2009 16:32

I decided to integrate some code highlighting into my blog rather than taking screen captures of my code and posting those into the blog.  As I found this requires integrating a code inserting plug-in for TinyMCE 3.x to insert code into the editor and format it properly and syntax highlighting extension for the actual posted code.  The two I chose were:

David Pokluda’s Windows Live Writer Source Code Plug-in found here. I simply took the BlogEngine.NET bits from the download and followed the instructions to install it.  This download also included Alex Gorbatchev’s syntax highlighter - v. 2.0.296 – found here.  The idea is there is a BlogEngine.NET extension that writes the SyntaxHighlighter js code to the pages.

And the code inserting plug-in for TinyMCE v3.x I got from Nawaf’s Blog found here.

I had to do some tweaking to get the TinyMCE plug-in to work the way I wanted.  By default the plug-in wraps the code in a HTML textarea along with some attributes (settings) in the class attribute.  I really don’t like that approach (having a textarea in a post and or the TinyMCE editor when editing).  So I ended up changing the plug-in code to output a HTML pre tag and mapping some of the settings from the plug-in to the settings of the syntax highlighter code since Alex Gorbatchev’s code looks for a pre tag anyway.

The changes I made to the plug-in were in the codehighlighting\js\codehighlighting.js file and these are the changes I made:

function Save_Button_onclick() {
    var lang = document.getElementById("ProgrammingLangauges").value;
    var code = WrapCode(lang);
    code = code + document.getElementById("CodeArea").value;
    code = code + "</pre>";
    if (document.getElementById("CodeArea").value == '') 
    {
        tinyMCEPopup.close();
        return false;
    }
    tinyMCEPopup.execCommand('mceInsertContent', false, code);
    tinyMCEPopup.close();
}

function WrapCode(lang) {

    var options = "auto-links: true; first-line: 1; light: false; ruler: false; smart-tabs: true; tab-size: 4;brush: " + lang + ";";
    var html = "";

    if (lang == 'js' || lang == 'jscript' || lang == 'javascript')
        options = options + "html-script: true;";
    else 
        options = options + "html-script: false;";
        
    if (document.getElementById("nogutter").checked == true)
        options = options + "gutter: false;";

    if (document.getElementById("collapse").checked == true)
        options = options + "collapse: true;";

    if (document.getElementById("nocontrols").checked == true)
        options = options + "toolbar: false;";

    html = "<pre class='" + options + "'>";

    return html;
}

function Cancel_Button_onclick() {
    tinyMCEPopup.close();
    return false;
}

As you can see most of the changes were to the WrapCode function (if you compare the original to my version side by side).  Other than that nothing really needed to be done to the syntax highlighter code or the BlogEngine.NET extension.

Instructions for installation can be found on each respective website.

Note: There is a bit of weirdness with the TinyMCE plug-in where if there is HTML tags in your code you will need to manually encode them, I am not sure what is going on but I find it's not that much of a big deal.

Enjoy

Tags: , , ,

Web Site

Comments

8/27/2009 6:44:23 PM #

Cirrus Hugger Fan

It sounds like you're creating problems yourself by trying to solve this issue instead of looking at why their is a problem in the first place.

Excellently written article, if only all bloggers offered the same content as you, the internet would be a much better place. Please keep it up!

Hmm strange this post is totaly irrelevant to the search query I entered in google but it was listed on the first page.

I spend my time on working about the coverage of website program because i want to create my new blog site using the latest version of blogengine. Finally i am on searching what topic and what application design to my new blog.

Yeah. As a normal user, I look for ease of use plus something which takes me there again and again. Functionality is the basic thing because without it no user would even visit it again. It sets very bad image if the website has problems. Web Development is not only creative but tough as well. You've to constantly look whether there's a new error coming up or not. Whether every part of the site is working right.

I'm still do not know how to do that with the helps by video i think it will good. but  thanks for sharing

Cirrus Hugger Fan United States

10/5/2009 1:48:50 PM #

Detroit Divorce Attorney

Valuable information and excellent design you got here! I would like to thank you for sharing your thoughts and time into the stuff you post!! Thumbs up

Detroit Divorce Attorney United States

11/7/2009 8:28:35 PM #

Nardu

Hey, great blog, but I don’t understand how to add your site in my rss reader. Can you Help me please?

Nardu United States

11/9/2009 12:07:35 PM #

Labradoodle

Keep 'em coming... you all do such a great job at such Concepts... can't tell you how much I, for one appreciate all you do!

Labradoodle United States

Comments are closed

Page List