1 | const
|
2 | sgUtil = require('../util'),
|
3 | Collector = require('./Collector');
|
4 |
|
5 | class PatternCollector extends Collector {
|
6 |
|
7 | constructor(...args) {
|
8 | super(...args);
|
9 | this.modifierDelimiter = {};
|
10 | this.warnOnMissingDataFile = false;
|
11 | }
|
12 |
|
13 | collectSection(sectionConfig) {
|
14 | const {modifierDelimiter = ['{{', '}}'], collector} = sectionConfig;
|
15 | this.modifierDelimiter[collector] = modifierDelimiter;
|
16 | super.collectSection(sectionConfig);
|
17 | }
|
18 |
|
19 | createFileObj(fileOptions) {
|
20 | const
|
21 | file = super.createFileObj(fileOptions),
|
22 | {filename, collector} = file;
|
23 |
|
24 | file.modifiers = this.getAssignedModifiers(filename, collector);
|
25 |
|
26 | return file;
|
27 | }
|
28 |
|
29 | getCopySourceFunction() {
|
30 | return () => {};
|
31 | }
|
32 |
|
33 | getAssignedModifiers(filename, collector) {
|
34 |
|
35 | const
|
36 | {modifierDelimiter: {[collector]: [leftDelimiter, rightDelimiter]}} = this,
|
37 | escapedLeftDelimiter = leftDelimiter.replace(/[^A-Za-z0-9_]/g, '\\$&'),
|
38 | escapedRightDelimiter = rightDelimiter.replace(/[^A-Za-z0-9_]/g, '\\$&'),
|
39 | regex = new RegExp(`${escapedLeftDelimiter}([^${escapedRightDelimiter}]+)${escapedRightDelimiter}`, "g"),
|
40 | template = sgUtil.readFileContents(filename),
|
41 | assignedModifiers = [];
|
42 |
|
43 | let m;
|
44 |
|
45 | while ((m = regex.exec(template)) !== null) {
|
46 |
|
47 | if (m.index === regex.lastIndex) {
|
48 | regex.lastIndex++;
|
49 | }
|
50 | const modifier = {};
|
51 |
|
52 | m.forEach((match, groupIndex) => {
|
53 |
|
54 | if (groupIndex === 0) {
|
55 | modifier['replaceKey'] = match.trim()
|
56 | }
|
57 | if (groupIndex === 1) {
|
58 | modifier['blockIdentifier'] = match.trim()
|
59 | }
|
60 | });
|
61 |
|
62 |
|
63 | assignedModifiers.push(modifier)
|
64 | }
|
65 |
|
66 | return assignedModifiers.filter((modifier, index) => assignedModifiers.indexOf(modifier) === index);
|
67 | }
|
68 | }
|
69 | module.exports = PatternCollector; |
\ | No newline at end of file |