mBerube.net
A journey to .Net

Integrate SyntaxHighlighter to BlogEngine.Net

Monday, 5 October 2009 23:22 by mBerube
(Also available in: français)

I was writing my first technical post (about asp.net MVC) and I realized that I had no way to display code snippets properly. It's inadmissible for a programming blog to not have that kind of feature. I started looking for a tool and I quickly found SyntaxHighlighter, an open-source product written in javascript and widely used on the Net. Documentation is clear and installation seems simple. It has been a little more complex that advertised but I've been able to integrate it in BE.Net with a little work.

Download SyntaxHighlighter

By going to the "official" site, we can download version 1.5. But, on the author personal site, we can download the new 2.0 version. I choose this one. The package contains some css and javascripts required for that tool to work properly. To get the formatting, you just have to wrap your code in a <PRE> tag with a class corresponding to the language to display. Ex :

<pre class="brush: html;">
<b>bold text</b>
</pre>

The scripts work very well, it's when you try to include it in BE.NET that the fun begins ! BE.Net use many URL rewriting rules, so it's difficult to refer the script relatively to the current page so we must do the reference to the application root.  The first replex is to put runat="server" on the scripts and to use tilde (˜) to translate to the application root. Unfortunately, it doesn't work because with that tag, the javascript code is evaluated by the compiler and some of the code in SH have errors (or at least, synx that VS2008 doesn't like. I also tried to integrate so code into the markup (ex: <%= SiteRoot() %>/Scripts/shcore.js) but it doesn't work either because BE.Net tried to add Meta tags at runtime, throwing this exception.

I finally rely on the ScriptManager, that help for 2 things : I can refer my scripts using the tilde (˜) now that it's a server control and I can at the same time combine my scripts for better performance.

<asp:ScriptManager ID="ScriptManager1" runat="server">
    <CompositeScript>
        <Scripts>
            <asp:ScriptReference Path="~/Scripts/shCore.js"/>
            <asp:ScriptReference Path="~/Scripts/shBrushBash.js"/>
            <asp:ScriptReference Path="~/Scripts/shBrushCpp.js"/>
            <asp:ScriptReference Path="~/Scripts/shBrushCSharp.js"/>
            <asp:ScriptReference Path="~/Scripts/shBrushCss.js"/>
            <asp:ScriptReference Path="~/Scripts/shBrushDelphi.js"/>
            <asp:ScriptReference Path="~/Scripts/shBrushDiff.js"/>
            <asp:ScriptReference Path="~/Scripts/shBrushJava.js"/>
            <asp:ScriptReference Path="~/Scripts/shBrushJScript.js"/>
            <asp:ScriptReference Path="~/Scripts/shBrushPlain.js"/>
            <asp:ScriptReference Path="~/Scripts/shBrushPython.js"/>
            <asp:ScriptReference Path="~/Scripts/shBrushRuby.js"/>
            <asp:ScriptReference Path="~/Scripts/shBrushPerl.js"/>
            <asp:ScriptReference Path="~/Scripts/shBrushSql.js"/>
            <asp:ScriptReference Path="~/Scripts/shBrushVb.js"/>
            <asp:ScriptReference Path="~/Scripts/shBrushXml.js"/>
            <asp:ScriptReference Path="~/Scripts/shBrushJavaFX.js"/>
            <asp:ScriptReference Path="~/Scripts/shBrushPowerShell.js"/>
        </Scripts>
    </CompositeScript>
</asp:ScriptManager>

The drawback of using the ScriptManager is that my implementation of BE.Net isn't totally comptible with .Net 2.0 anymore but how cares, .Net 4 is about to be release so we must live in the present so welcome .Net 3.5 !

Integration into the blog editor

BE.Net 1.5 useTinyMCE 3 to edit blog posts. The editor has a WYSIWYG mode but also an HTML mode. It's possible to generate the required markup to integrate code into posts but somes opertions still highly manual and error prone like convert special characters, configuring options, etc. So I ask Google and I found someone has already solve that problem and developped a plugin SyntaxHighlighter for Tiny MCE. It wasn't working at first but just a little tweeking and we're done ! Also, the character encoding wasn't done properly but a little search in StackOverflow and everything is all right.

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

function HtmlEncode(s) {
    var el = document.createElement("div");
    el.innerText = el.textContent = s;
    s = el.innerHTML;
    delete el;
    return s;
}

Good ! Eveything works and now that I finishing reading Professional ASP.NET MVC 1.0, I've many ideas to explore and share with you.

See you next time

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories:   BlogEngine.NET
Actions:   E-mail | del.icio.us | Permalink | Comments (25) | Comment RSSRss comment feed

Comments

19 March 2010 19:16

mBerube

The main advantage is that it's in .Net so if you're a .Net, programmer, you can customize the engine like you want.

mBerube qc

16 March 2011 03:49

cheese

i came here from aol. after reading�rather good ideas om your blog. i bookmark it.

cheese Greenland

16 March 2011 07:24

katie

Oh my goodness! an amazing article dude. Thank you However I am experiencing issue with ur rss . Don�t know why Unable to subscribe to it. Is there anyone getting identical rss problem? Anyone who knows kindly respond. Thnkx

katie Italy

17 March 2011 16:23

forex

bedroom decorating help? i just moved into a new apartment and i get everything done expect for my bedroom. i choose the color which is beher's peacock feather blue with white trimming and a white ceiling. my only trouble very soon , is that i'm having a hard time decorate it.

