-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathexample-block.js
140 lines (135 loc) · 3.8 KB
/
example-block.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*global module, require*/
module.exports = function ExampleBlock() {
'use strict';
var self = this,
RegexUtil = require('./regex-util'),
regexUtil = new RegexUtil(),
TableUtil = require('./table-util'),
tableUtil = new TableUtil(),
Normaliser = require('./normaliser'),
normaliser = new Normaliser(),
lines = [],
toItems = function (lines) {
return lines.map(tableUtil.cellValuesForRow);
},
toTable = function (lines) {
var tableItems = lines, result = {type: 'table'};
if (lines.length > 2 && regexUtil.isTableHeaderDivider(lines[1])) {
result.titles = tableUtil.cellValuesForRow(lines[0]);
if (normaliser.containsDuplicates(result.titles)) {
throw new SyntaxError('Attachment table has multiple equivalent column names');
}
if (result.titles.some(regexUtil.isEmpty)) {
throw new SyntaxError('Attachment table has a column without a name');
}
tableItems = lines.slice(2);
}
result.items = toItems(tableItems);
return result;
},
getAttachmentTable = function (tableLines) {
if (!regexUtil.isTableItem(tableLines[0])) {
return false;
}
return toTable(tableLines);
},
getAttachmentList = function (listLines) {
var listSymbol;
if (!regexUtil.isListItem(listLines[0])) {
return false;
}
listSymbol = regexUtil.getListSymbol(listLines[0]);
return {type: 'list',
ordered: !isNaN(parseFloat(listSymbol)),
items: listLines.map(regexUtil.lineItemContent),
symbol: listSymbol
};
};
self.addLine = function (lineText) {
lines.unshift(lineText);
};
self.getAttachment = function () {
var attachmentLines = self.getAttachmentLines().filter(regexUtil.assertionLine);
if (attachmentLines.length === 0) {
return false;
}
return getAttachmentList(attachmentLines) || getAttachmentTable(attachmentLines);
};
self.getAttachmentLines = function () {
var topLine,
isAttachmentLine = function (line) {
return regexUtil.isTableItem(line) || regexUtil.isListItem(line);
};
if (lines.length === 0) {
return [];
}
topLine = lines[0];
if (!regexUtil.assertionLine(topLine) || regexUtil.isTableItem(topLine) || regexUtil.isListItem(topLine)) {
return [];
}
return lines.filter(isAttachmentLine);
};
self.canAddLine = function (line) {
var lineType = function (theLine) {
if (regexUtil.isListItem(theLine)) {
return 'list';
} else if (regexUtil.isTableItem(theLine)) {
return 'table';
} else if (regexUtil.assertionLine(theLine)) {
return 'assertion';
} else if (regexUtil.isEmpty(theLine)) {
return 'empty';
} else {
return 'comment';
}
},
topline, newLineType, topLineType;
if (lines.length === 0) {
return true;
}
topline = lines[0];
newLineType = lineType(line);
topLineType = lineType(topline);
if (topLineType == 'assertion') {
return false;
}
switch (newLineType) {
case 'list':
case 'table':
case 'comment':
return topLineType === newLineType;
case 'assertion':
case 'empty':
return ['table', 'list', 'empty'].indexOf(topLineType) >= 0;
}
return false;
};
self.isTableBlock = function () {
var tableLines = lines.filter(regexUtil.isTableItem),
nonTableAssertionLine = function (line) {
return regexUtil.assertionLine(line) && !regexUtil.isTableItem(line);
};
if (tableLines.length === 0) {
return false;
}
if (lines.filter(nonTableAssertionLine).length > 0) {
return false;
}
return true;
};
self.getMatchText = function () {
var nonAttachmentLine = function (line) {
return !regexUtil.isListItem(line) && !regexUtil.isTableItem(line);
},
topLine;
if (lines.length === 0) {
return [];
}
topLine = lines[0];
if (nonAttachmentLine(topLine) && regexUtil.assertionLine(topLine)) {
return lines.filter(nonAttachmentLine);
} else {
return lines;
}
};
};