view thingy.js @ 8:f3f56cc75bfd

Code column is now colored differently.
author Atul Varma <varmaa@toolness.com>
date Mon, 05 Jan 2009 18:40:10 -0800
parents 0d5b69cd01db
children 5418d21d7751
line wrap: on
line source

var App = {
  FILENAME: "samplecode.js"
};

App.trim = function trim(str) {
  return str.replace(/^\s+|\s+$/g,"");
};

App.processCode = function processCode(code) {
  var lines = code.split('\n');
  var blocks = [];
  var blockText = "";
  var codeText = "";
  var firstCommentLine;
  var lastCommentLine;

  function maybeAppendBlock() {
    if (blockText)
      blocks.push({text: blockText,
                   lineno: firstCommentLine,
                   numLines: lastCommentLine - firstCommentLine + 1,
                   code: codeText});
  }

  jQuery.each(
    lines,
    function(lineNum) {
      var line = this;
      var isComment = (App.trim(line).indexOf("//") == 0);
      if (!isComment) {
        codeText += line + "\n";
        return;
      }
      var startIndex = line.indexOf("//");
      var text = line.slice(startIndex + 3);
      if (lineNum == lastCommentLine + 1) {
        blockText += text + "\n";
        lastCommentLine += 1;
      } else {
        maybeAppendBlock();
        firstCommentLine = lineNum;
        lastCommentLine = lineNum;
        blockText = text + "\n";
        codeText = "";
      }
    });
  maybeAppendBlock();

  var creole = new Parse.Simple.Creole(
    {interwiki: {
       WikiCreole: 'http://www.wikicreole.org/wiki/',
       Wikipedia: 'http://en.wikipedia.org/wiki/'
     },
     linkFormat: ''
    });

  jQuery.each(
    blocks,
    function(i) {
      var docs = $('<div class="documentation">');
      creole.parse(docs.get(0), this.text);
      $("#content").append(docs);
      var code = $('<div class="code">');
      code.text(this.code);
      $("#content").append(code);

      var docsSurplus = docs.height() - code.height();
      if (docsSurplus > 0)
        code.css({paddingBottom: docsSurplus + "px"});

      $("#content").append('<div class="divider">');
    });
};

$(window).ready(
  function() {
    jQuery.get(App.FILENAME,
               {},
               App.processCode,
               "text");
  });