-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.js
69 lines (48 loc) · 1.37 KB
/
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
function Block(jsonic, options) {
let { regexp, escre } = jsonic.util
jsonic.lex(function makeBlockMatcher() {
let blockre = regexp(
's',
'^(',
Object
.entries(options.marker)
.filter(([sm,em]) => 'string'===typeof(sm) && 'string'===typeof(em))
.map(([sm,em])=>
(escre(sm)+'(.*?)('+escre(em)+')'))
.join('|'),
')'
)
return function blockMatcher(lex) {
let pnt = lex.pnt
let fwd = lex.src.substring(pnt.sI)
let m = fwd.match(blockre)
if (m) {
let msrc = m[0]
let mlen = msrc.length
if (0 < mlen) {
let mf = m.filter(p=>null!=p)
let txt = mf[2]
.replace(/^\s*\r?\n/,'')
.replace(/\r?\n\s*$/,'')
let indent = (txt.match(/^(\s+)/)||['',''])[1]
txt = txt
.substring(indent.length)
.replace(regexp('g','(\\r?\\n)',indent),'$1')
let tkn = lex.token(jsonic.token.TX, txt, msrc, pnt)
let rows = msrc.replace(/[^\n]/g,'').length
pnt.sI += msrc.length
pnt.rI += rows
pnt.cI = (0<rows?1:pnt.cI)+(msrc.match(/[^\r\n]*$/)||[''])[0].length
return tkn
}
}
}
})
}
Block.defaults = {
// Block markers
marker: {
"'''": "'''",
},
}
module.exports = { Block }