
var parser = {

  subs: [
    {             // erases '+++'
      'replace':  /\+\+\+/g,
      'with':     ''
    }, {          // substitute 'h1. xxx \n' with '<h1>xxx</h1>'
      'replace':  /h(\d)\.([^\n]*)\n/g,
      'with':     '<h$1>$2</h$1>'
    }, {          // substitute '**xxx**' with '<b>xxx</b>'
      'replace':  /\*\*([^\*]+)\*\*/g,
      'with':     '<b>$1</b>'
    }, {          // substitute '*xxx*' with '<strong>xxx</strong>'
      'replace':  /\*([^\*]+)\*/g,
      'with':     '<strong>$1</strong>'
    }, {          // substitute '!xxx!' with '<img src="content/xxx" />'
      'replace':  /!(\S+)!/g,
      'with':     '<img src="content/$1" />'
    }, {          // substitute '"xxx":http://yyy' with '<a href="http://yyy">xxx</a>'
      'replace':  /\"([^\"]+)\":(http:\/\/[^,\s]+)/g,
      'with':     '<a href="$2" target="_new">$1</a>'
    }, {          // substitute '"xxx":project/yyy' with '<a href="javascript:site.navigate('project/yyy');">xxx</a>'
      'replace':  /\"([^\"]+)\":(page|category|project)\/([^,\s]+)/g,
      'with':     '<a href="javascript:site.navigate(\'$2/$3\');">$1</a>'
    }, {          // substitute '"xxx":yyy' with '<a href="content/yyy">xxx</a>'
      'replace':  /\"([^\"]+)\":([^,\s]+)/g,
      'with':     '<a href="content/$2">$1</a>'
    }, {          // substitute '\n' with '<br/>'
      'replace':  /\n/g,
      'with':     '<br/>'
    }
  ],

  /**
   * This function uses a list of substitutions (see above) to replace some styling markup by
   * html tags. The markups are close to the textile (see http://hobix.com/textile) markup syntax.
   */
  parseContent: function(content) {
    if (!content) return null;

    $each(this.subs, function(sub) {
      content = content.replace(sub['replace'], sub['with']);
    });
    return content;
  },

  /**
   * This function returns the text inside the given content parameter, that is surrounded by
   * a triple '+' sign. This function has be called _before_ parseContent, because it
   * removes all triple '+' signs.
   */
  parseTeaser: function(content) {
    var teaser = /\+\+\+([^\+]+)\+\+\+/;
    teaser.exec(content);
    return RegExp.$1;
  }

};

