theme.js 6.5 KB

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