Skip to content

Commit aff22f1

Browse files
committed
Update to offline version
1 parent 62825dd commit aff22f1

File tree

3 files changed

+68
-119
lines changed

3 files changed

+68
-119
lines changed

GlowScriptOffline/GlowScript.html

Lines changed: 59 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,26 @@
5959
   
6060

6161
<script>
62-
var gsversion = '3.2'
63-
var printpane = false
64-
var exporting = false // not currently in export mode
65-
var lastprintwidth = null
62+
const gsversion = '3.2'
63+
let printpane = false
64+
let exporting = false // not currently in export mode
65+
let lastprintwidth = null
66+
let usertext
67+
let lang
6668

6769
window.Jupyter_VPython = undefined
6870
if (!navigator.onLine) window.Jupyter_VPython = 'glowscript_data/' // get textures when offline
6971

7072
// TAB at end of line should lengthen the line; implement Ctrl-1 and Ctrl-2
7173

7274
// localCompile is a modification of https://github.com/vpython/glowscript/blob/master/ide/ide.js
73-
function localCompile(header, compReady, errordiv) {
74-
header.source = 'GlowScript '+gsversion+' VPython\n'+header.source
75+
function localCompile(compReady, errordiv) {
7576
errordiv.innerHTML = ""
76-
var compiler_url
77-
if (header.lang == 'vpython') {
78-
compiler_url = "glowscript_libraries/RScompiler." + header.version + ".min.js"
79-
} else compiler_url = "glowscript_libraries/compiler." + header.version + ".min.js"
77+
let compiler_url
78+
if (lang == 'vpython') {
79+
compiler_url = "glowscript_libraries/RScompiler." + gsversion + ".min.js"
80+
} else
81+
compiler_url = "glowscript_libraries/compiler." + gsversion + ".min.js"
8082
window.glowscript_compile = undefined
8183
$.ajax({
8284
url: compiler_url,
@@ -91,15 +93,15 @@
9193
alert("Failed to load compiler from " + compiler_url)
9294
return
9395
}
94-
95-
var embedScript
96+
97+
let embedScript
9698
try {
97-
embedScript = window.glowscript_compile(header.source, {lang: header.lang,
98-
version: header.version.substr(0,3)})
99+
embedScript = window.glowscript_compile(usertext, {lang: lang, version: gsversion})
100+
// version: header.version.substr(0,3)})
99101
} catch(err) { // need to decrement 3 -> 2 in Error: Missing right parenthesis, see line 3: b = box(pos=37
100102
err = err.toString() // gets the error message
101-
var patt = new RegExp('line(\\s*)([0-9]*):')
102-
var m = err.match(patt)
103+
const patt = new RegExp('line(\\s*)([0-9]*):')
104+
let m = err.match(patt)
103105
if (m !== null) {
104106
var colonindex = m.index + 4 + m[1].length + m[2].length
105107
var n = parseFloat(m[2])-1
@@ -112,80 +114,47 @@
112114
})
113115
}
114116

115-
function parseVersionHeader( source ) {
116-
var sourceLines = source.split("\n")
117-
var header = sourceLines[0]
118-
// Remove a newline or similar character at the end of header:
119-
if (header.charCodeAt(header.length-1) < 32)
120-
header = header.substring(0,header.length-1)
121-
var rest = source.substring( header.length+1 )
122-
var ret = {
123-
version: null,
124-
lang: '', // 'vpython' (default) or 'rapydscript' or 'javascript' or a string that is neither (e.g. when editing header)
125-
source: rest,
126-
ok: false,
127-
unpackaged: false,
128-
isCurrent: false
129-
}
130-
header = header.split(" ")
131-
if (header.length === undefined) return ret
132-
if (header[0] == ' ') return ret
133-
var elements = []
134-
for (var i=0; i<header.length; i++) { // remove empty strings corresponding to spaces
135-
if (header[i] != '') elements.push(header[i])
136-
}
137-
if (elements.length < 2 || elements.length > 3) return ret
138-
if (elements[0] != 'GlowScript') return ret
139-
ret.lang = 'javascript' // the default if no language is specified
140-
if (elements.length == 3) {
141-
ret.lang = elements[2].toLowerCase()
142-
if (!(ret.lang == 'javascript' || ret.lang == 'vpython')) return ret
143-
}
144-
var ver = elements[1]
145-
if (ver != gsversion) alert('The version number, '+ver+', should be '+gsversion)
146-
var okv = true
147-
return {
148-
version: ver,
149-
lang: ret.lang,
150-
source: rest,
151-
}
152-
}
117+
let wp = 'Web VPython '+gsversion
118+
let js = 'JavaScript '+gsversion
119+
120+
const r0 = new RegExp('\\s*from\\s*vpython\\s*import\\s*\\*\\s*\n') // no version number
121+
const r1 = new RegExp('\\s*Web\\s*VPython\\s*([[1-9]\\.[1-9])\\s*\n')
122+
const r2 = new RegExp('\\s*\\sGlowScript\\s*([[1-9]\\.[1-9])\\s*VPython\\s*\n')
123+
const r3 = new RegExp('\\s*\\sGlowScript\\s*([[1-9]\\.[1-9])\\s*JavaScript\\s*\n')
124+
const r4 = new RegExp('\\s*JavaScript\\s*([[1-9]\\.[1-9])\\s*\n')
125+
const r5 = new RegExp('\\s*GlowScript\\s*([[1-9]\\.[1-9])\\s*\n')
126+
let langtype = [ [r0, wp], [r1, wp], [r2, wp], [r3, js], [r4, js], [r5,js] ]
153127

154128
function getHeader(exporting) {
155-
var text = GSedit.getValue()
156-
var end = text.indexOf('\n')
157-
var i = text.slice(0,end).indexOf("GlowScript") // Look for "GlowScipt" in first line
158-
var j = text.slice(0,end).indexOf("vpython") // Look for "vpython" in first line
159-
if (i < 0 && j) text = "GlowScript "+gsversion+" VPython" + "\n" + text
160-
var header = parseVersionHeader(text)
161-
printpane = false
162-
if (header.source.search(/print\s*\(/) >= 0) { // if the program uses print() or GSprint(), expand 3rd pane
163-
printpane = true
164-
if (!exporting) {
165-
var end = header.source.indexOf('\n')
166-
var insert
167-
var w = (lastprintwidth === null) ? 300 : 0.01*lastprintwidth*window.innerWidth
168-
if (header.lang == 'vpython')
169-
insert = "print_options(place=$('#printing'), width="+w+", height=window.innerHeight, clear=True)\n"
170-
else insert = "print_options({place:$('#printing'), width:"+w+", height:window.innerHeight, clear:true})\n"
171-
header.source = insert + header.source
129+
usertext = GSedit.getValue()
130+
let end = usertext.indexOf('\n')
131+
let firstline = usertext.slice(0,end+1)
132+
lang = null
133+
for (let i=0; i<langtype.length; i++) {
134+
let m = firstline.match(langtype[i][0])
135+
if (m !== null) {
136+
usertext = langtype[i][1] + usertext.slice(end)
137+
lang = 'vpython'
138+
if (i>2) lang = 'javascript'
139+
break
172140
}
173141
}
174-
// Look for mention of MathJax in program and attempt to get it (need internet access; files too big to include in package)
175-
if (header.source.indexOf('MathJax') >= 0) {
176-
alert('Cannot currently use MathJax in GlowScript Offline.')
142+
if (lang == null) {
143+
usertext = wp+'\n'+usertext
144+
lang = 'vpython'
177145
}
178-
return header
146+
GSedit.setValue(usertext)
147+
return usertext
179148
}
180149

181-
var gsErrordiv
182-
var savecode = null
150+
let gsErrordiv
151+
let savecode = null
183152

184153
function runCode() {
185-
var header = getHeader(false)
154+
usertext = getHeader(false)
186155
splitAdjust()
187156
gsErrordiv = $("#gserrors")[0]
188-
localCompile(header, ready, gsErrordiv, false)
157+
localCompile(ready, gsErrordiv)
189158
}
190159

191160
async function runprog(prog) {
@@ -215,24 +184,24 @@
215184

216185
} else { // Export operation
217186
exporting = true
218-
var header = getHeader(true)
187+
//var header = getHeader(true)
219188
gsErrordiv = $("#gserrors")[0]
220-
localCompile(header, showcode, gsErrordiv)
189+
localCompile(showcode, gsErrordiv)
221190
}
222191
}
223192

224193
function showcode(sc) {
225194
// In creating the string embedHTML it was necessary to break 'script' into 'scr'+'ipt' to avoid problems parsing GlowScript.html
226-
var exporturl = "https://s3.amazonaws.com/glowscript/"
227-
var verdir = '2.1'
228-
var divid = "glowscript"
229-
var embedHTML = (
195+
let exporturl = "https://www.glowscript.org/"
196+
let divid = "glowscript"
197+
let jqversion = '2.1'
198+
let embedHTML = (
230199
'<div id="' + divid + '" class="glowscript">\n' +
231-
'<meta http-equiv="Content-Type" content="text/html; charset=utf-8">' +
232-
'<link type="text/css" href="'+exporturl+'css/redmond/' + verdir + '/jquery-ui.custom.css" rel="stylesheet" />\n' +
200+
'<meta http-equiv="Content-Type" content="text/html; charset=utf-8">' + '\n' +
201+
'<link type="text/css" href="'+exporturl+'css/redmond/' + jqversion + '/jquery-ui.custom.css" rel="stylesheet" />\n' +
233202
'<link type="text/css" href="' + exporturl + 'css/ide.css" rel="stylesheet" />\n' +
234-
'<scr'+'ipt type="text/javascript" src="' + exporturl + 'lib/jquery/' + verdir + '/jquery.min.js"></scr'+'ipt>\n' +
235-
'<scr'+'ipt type="text/javascript" src="' + exporturl + 'lib/jquery/' + verdir + '/jquery-ui.custom.min.js"></scr'+'ipt>\n' +
203+
'<scr'+'ipt type="text/javascript" src="' + exporturl + 'lib/jquery/' + jqversion + '/jquery.min.js"></scr'+'ipt>\n' +
204+
'<scr'+'ipt type="text/javascript" src="' + exporturl + 'lib/jquery/' + jqversion + '/jquery-ui.custom.min.js"></scr'+'ipt>\n' +
236205
'<scr'+'ipt type="text/javascript" src="' + exporturl + 'package/glow.' + gsversion + '.min.js"></scr'+'ipt>\n' +
237206
'<scr'+'ipt type="text/javascript" src="' + exporturl + 'package/RSrun.' + gsversion + '.min.js"></scr'+'ipt>\n' +
238207
'<scr'+'ipt type="text/javascript"><!--//--><![CDATA[//><!--\n' +
@@ -429,7 +398,7 @@
429398
}
430399
}
431400
if (L === undefined) continue
432-
var N = Number(L)-1
401+
var N = Number(L)
433402
if (first) traceback.push('At or near line '+N+': '+window.__original.text[N-1])
434403
else traceback.push('Called from line '+N+': '+window.__original.text[N-1])
435404
first = false

GlowScriptOffline/README.html

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<h4>OBTAINING THE LATEST VERSION OF GLOWSCRIPT OFFLINE</h4>
1414
<p>Based on the work of Vesa Lappalainen of Finland, this package makes it possible to write and run GlowScript programs even when disconnected from the internet.&nbsp;</p>
1515
<p>To obtain the latest version of this package, go to the following location:</p>
16-
<p>&nbsp;&nbsp;&nbsp;&nbsp;<a href="https://github.com/vpython/glowscript/blob/master/GlowScriptOffline2.9.zip">https://github.com/vpython/glowscript/blob/master/</a></p>
16+
<p>&nbsp;&nbsp;&nbsp;&nbsp;<a href="https://github.com/vpython/glowscript/blob/master/">https://github.com/vpython/glowscript/blob/master/</a></p>
1717
<p>Click the latest version of GlowScriptOfflineX.Y.zip and click the download icon.&nbsp;&nbsp;</p>
1818
<h4>CHOICE OF BROWSER</h4>
1919
<p>It is recommended to use the Chrome browser, as it is the only browser that provides detailed information on run-time errors and also makes it possible to use textures and JavaScript libraries. You can use a different browser if these issues are not important to you.</p>
@@ -60,14 +60,7 @@ <h4>POSSIBLE SLOWDOWN</h4>
6060
<p>If you make a large number of runs, performance may degrade due to an accumulation of "WebGL contexts". However, there is a simple remedy: reload the web page.</p>
6161

6262
<h4>IMPORT ISSUES, AND USING JAVASCRIPT</h4>
63-
<p>By default, if you don't start a program with a statement of the form "GlowScript 2.7 JavaScript" or "GlowScript 2.7 RapydScript", it is assumed that this is a VPython program. You can also start a program with one of these kinds of Python import statements (or include such an import statement after "GlowScript 2.7 VPython"):</p>
64-
65-
<p>&nbsp;&nbsp;&nbsp;&nbsp;from vpython import *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# The default; all VPython elements are available<br>
66-
&nbsp;&nbsp;&nbsp;&nbsp;from vpython import canvas, box, sphere, vec&nbsp;&nbsp;&nbsp;# This MUST include canvas<br>
67-
&nbsp;&nbsp;&nbsp;&nbsp;import vpython&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# A red box: vpython.box(color=vpython.color.red)<br>
68-
&nbsp;&nbsp;&nbsp;&nbsp;import vpython as vp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# A red box: vp.box(color=vp.color.red)</p>
69-
70-
<p>If you don't include a GlowScript or import statement, this is the same as specifying "from vpython import *".</p>
63+
<p>By default, if you don't specify VPython or JavaScript in the first line, it is assumed that this is a VPython program.</p>
7164

7265
<h4><a name="textures"></a>USING TEXTURES OR JAVASCRIPT LIBRARIES</h4>
7366
<p>If you don't use textures or JavaScript libraries (using the get_library function), the instructions given above are adequate. If you do however use textures or JavaScript libraries, you need to follow these instructions:</p>

GlowScriptOffline/README.md

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
### OBTAINING THE LATEST VERSION OF GLOWSCRIPT OFFLINE
22
Based on the work of [Vesa Lappalainen](https://github.com/vesal) of Finland, this package makes it possible to write and run
3-
GlowScript programs even when disconnected from the internet.
3+
GlowScript programs even when disconnected from the internet.
44

55
To obtain the latest version of this package, go to the following location:
6-
7-
https://github.com/vpython/glowscript/blob/master/
8-
9-
Click the latest version of GlowScriptOfflineX.Y.zip and click the download icon.
10-
11-
To obtain the latest version of this package,
12-
go to the following location and click Download, then unzip the package to any convenient place on your computer:
13-
* https://github.com/vpython/glowscript/blob/master/GlowScriptOffline2.9.zip
6+
* https://github.com/vpython/glowscript/blob/master/
7+
Click the latest version of GlowScriptOfflineX.Y.zip and click the download icon.
148

159
### CHOICE OF BROWSER
1610
It is recommended to use the Chrome browser, as it is the only browser that provides detailed information on run-time errors and also makes it possible to use textures and JavaScript libraries. You can use a different browser if these issues are not important to you.
@@ -32,21 +26,21 @@ You can drag the vertical gray bar (and/or make the window wider or narrower) to
3226
You can run or restart your program by pressing `Ctrl-1` or `Ctrl-2`, just as you can at glowscript.org.
3327

3428
### SAVING YOUR CHANGES
35-
Click Save to save your program. It will be written to your `Download` folder (browser security rules forbid writing it anywhere else). You will be asked for a name. Suppose you specify `test`. If your program is VPython or RapydScript, the `Download` folder will have a file named `test.py`, otherwise it will be `test.js` (JavaScript). If in one session you do multiple saves, your `Download` folder will have multiple copies: `test.py`, `test(1).py`, etc.
29+
Click Save to save your program. It will be written to your `Download` folder (browser security rules forbid writing it anywhere else). You will be asked for a name. Suppose you specify `test`. If your program is VPython, the `Download` folder will have a file named `test.py`, otherwise it will be `test.js` (JavaScript). If in one session you do multiple saves, your `Download` folder will have multiple copies: `test.py`, `test(1).py`, etc.
3630

3731
If you create or modify a program without saving it and click `Choose File` (`Browse`... on Firefox or Edge), you'll see a warning that you might wish to save your work. Similarly, when you attempt to close the browser or the browser tab, if you have modified the current program you'll be warned about saving the file.
3832

3933
### DEMO PROGRAMS INCLUDED
4034
Click `Choose File` and navigate to the `GlowScriptOffline` folder to choose from programs in the `Demos` folder. Those demo programs that use textures will not display correctly when offline unless you start the package in a special way [described below](#using-textures-or-javascript-libraries) in the section "Using Textures or JavaScript Libraries."
4135

4236
### DOCUMENTATION INCLUDED
43-
The GlowScript documentation is included in the package and is accessible by clicking `Help`.
37+
The Web VPython documentation is included in the package and is accessible by clicking `Help`.
4438

4539
### USING THE TEXT EDITOR
4640
In the text editor, as in the editor at glowscript.org, select one or more lines and press `TAB` to indent or `Shift-TAB` to unindent; press `Ctrl-/` to toggle commenting of the lines. Pressing `TAB` with the cursor at the end of the line adds spaces to the end of that line. At the moment, find and replace options are not yet available; you can of course copy the program to a local text editor to use find and replace, then copy it back.
4741

4842
### USING PROGRAMS FROM glowscript.org OR FROM VPYTHON 7
49-
When you download programs from [glowscript.org](http://www.glowscript.org), they are in the form of `.py` files whose first line is a statement about importing Vpython, like programs created with VPython 7. This first line is understood by the offline package, as is `GlowScript X.Y VPython` or `GlowScript X.Y JavaScript`.
43+
When you download programs from [glowscript.org](http://www.glowscript.org), they are in the form of `.py` files whose first line is a statement about importing Vpython, like programs created with VPython 7. This first line is understood by the offline package, as is `Web VPython X.Y` or `JavaScript X.Y` or `GlowScript X.Y VPython` or `GlowScript X.Y JavaScript`.
5044

5145
### EXPORTING A PROGRAM
5246
When you click `Export`, your program is processed to create code that can be embedded in your own web site, just like using the option `Share or export this program` at glowscript.org. This processed code temporarily replaces your own program code. If you click `Save` and give the name "test", a file named `test.html` will be written to your `Download` folder. Alternatively, press `Ctrl-C` to copy the code (all of which is preselected for you), then use a text editor to save this code to a local file that should have the extension `.html`.
@@ -57,14 +51,7 @@ When you click `Export`, your program is processed to create code that can be em
5751
If you make a large number of runs, performance may degrade due to an accumulation of "WebGL contexts". However, there is a simple remedy: reload the web page
5852

5953
### IMPORT ISSUES, AND USING JAVASCRIPT
60-
By default, if you don't start a program with a statement of the form `GlowScript 2.7 JavaScript` or `GlowScript 2.7 RapydScript`, it is assumed that this is a VPython program. You can also start a program with one of these kinds of Python import statements (or include such an import statement after `GlowScript 2.7 VPython`):
61-
62-
* `from vpython import *` The default; all VPython elements are available
63-
* `from vpython import canvas, box, sphere, vec` This MUST include `canvas`
64-
* `import vpython` A red box: `vpython.box(color=vpython.color.red)`
65-
* `import vpython as vp` A red box: `vp.box(color=vp.color.red)`
66-
67-
If you don't include a GlowScript or import statement, this is the same as specifying `from vpython import *`
54+
By default, if you don't specify VPython or JavaScript in the first line, it is assumed that this is a VPython program.
6855

6956
### USING TEXTURES OR JAVASCRIPT LIBRARIES
7057
If you don't use textures or JavaScript libraries (using the `get_library` function), the instructions given above are adequate. If you do however use textures or JavaScript libraries, you need to follow these instructions:

0 commit comments

Comments
 (0)