-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess.mts
More file actions
208 lines (185 loc) · 5.37 KB
/
Copy pathpreprocess.mts
File metadata and controls
208 lines (185 loc) · 5.37 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
200
201
202
203
204
205
206
207
208
/**
* @file preprocess
* @module mark-parser/preprocess
*/
import type {
PreprocessOptions,
Preprocessor
} from '@flex-development/mark-parser'
import { decode } from '@flex-development/mark-parser/utils'
import { codes, constants } from '@flex-development/mark-util-symbol'
import type {
Chunk,
Code,
Encoding,
FileLike,
Value
} from '@flex-development/mark/parse'
import { equal } from 'devlop'
import nil from './internal/nil.mts'
export default preprocess
/**
* Create a preprocessor.
*
* @see {@linkcode Code}
* @see {@linkcode Preprocessor}
* @see {@linkcode PreprocessOptions}
*
* @this {void}
*
* @param {PreprocessOptions | null | undefined} [options]
* The configuration options
* @return {Preprocessor<Code>}
* The character code preprocessor
*/
function preprocess(
this: void,
options?: PreprocessOptions | null | undefined
): Preprocessor<Code> {
/**
* The number of columns represented by a horizontal tab.
*
* @const {number} tabSize
*/
const tabSize: number = options?.tabSize ?? constants.tabSize
return preprocessor as Preprocessor<Code>
/**
* Turn `value` into character code chunks.
*
* @this {void}
*
* @param {Code | FileLike | Value | undefined} value
* The code, file, or value to preprocess
* @param {Encoding | null | undefined} [encoding]
* The character encoding to use when `value`
* or its contents is an {@linkcode Uint8Array}
* @param {boolean | null | undefined} [end]
* Whether the end of stream has been reached
* @return {Code[]}
* The list of character code chunks
*/
function preprocessor(
this: void,
value: Code | FileLike | Value | undefined,
encoding?: Encoding | null | undefined,
end?: boolean | null | undefined
): Code[] {
/**
* The list of character code chunks.
*
* @const {Code[]} chunks
*/
const chunks: Code[] = []
/**
* Whether the previous chunk was a carriage return.
*
* If the next chunk is a line feed, both characters are normalized into a
* single CRLF virtual code.
*
* @var {boolean} atCarriageReturn
*/
let atCarriageReturn: boolean = false
/**
* The current visual column.
*
* Used to determine how many virtual spaces follow a horizontal tab.
*
* @var {number} column
*/
let column: number = 1
/**
* The index of the current character code.
*
* @var {number} index
*/
let index: number = 0
// add character code chunk.,
// or decode file or value and extract character code chunks.
if (typeof value === 'number') {
chunks.push(value)
} else if (!nil(value)) {
/**
* The decoded chunk.
*
* @var {Chunk | undefined} decoded
*/
let decoded: Chunk | undefined = decode(value, encoding)
// empty file or value was decoded.
if (typeof decoded === 'number') {
equal(decoded, codes.empty, 'expected `codes.empty`')
if (options?.allowEmptyChunk) chunks.push(codes.empty)
} else {
// value is now decoded.
value = decoded
// move past byte order mark.
if (options?.ignoreBOM && value.codePointAt(0) === codes.bom) index++
// store character code chunks.
while (index < value.length) {
/**
* The current character code.
*
* @const {NonNullable<Code>} code
*/
const code: NonNullable<Code> = value.codePointAt(index)!
/**
* The difference between the next column and the current column.
*
* @var {number} k
*/
let k: number = code > 0xffff ? 2 : 1
// handle carriage return.
if (atCarriageReturn) {
atCarriageReturn = false
// if the next chunk is a line feed,
// both characters are normalized into a single crlf virtual code.
// normalize carriage return and line feed.
if (code === codes.lf) {
chunks.push(codes.crlf)
index += k
continue
}
// carriage return without line feed.
chunks.push(codes.vcr)
}
// process character code.
switch (code) {
case codes.cr:
atCarriageReturn = true
column = 1
break
case codes.ht:
/**
* The next column.
*
* @const {number} n
*/
const n: number = Math.ceil(column / tabSize) * tabSize
// normalize htab into virtual htab and spaces.
chunks.push(codes.vht)
while (column++ < n) chunks.push(codes.vs)
break
case codes.lf: // add virtual line feed.
chunks.push(codes.vlf)
column = 1
break
case codes.nul: // substitute nul with replacement character.
chunks.push(options?.nul ? codes.nul : codes.replacement)
column++
break
default: // normal code point.
chunks.push(code)
column++
break
}
index += k
}
}
}
// end of stream.
if (end) {
if (atCarriageReturn) chunks.push(codes.vcr)
chunks.push(codes.eos)
}
return chunks
}
}