tweetcast/twisted/client/lib/mustache/mustache.js
author Raphael Velt <raph.velt@gmail.com>
Wed, 30 Nov 2011 12:11:59 +0100
changeset 403 dd1686ae5506
parent 311 13702105c5ee
permissions -rw-r--r--
minor changes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
311
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     1
/*
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     2
  mustache.js — Logic-less templates in JavaScript
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     3
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     4
  See http://mustache.github.com/ for more info.
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     5
*/
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     6
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     7
var Mustache = function() {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     8
  var regexCache = {};
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
     9
  var Renderer = function() {};
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    10
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    11
  Renderer.prototype = {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    12
    otag: "{{",
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    13
    ctag: "}}",
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    14
    pragmas: {},
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    15
    buffer: [],
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    16
    pragmas_implemented: {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    17
      "IMPLICIT-ITERATOR": true
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    18
    },
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    19
    context: {},
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    20
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    21
    render: function(template, context, partials, in_recursion) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    22
      // reset buffer & set context
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    23
      if(!in_recursion) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    24
        this.context = context;
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    25
        this.buffer = []; // TODO: make this non-lazy
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    26
      }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    27
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    28
      // fail fast
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    29
      if(!this.includes("", template)) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    30
        if(in_recursion) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    31
          return template;
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    32
        } else {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    33
          this.send(template);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    34
          return;
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    35
        }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    36
      }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    37
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    38
      // get the pragmas together
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    39
      template = this.render_pragmas(template);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    40
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    41
      // render the template
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    42
      var html = this.render_section(template, context, partials);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    43
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    44
      // render_section did not find any sections, we still need to render the tags
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    45
      if (html === false) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    46
        html = this.render_tags(template, context, partials, in_recursion);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    47
      }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    48
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    49
      if (in_recursion) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    50
        return html;
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    51
      } else {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    52
        this.sendLines(html);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    53
      }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    54
    },
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    55
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    56
    /*
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    57
      Sends parsed lines
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    58
    */
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    59
    send: function(line) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    60
      if(line !== "") {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    61
        this.buffer.push(line);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    62
      }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    63
    },
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    64
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    65
    sendLines: function(text) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    66
      if (text) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    67
        var lines = text.split("\n");
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    68
        for (var i = 0; i < lines.length; i++) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    69
          this.send(lines[i]);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    70
        }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    71
      }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    72
    },
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    73
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    74
    /*
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    75
      Looks for %PRAGMAS
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    76
    */
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    77
    render_pragmas: function(template) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    78
      // no pragmas
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    79
      if(!this.includes("%", template)) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    80
        return template;
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    81
      }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    82
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    83
      var that = this;
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    84
      var regex = this.getCachedRegex("render_pragmas", function(otag, ctag) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    85
        return new RegExp(otag + "%([\\w-]+) ?([\\w]+=[\\w]+)?" + ctag, "g");
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    86
      });
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    87
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    88
      return template.replace(regex, function(match, pragma, options) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    89
        if(!that.pragmas_implemented[pragma]) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    90
          throw({message:
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    91
            "This implementation of mustache doesn't understand the '" +
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    92
            pragma + "' pragma"});
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    93
        }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    94
        that.pragmas[pragma] = {};
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    95
        if(options) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    96
          var opts = options.split("=");
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    97
          that.pragmas[pragma][opts[0]] = opts[1];
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    98
        }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
    99
        return "";
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   100
        // ignore unknown pragmas silently
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   101
      });
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   102
    },
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   103
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   104
    /*
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   105
      Tries to find a partial in the curent scope and render it
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   106
    */
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   107
    render_partial: function(name, context, partials) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   108
      name = this.trim(name);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   109
      if(!partials || partials[name] === undefined) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   110
        throw({message: "unknown_partial '" + name + "'"});
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   111
      }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   112
      if(typeof(context[name]) != "object") {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   113
        return this.render(partials[name], context, partials, true);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   114
      }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   115
      return this.render(partials[name], context[name], partials, true);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   116
    },
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   117
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   118
    /*
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   119
      Renders inverted (^) and normal (#) sections
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   120
    */
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   121
    render_section: function(template, context, partials) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   122
      if(!this.includes("#", template) && !this.includes("^", template)) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   123
        // did not render anything, there were no sections
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   124
        return false;
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   125
      }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   126
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   127
      var that = this;
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   128
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   129
      var regex = this.getCachedRegex("render_section", function(otag, ctag) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   130
        // This regex matches _the first_ section ({{#foo}}{{/foo}}), and captures the remainder
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   131
        return new RegExp(
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   132
          "^([\\s\\S]*?)" +         // all the crap at the beginning that is not {{*}} ($1)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   133
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   134
          otag +                    // {{
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   135
          "(\\^|\\#)\\s*(.+)\\s*" + //  #foo (# == $2, foo == $3)
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   136
          ctag +                    // }}
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   137
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   138
          "\n*([\\s\\S]*?)" +       // between the tag ($2). leading newlines are dropped
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   139
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   140
          otag +                    // {{
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   141
          "\\/\\s*\\3\\s*" +        //  /foo (backreference to the opening tag).
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   142
          ctag +                    // }}
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   143
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   144
          "\\s*([\\s\\S]*)$",       // everything else in the string ($4). leading whitespace is dropped.
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   145
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   146
        "g");
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   147
      });
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   148
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   149
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   150
      // for each {{#foo}}{{/foo}} section do...
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   151
      return template.replace(regex, function(match, before, type, name, content, after) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   152
        // before contains only tags, no sections
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   153
        var renderedBefore = before ? that.render_tags(before, context, partials, true) : "",
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   154
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   155
        // after may contain both sections and tags, so use full rendering function
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   156
            renderedAfter = after ? that.render(after, context, partials, true) : "",
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   157
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   158
        // will be computed below
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   159
            renderedContent,
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   160
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   161
            value = that.find(name, context);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   162
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   163
        if (type === "^") { // inverted section
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   164
          if (!value || that.is_array(value) && value.length === 0) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   165
            // false or empty list, render it
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   166
            renderedContent = that.render(content, context, partials, true);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   167
          } else {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   168
            renderedContent = "";
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   169
          }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   170
        } else if (type === "#") { // normal section
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   171
          if (that.is_array(value)) { // Enumerable, Let's loop!
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   172
            renderedContent = that.map(value, function(row) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   173
              return that.render(content, that.create_context(row), partials, true);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   174
            }).join("");
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   175
          } else if (that.is_object(value)) { // Object, Use it as subcontext!
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   176
            renderedContent = that.render(content, that.create_context(value),
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   177
              partials, true);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   178
          } else if (typeof value === "function") {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   179
            // higher order section
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   180
            renderedContent = value.call(context, content, function(text) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   181
              return that.render(text, context, partials, true);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   182
            });
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   183
          } else if (value) { // boolean section
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   184
            renderedContent = that.render(content, context, partials, true);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   185
          } else {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   186
            renderedContent = "";
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   187
          }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   188
        }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   189
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   190
        return renderedBefore + renderedContent + renderedAfter;
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   191
      });
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   192
    },
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   193
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   194
    /*
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   195
      Replace {{foo}} and friends with values from our view
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   196
    */
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   197
    render_tags: function(template, context, partials, in_recursion) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   198
      // tit for tat
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   199
      var that = this;
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   200
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   201
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   202
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   203
      var new_regex = function() {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   204
        return that.getCachedRegex("render_tags", function(otag, ctag) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   205
          return new RegExp(otag + "(=|!|>|\\{|%)?([^\\/#\\^]+?)\\1?" + ctag + "+", "g");
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   206
        });
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   207
      };
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   208
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   209
      var regex = new_regex();
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   210
      var tag_replace_callback = function(match, operator, name) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   211
        switch(operator) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   212
        case "!": // ignore comments
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   213
          return "";
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   214
        case "=": // set new delimiters, rebuild the replace regexp
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   215
          that.set_delimiters(name);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   216
          regex = new_regex();
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   217
          return "";
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   218
        case ">": // render partial
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   219
          return that.render_partial(name, context, partials);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   220
        case "{": // the triple mustache is unescaped
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   221
          return that.find(name, context);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   222
        default: // escape the value
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   223
          return that.escape(that.find(name, context));
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   224
        }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   225
      };
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   226
      var lines = template.split("\n");
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   227
      for(var i = 0; i < lines.length; i++) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   228
        lines[i] = lines[i].replace(regex, tag_replace_callback, this);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   229
        if(!in_recursion) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   230
          this.send(lines[i]);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   231
        }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   232
      }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   233
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   234
      if(in_recursion) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   235
        return lines.join("\n");
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   236
      }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   237
    },
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   238
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   239
    set_delimiters: function(delimiters) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   240
      var dels = delimiters.split(" ");
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   241
      this.otag = this.escape_regex(dels[0]);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   242
      this.ctag = this.escape_regex(dels[1]);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   243
    },
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   244
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   245
    escape_regex: function(text) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   246
      // thank you Simon Willison
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   247
      if(!arguments.callee.sRE) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   248
        var specials = [
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   249
          '/', '.', '*', '+', '?', '|',
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   250
          '(', ')', '[', ']', '{', '}', '\\'
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   251
        ];
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   252
        arguments.callee.sRE = new RegExp(
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   253
          '(\\' + specials.join('|\\') + ')', 'g'
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   254
        );
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   255
      }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   256
      return text.replace(arguments.callee.sRE, '\\$1');
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   257
    },
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   258
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   259
    /*
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   260
      find `name` in current `context`. That is find me a value
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   261
      from the view object
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   262
    */
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   263
    find: function(name, context) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   264
      name = this.trim(name);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   265
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   266
      // Checks whether a value is thruthy or false or 0
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   267
      function is_kinda_truthy(bool) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   268
        return bool === false || bool === 0 || bool;
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   269
      }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   270
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   271
      var value;
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   272
      if(is_kinda_truthy(context[name])) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   273
        value = context[name];
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   274
      } else if(is_kinda_truthy(this.context[name])) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   275
        value = this.context[name];
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   276
      }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   277
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   278
      if(typeof value === "function") {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   279
        return value.apply(context);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   280
      }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   281
      if(value !== undefined) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   282
        return value;
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   283
      }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   284
      // silently ignore unkown variables
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   285
      return "";
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   286
    },
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   287
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   288
    // Utility methods
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   289
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   290
    /* includes tag */
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   291
    includes: function(needle, haystack) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   292
      return haystack.indexOf(this.otag + needle) != -1;
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   293
    },
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   294
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   295
    /*
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   296
      Does away with nasty characters
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   297
    */
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   298
    escape: function(s) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   299
      s = String(s === null ? "" : s);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   300
      return s.replace(/&(?!\w+;)|["'<>\\]/g, function(s) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   301
        switch(s) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   302
        case "&": return "&amp;";        
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   303
        case '"': return '&quot;';
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   304
        case "'": return '&#39;';
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   305
        case "<": return "&lt;";
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   306
        case ">": return "&gt;";
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   307
        default: return s;
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   308
        }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   309
      });
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   310
    },
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   311
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   312
    // by @langalex, support for arrays of strings
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   313
    create_context: function(_context) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   314
      if(this.is_object(_context)) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   315
        return _context;
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   316
      } else {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   317
        var iterator = ".";
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   318
        if(this.pragmas["IMPLICIT-ITERATOR"]) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   319
          iterator = this.pragmas["IMPLICIT-ITERATOR"].iterator;
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   320
        }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   321
        var ctx = {};
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   322
        ctx[iterator] = _context;
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   323
        return ctx;
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   324
      }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   325
    },
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   326
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   327
    is_object: function(a) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   328
      return a && typeof a == "object";
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   329
    },
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   330
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   331
    is_array: function(a) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   332
      return Object.prototype.toString.call(a) === '[object Array]';
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   333
    },
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   334
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   335
    /*
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   336
      Gets rid of leading and trailing whitespace
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   337
    */
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   338
    trim: function(s) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   339
      return s.replace(/^\s*|\s*$/g, "");
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   340
    },
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   341
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   342
    /*
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   343
      Why, why, why? Because IE. Cry, cry cry.
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   344
    */
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   345
    map: function(array, fn) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   346
      if (typeof array.map == "function") {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   347
        return array.map(fn);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   348
      } else {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   349
        var r = [];
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   350
        var l = array.length;
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   351
        for(var i = 0; i < l; i++) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   352
          r.push(fn(array[i]));
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   353
        }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   354
        return r;
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   355
      }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   356
    },
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   357
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   358
    getCachedRegex: function(name, generator) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   359
      var byOtag = regexCache[this.otag];
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   360
      if (!byOtag) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   361
        byOtag = regexCache[this.otag] = {};
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   362
      }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   363
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   364
      var byCtag = byOtag[this.ctag];
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   365
      if (!byCtag) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   366
        byCtag = byOtag[this.ctag] = {};
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   367
      }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   368
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   369
      var regex = byCtag[name];
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   370
      if (!regex) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   371
        regex = byCtag[name] = generator(this.otag, this.ctag);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   372
      }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   373
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   374
      return regex;
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   375
    }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   376
  };
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   377
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   378
  return({
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   379
    name: "mustache.js",
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   380
    version: "0.4.0-dev",
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   381
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   382
    /*
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   383
      Turns a template and view into HTML
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   384
    */
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   385
    to_html: function(template, view, partials, send_fun) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   386
      var renderer = new Renderer();
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   387
      if(send_fun) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   388
        renderer.send = send_fun;
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   389
      }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   390
      renderer.render(template, view || {}, partials);
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   391
      if(!send_fun) {
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   392
        return renderer.buffer.join("\n");
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   393
      }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   394
    }
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   395
  });
13702105c5ee Réorganisation de Tweetcast
Raphael Velt <raph.velt@gmail.com>
parents:
diff changeset
   396
}();