Skip to content

Commit 31b83d8

Browse files
committed
CSS snippets support
1 parent 8701550 commit 31b83d8

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

index.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export class SnippetInjector {
6464
this._fileProcessors['.xml'] = this.processXMLFileCore;
6565
this._fileProcessors['.java'] = this.processTSFileCore;
6666
this._fileProcessors['.cs'] = this.processTSFileCore;
67+
this._fileProcessors['.css'] = this.processCSSFileCore;
6768
}
6869

6970
/**
@@ -167,7 +168,6 @@ export class SnippetInjector {
167168

168169
private processTSFileCore(path: string, extensionFilter: string) {
169170
console.log("Processing source file: " + path);
170-
var extname = pathModule.extname(path);
171171
var fileContents = fsModule.readFileSync(path, 'utf8');
172172
var regExpOpen = /\/\/\s*>>\s*(([a-z]+\-)+[a-z]+)\s*/g;
173173
var regExpOpenReplacer = /\/\/\s*>>\s*(?:([a-z]+\-)+[a-z]+)\s+/g;
@@ -181,7 +181,38 @@ export class SnippetInjector {
181181
match = regExpOpen.exec(fileContents);
182182
continue;
183183
}
184-
var regExpCurrentClosing = new RegExp("//\\s*<<\\s*" + match[1]);
184+
var regExpCurrentClosing = new RegExp("//\\s*<<\\s*" + idOfSnippet);
185+
var closingTagMatch = regExpCurrentClosing.exec(fileContents);
186+
if (!closingTagMatch){
187+
throw new Error("Closing tag not found for: " + idOfSnippet);
188+
}
189+
var indexOfClosingTag = closingTagMatch.index;
190+
var snippet = fileContents.substr(matchIndex + matchLength, indexOfClosingTag - matchIndex - matchLength);
191+
snippet = snippet.replace(regExpOpenReplacer, "");
192+
snippet = snippet.replace(regExpCloseReplacer, "");
193+
console.log("Snippet resolved: " + snippet);
194+
this._storedSnippets[extensionFilter + idOfSnippet] = snippet;
195+
match = regExpOpen.exec(fileContents);
196+
}
197+
}
198+
199+
private processCSSFileCore(path: string, extensionFilter: string) {
200+
console.log("Processing source file: " + path);
201+
var fileContents = fsModule.readFileSync(path, 'utf8');
202+
203+
var regExpOpen = /\/\*\s*>>\s*(([a-z]+\-)+[a-z]+)\s*\*\//g;
204+
var regExpOpenReplacer = /\/\*\s*>>\s*(?:([a-z]+\-)+[a-z]+)\s+\*\//g;
205+
var regExpCloseReplacer = /\/\*\s*<<\s*(?:([a-z]+\-)+[a-z]+)\s+\*\//g;
206+
var match = regExpOpen.exec(fileContents);
207+
while (match) {
208+
var matchIndex = match.index;
209+
var matchLength = match[0].length;
210+
var idOfSnippet = match[1];
211+
if (this._storedSnippets[extensionFilter + idOfSnippet] !== undefined) {
212+
match = regExpOpen.exec(fileContents);
213+
continue;
214+
}
215+
var regExpCurrentClosing = new RegExp("/\\*\\s*<<\\s*" + idOfSnippet +"\\s+\\*/");
185216
var closingTagMatch = regExpCurrentClosing.exec(fileContents);
186217
if (!closingTagMatch){
187218
throw new Error("Closing tag not found for: " + idOfSnippet);
@@ -212,7 +243,7 @@ export class SnippetInjector {
212243
match = regExpOpen.exec(fileContents);
213244
continue;
214245
}
215-
var regExpCurrentClosing = new RegExp("<!--\\s*<<\\s*" + match[1] + "\\s+-->");
246+
var regExpCurrentClosing = new RegExp("<!--\\s*<<\\s*" + idOfSnippet + "\\s+-->");
216247
var closingTagMatch = regExpCurrentClosing.exec(fileContents);
217248
if (!closingTagMatch){
218249
throw new Error("Closing tag not found for: " + idOfSnippet);

0 commit comments

Comments
 (0)