view thingy.js @ 6:dc88b0e46f2d

We now record number of lines of comment blocks.
author Atul Varma <varmaa@toolness.com>
date Mon, 05 Jan 2009 17:17:33 -0800
parents dcae71b1c3b4
children 0d5b69cd01db
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 firstCommentLine;
  var lastCommentLine;

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

  jQuery.each(
    lines,
    function(lineNum) {
      var line = this;
      var isComment = (App.trim(line).indexOf("//") == 0);
      if (!isComment)
        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";
      }
    });
  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) {
      creole.parse($("#content").get(0), this.text);
    });
};

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