Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

gonzales.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env node
  2. 'use strict';
  3. var parseArgs = require('minimist');
  4. var gonzales = require('..');
  5. var fs = require('fs');
  6. var path = require('path');
  7. var options = getOptions();
  8. if (options.help) {
  9. displayHelp();
  10. process.exit(0);
  11. }
  12. if (isSTDIN()) {
  13. processSTDIN();
  14. } else {
  15. processFile(options._[0]);
  16. }
  17. function getOptions() {
  18. var parserOptions = {
  19. boolean: ['silent', 'simple'],
  20. alias: {
  21. help: 'h',
  22. syntax: 's',
  23. context: 'c'
  24. }
  25. };
  26. return parseArgs(process.argv.slice(2), parserOptions);
  27. }
  28. function isSTDIN() {
  29. return options._.indexOf('-') !== -1;
  30. }
  31. function processSTDIN() {
  32. var input = '';
  33. process.stdin.resume();
  34. process.stdin.setEncoding('utf8');
  35. process.stdin.on('data', data => {
  36. input += data;
  37. });
  38. process.stdin.on('end', () => {
  39. processInputData(input);
  40. });
  41. }
  42. function processFile(file) {
  43. if (!file) process.exit(0);
  44. if (!options.syntax) options.syntax = path.extname(file).substring(1);
  45. var css = fs.readFileSync(file, 'utf-8').trim();
  46. processInputData(css);
  47. }
  48. function processInputData(input) {
  49. try {
  50. var ast = gonzales.parse(input, {
  51. syntax: options.syntax,
  52. context: options.context
  53. });
  54. printTree(ast);
  55. process.exit(0);
  56. } catch (e) {
  57. if (!options.silent) process.stderr.write(e.toString());
  58. process.exit(1);
  59. }
  60. }
  61. function printTree(ast) {
  62. if (!options.simple) {
  63. var tree = ast.toJson();
  64. process.stdout.write(tree);
  65. } else {
  66. var lastLevel;
  67. ast.traverse(function(node, i, parent, lastLevel) {
  68. var type = node.type;
  69. var spaces = new Array(lastLevel).join(' |');
  70. if (typeof node.content === 'string') {
  71. var content = JSON.stringify(node.content);
  72. console.log(spaces, '->', type);
  73. console.log(spaces, ' ', content);
  74. } else {
  75. console.log(spaces, '->', type);
  76. }
  77. });
  78. var spaces = new Array(lastLevel).join(' -');
  79. console.log(spaces);
  80. }
  81. }
  82. function displayHelp() {
  83. var help = [
  84. 'NAME',
  85. ' gonzlaes-pe — Parse a css file and print its parse tree in JSON',
  86. '',
  87. 'SYNOPSIS',
  88. ' gonzales-pe [options] file.js',
  89. ' cat file.js | gonzales-pe [options] -',
  90. '',
  91. 'OPTIONS',
  92. ' -s, --syntax',
  93. ' Syntax name: css, less, sass or scss.',
  94. ' -c, --context',
  95. ' Context of code part. See docs on node types for more info.',
  96. ' --simple',
  97. ' Print a simplified parse tree structure instead of JSON.',
  98. ' --silent',
  99. ' Don\'t print any error messages.'
  100. ];
  101. console.log(help.join('\n'));
  102. }