beautyOfCode – jQuery plugin for Syntax Highlighter 2.0 by Alex Gorbatchev

One of my most-visited posts is the jQuery plugin “beautyOfCode” for the version 1.5 of Alex’ Syntax Highlighter. The main point of doing a wrapper was, that version 1.5 did require invalid html code.

In the new version of Syntax Highlighter this is fixed. But still, people ask for a new version of my code, too.

I finally released a new version with some more features:

  • Uses online hosting of styles, scripts and brushes. This means you only need to reference jQuery and beautyOfCode!
  • Supports the new features as smart-tab, html-script, themes, line-wrapping and much more
  • Still uses a code-tag inside a pre-tag as well as a more css-like configuration with classes instead of property syntaxes

Referencing  the plugin and configuring brushes

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript" src="Scripts/jquery.beautyOfCode.js"></script>

<script type="text/javascript">
    $.beautyOfCode.init({
      brushes: ['Xml', 'JScript', 'CSharp', 'Plain', 'Php']
    }); 
</script>

Detection, default behaviour

By default, beautyOfCode will beautify all elements matching ‘pre.code:has(code[class])”:

<pre class="code">
  <code class="javasript">
    alert("Hello, World!");
  </code>
</pre>

Explicit

If auto detection is not desired, the plugin has to be initialized differently:

<script type="text/javascript">
    $.beautyOfCode.init({
      brushes: ['Xml', 'JScript'],
      ready: function() {
        $('#mycode').beautify('javascript');
      }
    }); 
</script>

This would then only beautify following code:

<pre id="mycode">
  <code>
    alert("Hello, World!");
  </code>
</pre>

Initialization Options

These are the default options, which can be overridden by $.jQuery.beautyOfCode.init:

settings: {
    // should the syntax highlighter and brushes
    // be loaded dynamically
    autoLoad: true,
    
    // the base url to alex' hosted sources
    // http://alexgorbatchev.com/wiki/SyntaxHighlighter:Hosting
    baseUrl: 'http://alexgorbatchev.com/pub/sh/2.0.320/',
    
    // the baseurl for the hosted scripts
    scripts: 'scripts/',
    
    // the baseurl for the hosted styles
    styles: 'styles/',
    
    // themes from http://alexgorbatchev.com/wiki/SyntaxHighlighter:Themes
    theme: 'Default',
    
    // the brushes that should be loaded - case sensitive!
    // http://alexgorbatchev.com/wiki/SyntaxHighlighter:Brushes
    brushes: ['Xml', 'JScript', 'CSharp', 'Plain'],
    
    // overrides for configurations and defaults
    // http://alexgorbatchev.com/wiki/SyntaxHighlighter:Configuration
    config: {},
    defaults: {},
    
    // function to be called, when all scripts are loaded
    ready: function() {
        jQuery.beautyOfCode.beautifyAll();
    }
}

Example with current version, Django-theme and no gutter

<script type="text/javascript">
    $.beautyOfCode.init({
        theme: 'Django',
        baseUrl: 'http://alexgorbatchev.com/pub/sh/current/'
        defaults: { gutter: false },
        brushes: ['Xml', 'JScript']
    }); 
</script>

Options

Find a reference for all options on SyntaxHighlighter Configuration.

The options can either be specified as defaults in init, on the beautify-call or as css classes.

Specify options in code:

$('#myCode').beautify('javascript', {'wrap-lines':true, 'first-line:2'});

Specify options in html:

In this case the gutter would not be shown and the lines 3 and 6 (5 and 8 in Listing) would be highlighted.

<pre class="code">
  <code class="javasript boc-no-gutter boc-highlight&#91;3,6&#93;">
    alert("Hello, World!");
    
    function x() {
        if (true)
            x();
    }    
  </code>
</pre>

Syntax for options in html:

  • The flags smart-tabs, ruler, gutter, toolbar, collapse, auto-links, wrap-lines and html-script mean true, then prefixed with boc-, and false, when prefixed with boc-no-.
  • class-name, first-line and tab-size are prefixed with boc- and followed by their values in square brackets. For example first-line[3] means, the gutter starts with a three.
  • highlight is also prefixed with a boc- and followed by a comma-separated list of line numbers in square brackets that are to be highlighted.

Sources

Find the plugin source code and a demo page on the bitbucked beautyOfCode project page. If you like the plugin, please also vote on the jQuey plugin site.

Feel free to submit issues and patches. The code is not very well tested, since I don’t use it in any production environment today.

Advertisement

Microsoft “Oslo” May CTP available (Including Quadrant)

The "Oslo” team has released the third CTP of Microsoft’s forthcoming modeling platform.

Some of the  changes are:

For more detailed information check out the team announcement and the release notes.

New Installer

oslo setup

Empty Quadrant Workspace

empty quadrant

Intellipad Splitting

intellipad-multisplit And yes, I’ll be blogging about my web-layout DSL 🙂

kick it on DotNetKicks.com

Architecture.NET Open Space 2009 (Germany)

Architecture.NET Open Space 2009

Am 5. und 6. Juni 2009 veranstaltet mein Arbeitgeber itemis zusammen mit Die Software-Architekten und SOPTIM einen Open Space mit dem Thema “Erprobte Konzepte für Unternehmensanwendungen in .NET”.

Die Zielgruppe sind erfahrene und angehende Softwarearchitekten.

Mehr Informationen und das Anmeldeformular gibts auf der Webseite zum Event: Architecture.NET @ mixxt

Die Idee zu diesem Event ist auf dem .NET Open Space 2008 in Leipzig entstanden.

Wir freuen uns Dein Erscheinen!

RT: History of Programming Languages

.

.

1996 – James Gosling invents Java. Java is a relatively verbose, garbage collected, class based, statically typed, single dispatch, object oriented language with single implementation inheritance and multiple interface inheritance. Sun loudly heralds Java’s novelty.

2001 – Anders Hejlsberg invents C#. C# is a relatively verbose, garbage collected, class based, statically typed, single dispatch, object oriented language with single implementation inheritance and multiple interface inheritance. Microsoft loudly heralds C#’s novelty.

.

.

From: One Div Zero: A Brief, Incomplete, and Mostly Wrong History of Programming Languages

The transaction has aborted. Tricky .NET TransactionScope Behavior.

This Error took me a bunch of time to find. Why? Well, I’m just not smart enough.

The code says everything:

[Test]
public void When_some_transaction_disposes_without_error_or_complete()
{
    // entering an ambient transaction
    new TransactionScope();

    // start a nested transaction
    var t2 = new TransactionScope();
    // no complete, no exception
    // cancels ambient transaction, too
    t2.Dispose();

    // creating a new scope fails
    Action createAScope = () => new TransactionScope();
    createAScope.ShouldThrow<TransactionAbortedException>()
            .Message.ShouldEqual("The transaction has aborted.");
}

In my case, t2 was opened somewhere else only when some conditions were given. To me, the exception seemed to occur randomly.

For sure, after finding the cause the message starts to make sense too. But in the beginning I couldn’t figure out what the error was about.

Hope this post helps someone who consults Google for “TransactionAbortedException” or “The transaction has aborted.”