Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

better find runtime

  • Loading branch information...
commit 4c07e1fa6f51b056aef3d337d0a52123a9930ad6 1 parent b4c868c
Greg Allen authored April 20, 2012
171  example/templates.js
... ...
@@ -0,0 +1,171 @@
  1
+
  2
+var jade = (function(exports){
  3
+/*!
  4
+ * Jade - runtime
  5
+ * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
  6
+ * MIT Licensed
  7
+ */
  8
+
  9
+/**
  10
+ * Lame Array.isArray() polyfill for now.
  11
+ */
  12
+
  13
+if (!Array.isArray) {
  14
+  Array.isArray = function(arr){
  15
+    return '[object Array]' == Object.prototype.toString.call(arr);
  16
+  };
  17
+}
  18
+
  19
+/**
  20
+ * Lame Object.keys() polyfill for now.
  21
+ */
  22
+
  23
+if (!Object.keys) {
  24
+  Object.keys = function(obj){
  25
+    var arr = [];
  26
+    for (var key in obj) {
  27
+      if (obj.hasOwnProperty(key)) {
  28
+        arr.push(key);
  29
+      }
  30
+    }
  31
+    return arr;
  32
+  } 
  33
+}
  34
+
  35
+/**
  36
+ * Render the given attributes object.
  37
+ *
  38
+ * @param {Object} obj
  39
+ * @return {String}
  40
+ * @api private
  41
+ */
  42
+
  43
+exports.attrs = function attrs(obj){
  44
+  var buf = []
  45
+    , terse = obj.terse;
  46
+  delete obj.terse;
  47
+  var keys = Object.keys(obj)
  48
+    , len = keys.length;
  49
+  if (len) {
  50
+    buf.push('');
  51
+    for (var i = 0; i < len; ++i) {
  52
+      var key = keys[i]
  53
+        , val = obj[key];
  54
+      if ('boolean' == typeof val || null == val) {
  55
+        if (val) {
  56
+          terse
  57
+            ? buf.push(key)
  58
+            : buf.push(key + '="' + key + '"');
  59
+        }
  60
+      } else if ('class' == key && Array.isArray(val)) {
  61
+        buf.push(key + '="' + exports.escape(val.join(' ')) + '"');
  62
+      } else {
  63
+        buf.push(key + '="' + exports.escape(val) + '"');
  64
+      }
  65
+    }
  66
+  }
  67
+  return buf.join(' ');
  68
+};
  69
+
  70
+/**
  71
+ * Escape the given string of `html`.
  72
+ *
  73
+ * @param {String} html
  74
+ * @return {String}
  75
+ * @api private
  76
+ */
  77
+
  78
+exports.escape = function escape(html){
  79
+  return String(html)
  80
+    .replace(/&(?!\w+;)/g, '&amp;')
  81
+    .replace(/</g, '&lt;')
  82
+    .replace(/>/g, '&gt;')
  83
+    .replace(/"/g, '&quot;');
  84
+};
  85
+
  86
+/**
  87
+ * Re-throw the given `err` in context to the
  88
+ * the jade in `filename` at the given `lineno`.
  89
+ *
  90
+ * @param {Error} err
  91
+ * @param {String} filename
  92
+ * @param {String} lineno
  93
+ * @api private
  94
+ */
  95
+
  96
+exports.rethrow = function rethrow(err, filename, lineno){
  97
+  if (!filename) throw err;
  98
+
  99
+  var context = 3
  100
+    , str = require('fs').readFileSync(filename, 'utf8')
  101
+    , lines = str.split('\n')
  102
+    , start = Math.max(lineno - context, 0)
  103
+    , end = Math.min(lines.length, lineno + context); 
  104
+
  105
+  // Error context
  106
+  var context = lines.slice(start, end).map(function(line, i){
  107
+    var curr = i + start + 1;
  108
+    return (curr == lineno ? '  > ' : '    ')
  109
+      + curr
  110
+      + '| '
  111
+      + line;
  112
+  }).join('\n');
  113
+
  114
+  // Alter exception message
  115
+  err.path = filename;
  116
+  err.message = (filename || 'Jade') + ':' + lineno 
  117
+    + '\n' + context + '\n\n' + err.message;
  118
+  throw err;
  119
+};
  120
+
  121
+  return exports;
  122
+
  123
+})({});
  124
+jade.templates = {};
  125
+jade.render = function(node, template, data) {
  126
+  var tmp = jade.templates[template](data);
  127
+  node.innerHTML = tmp;
  128
+};
  129
+
  130
+jade.templates.test1 = function(locals, attrs, escape, rethrow) {
  131
+var attrs = jade.attrs, escape = jade.escape, rethrow = jade.rethrow;
  132
+var buf = [];
  133
+with (locals || {}) {
  134
+var interp;
  135
+buf.push('<p>Hello, my name is ' + escape((interp = name) == null ? '' : interp) + '</p>');
  136
+}
  137
+return buf.join("");
  138
+}
  139
+jade.templates.test2 = function(locals, attrs, escape, rethrow) {
  140
+var attrs = jade.attrs, escape = jade.escape, rethrow = jade.rethrow;
  141
+var buf = [];
  142
+with (locals || {}) {
  143
+var interp;
  144
+buf.push('<ul>');
  145
+// iterate items
  146
+(function(){
  147
+  if ('number' == typeof items.length) {
  148
+    for (var $index = 0, $$l = items.length; $index < $$l; $index++) {
  149
+      var item = items[$index];
  150
+
  151
+buf.push('<li>');
  152
+var __val__ = item
  153
+buf.push(escape(null == __val__ ? "" : __val__));
  154
+buf.push('</li>');
  155
+    }
  156
+  } else {
  157
+    for (var $index in items) {
  158
+      var item = items[$index];
  159
+
  160
+buf.push('<li>');
  161
+var __val__ = item
  162
+buf.push(escape(null == __val__ ? "" : __val__));
  163
+buf.push('</li>');
  164
+   }
  165
+  }
  166
+}).call(this);
  167
+
  168
+buf.push('</ul>');
  169
+}
  170
+return buf.join("");
  171
+}
0  example/jade.js → example/templates.min.js
File renamed without changes
16  lib/compile.js
@@ -12,10 +12,22 @@ var jadeCompile = function(file, str) {
12 12
   });
