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.

84 lines
6.1 KiB

4 years ago
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _chalk = _interopRequireDefault(require("chalk"));
  5. var _tokenize = _interopRequireDefault(require("./tokenize"));
  6. var _input = _interopRequireDefault(require("./input"));
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. var HIGHLIGHT_THEME = {
  9. 'brackets': _chalk.default.cyan,
  10. 'at-word': _chalk.default.cyan,
  11. 'comment': _chalk.default.gray,
  12. 'string': _chalk.default.green,
  13. 'class': _chalk.default.yellow,
  14. 'call': _chalk.default.cyan,
  15. 'hash': _chalk.default.magenta,
  16. '(': _chalk.default.cyan,
  17. ')': _chalk.default.cyan,
  18. '{': _chalk.default.yellow,
  19. '}': _chalk.default.yellow,
  20. '[': _chalk.default.yellow,
  21. ']': _chalk.default.yellow,
  22. ':': _chalk.default.yellow,
  23. ';': _chalk.default.yellow
  24. };
  25. function getTokenType(_ref, processor) {
  26. var type = _ref[0],
  27. value = _ref[1];
  28. if (type === 'word') {
  29. if (value[0] === '.') {
  30. return 'class';
  31. }
  32. if (value[0] === '#') {
  33. return 'hash';
  34. }
  35. }
  36. if (!processor.endOfFile()) {
  37. var next = processor.nextToken();
  38. processor.back(next);
  39. if (next[0] === 'brackets' || next[0] === '(') return 'call';
  40. }
  41. return type;
  42. }
  43. function terminalHighlight(css) {
  44. var processor = (0, _tokenize.default)(new _input.default(css), {
  45. ignoreErrors: true
  46. });
  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. var _default = terminalHighlight;
  65. exports.default = _default;
  66. module.exports = exports.default;
  67. //# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlcm1pbmFsLWhpZ2hsaWdodC5lczYiXSwibmFtZXMiOlsiSElHSExJR0hUX1RIRU1FIiwiY2hhbGsiLCJjeWFuIiwiZ3JheSIsImdyZWVuIiwieWVsbG93IiwibWFnZW50YSIsImdldFRva2VuVHlwZSIsInByb2Nlc3NvciIsInR5cGUiLCJ2YWx1ZSIsImVuZE9mRmlsZSIsIm5leHQiLCJuZXh0VG9rZW4iLCJiYWNrIiwidGVybWluYWxIaWdobGlnaHQiLCJjc3MiLCJJbnB1dCIsImlnbm9yZUVycm9ycyIsInJlc3VsdCIsInRva2VuIiwiY29sb3IiLCJzcGxpdCIsIm1hcCIsImkiLCJqb2luIl0sIm1hcHBpbmdzIjoiOzs7OztBQUFBOztBQUVBOztBQUNBOzs7O0FBRUEsSUFBTUEsZUFBZSxHQUFHO0FBQ3RCLGNBQVlDLGVBQU1DLElBREk7QUFFdEIsYUFBV0QsZUFBTUMsSUFGSztBQUd0QixhQUFXRCxlQUFNRSxJQUhLO0FBSXRCLFlBQVVGLGVBQU1HLEtBSk07QUFLdEIsV0FBU0gsZUFBTUksTUFMTztBQU10QixVQUFRSixlQUFNQyxJQU5RO0FBT3RCLFVBQVFELGVBQU1LLE9BUFE7QUFRdEIsT0FBS0wsZUFBTUMsSUFSVztBQVN0QixPQUFLRCxlQUFNQyxJQVRXO0FBVXRCLE9BQUtELGVBQU1JLE1BVlc7QUFXdEIsT0FBS0osZUFBTUksTUFYVztBQVl0QixPQUFLSixlQUFNSSxNQVpXO0FBYXRCLE9BQUtKLGVBQU1JLE1BYlc7QUFjdEIsT0FBS0osZUFBTUksTUFkVztBQWV0QixPQUFLSixlQUFNSTtBQWZXLENBQXhCOztBQWtCQSxTQUFTRSxZQUFULE9BQXNDQyxTQUF0QyxFQUFpRDtBQUFBLE1BQXpCQyxJQUF5QjtBQU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