includes/clientside/static/template-compiler.js
author Dan
Mon, 16 Feb 2009 16:17:25 -0500
changeset 832 7152ca0a0ce9
parent 582 a38876c0793c
child 1227 bdac73ed481e
permissions -rw-r--r--
Major redesign of rendering pipeline that separates pages saved with MCE from pages saved with the plaintext editor (full description in long commit message) - Pages are now stored with an extra metadata field called page_format which is "wikitext" or "xhtml" - New $flags parameter + RENDER_* constants added that control RenderMan::render() behavior - Several other changes: * Added a sprite API for Javascript and made editor use sprites when possible * Removed a number of config options from the default install schema, replaced with second parameter to getConfig() calls * MessageBox in editor mostly replaced with miniPrompt * A few bugfixes related to password changes (registration didn't even work) * Rewrote the bitfield compression algorithm used to serialize allowed MIME types * Fixed some typos in language files and strings * Fixed a Text_Wiki bug in Heading parser

// An implementation of Enano's template compiler in Javascript. Same exact API
// as the PHP version - constructor accepts text, then the assign_vars, assign_bool, and run methods.

window.templateParser = function(text)
{
  this.tpl_code    = text;
  this.tpl_strings = new Object();
  this.tpl_bool    = new Object();
  this.assign_vars = __tpAssignVars;
  this.assign_bool = __tpAssignBool;
  this.run         = __tpRun;
}

window.__tpAssignVars = function(vars)
{
  for(var i in vars)
  {
    this.tpl_strings[i] = vars[i];
  }
}

window.__tpAssignBool = function(vars)
{
  for(var i in vars)
  {
    this.tpl_bool[i] = ( vars[i] ) ? true : false; 
  }
}

window.__tpRun = function()
{
  if(typeof(this.tpl_code) == 'string')
  {
    tpl_code = __tpCompileTemplate(this.tpl_code);
    try {
      compiled = eval(tpl_code);
    }
    catch(e)
    {
      alert(e);
      aclDebug(tpl_code);
    }
    return compiled;
  }
  return false;
}

window.__tpCompileTemplate = function(code)
{
  // Compile plaintext/template code to javascript code
  code = code.replace(/\\/g, "\\\\");
  code = code.replace(/\'/g,  "\\'");
  code = code.replace(/\"/g,  '\\"');
  code = code.replace(new RegExp(unescape('%0A'), 'g'), '\\n');
  code = "'" + code + "'";
  code = code.replace(/\{([A-z0-9_-]+)\}/ig, "' + this.tpl_strings['$1'] + '");
  code = code.replace(/\{lang:([a-z0-9_]+)\}/g, "' + $lang.get('$1') + '");
  code = code.replace(/\<!-- IFSET ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- BEGINELSE \1 --\>([\s\S]*?)\<!-- END \1 --\>/ig, "' + ( ( typeof(this.tpl_strings['$1']) == 'string' ) ? '$2' : '$3' ) + '");
  code = code.replace(/\<!-- IFSET ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- END \1 --\>/ig, "' + ( ( typeof(this.tpl_strings['$1']) == 'string' ) ? '$2' : '' ) + '");
  code = code.replace(/\<!-- BEGIN ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- BEGINELSE \1 --\>([\s\S]*?)\<!-- END \1 --\>/ig, "' + ( ( this.tpl_bool['$1'] == true ) ? '$2' : '$3' ) + '");
  code = code.replace(/\<!-- BEGIN ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- END \1 --\>/ig, "' + ( ( this.tpl_bool['$1'] == true ) ? '$2' : '' ) + '");
  return code;
}

window.__tpExtractVars = function(code)
{
  code = code.replace('\\', "\\\\");
  code = code.replace("'",  "\\'");
  code = code.replace('"',  '\\"');
  code = code.replace(new RegExp(unescape('%0A'), 'g'), "\\n");
  code = code.match(/\<!-- VAR ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- ENDVAR \1 -->/g);
  code2 = '';
  for(var i in code)
    if(typeof(code[i]) == 'string')
      code2 = code2 + code[i];
  code = code2.replace(/\<!-- VAR ([A-z0-9_-]+) --\>([\s\S]*?)\<!-- ENDVAR \1 -->/g, "'$1' : \"$2\",");
  code = '( { ' + code + ' "________null________" : false } )';
  vars = eval(code);
  return vars;
}