Tuesday, November 12, 2013

Showing source code in a post

Since I'm interested in computer science, I'll probably want to show some source code at some point in the future. There appear to be many possibilities, of which I looked at 3.
  1. The obvious way would be to use <pre> like so:
    int fib(unsigned int n) {
      if ( n <=1 )
        return n;
      else
        return fib(n-1) + fib(n-2);
    }
    
    unsigned int a[] = 
      { fib(1), fib(2), fib(3) };
    
  2. Using SyntaxHighlighter: this gives nice results but the method seems complicated and I wondered whether something simpler is available.
  3. Google-code's prettify seems simple enough. Here's the input: <script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script> <pre class="prettyprint lang-cpp"> int fib(unsigned int n) { ... </pre> And here's the result:
    int fib(unsigned int n) {
      if ( n <=1 )
        return n;
      else
        return fib(n-1) + fib(n-2);
    }
    
    unsigned int a[] = 
      { fib(1), fib(2), fib(3) };
    
    So adding one "script" line gives you syntax coloring and a box around the code.
I guess, I'll use (1) or (3).

No comments:

Post a Comment