How To Display Code In Your Posts

This is just a quick post to let you know how to easily include Xojo syntax in your posts in case you’re not familiar with Markdown.

There are two ways: code spans and fenced code blocks. the links contains full explanations from the CommonMark specification but for a quick summary, see below.

Code spans

Simply enclose text you want displayed as generic code in a matching number of backticks: `like this` (the backtracks have been left in on purpose here by flanking the text like this with two backticks).

Code spans do not offer any form of syntax highlighting.

Fenced code blocks

If you want to display more than a few words of code or would like to take advantage of INN’s syntax highlighting engine then use a fenced code block. This is done by flanking your code with three backticks or tildes. Whilst the highlighting engine is able to detect the language type within the code fence, you can specify the language to use by following the opening three backticks or tildes with an optional info string. For Xojo code it’s xojo. This is highly recommended:

```
Put your code here
```
```xojo <--- note the info string.
Some Xojo code here.
```

I wrote a custom plugin for the forum specifically for Xojo so it highlights all aspects of the language. It even handles Color literals:

' Commment 1
// Comment type 2
Rem Old school comment
Var a As Integer = 10
Var c As Color = &cFFDDAA00
Var d As Double = 123.5
Var s As String = "Hello"
#Pragma Warning "Fix bugs!"

The engine supports most common programming languages (see here). Below are some examples:

Python

n = int(input('Type a number, then its factorial will be printed: '))

if n < 0:
    raise ValueError('You must enter a positive number')

fact = 1
i = 2
while i <= n:
    fact = fact * i
    i += 1

print(fact)

C#

using System;

class Program
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Hello, world!");
    }
}

Java

public class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Prints the string to the console.
    }
}

Javascript

function factorial(n) {
    if (n === 0)
        return 1; // 0! = 1

    return n * factorial(n - 1);
}

factorial(3); // returns 6
2 Likes