My Web Adventures

A blog for hackers by a dinosaur hacker.

Code Blocks Options With Octopress

I actually figured out several different options that we can use to insert code blocks with Octopress

1. Markdown codeblocks:

We have to use 4 spaces or 1 tab to designate that this is a codeblock.

    var respond = function(response, data, err) {
        var responseObj = {data: data, error: err};
        response.writeHead(200, {"Content-Type":"application/json"});
        response.write(JSON.stringify(responseObj));
        response.end();
    };
    var arr1 = new Array(arrayLength);
    var arr2 = new Array(element0, element1, ..., elementN);

And this is what we get with the 4 spaces, or the tabs removed from the start of the line.

var respond = function(response, data, err) {
    var responseObj = {data: data, error: err};
    response.writeHead(200, {"Content-Type":"application/json"});
    response.write(JSON.stringify(responseObj));
    response.end();
};
var arr1 = new Array(arrayLength);
var arr2 = new Array(element0, element1, ..., elementN);

And this is the HTML that is written for you:

HTML Result
1
2
3
4
5
6
7
8
9
<pre><code>var respond = function(response, data, err) {
    var responseObj = {data: data, error: err};
    response.writeHead(200, {"Content-Type":"application/json"});
    response.write(JSON.stringify(responseObj));
    response.end();
};
var arr1 = new Array(arrayLength);
var arr2 = new Array(element0, element1, ..., elementN);
</code></pre>

2. Backtick Code Blocks (GitHub Flavored Markdown):

``` [language] [title] [url] [link text]
var respond = function(response, data, err) {
    var responseObj = {data: data, error: err};
    response.writeHead(200, {"Content-Type":"application/json"});
    response.write(JSON.stringify(responseObj));
    response.end();
};
var arr1 = new Array(arrayLength);
var arr2 = new Array(element0, element1, ..., elementN);
```    
Backtick Code Blocks (GitHub Flavored Markdown)
1
2
3
4
5
6
7
8
var respond = function(response, data, err) {
    var responseObj = {data: data, error: err};
    response.writeHead(200, {"Content-Type":"application/json"});
    response.write(JSON.stringify(responseObj));
    response.end();
};
var arr1 = new Array(arrayLength);
var arr2 = new Array(element0, element1, ..., elementN);

3. Codeblocks

{% codeblock [title] [lang:language] [url] [link text] %}
var respond = function(response, data, err) {
    var responseObj = {data: data, error: err};
    response.writeHead(200, {"Content-Type":"application/json"});
    response.write(JSON.stringify(responseObj));
    response.end();
};
{% endcodeblock %}
Javascript Example
1
2
3
4
5
6
7
8
var respond = function(response, data, err) {
    var responseObj = {data: data, error: err};
    response.writeHead(200, {"Content-Type":"application/json"});
    response.write(JSON.stringify(responseObj));
    response.end();
};
var arr1 = new Array(arrayLength);
var arr2 = new Array(element0, element1, ..., elementN);

Note:

For code-coloring to work, be sure you have Python27 at your Path.