terminal-highlight.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _chalk = require('chalk');
  4. var _chalk2 = _interopRequireDefault(_chalk);
  5. var _tokenize = require('./tokenize');
  6. var _tokenize2 = _interopRequireDefault(_tokenize);
  7. var _input = require('./input');
  8. var _input2 = _interopRequireDefault(_input);
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. var HIGHLIGHT_THEME = {
  11. 'brackets': _chalk2.default.cyan,
  12. 'at-word': _chalk2.default.cyan,
  13. 'call': _chalk2.default.cyan,
  14. 'comment': _chalk2.default.gray,
  15. 'string': _chalk2.default.green,
  16. 'class': _chalk2.default.yellow,
  17. 'hash': _chalk2.default.magenta,
  18. '(': _chalk2.default.cyan,
  19. ')': _chalk2.default.cyan,
  20. '{': _chalk2.default.yellow,
  21. '}': _chalk2.default.yellow,
  22. '[': _chalk2.default.yellow,
  23. ']': _chalk2.default.yellow,
  24. ':': _chalk2.default.yellow,
  25. ';': _chalk2.default.yellow
  26. };
  27. function getTokenType(_ref, processor) {
  28. var type = _ref[0],
  29. value = _ref[1];
  30. if (type === 'word') {
  31. if (value[0] === '.') {
  32. return 'class';
  33. }
  34. if (value[0] === '#') {
  35. return 'hash';
  36. }
  37. }
  38. if (!processor.endOfFile()) {
  39. var next = processor.nextToken();
  40. processor.back(next);
  41. if (next[0] === 'brackets' || next[0] === '(') return 'call';
  42. }
  43. return type;
  44. }
  45. function terminalHighlight(css) {
  46. var processor = (0, _tokenize2.default)(new _input2.default(css), { ignoreErrors: true });
  47. var result = '';
  48. var _loop = function _loop() {
  49. var token = processor.nextToken();
  50. var color = HIGHLIGHT_THEME[getTokenType(token, processor)];
  51. if (color) {
  52. result += token[1].split(/\r?\n/).map(function (i) {
  53. return color(i);
  54. }).join('\n');
  55. } else {
  56. result += token[1];
  57. }
  58. };
  59. while (!processor.endOfFile()) {
  60. _loop();
  61. }
  62. return result;
  63. }
  64. exports.default = terminalHighlight;
  65. module.exports = exports['default'];
  66. //# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlcm1pbmFsLWhpZ2hsaWdodC5lczYiXSwibmFtZXMiOlsiSElHSExJR0hUX1RIRU1FIiwiY2hhbGsiLCJjeWFuIiwiZ3JheSIsImdyZWVuIiwieWVsbG93IiwibWFnZW50YSIsImdldFRva2VuVHlwZSIsInByb2Nlc3NvciIsInR5cGUiLCJ2YWx1ZSIsImVuZE9mRmlsZSIsIm5leHQiLCJuZXh0VG9rZW4iLCJiYWNrIiwidGVybWluYWxIaWdobGlnaHQiLCJjc3MiLCJJbnB1dCIsImlnbm9yZUVycm9ycyIsInJlc3VsdCIsInRva2VuIiwiY29sb3IiLCJzcGxpdCIsIm1hcCIsImkiLCJqb2luIl0sIm1hcHBpbmdzIjoiOzs7O0FBQUE7Ozs7QUFFQTs7OztBQUNBOzs7Ozs7QUFFQSxJQUFNQSxrQkFBa0I7QUFDcEIsZ0JBQVlDLGdCQUFNQyxJQURFO0FBRXBCLGVBQVlELGdCQUFNQyxJQUZFO0FBR3BCLFlBQVlELGdCQUFNQyxJQUhFO0FBSXBCLGVBQVlELGdCQUFNRSxJQUpFO0FBS3BCLGNBQVlGLGdCQUFNRyxLQUxFO0FBTXBCLGFBQVlILGdCQUFNSSxNQU5FO0FBT3BCLFlBQVlKLGdCQUFNSyxPQVBFO0FBUXBCLFNBQVlMLGdCQUFNQyxJQVJFO0FBU3BCLFNBQVlELGdCQUFNQyxJQVRFO0FBVXBCLFNBQVlELGdCQUFNSSxNQVZFO0FBV3BCLFNBQVlKLGdCQUFNSSxNQVhFO0FBWXBCLFNBQVlKLGdCQUFNSSxNQVpFO0FBYXBCLFNBQVlKLGdCQUFNSSxNQWJFO0FBY3BCLFNBQVlKLGdCQUFNSSxNQWRFO0FBZXBCLFNBQVlKLGdCQUFNSTtBQWZ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