Last change
on this file since 59329aa was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.3 KB
|
Line | |
---|
1 | 'use strict'
|
---|
2 |
|
---|
3 | let { cyan, gray, green, yellow, magenta } = require('colorette')
|
---|
4 |
|
---|
5 | let tokenizer = require('./tokenize')
|
---|
6 |
|
---|
7 | let Input
|
---|
8 |
|
---|
9 | function registerInput(dependant) {
|
---|
10 | Input = dependant
|
---|
11 | }
|
---|
12 |
|
---|
13 | const HIGHLIGHT_THEME = {
|
---|
14 | 'brackets': cyan,
|
---|
15 | 'at-word': cyan,
|
---|
16 | 'comment': gray,
|
---|
17 | 'string': green,
|
---|
18 | 'class': yellow,
|
---|
19 | 'hash': magenta,
|
---|
20 | 'call': cyan,
|
---|
21 | '(': cyan,
|
---|
22 | ')': cyan,
|
---|
23 | '{': yellow,
|
---|
24 | '}': yellow,
|
---|
25 | '[': yellow,
|
---|
26 | ']': yellow,
|
---|
27 | ':': yellow,
|
---|
28 | ';': yellow
|
---|
29 | }
|
---|
30 |
|
---|
31 | function getTokenType([type, value], processor) {
|
---|
32 | if (type === 'word') {
|
---|
33 | if (value[0] === '.') {
|
---|
34 | return 'class'
|
---|
35 | }
|
---|
36 | if (value[0] === '#') {
|
---|
37 | return 'hash'
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | if (!processor.endOfFile()) {
|
---|
42 | let next = processor.nextToken()
|
---|
43 | processor.back(next)
|
---|
44 | if (next[0] === 'brackets' || next[0] === '(') return 'call'
|
---|
45 | }
|
---|
46 |
|
---|
47 | return type
|
---|
48 | }
|
---|
49 |
|
---|
50 | function terminalHighlight(css) {
|
---|
51 | let processor = tokenizer(new Input(css), { ignoreErrors: true })
|
---|
52 | let result = ''
|
---|
53 | while (!processor.endOfFile()) {
|
---|
54 | let token = processor.nextToken()
|
---|
55 | let color = HIGHLIGHT_THEME[getTokenType(token, processor)]
|
---|
56 | if (color) {
|
---|
57 | result += token[1]
|
---|
58 | .split(/\r?\n/)
|
---|
59 | .map(i => color(i))
|
---|
60 | .join('\n')
|
---|
61 | } else {
|
---|
62 | result += token[1]
|
---|
63 | }
|
---|
64 | }
|
---|
65 | return result
|
---|
66 | }
|
---|
67 |
|
---|
68 | terminalHighlight.registerInput = registerInput
|
---|
69 |
|
---|
70 | module.exports = terminalHighlight
|
---|
Note:
See
TracBrowser
for help on using the repository browser.