Ein Projekt das es ermöglicht Beerpong über das Internet von zwei unabhängigen positionen aus zu spielen. Entstehung im Rahmen einer Praktikumsaufgabe im Fach Interaktion.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jade.js 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. 'use strict';
  2. /*!
  3. * Jade
  4. * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
  5. * MIT Licensed
  6. */
  7. /**
  8. * Module dependencies.
  9. */
  10. var Parser = require('./parser')
  11. , Lexer = require('./lexer')
  12. , Compiler = require('./compiler')
  13. , runtime = require('./runtime')
  14. , addWith = require('with')
  15. , fs = require('fs');
  16. /**
  17. * Expose self closing tags.
  18. */
  19. exports.selfClosing = require('void-elements');
  20. /**
  21. * Default supported doctypes.
  22. */
  23. exports.doctypes = require('./doctypes');
  24. /**
  25. * Text filters.
  26. */
  27. exports.filters = require('./filters');
  28. /**
  29. * Utilities.
  30. */
  31. exports.utils = require('./utils');
  32. /**
  33. * Expose `Compiler`.
  34. */
  35. exports.Compiler = Compiler;
  36. /**
  37. * Expose `Parser`.
  38. */
  39. exports.Parser = Parser;
  40. /**
  41. * Expose `Lexer`.
  42. */
  43. exports.Lexer = Lexer;
  44. /**
  45. * Nodes.
  46. */
  47. exports.nodes = require('./nodes');
  48. /**
  49. * Jade runtime helpers.
  50. */
  51. exports.runtime = runtime;
  52. /**
  53. * Template function cache.
  54. */
  55. exports.cache = {};
  56. /**
  57. * Parse the given `str` of jade and return a function body.
  58. *
  59. * @param {String} str
  60. * @param {Object} options
  61. * @return {Object}
  62. * @api private
  63. */
  64. function parse(str, options){
  65. // Parse
  66. var parser = new (options.parser || Parser)(str, options.filename, options);
  67. var tokens;
  68. try {
  69. // Parse
  70. tokens = parser.parse();
  71. } catch (err) {
  72. parser = parser.context();
  73. runtime.rethrow(err, parser.filename, parser.lexer.lineno, parser.input);
  74. }
  75. // Compile
  76. var compiler = new (options.compiler || Compiler)(tokens, options);
  77. var js;
  78. try {
  79. js = compiler.compile();
  80. } catch (err) {
  81. if (err.line && (err.filename || !options.filename)) {
  82. runtime.rethrow(err, err.filename, err.line, parser.input);
  83. }
  84. }
  85. // Debug compiler
  86. if (options.debug) {
  87. console.error('\nCompiled Function:\n\n\u001b[90m%s\u001b[0m', js.replace(/^/gm, ' '));
  88. }
  89. var globals = [];
  90. globals.push('jade');
  91. globals.push('jade_mixins');
  92. globals.push('jade_interp');
  93. globals.push('jade_debug');
  94. globals.push('buf');
  95. var body = ''
  96. + 'var buf = [];\n'
  97. + 'var jade_mixins = {};\n'
  98. + 'var jade_interp;\n'
  99. + (options.self
  100. ? 'var self = locals || {};\n' + js
  101. : addWith('locals || {}', '\n' + js, globals)) + ';'
  102. + 'return buf.join("");';
  103. return {body: body, dependencies: parser.dependencies};
  104. }
  105. /**
  106. * Compile a `Function` representation of the given jade `str`.
  107. *
  108. * Options:
  109. *
  110. * - `compileDebug` when `false` debugging code is stripped from the compiled
  111. template, when it is explicitly `true`, the source code is included in
  112. the compiled template for better accuracy.
  113. * - `filename` used to improve errors when `compileDebug` is not `false` and to resolve imports/extends
  114. *
  115. * @param {String} str
  116. * @param {Options} options
  117. * @return {Function}
  118. * @api public
  119. */
  120. exports.compile = function(str, options){
  121. var options = options || {}
  122. , filename = options.filename
  123. ? JSON.stringify(options.filename)
  124. : 'undefined'
  125. , fn;
  126. str = String(str);
  127. var parsed = parse(str, options);
  128. if (options.compileDebug !== false) {
  129. fn = [
  130. 'var jade_debug = [{ lineno: 1, filename: ' + filename + ' }];'
  131. , 'try {'
  132. , parsed.body
  133. , '} catch (err) {'
  134. , ' jade.rethrow(err, jade_debug[0].filename, jade_debug[0].lineno' + (options.compileDebug === true ? ',' + JSON.stringify(str) : '') + ');'
  135. , '}'
  136. ].join('\n');
  137. } else {
  138. fn = parsed.body;
  139. }
  140. fn = new Function('locals, jade', fn)
  141. var res = function(locals){ return fn(locals, Object.create(runtime)) };
  142. if (options.client) {
  143. res.toString = function () {
  144. var err = new Error('The `client` option is deprecated, use the `jade.compileClient` method instead');
  145. err.name = 'Warning';
  146. console.error(err.stack || err.message);
  147. return exports.compileClient(str, options);
  148. };
  149. }
  150. res.dependencies = parsed.dependencies;
  151. return res;
  152. };
  153. /**
  154. * Compile a JavaScript source representation of the given jade `str`.
  155. *
  156. * Options:
  157. *
  158. * - `compileDebug` When it is `true`, the source code is included in
  159. * the compiled template for better error messages.
  160. * - `filename` used to improve errors when `compileDebug` is not `true` and to resolve imports/extends
  161. * - `name` the name of the resulting function (defaults to "template")
  162. *
  163. * @param {String} str
  164. * @param {Options} options
  165. * @return {String}
  166. * @api public
  167. */
  168. exports.compileClient = function(str, options){
  169. var options = options || {};
  170. var name = options.name || 'template';
  171. var filename = options.filename ? JSON.stringify(options.filename) : 'undefined';
  172. var fn;
  173. str = String(str);
  174. if (options.compileDebug) {
  175. options.compileDebug = true;
  176. fn = [
  177. 'var jade_debug = [{ lineno: 1, filename: ' + filename + ' }];'
  178. , 'try {'
  179. , parse(str, options).body
  180. , '} catch (err) {'
  181. , ' jade.rethrow(err, jade_debug[0].filename, jade_debug[0].lineno, ' + JSON.stringify(str) + ');'
  182. , '}'
  183. ].join('\n');
  184. } else {
  185. options.compileDebug = false;
  186. fn = parse(str, options).body;
  187. }
  188. return 'function ' + name + '(locals) {\n' + fn + '\n}';
  189. };
  190. /**
  191. * Compile a `Function` representation of the given jade file.
  192. *
  193. * Options:
  194. *
  195. * - `compileDebug` when `false` debugging code is stripped from the compiled
  196. template, when it is explicitly `true`, the source code is included in
  197. the compiled template for better accuracy.
  198. *
  199. * @param {String} path
  200. * @param {Options} options
  201. * @return {Function}
  202. * @api public
  203. */
  204. exports.compileFile = function (path, options) {
  205. options = options || {};
  206. var key = path + ':string';
  207. options.filename = path;
  208. var str = options.cache
  209. ? exports.cache[key] || (exports.cache[key] = fs.readFileSync(path, 'utf8'))
  210. : fs.readFileSync(path, 'utf8');
  211. return options.cache
  212. ? exports.cache[path] || (exports.cache[path] = exports.compile(str, options))
  213. : exports.compile(str, options);
  214. };
  215. /**
  216. * Render the given `str` of jade.
  217. *
  218. * Options:
  219. *
  220. * - `cache` enable template caching
  221. * - `filename` filename required for `include` / `extends` and caching
  222. *
  223. * @param {String} str
  224. * @param {Object|Function} options or fn
  225. * @param {Function|undefined} fn
  226. * @returns {String}
  227. * @api public
  228. */
  229. exports.render = function(str, options, fn){
  230. // support callback API
  231. if ('function' == typeof options) {
  232. fn = options, options = undefined;
  233. }
  234. if (typeof fn === 'function') {
  235. var res
  236. try {
  237. res = exports.render(str, options);
  238. } catch (ex) {
  239. return fn(ex);
  240. }
  241. return fn(null, res);
  242. }
  243. options = options || {};
  244. // cache requires .filename
  245. if (options.cache && !options.filename) {
  246. throw new Error('the "filename" option is required for caching');
  247. }
  248. var path = options.filename;
  249. var tmpl = options.cache
  250. ? exports.cache[path] || (exports.cache[path] = exports.compile(str, options))
  251. : exports.compile(str, options);
  252. return tmpl(options);
  253. };
  254. /**
  255. * Render a Jade file at the given `path`.
  256. *
  257. * @param {String} path
  258. * @param {Object|Function} options or callback
  259. * @param {Function|undefined} fn
  260. * @returns {String}
  261. * @api public
  262. */
  263. exports.renderFile = function(path, options, fn){
  264. // support callback API
  265. if ('function' == typeof options) {
  266. fn = options, options = undefined;
  267. }
  268. if (typeof fn === 'function') {
  269. var res
  270. try {
  271. res = exports.renderFile(path, options);
  272. } catch (ex) {
  273. return fn(ex);
  274. }
  275. return fn(null, res);
  276. }
  277. options = options || {};
  278. var key = path + ':string';
  279. options.filename = path;
  280. var str = options.cache
  281. ? exports.cache[key] || (exports.cache[key] = fs.readFileSync(path, 'utf8'))
  282. : fs.readFileSync(path, 'utf8');
  283. return exports.render(str, options);
  284. };
  285. /**
  286. * Compile a Jade file at the given `path` for use on the client.
  287. *
  288. * @param {String} path
  289. * @param {Object} options
  290. * @returns {String}
  291. * @api public
  292. */
  293. exports.compileFileClient = function(path, options){
  294. options = options || {};
  295. var key = path + ':string';
  296. options.filename = path;
  297. var str = options.cache
  298. ? exports.cache[key] || (exports.cache[key] = fs.readFileSync(path, 'utf8'))
  299. : fs.readFileSync(path, 'utf8');
  300. return exports.compileClient(str, options);
  301. };
  302. /**
  303. * Express support.
  304. */
  305. exports.__express = exports.renderFile;