theme.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.parse = exports.stringify = exports.toJson = exports.fromJson = exports.DEFAULT_THEME = exports.plain = void 0;
  7. var chalk_1 = __importDefault(require("chalk"));
  8. /**
  9. * Identity function for tokens that should not be styled (returns the input string as-is).
  10. * See [[Theme]] for an example.
  11. */
  12. var plain = function (codePart) { return codePart; };
  13. exports.plain = plain;
  14. /**
  15. * The default theme. It is possible to override just individual keys.
  16. */
  17. exports.DEFAULT_THEME = {
  18. /**
  19. * keyword in a regular Algol-style language
  20. */
  21. keyword: chalk_1.default.blue,
  22. /**
  23. * built-in or library object (constant, class, function)
  24. */
  25. built_in: chalk_1.default.cyan,
  26. /**
  27. * user-defined type in a language with first-class syntactically significant types, like
  28. * Haskell
  29. */
  30. type: chalk_1.default.cyan.dim,
  31. /**
  32. * special identifier for a built-in value ("true", "false", "null")
  33. */
  34. literal: chalk_1.default.blue,
  35. /**
  36. * number, including units and modifiers, if any.
  37. */
  38. number: chalk_1.default.green,
  39. /**
  40. * literal regular expression
  41. */
  42. regexp: chalk_1.default.red,
  43. /**
  44. * literal string, character
  45. */
  46. string: chalk_1.default.red,
  47. /**
  48. * parsed section inside a literal string
  49. */
  50. subst: exports.plain,
  51. /**
  52. * symbolic constant, interned string, goto label
  53. */
  54. symbol: exports.plain,
  55. /**
  56. * class or class-level declaration (interfaces, traits, modules, etc)
  57. */
  58. class: chalk_1.default.blue,
  59. /**
  60. * function or method declaration
  61. */
  62. function: chalk_1.default.yellow,
  63. /**
  64. * name of a class or a function at the place of declaration
  65. */
  66. title: exports.plain,
  67. /**
  68. * block of function arguments (parameters) at the place of declaration
  69. */
  70. params: exports.plain,
  71. /**
  72. * comment
  73. */
  74. comment: chalk_1.default.green,
  75. /**
  76. * documentation markup within comments
  77. */
  78. doctag: chalk_1.default.green,
  79. /**
  80. * flags, modifiers, annotations, processing instructions, preprocessor directive, etc
  81. */
  82. meta: chalk_1.default.grey,
  83. /**
  84. * keyword or built-in within meta construct
  85. */
  86. 'meta-keyword': exports.plain,
  87. /**
  88. * string within meta construct
  89. */
  90. 'meta-string': exports.plain,
  91. /**
  92. * heading of a section in a config file, heading in text markup
  93. */
  94. section: exports.plain,
  95. /**
  96. * XML/HTML tag
  97. */
  98. tag: chalk_1.default.grey,
  99. /**
  100. * name of an XML tag, the first word in an s-expression
  101. */
  102. name: chalk_1.default.blue,
  103. /**
  104. * s-expression name from the language standard library
  105. */
  106. 'builtin-name': exports.plain,
  107. /**
  108. * name of an attribute with no language defined semantics (keys in JSON, setting names in
  109. * .ini), also sub-attribute within another highlighted object, like XML tag
  110. */
  111. attr: chalk_1.default.cyan,
  112. /**
  113. * name of an attribute followed by a structured value part, like CSS properties
  114. */
  115. attribute: exports.plain,
  116. /**
  117. * variable in a config or a template file, environment var expansion in a script
  118. */
  119. variable: exports.plain,
  120. /**
  121. * list item bullet in text markup
  122. */
  123. bullet: exports.plain,
  124. /**
  125. * code block in text markup
  126. */
  127. code: exports.plain,
  128. /**
  129. * emphasis in text markup
  130. */
  131. emphasis: chalk_1.default.italic,
  132. /**
  133. * strong emphasis in text markup
  134. */
  135. strong: chalk_1.default.bold,
  136. /**
  137. * mathematical formula in text markup
  138. */
  139. formula: exports.plain,
  140. /**
  141. * hyperlink in text markup
  142. */
  143. link: chalk_1.default.underline,
  144. /**
  145. * quotation in text markup
  146. */
  147. quote: exports.plain,
  148. /**
  149. * tag selector in CSS
  150. */
  151. 'selector-tag': exports.plain,
  152. /**
  153. * #id selector in CSS
  154. */
  155. 'selector-id': exports.plain,
  156. /**
  157. * .class selector in CSS
  158. */
  159. 'selector-class': exports.plain,
  160. /**
  161. * [attr] selector in CSS
  162. */
  163. 'selector-attr': exports.plain,
  164. /**
  165. * :pseudo selector in CSS
  166. */
  167. 'selector-pseudo': exports.plain,
  168. /**
  169. * tag of a template language
  170. */
  171. 'template-tag': exports.plain,
  172. /**
  173. * variable in a template language
  174. */
  175. 'template-variable': exports.plain,
  176. /**
  177. * added or changed line in a diff
  178. */
  179. addition: chalk_1.default.green,
  180. /**
  181. * deleted line in a diff
  182. */
  183. deletion: chalk_1.default.red,
  184. /**
  185. * things not matched by any token
  186. */
  187. default: exports.plain,
  188. };
  189. /**
  190. * Converts a [[JsonTheme]] with string values to a [[Theme]] with formatter functions. Used by [[parse]].
  191. */
  192. function fromJson(json) {
  193. var theme = {};
  194. for (var _i = 0, _a = Object.keys(json); _i < _a.length; _i++) {
  195. var key = _a[_i];
  196. var style = json[key];
  197. if (Array.isArray(style)) {
  198. ;
  199. theme[key] = style.reduce(function (previous, current) { return (current === 'plain' ? exports.plain : previous[current]); }, chalk_1.default);
  200. }
  201. else {
  202. ;
  203. theme[key] = chalk_1.default[style];
  204. }
  205. }
  206. return theme;
  207. }
  208. exports.fromJson = fromJson;
  209. /**
  210. * Converts a [[Theme]] with formatter functions to a [[JsonTheme]] with string values. Used by [[stringify]].
  211. */
  212. function toJson(theme) {
  213. var jsonTheme = {};
  214. for (var _i = 0, _a = Object.keys(jsonTheme); _i < _a.length; _i++) {
  215. var key = _a[_i];
  216. var style = jsonTheme[key];
  217. jsonTheme[key] = style._styles;
  218. }
  219. return jsonTheme;
  220. }
  221. exports.toJson = toJson;
  222. /**
  223. * Stringifies a [[Theme]] with formatter functions to a JSON string.
  224. *
  225. * ```ts
  226. * import chalk = require('chalk');
  227. * import {stringify} from 'cli-highlight';
  228. * import * as fs from 'fs';
  229. *
  230. * const myTheme: Theme = {
  231. * keyword: chalk.red.bold,
  232. * addition: chalk.green,
  233. * deletion: chalk.red.strikethrough,
  234. * number: plain
  235. * }
  236. * const json = stringify(myTheme);
  237. * fs.writeFile('mytheme.json', json, (err: any) => {
  238. * if (err) throw err;
  239. * console.log('Theme saved');
  240. * });
  241. * ```
  242. */
  243. function stringify(theme) {
  244. return JSON.stringify(toJson(theme));
  245. }
  246. exports.stringify = stringify;
  247. /**
  248. * Parses a JSON string into a [[Theme]] with formatter functions.
  249. *
  250. * ```ts
  251. * import * as fs from 'fs';
  252. * import {parse, highlight} from 'cli-highlight';
  253. *
  254. * fs.readFile('mytheme.json', 'utf8', (err: any, json: string) => {
  255. * if (err) throw err;
  256. * const code = highlight('SELECT * FROM table', {theme: parse(json)});
  257. * console.log(code);
  258. * });
  259. * ```
  260. */
  261. function parse(json) {
  262. return fromJson(JSON.parse(json));
  263. }
  264. exports.parse = parse;
  265. //# sourceMappingURL=theme.js.map