Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 39537
1
###############################################################################
2
#
3
# CoCalc: Collaborative Calculation in the Cloud
4
#
5
# Copyright (C) 2016, Sagemath Inc.
6
#
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
#
20
###############################################################################
21
22
###
23
Some functions for working with Sage worksheets (sagews files) --
24
###
25
26
#---------------------------------------------------------------------------------------------------------
27
# Support for using synchronized docs to represent Sage Worksheets (i.e., live compute documents)
28
#---------------------------------------------------------------------------------------------------------
29
30
# WARNING: in Codemirror, to avoid issues with parsing I also set the output marker to be a comment character
31
# by modifying the python mode as follows: if (ch == "#" || ch == "\uFE21") {
32
33
exports.MARKERS =
34
cell : "\uFE20"
35
output : "\uFE21"
36
37
exports.FLAGS = FLAGS =
38
execute : "x" # request that cell be executed
39
waiting : "w" # request to execute received, but still not running (because of another cell running)
40
running : "r" # cell currently running
41
interrupt : "c" # request execution of cell be interrupted
42
this_session : "s" # if set, cell was executed during the current sage session.
43
hide_input : "i" # hide input part of cell
44
hide_output : "o" # hide output part of cell
45
46
exports.ACTION_FLAGS = [FLAGS.execute, FLAGS.running, FLAGS.waiting, FLAGS.interrupt]
47
exports.ACTION_SESSION_FLAGS = [FLAGS.execute, FLAGS.running, FLAGS.waiting, FLAGS.interrupt, FLAGS.this_session]
48
49
# Return a list of the uuids of files that are displayed in the given document,
50
# where doc is the string representation of a worksheet.
51
# At present, this function finds all output messages of the form
52
# {"file":{"uuid":"806f4f54-96c8-47f0-9af3-74b5d48d0a70",...}}
53
# but it could do more at some point in the future.
54
55
exports.uuids_of_linked_files = (doc) ->
56
uuids = []
57
i = 0
58
while true
59
i = doc.indexOf(exports.MARKERS.output, i)
60
if i == -1
61
return uuids
62
j = doc.indexOf('\n', i)
63
if j == -1
64
j = doc.length
65
line = doc.slice(i, j)
66
for m in line.split(exports.MARKERS.output).slice(1)
67
# Only bother to run the possibly slow JSON.parse on file messages; since
68
# this function would block the global hub server, this is important.
69
if m.slice(0,8) == '{"file":'
70
mesg = JSON.parse(m)
71
uuid = mesg.file?.uuid
72
if uuid?
73
uuids.push(uuid)
74
i = j
75
76
77
class SageWS
78
constructor: (@content) ->
79
80
find_cell_meta: (id, start) =>
81
i = @content.indexOf(exports.MARKERS.cell + id, start)
82
j = @content.indexOf(exports.MARKERS.cell, i+1)
83
if j == -1
84
return undefined
85
return {start:i, end:j}
86
87
get_cell_flagstring: (id) =>
88
pos = @find_cell_meta(id)
89
if pos?
90
return @content.slice(pos.start+37, pos.end)
91
92
set_cell_flagstring: (id, flags) =>
93
pos = @find_cell_meta(id)
94
if pos?
95
@content = @content.slice(0, pos.start+37) + flags + @content.slice(pos.end)
96
97
remove_cell_flag: (id, flag) =>
98
s = @get_cell_flagstring(id)
99
if s? and flag in s
100
@content = @set_cell_flagstring(id, s.replace(new RegExp(flag, "g"), ""))
101
102
set_cell_flag: (id, flag) =>
103
s = @get_cell_flagstring(id)
104
if s? and flag not in s
105
@content = @set_cell_flagstring(id, s + flag)
106
107
exports.sagews = (content) ->
108
return new SageWS(content)
109