forex Singapore

17 March 2011 16:35

ufo videos

any of you guys play pc games?��if so i don't suppose any of you have been looking into blizzard's new rts?

ufo videos Venezuela

13 April 2011 03:17

cheese

i am very impressed with your writing skills and also with the design of your site. is this a paid theme or did you edit it yourself? either way keep up the great quality writing, it is not often to see a nice blog such as this one nowadays!!

cheese Lithuania

13 April 2011 13:37

science

updating with new feature and design makes this blog very interesting. feel good to be here. i have bookmarked for further visit.

science Dominican Republic

15 April 2011 04:57

Colorado springs homes for sale

bedroom decorating help? i just moved into a new apartment and i get everything done expect for my bedroom. i choose the color which is beher's peacock feather blue with white trimming and a white ceiling. my only trouble very soon , is that i'm having a hard time decorate it.

Colorado springs homes for sale United States

11 May 2011 02:54

杭州酒吧

杭州酒吧街(hz98j.com)提供杭州地区最专业的杭州酒吧网,提供丰富的酒吧消费指南、酒吧人才、酒吧音乐、夜生活、夜店、杭州交友酒吧、杭州演艺酒吧和休闲娱乐资讯信息

杭州酒吧 People's Republic of China

27 May 2011 15:57

Radian Car Seats

Could you email me with a few hints about how you made this website look this good , I would be appreciative. Wow! This blog site is cool! How do you  make it look this good !? and nice Post but where is the link to tweet your post.

Radian Car Seats Malaysia

29 September 2011 11:31

英文URL外链收割机

新版本URL外链收割机收割思路
1、根据别人的某个站,找他的外链发布地址,找出它是在哪里发的外链;
或根据URL和页面的特征,找针对性页,比如blogengine类型BLOG的评论页,留言本,论坛帖子页等;
2、去分析这些收割来的地址,
(1)看看是否dofollow(2)是否是可以发布的页(3)出链数多少以及(4)谷歌是否收录(5)页面的pr值;
3、这些已经是可以用的外链发布页了,再拿这些页去找,都有哪些站在这里发过外链 (其实又找到了很多 所谓的别人的站 就又可以进行第一步开始的循环)
最终就是找到大量的可以发布外链的地址!

英文URL外链收割机 People's Republic of China

29 September 2011 11:31

尼龙扎带

温州尼龙扎带,温州防水电缆接头,温州防水接线盒厂,扎带固定座报价

尼龙扎带 People's Republic of China

29 September 2011 11:31

尼龙扎带

尼龙扎带首选乐清市创新机械有限公司,专业尼龙扎带厂家,产品规格齐全,品质优异。

尼龙扎带 People's Republic of China

30 September 2011 21:55

John

Wow , what an informative blog. Keep up the good work!

John

05 October 2011 10:41

英文外链

新版本URL外链收割机收割思路
1、根据别人的某个站,找他的外链发布地址,找出它是在哪里发的外链;
或根据URL和页面的特征,找针对性页,比如blogengine类型BLOG的评论页,留言本,论坛帖子页等;
2、去分析这些收割来的地址,
(1)看看是否dofollow(2)是否是可以发布的页(3)出链数多少以及(4)谷歌是否收录(5)页面的pr值;
3、这些已经是可以用的外链发布页了,再拿这些页去找,都有哪些站在这里发过外链 (其实又找到了很多 所谓的别人的站 就又可以进行第一步开始的循环)
最终就是找到大量的可以发布外链的地址!

英文外链 People's Republic of China

05 October 2011 10:41

尼龙扎带

尼龙扎带首选乐清市创新机械有限公司,专业尼龙扎带厂家,产品规格齐全,品质优异。

尼龙扎带 People's Republic of China

14 November 2011 10:16

Johnny H

Great delivery. Solid arguments. Keep up the amazing effort.

Johnny H United States

11 January 2012 06:44

Blair Lickliter

I downloaded the jailbreak but the Custom Packages are out of date. Is there somewhere else to download the Custom Packages?

Blair Lickliter United States

18 January 2012 06:17

Carley Golbin

Are these copywrited? May my band lift some of this for lyrics?

Carley Golbin United States

02 February 2012 15:57

Crysta Vanvalkenburg

Wow! Haven't seen this one in ages  looks better than ever. Thanks so much for the hard work and for posting the results. I love the very early Fleischer shorts  they're so unselfconscious and unpretentious  just wacky and whimsical drawing.

Crysta Vanvalkenburg United States

04 February 2012 08:11

Paco

Hola,

Muy bueno el post, guay

Paco

19 February 2012 08:45

Citra Fernandes

Very gooooooood

Citra Fernandes United States

01 March 2012 06:17

Facebook Fans

Hi, I appreciate the information that you have provided in the post related to Facebook Fans. It is worth nothing and I really liked the presentation as well. I will surely come back for more of interesting posts.

Facebook Fans United States

14 April 2012 06:11

Pregnancy Yoga Benefits

Hi, I must say that you have made some good points about Pregnancy Yoga Benefits in the post. I performed searches on this topic and found most people will agree with your blog. Thanks for sharing this information.

Pregnancy Yoga Benefits United States

20 April 2012 01:40

Java Training In Noida

I really enjoyed your blog. I just bookmarked it. I am a regular visitor of your website I will share It with my friends .Thanks.

Java Training In Noida India

Add comment




  Country flag

biuquote
Click to change captcha
  • Comment
  • Preview
Loading