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 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #!/usr/bin/env node
  2. /**
  3. * Module dependencies.
  4. */
  5. var fs = require('fs')
  6. , program = require('commander')
  7. , path = require('path')
  8. , basename = path.basename
  9. , dirname = path.dirname
  10. , resolve = path.resolve
  11. , exists = fs.existsSync || path.existsSync
  12. , join = path.join
  13. , monocle = require('monocle')()
  14. , mkdirp = require('mkdirp')
  15. , jade = require('../');
  16. // jade options
  17. var options = {};
  18. // options
  19. program
  20. .version(require('../package.json').version)
  21. .usage('[options] [dir|file ...]')
  22. .option('-O, --obj <str>', 'javascript options object')
  23. .option('-o, --out <dir>', 'output the compiled html to <dir>')
  24. .option('-p, --path <path>', 'filename used to resolve includes')
  25. .option('-P, --pretty', 'compile pretty html output')
  26. .option('-c, --client', 'compile function for client-side runtime.js')
  27. .option('-n, --name <str>', 'The name of the compiled template (requires --client)')
  28. .option('-D, --no-debug', 'compile without debugging (smaller functions)')
  29. .option('-w, --watch', 'watch files for changes and automatically re-render')
  30. .option('--name-after-file', 'Name the template after the last section of the file path (requires --client and overriden by --name)')
  31. program.on('--help', function(){
  32. console.log(' Examples:');
  33. console.log('');
  34. console.log(' # translate jade the templates dir');
  35. console.log(' $ jade templates');
  36. console.log('');
  37. console.log(' # create {foo,bar}.html');
  38. console.log(' $ jade {foo,bar}.jade');
  39. console.log('');
  40. console.log(' # jade over stdio');
  41. console.log(' $ jade < my.jade > my.html');
  42. console.log('');
  43. console.log(' # jade over stdio');
  44. console.log(' $ echo \'h1 Jade!\' | jade');
  45. console.log('');
  46. console.log(' # foo, bar dirs rendering to /tmp');
  47. console.log(' $ jade foo bar --out /tmp ');
  48. console.log('');
  49. });
  50. program.parse(process.argv);
  51. // options given, parse them
  52. if (program.obj) {
  53. if (exists(program.obj)) {
  54. options = JSON.parse(fs.readFileSync(program.obj));
  55. } else {
  56. options = eval('(' + program.obj + ')');
  57. }
  58. }
  59. // --filename
  60. if (program.path) options.filename = program.path;
  61. // --no-debug
  62. options.compileDebug = program.debug;
  63. // --client
  64. options.client = program.client;
  65. // --pretty
  66. options.pretty = program.pretty;
  67. // --watch
  68. options.watch = program.watch;
  69. // --name
  70. options.name = program.name;
  71. // left-over args are file paths
  72. var files = program.args;
  73. // compile files
  74. if (files.length) {
  75. console.log();
  76. if (options.watch) {
  77. // keep watching when error occured.
  78. process.on('uncaughtException', function(err) {
  79. console.error(err);
  80. });
  81. files.forEach(renderFile);
  82. monocle.watchFiles({
  83. files: files,
  84. listener: function(file) {
  85. renderFile(file.absolutePath);
  86. }
  87. });
  88. } else {
  89. files.forEach(renderFile);
  90. }
  91. process.on('exit', function () {
  92. console.log();
  93. });
  94. // stdio
  95. } else {
  96. stdin();
  97. }
  98. /**
  99. * Compile from stdin.
  100. */
  101. function stdin() {
  102. var buf = '';
  103. process.stdin.setEncoding('utf8');
  104. process.stdin.on('data', function(chunk){ buf += chunk; });
  105. process.stdin.on('end', function(){
  106. var output;
  107. if (options.client) {
  108. output = jade.compileClient(buf, options);
  109. } else {
  110. var fn = jade.compile(buf, options);
  111. var output = fn(options);
  112. }
  113. process.stdout.write(output);
  114. }).resume();
  115. process.on('SIGINT', function() {
  116. process.stdout.write('\n');
  117. process.stdin.emit('end');
  118. process.stdout.write('\n');
  119. process.exit();
  120. })
  121. }
  122. /**
  123. * Process the given path, compiling the jade files found.
  124. * Always walk the subdirectories.
  125. */
  126. function renderFile(path) {
  127. var re = /\.jade$/;
  128. fs.lstat(path, function(err, stat) {
  129. if (err) throw err;
  130. // Found jade file
  131. if (stat.isFile() && re.test(path)) {
  132. fs.readFile(path, 'utf8', function(err, str){
  133. if (err) throw err;
  134. options.filename = path;
  135. if (program.nameAfterFile) {
  136. options.name = getNameFromFileName(path);
  137. }
  138. var fn = options.client ? jade.compileClient(str, options) : jade.compile(str, options);
  139. var extname = options.client ? '.js' : '.html';
  140. path = path.replace(re, extname);
  141. if (program.out) path = join(program.out, basename(path));
  142. var dir = resolve(dirname(path));
  143. mkdirp(dir, 0755, function(err){
  144. if (err) throw err;
  145. try {
  146. var output = options.client ? fn : fn(options);
  147. fs.writeFile(path, output, function(err){
  148. if (err) throw err;
  149. console.log(' \033[90mrendered \033[36m%s\033[0m', path);
  150. });
  151. } catch (e) {
  152. if (options.watch) {
  153. console.error(e.stack || e.message || e);
  154. } else {
  155. throw e
  156. }
  157. }
  158. });
  159. });
  160. // Found directory
  161. } else if (stat.isDirectory()) {
  162. fs.readdir(path, function(err, files) {
  163. if (err) throw err;
  164. files.map(function(filename) {
  165. return path + '/' + filename;
  166. }).forEach(renderFile);
  167. });
  168. }
  169. });
  170. }
  171. /**
  172. * Get a sensible name for a template function from a file path
  173. *
  174. * @param {String} filename
  175. * @returns {String}
  176. */
  177. function getNameFromFileName(filename) {
  178. var file = path.basename(filename, '.jade');
  179. return file.toLowerCase().replace(/[^a-z0-9]+([a-z])/g, function (_, character) {
  180. return character.toUpperCase();
  181. }) + 'Template';
  182. }