13 13
   var result = f.toString();
14 14
   var basename = path.basename(file, '.jade');
15  
-  result = result.replace(/function anonymous/, 'jade.templates.'+basename+' = function');
  15
+  result = result.replace(/function anonymous/, 'jade.templates["'+basename+'"] = function');
16 16
   return result;
17 17
 };
18 18
 
  19
+var findRuntime = function() {
  20
+  var runtime = path.join(__dirname, '../node_modules/jade/runtime.js');
  21
+  if (path.existsSync(runtime))
  22
+    return runtime;
  23
+
  24
+  runtime = path.join(__dirname, '../../jade/runtime.js')
  25
+  if (path.existsSync(runtime))
  26
+    return runtime;
  27
+
  28
+  throw new Error('runtime.js not found');
  29
+};
  30
+
19 31
 var compile = function(options, callback) {
20 32
 
21 33
   var queue = res.queue(function(file, callback) {
@@ -24,8 +36,8 @@ var compile = function(options, callback) {
24 36
     });
25 37
   });
26 38
 
27  
-  var runtime = path.join(__dirname, '../node_modules/jade/runtime.js');
28 39
   var render = path.join(__dirname, 'browser/render.js');
  40
+  var runtime = findRuntime();
29 41
   queue.push(runtime);
30 42
   queue.push(render);
31 43
 

0 notes on commit 4c07e1f

Please sign in to comment.
Something went wrong with that request. Please try again.