hanabi.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  3. typeof define === 'function' && define.amd ? define(factory) :
  4. (global.hanabi = factory());
  5. }(this, (function () {
  6. 'use strict';
  7. function createCommonjsModule(fn, module) {
  8. return module = { exports: {} }, fn(module, module.exports), module.exports;
  9. }
  10. var index$1 = createCommonjsModule(function (module) {
  11. 'use strict';
  12. var comment = module.exports = function () {
  13. return new RegExp('(?:' + comment.line().source + ')|(?:' + comment.block().source + ')', 'gm');
  14. };
  15. comment.line = function () {
  16. return /(?:^|\s)\/\/(.+?)$/gm;
  17. };
  18. comment.block = function () {
  19. return /\/\*([\S\s]*?)\*\//gm;
  20. };
  21. });
  22. var defaultColors = ['23AC69', '91C132', 'F19726', 'E8552D', '1AAB8E', 'E1147F', '2980C1', '1BA1E6', '9FA0A0', 'F19726', 'E30B20', 'E30B20', 'A3338B'];
  23. var index = function (input, ref) {
  24. if (ref === void 0) ref = {};
  25. var colors = ref.colors; if (colors === void 0) colors = defaultColors;
  26. var index = 0;
  27. var cache = {};
  28. var wordRe = /[\u4E00-\u9FFF\u3400-\u4dbf\uf900-\ufaff\u3040-\u309f\uac00-\ud7af\u0400-\u04FF]+|\w+/;
  29. var leftAngleRe = /</;
  30. var re = new RegExp(("(" + (wordRe.source) + "|" + (leftAngleRe.source) + ")|(" + (index$1().source) + ")"), 'gmi');
  31. return input
  32. .replace(re, function (m, word, cm) {
  33. if (cm) {
  34. return toComment(cm)
  35. }
  36. if (word === '<') {
  37. return '&lt;'
  38. }
  39. var color;
  40. if (cache[word]) {
  41. color = cache[word];
  42. } else {
  43. color = colors[index];
  44. cache[word] = color;
  45. }
  46. var out = "<span style=\"color: #" + color + "\">" + word + "</span>";
  47. index = ++index % colors.length;
  48. return out
  49. })
  50. };
  51. function toComment(cm) {
  52. return ("<span style=\"color: slategray\">" + cm + "</span>")
  53. }
  54. return index;
  55. })));