Making A Devblog


Today's theme is: ouroboros.

12/11/2015

Seeing as I spend a bunch of my free time making stuff for this website and am now looking out to the great blue yonder that is Android development I thought I'd start documenting some of the things I'm working on. If I'm going to document development I may as well document the development of the documentation.

Part of blogging this will involve showing code samples so I thought it'd be nice to show snippets with syntactic highlighting. I started on writing some JavaScript to highlight a basic sample of Java code. It wasn't nice code and I thought I was kind of wasting time when there would probably be a good solution out there. So here's that abandoned attempt:

$(document).ready(function() {
    var code = $("code").html();

    code = code.replace(/</g,"<");
    code = code.replace(/>/g,">");

    code = code.replace(/(public|private|default|protected|class|static|final|for|while|return|if|else|switch|\!|\||\&|<|>|\+|\=|\-|\/|\*)/g,"$1");

    code = code.replace(/(\{|\}|\;)/g,"$1
"); //code = code.replace(/([a-zA-Z0-9_]+)(\s?(\+|\=|\<|\>|\/|\*|\)|\(|\{))/g, "$1$2"); $("code").html(code); });

A quick Google search lead me to highlight.js a free solution with that supported a heap of languages with a bountiful variety of formatting styles too. Here's it in effect on my Java test case:

public class Factorial
{
    public static void main (String[] args)
    {
        final int NUM_FACTS = 100;
        for (int i = 0; i < NUM_FACTS; i++)
        {
            System.out.println( i + "! is " + factorial(i));
        }
    }

    public static int factorial (int n)
    {
        int result = 1;
        for (int i = 2; i < n; i++)
        {
            result *= i;
        }
        return result;
    }
}

Unfortunately the "String[]" parameter type declaration in the "main" method isn't recognised as a type declaration due to the square brackets. I'm considering diving down into the highlight.js code to modify this in the future.

It was painless to set up. I just simply added the appropriate references to my global includes file with a small script to execute the syntactic highlighting and I was away:

<link rel="stylesheet" href="/include/styles/zenburn.css">
<script type="text/javascript" src="/include/highlight.pack.js"></script>
<script type="text/javascript">hljs.initHighlightingOnLoad();</script>

Amusingly enough posting that snippet there was a bastard as to render the '<' and '>' characters they needed to be converted to "&gt;" and "&lt;" respectively. Additionally to write the phrases "&gt;" and "&lt;" I needed to write the ampersand as "&amp;" making them "&amp;gt;" and "&amp;lt;". So now for me to write "&amp;gt;" and "&amp;lt;" I must enter "&amp;amp;gt;" and "&amp;amp;lt;"... What a nightmare... Here's this paragraph on the back-end except when writing this example where you see each '&' it's actually "&amp;":

<p>Amusingly enough posting that snippet there was a bastard as to render the '&lt;' and '&gt;' characters they needed to be converted to "&amp;gt;" and "&amp;lt;" respectively. Additionally to write the phrases "&amp;gt;" and "&amp;lt;" I needed to write the ampersand as "&amp;amp;" making them "&amp;amp;gt;" and "&amp;amp;lt;". So now for me to write "&amp;amp;gt;" and "&amp;amp;lt;" I must enter "&amp;amp;amp;gt;" and "&amp;amp;amp;lt;"... What a nightmare... Here's this paragraph on the back-end only when writing this example where you see each '&amp;' it's actually "&amp;amp;":</p>

Anyway that headache hopefully won't happen again because I usually don't like to cause myself so much grief.

So there we have the development blog about developing a development blog which discusses itself being a development blog about developing a development blog with escape characters escaping from escape characters escaping from escape characters. Gross... but how's my SEO? Registered as spam? Probably registered as spam.