Skip to content

Commit bbe0a50

Browse files
Merge pull request #8 from phillip-kruger/main
Added support for more file extensions
2 parents 87723cf + e5448b0 commit bbe0a50

File tree

6 files changed

+588
-2
lines changed

6 files changed

+588
-2
lines changed

diff.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var TOKEN_NAMES = {
2+
'+': 'inserted',
3+
'-': 'deleted',
4+
'@': 'meta'
5+
};
6+
7+
export const diff = {
8+
name: "diff",
9+
token: function(stream) {
10+
var tw_pos = stream.string.search(/[\t ]+?$/);
11+
12+
if (!stream.sol() || tw_pos === 0) {
13+
stream.skipToEnd();
14+
return ("error " + (
15+
TOKEN_NAMES[stream.string.charAt(0)] || '')).replace(/ $/, '');
16+
}
17+
18+
var token_name = TOKEN_NAMES[stream.peek()] || stream.skipToEnd();
19+
20+
if (tw_pos === -1) {
21+
stream.skipToEnd();
22+
} else {
23+
stream.pos = tw_pos;
24+
}
25+
26+
return token_name;
27+
}
28+
};
29+

dockerfile.js

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
import {simpleMode} from "./simple-mode.js"
2+
3+
var from = "from";
4+
var fromRegex = new RegExp("^(\\s*)\\b(" + from + ")\\b", "i");
5+
6+
var shells = ["run", "cmd", "entrypoint", "shell"];
7+
var shellsAsArrayRegex = new RegExp("^(\\s*)(" + shells.join('|') + ")(\\s+\\[)", "i");
8+
9+
var expose = "expose";
10+
var exposeRegex = new RegExp("^(\\s*)(" + expose + ")(\\s+)", "i");
11+
12+
var others = [
13+
"arg", "from", "maintainer", "label", "env",
14+
"add", "copy", "volume", "user",
15+
"workdir", "onbuild", "stopsignal", "healthcheck", "shell"
16+
];
17+
18+
// Collect all Dockerfile directives
19+
var instructions = [from, expose].concat(shells).concat(others),
20+
instructionRegex = "(" + instructions.join('|') + ")",
21+
instructionOnlyLine = new RegExp("^(\\s*)" + instructionRegex + "(\\s*)(#.*)?$", "i"),
22+
instructionWithArguments = new RegExp("^(\\s*)" + instructionRegex + "(\\s+)", "i");
23+
24+
export const dockerFile = simpleMode({
25+
start: [
26+
// Block comment: This is a line starting with a comment
27+
{
28+
regex: /^\s*#.*$/,
29+
sol: true,
30+
token: "comment"
31+
},
32+
{
33+
regex: fromRegex,
34+
token: [null, "keyword"],
35+
sol: true,
36+
next: "from"
37+
},
38+
// Highlight an instruction without any arguments (for convenience)
39+
{
40+
regex: instructionOnlyLine,
41+
token: [null, "keyword", null, "error"],
42+
sol: true
43+
},
44+
{
45+
regex: shellsAsArrayRegex,
46+
token: [null, "keyword", null],
47+
sol: true,
48+
next: "array"
49+
},
50+
{
51+
regex: exposeRegex,
52+
token: [null, "keyword", null],
53+
sol: true,
54+
next: "expose"
55+
},
56+
// Highlight an instruction followed by arguments
57+
{
58+
regex: instructionWithArguments,
59+
token: [null, "keyword", null],
60+
sol: true,
61+
next: "arguments"
62+
},
63+
{
64+
regex: /./,
65+
token: null
66+
}
67+
],
68+
from: [
69+
{
70+
regex: /\s*$/,
71+
token: null,
72+
next: "start"
73+
},
74+
{
75+
// Line comment without instruction arguments is an error
76+
regex: /(\s*)(#.*)$/,
77+
token: [null, "error"],
78+
next: "start"
79+
},
80+
{
81+
regex: /(\s*\S+\s+)(as)/i,
82+
token: [null, "keyword"],
83+
next: "start"
84+
},
85+
// Fail safe return to start
86+
{
87+
token: null,
88+
next: "start"
89+
}
90+
],
91+
single: [
92+
{
93+
regex: /(?:[^\\']|\\.)/,
94+
token: "string"
95+
},
96+
{
97+
regex: /'/,
98+
token: "string",
99+
pop: true
100+
}
101+
],
102+
double: [
103+
{
104+
regex: /(?:[^\\"]|\\.)/,
105+
token: "string"
106+
},
107+
{
108+
regex: /"/,
109+
token: "string",
110+
pop: true
111+
}
112+
],
113+
array: [
114+
{
115+
regex: /\]/,
116+
token: null,
117+
next: "start"
118+
},
119+
{
120+
regex: /"(?:[^\\"]|\\.)*"?/,
121+
token: "string"
122+
}
123+
],
124+
expose: [
125+
{
126+
regex: /\d+$/,
127+
token: "number",
128+
next: "start"
129+
},
130+
{
131+
regex: /[^\d]+$/,
132+
token: null,
133+
next: "start"
134+
},
135+
{
136+
regex: /\d+/,
137+
token: "number"
138+
},
139+
{
140+
regex: /[^\d]+/,
141+
token: null
142+
},
143+
// Fail safe return to start
144+
{
145+
token: null,
146+
next: "start"
147+
}
148+
],
149+
arguments: [
150+
{
151+
regex: /^\s*#.*$/,
152+
sol: true,
153+
token: "comment"
154+
},
155+
{
156+
regex: /"(?:[^\\"]|\\.)*"?$/,
157+
token: "string",
158+
next: "start"
159+
},
160+
{
161+
regex: /"/,
162+
token: "string",
163+
push: "double"
164+
},
165+
{
166+
regex: /'(?:[^\\']|\\.)*'?$/,
167+
token: "string",
168+
next: "start"
169+
},
170+
{
171+
regex: /'/,
172+
token: "string",
173+
push: "single"
174+
},
175+
{
176+
regex: /[^#"']+[\\`]$/,
177+
token: null
178+
},
179+
{
180+
regex: /[^#"']+$/,
181+
token: null,
182+
next: "start"
183+
},
184+
{
185+
regex: /[^#"']+/,
186+
token: null
187+
},
188+
// Fail safe return to start
189+
{
190+
token: null,
191+
next: "start"
192+
}
193+
],
194+
languageData: {
195+
commentTokens: {line: "#"}
196+
}
197+
});
198+

protobuf.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
function wordRegexp(words) {
2+
return new RegExp("^((" + words.join(")|(") + "))\\b", "i");
3+
};
4+
5+
var keywordArray = [
6+
"package", "message", "import", "syntax",
7+
"required", "optional", "repeated", "reserved", "default", "extensions", "packed",
8+
"bool", "bytes", "double", "enum", "float", "string",
9+
"int32", "int64", "uint32", "uint64", "sint32", "sint64", "fixed32", "fixed64", "sfixed32", "sfixed64",
10+
"option", "service", "rpc", "returns"
11+
];
12+
var keywords = wordRegexp(keywordArray);
13+
14+
var identifiers = new RegExp("^[_A-Za-z\xa1-\uffff][_A-Za-z0-9\xa1-\uffff]*");
15+
16+
function tokenBase(stream) {
17+
// whitespaces
18+
if (stream.eatSpace()) return null;
19+
20+
// Handle one line Comments
21+
if (stream.match("//")) {
22+
stream.skipToEnd();
23+
return "comment";
24+
}
25+
26+
// Handle Number Literals
27+
if (stream.match(/^[0-9\.+-]/, false)) {
28+
if (stream.match(/^[+-]?0x[0-9a-fA-F]+/))
29+
return "number";
30+
if (stream.match(/^[+-]?\d*\.\d+([EeDd][+-]?\d+)?/))
31+
return "number";
32+
if (stream.match(/^[+-]?\d+([EeDd][+-]?\d+)?/))
33+
return "number";
34+
}
35+
36+
// Handle Strings
37+
if (stream.match(/^"([^"]|(""))*"/)) { return "string"; }
38+
if (stream.match(/^'([^']|(''))*'/)) { return "string"; }
39+
40+
// Handle words
41+
if (stream.match(keywords)) { return "keyword"; }
42+
if (stream.match(identifiers)) { return "variable"; } ;
43+
44+
// Handle non-detected items
45+
stream.next();
46+
return null;
47+
};
48+
49+
export const protobuf = {
50+
name: "protobuf",
51+
token: tokenBase,
52+
languageData: {
53+
autocomplete: keywordArray
54+
}
55+
}

qui-code-block.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ import { StreamLanguage } from "@codemirror/language"
1616
import { properties } from './properties.js';
1717
import { asciiArmor } from './asciiarmor.js';
1818
import { powerShell } from './powershell.js';
19+
import { shell } from './shell.js';
20+
import { protobuf } from './protobuf.js';
21+
import { dockerFile } from './dockerfile.js';
22+
import { diff } from './diff.js';
1923
import { basicLight } from './lightTheme.js';
2024
import { basicDark } from './darkTheme.js';
2125

@@ -231,7 +235,6 @@ class QuiCodeBlock extends LitElement {
231235
switch (this.mode) {
232236
case 'js':
233237
return javascript();
234-
case 'pom':
235238
case 'xml':
236239
return xml();
237240
case 'json':
@@ -241,12 +244,16 @@ class QuiCodeBlock extends LitElement {
241244
case 'sql':
242245
return sql();
243246
case 'yaml':
247+
case 'yml':
244248
return yaml();
245249
case 'html':
250+
case 'xhtml':
251+
case 'htm':
246252
return htmlLanguage();
247253
case 'css':
248254
return cssLanguage();
249255
case 'markdown':
256+
case 'md':
250257
return markdown();
251258
case 'scss':
252259
case 'sass':
@@ -255,8 +262,25 @@ class QuiCodeBlock extends LitElement {
255262
return StreamLanguage.define(asciiArmor);
256263
case 'properties':
257264
return StreamLanguage.define(properties);
258-
default:
265+
case 'dockerFile':
266+
case 'dockerfile':
267+
case 'Dockerfile':
268+
return StreamLanguage.define(dockerFile);
269+
case 'ps1':
270+
case 'psd1':
271+
case 'psm1':
259272
return StreamLanguage.define(powerShell);
273+
case 'protobuf':
274+
case 'pb':
275+
case 'txtpb':
276+
case 'binpb':
277+
return StreamLanguage.define(protobuf);
278+
case 'diff':
279+
case 'difffile':
280+
case 'patch':
281+
return StreamLanguage.define(diff);
282+
default:
283+
return StreamLanguage.define(shell);
260284
}
261285
}
262286

0 commit comments

Comments
 (0)