-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
superscript.b
56 lines (42 loc) · 1.19 KB
/
superscript.b
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
import ..common.utils { UNESCAPE_RE, UNESCAPE_SPACE_RE }
def superscript(state, silent) {
var found,
content,
token,
max = state.pos_max,
start = state.pos
if state.src[start] != '^' return false
if silent return false # don't run any pairs in validation mode
if start + 2 >= max return false
state.pos = start + 1
while state.pos < max {
if state.src[state.pos] == '^' {
found = true
break
}
state.md.inline.skip_token(state)
}
if !found or start + 1 == state.pos {
state.pos = start
return false
}
content = state.src[start + 1, state.pos]
# don't allow unescaped spaces/newlines inside
if content.match(UNESCAPE_SPACE_RE) {
state.pos = start
return false
}
# found!
state.pos_max = state.pos
state.pos = start + 1
# Earlier we checked !silent, but this implementation does not need it
token = state.push('sup_open', 'sup', 1)
token.markup = '^'
token = state.push('text', '', 0)
token.content = content.replace(UNESCAPE_RE, '$1')
token = state.push('sup_close', 'sup', -1)
token.markup = '^'
state.pos = state.pos_max + 1
state.pos_max = max
return true
}