-
Notifications
You must be signed in to change notification settings - Fork 4
/
cup.co
executable file
·78 lines (72 loc) · 2.17 KB
/
cup.co
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
@print = !(msg, tag) ->
return if msg is void
msg += ''
if print.last is print.last = msg
if lmn = pout.firstChild
lmn.setAttribute \data-x, -~(lmn.getAttribute \data-x or 1)
return
lmn = document.createElement tag || \span
lmn.appendChild document.createTextNode msg
pout.insertBefore lmn, pout.insertBefore do
document.createElement \br
pout.firstChild
@say = !-> print @@ * ''
@puts = !-> print @@ * \\n
@warn = !-> print it, \em
@p = !-> print (pp x for x of @@) * \\n
@pp = uneval ? -> JSON.stringify it, null 1
@clear = !-> pout.innerHTML = ''
Coco.eval = -> do Function \return + @compile it, {+eval}
$ = document~getElementById
code = $ \code
ctrl = $ \ctrl
pout = $ \pout
btns = {}
poem = '''
Coco.VERSION
\n\n\n\n
# Not to abandon the evil parts
# But to embrace the good parts
'''
kick = !->
code.focus!
{value} = code
location.hash = @id.charAt! + \: + encode value unless value is poem
try r = Coco[@id] value, {+bare}
catch return warn e
switch @accessKey
case \t \l
lines = []
for [tag, val, lno] of r
lines@@[lno]push if tag.toLowerCase! is val then tag else "#tag:#val"
for l, i of lines
lines[i] = l?join(' ')replace /\n/g \\\n or ''
r = lines.join \\n
case \a then r.=lines.join('')slice 1
case \e then self._ = r
print r
encode = ->
encodeURI it.replace /%/g \\0 .replace /[()]/g -> escape it
decode = ->
decodeURIComponent it .replace /\0/g \%
for Key of <[ Lex Tokens AST Compile Eval ]>
btn = document.createElement \button
btn.id = key = Key.toLowerCase!
btn.onclick = kick
btn.innerHTML = Key
btns[key] = btns[btn.accessKey = key.charAt!] = ctrl.appendChild btn
b.innerHTML += " <small><kbd>\\#{k}-RET" for k, b in C: btns.e, S: btns.c
document.onkeydown = (ev || event) ->
return if ev.keyCode != 13_RET || ev.altKey
if (ev.ctrlKey || ev.metaKey) && btns.e || ev.shiftKey && btns.c
that.click!
false
@onfocus = !-> code.focus!
do @onhashchange = !(ev) ->
code.value = if hash = location.hash.slice 1
try hash = decode hash
[, op, hash]? = /^([ltace]+):([\s\S]*)/iexec hash
hash
else
code.value or poem
ev or btns[op?toLowerCase! or \e]click!