Skip to content
This repository was archived by the owner on Feb 1, 2023. It is now read-only.

Commit 26dfb51

Browse files
committed
Allow the title of Three.js plot pages to be customized
Also, use title as suggested filename when saving as HTML.
1 parent 5ec24db commit 26dfb51

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

src/doc/en/reference/plot3d/threejs.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ Options currently supported by the viewer:
4545

4646
- ``opacity`` -- (default: 1) numeric value for transparency of lines and surfaces
4747

48+
- ``page_title`` -- (default: None) string containing the title of the generated HTML page; often
49+
displayed in the browser window's title bar, its tab list, and/or the operating system's task bar
50+
4851
- ``projection`` -- (default: 'perspective') the type of camera projection to use;
4952
'perspective' or 'orthographic'
5053

src/sage/ext_data/threejs/threejs_template.html

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<title></title>
4+
<title>SAGE_TITLE</title>
55
<meta charset="utf-8">
66
<meta name=viewport content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
77
<style>
@@ -435,9 +435,19 @@
435435
var blob = new Blob( [ '<!DOCTYPE html>\n' + document.documentElement.outerHTML ] );
436436
var a = document.body.appendChild( document.createElement( 'a' ) );
437437
a.href = window.URL.createObjectURL( blob );
438-
a.download = 'graphic.html';
438+
a.download = suggestFilename();
439439
a.click();
440440

441+
function suggestFilename() {
442+
if ( !document.title ) {
443+
return 'graphic.html';
444+
} else if ( /\.html?$/i.test( document.title ) ) {
445+
return document.title; // already ends in .htm or .html
446+
} else {
447+
return document.title + '.html';
448+
}
449+
}
450+
441451
}
442452

443453
function getViewpoint() {

src/sage/plot/plot3d/base.pyx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,13 @@ cdef class Graphics3d(SageObject):
398398
sage: (css in str) or (html in str)
399399
False
400400
401+
If a page title is provided, it is stripped and HTML-escaped::
402+
403+
sage: d = dodecahedron(page_title='\t"Page" & <Title>\n')
404+
sage: str = d._rich_repr_threejs(online=True).html.get_str()
405+
sage: '<title>&quot;Page&quot; &amp; &lt;Title&gt;</title>' in str
406+
True
407+
401408
"""
402409
options = self._process_viewing_options(kwds)
403410
options.setdefault('online', False)
@@ -484,6 +491,14 @@ cdef class Graphics3d(SageObject):
484491
with open(os.path.join(SAGE_EXTCODE, 'threejs', 'animation.js')) as f:
485492
extra_html += '<script>' + f.read() + '</script>'
486493

494+
page_title = options.get('page_title')
495+
if page_title is None:
496+
page_title = ""
497+
else:
498+
from html import escape as html_escape
499+
page_title = html_escape(str(page_title).strip())
500+
501+
html = html.replace('SAGE_TITLE', page_title)
487502
html = html.replace('SAGE_SCRIPTS', scripts)
488503
html = html.replace('SAGE_STYLES', styles)
489504
html = html.replace('SAGE_EXTRA_HTML', extra_html)

0 commit comments

Comments
 (0)