Forum: specify code language

The most common languages on the Processing forum are Java, JavaScript / HTML+CSS, and Python – although lots of other things get talked about, including Ruby, R, CoffeeScript, et cetera.

The new Discourse forum uses highlight.js, which auto-detects the language of code inside of <pre><code></code></pre> (generated by markdown).

Languages may be specified by manually specified by writing the language keyword after the three backticks of an opening code gate, like this: ```java

These keywords are specific and case sensitive – “java” will work, “Java” will not.

Doing so generates HTML like this:

<pre><code class="lang-java">...</code></pre>

The auto-format styling and manual format styling are not the same.

Here is an example of two code blocks – one written with nothing after the opening backtick gate, the other with the keyword java.

Java (auto)

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

Java (labeled): java

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

JavaScript (auto)

console.log("Hello World!");

JavaScript (labeled): javascript

console.log("Hello World!");

Python (auto)

print("Hello World")

Python (labeled): python

print("Hello World")

Ruby (auto)

puts 'Hello World!'

Ruby (labeled): ruby

puts 'Hello World!'

It looks like adding additional highlightjs language to discourse may be an admin feature:

1 Like

Is there a way to include custom code highlighting rules, especially for Processing code?

Yes, it looks like there are docs on writing highlightjs languages, however there are currently 176 listed on theit homepage and Processing is one of the languages listed. You can also set a Discourse site to have a default code language.

I haven’t been involved in the Processing website, but I think the fact that this exists has been on their radar for a long time:


@jeremydouglass Could you rename your post as how to “Format your code”? We could use it to show ppl how to format code in the forum.

Kf

Good idea to add FAQ information for formatting like on the old forum (but different) – although I think particular post is too technical to also serve that purpose.