-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathremote-include-processor.js
More file actions
199 lines (182 loc) · 6.83 KB
/
remote-include-processor.js
File metadata and controls
199 lines (182 loc) · 6.83 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const CIRCUMFIX_COMMENT_SUFFIX_RX = / (?:\*[/)]|--%?>)$/
const NEWLINE_RX = /\r\n?|\n/
const TAG_DELIMITER_RX = /[,;]/
const TAG_DIRECTIVE_RX = /\b(?:tag|(end))::(\S+)\[\]$/
const LINES_DOTDOT_RX = /\.\./
function getTags (attrs) {
if ( 'tag' in attrs ) {
const tag = attrs['tag']
if (tag && tag !== '!') {
return tag.charAt() === '!' ? new Map().set(tag.substr(1), false) : new Map().set(tag, true)
}
} else if ( 'tags' in attrs ) {
const tags = attrs['tags']
if (tags) {
let result = new Map()
let any = false
tags.split(TAG_DELIMITER_RX).forEach((tag) => {
if (tag && tag !== '!') {
any = true
tag.charAt() === '!' ? result.set(tag.substr(1), false) : result.set(tag, true)
}
})
if (any) return result
}
}
}
function applyTagFiltering (contents, tags) {
let selecting, selectingDefault, wildcard
if (tags.has('**')) {
if (tags.has('*')) {
selectingDefault = selecting = tags.get('**')
wildcard = tags.get('*')
tags.delete('*')
} else {
selectingDefault = selecting = wildcard = tags.get('**')
}
tags.delete('**')
} else {
selectingDefault = selecting = !Array.from(tags.values()).includes(true)
if (tags.has('*')) {
wildcard = tags.get('*')
tags.delete('*')
}
}
const lines = []
const tagStack = []
const usedTags = []
let activeTag
let lineNum = 0
let startLineNum
contents.split(NEWLINE_RX).forEach((line) => {
lineNum++
let m
let l = line
if (
(l.endsWith('[]') ||
(~l.indexOf('[] ') &&
(m = l.match(CIRCUMFIX_COMMENT_SUFFIX_RX)) &&
(l = l.substr(0, m.index)).endsWith('[]'))) &&
(m = l.match(TAG_DIRECTIVE_RX))
) {
const thisTag = m[2]
if (m[1]) {
if (thisTag === activeTag) {
tagStack.shift()
;[activeTag, selecting] = tagStack.length ? tagStack[0] : [undefined, selectingDefault]
} else if (tags.has(thisTag)) {
const idx = tagStack.findIndex(([name]) => name === thisTag)
if (~idx) {
tagStack.splice(idx, 1)
//console.warn(`line ${lineNum}: mismatched end tag in include: expected ${activeTag}, found ${thisTag}`)
}
//} else {
// //console.warn(`line ${lineNum}: unexpected end tag in include: ${thisTag}`)
//}
}
} else if (tags.has(thisTag)) {
usedTags.push(thisTag)
tagStack.unshift([(activeTag = thisTag), (selecting = tags.get(thisTag))])
} else if (wildcard !== undefined) {
selecting = activeTag && !selecting ? false : wildcard
tagStack.unshift([(activeTag = thisTag), selecting])
}
} else if (selecting) {
if (!startLineNum) startLineNum = lineNum
lines.push(line)
}
})
// Q: use _.difference(Object.keys(tags), usedTags)?
//const missingTags = Object.keys(tags).filter((e) => !usedTags.includes(e))
//if (missingTags.length) {
// console.warn(`tag${missingTags.length > 1 ? 's' : ''} '${missingTags.join(',')}' not found in include`)
//}
return [lines, startLineNum || 1]
}
function getLines (attrs) {
if ( 'lines' in attrs ) {
const lines = attrs['lines']
if (lines) {
// console.warn(`have lines` + lines)
let result = [] // new Map()
let any = false
lines.split(TAG_DELIMITER_RX).forEach((line) => {
if (line && line !== '!') {
let tryMultipleLines = line.split(LINES_DOTDOT_RX)
if ( tryMultipleLines.length === 1 ) {
any = true
result.push([tryMultipleLines[0], tryMultipleLines[0]])
}
else if ( tryMultipleLines.length === 2 ) {
any = true
result.push([tryMultipleLines[0], tryMultipleLines[1]])
}
}
})
if (any) return result
}
}
}
function filterChars(content){
let myRegexp = new RegExp(/[^${\}]+(?=})/g);
let matches = myRegexp.exec(content);
if(matches == null)
return content;
let ret = content;
// matches.forEach(x => {
// let a = new RegExp("\\${"+x+"}",'gm');
// ret = ret.replace(a,'asd');
// })
return ret;
}
function applyLineFiltering (contents, linesToInclude) {
const lines = []
let lineNum = 0
let startLineNum
let registerCurrentLine = false
const nLinesPair = linesToInclude.length
let currentLinePair = 0
let startLine = linesToInclude[currentLinePair][0]
let endLine = linesToInclude[currentLinePair][1]
// console.warn(`applyLineFiltering ` + startLine + ' and ' + endLine )
contents.split(NEWLINE_RX).forEach((line) => {
lineNum++
if ( !registerCurrentLine ) {
if ( lineNum == startLine )
registerCurrentLine = true
}
if ( registerCurrentLine )
{
if (!startLineNum) startLineNum = lineNum
lines.push(line)
if ( lineNum == endLine ) {
registerCurrentLine = false
currentLinePair++
if ( currentLinePair >= nLinesPair )
return [lines, startLineNum]
else {
startLine = linesToInclude[currentLinePair][0]
endLine = linesToInclude[currentLinePair][1]
}
}
}
})
return [lines, startLineNum || 1]
}
module.exports = function () {
this.includeProcessor(function () {
this.$option('position', '>>')
this.handles((target) => target.startsWith('https://'))
this.process((doc, reader, target, attrs) => {
const contents = require('child_process').execFileSync('curl', ['--silent', '-L', target], { encoding: 'utf8' })
let includeContents = filterChars(contents)
let startLineNum = 1
const tags = getTags(attrs)
const lines = getLines(attrs)
if (tags) [includeContents, startLineNum] = applyTagFiltering(includeContents, tags)
else if (lines) [includeContents, startLineNum] = applyLineFiltering(includeContents, lines)
reader.pushInclude(includeContents, target, target, startLineNum, attrs)
// reader.pushInclude(contents, target, target, 1, attrs)
})
})
}