Archive for the 'Code plugin' Category

Published by Niels on 02 Jul 2006

NanoBlogger plug-ins

New releases of the code and gallery plug-ins for NanoBlogger. The code plug-in now no longer causes two blank line to be sent to stdout. The gallery plug-in now handles different versions of NanoBlogger better, i.e. the version check to determine which version of NanoBlogger is running does not cause an error anymore. It also delays the cleanup of meta files on Apple Mac OS X so I hope this is handles better now.

  • The new gallery plug-in can be downloaded here.
  • The new code plug-in can be downloaded here.

Enjoy and if there are any questions or problems contact me.

Published by Niels on 12 Jan 2006

The first release of the Code Formatting Plugin – Version 0.1.0

As mentioned in a previous blog, a plugin to format code snippets in NanoBlogger entries has been in the making. It seems that a first workable version can be downloaded here.

How does it work
In short: just start a line before the code snippet with ‘CODE(<language>){‘ and start a line with ‘}CODE‘ after the code snippet. No other characters may appear on this line. Example:

CODE(c){
#include <stdio.h>

int main( int argc, char* argv[] )
{
    (void)printf( "Hello, World!n" );

    return 0;
}
}CODE

This results in the following:

1
2
3
4
5
6
7
8
include <stdio.h>
 
int main( int argc, char* argv[] )
{
    (void)printf( "Hello, World!n" );
 
    return 0;
}

Here are some other examples:

C#:

1
2
3
4
5
6
7
8
9
10
11
12
using System;
 
namespace Hello
{
    class World
    {
        public static void Main()
        {
            Console.WriteLine( "Hello, World!" );
        }
    }
}

Bourne Shell/BASH:

1
2
3
#!/bin/bash
 
echo "Hello, World!"

The real XHTML generation is done by ViM, so be sure that that is installed on the PC you use for NanoBlogger. Since XHTML is basically XML where you are expected to do the formatting in a stylesheet, you can add the following to your stylesheet if the code needs to have a black background:

.nbcode { line-height: 120%; }
.nbcode .Comment { color: #00ffff; font-weight: bold; }
.nbcode .Constant { color: #ff40ff; font-weight: bold; }
.nbcode .Error { color: #ffffff; background-color: #ff6060; font-weight: bold; }
.nbcode .Identifier { color: #00ffff; font-weight: bold; }
.nbcode .PreProc { color: #8080ff; font-weight: bold; }
.nbcode .Special { color: #ff6060; font-weight: bold; }
.nbcode .Statement { color: #ffff00; font-weight: bold; }
.nbcode .Title { color: #ff40ff; font-weight: bold; }
.nbcode .Todo { color: #808080; background-color: #ffff00; }
.nbcode .Type { color: #00ff00; font-weight: bold; }
.nbcode .lnr { color: #ffff00; font-weight: bold; }
.nbcode body { color: #ffffff; background-color: #000000; }
.nbcode pre { color: #ffffff; background-color: #000000; }

Or the following if a white background is preferred:

.nbcode { line-height: 120%; }
.nbcode .Comment { color: #8080ff; }
.nbcode .Constant { color: #ff6060; }
.nbcode .Error { color: #ffffff; background-color: #ff6060; font-weight: bold; }
.nbcode .Identifier { color: #00ffff; }
.nbcode .PreProc { color: #ff40ff; }
.nbcode .Special { color: #ff40ff; }
.nbcode .Statement { color: #ffff00; }
.nbcode .Title { color: #ff40ff; }
.nbcode .Todo { color: #808080; background-color: #ffff00; }
.nbcode .Type { color: #00ff00; }
.nbcode .lnr { color: #ffff00; }
.nbcode body { color: #000000; background-color: #ffffff; }
.nbcode pre { color: #000000; background-color: #ffffff; }

Installation:
Depending on the version of NanoBlogger used place the plugin code.sh in one of the following directories:

  • plugins/entry/mod (>=3.2)
  • plugins/entry/body (3.1)
  • plugins/entry (>=2.8 && <3.1)

The plugin combines nicely with for example Markdown, as long as the plugin is called on the entry body before the Markdown plugin. Since code.sh comes before markdown.sh alphabetically this should not be a problem.

If you do not want NanoBlogger to call code.sh for every entry, the plugin can alternatively be placed in plugins/entry/format. If the code formatting plugin capabilities are needed just add code to the FORMAT: meta tag in the entry. As mentioned above, be sure to put it before any other format setting like markdown.

From blog.conf, nb.conf or using environment variables, the following variables can be set to influence the behavior of the plugin:

  • NBCODE_VIM
    Where ViM can be found. Defaults to vim.
  • NBCODE_TABSTOP
    Number of spaces a tab counts for. Defaults to 4.
  • NBCODE_SHIFTWIDTH
    Number of spaces for each indent. Defaults to 4.
  • NBCODE_EXPANDTAB
    Replace tabs with spaces (yes/no). Defaults to yes.
  • NBCODE_NUMBERING
    Add line numbering (yes/no). Defaults to yes.

Since this is the first release expect some bugs. Feel free to contact me for comments, questions and suggestions.