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

Commit 16741ff

Browse files
author
Release Manager
committed
Trac #30612: Set title of HTML page with Three.js viewer
When having several browser windows/tabs open with Three.js plots in them, it can be difficult to remember which is which. Since the `title` element in the generated pages is empty, the filename/path is displayed in !Chrome/FireFox, and something like `tmp_e0or9kcy.html` isn't very helpful. This ticket proposes a new `page_title` viewer option to set the title of the HTML page. Additionally, the page title should be used to suggest a filename when using the "Save as HTML" menu item instead of the generic `graphic.html`. For example: {{{ show(dodecahedron(), page_title="Dodecahedron") }}} ...should produce a page titled "Dodecahedron" that suggests the filename `Dodecahedron.html` upon saving. URL: https://trac.sagemath.org/30612 Reported by: gh-jcamp0x2a Ticket author(s): Joshua Campbell Reviewer(s): Eric Gourgoulhon
2 parents c25b076 + 26dfb51 commit 16741ff

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>
@@ -469,9 +469,19 @@
469469
var blob = new Blob( [ '<!DOCTYPE html>\n' + document.documentElement.outerHTML ] );
470470
var a = document.body.appendChild( document.createElement( 'a' ) );
471471
a.href = window.URL.createObjectURL( blob );
472-
a.download = 'graphic.html';
472+
a.download = suggestFilename();
473473
a.click();
474474

475+
function suggestFilename() {
476+
if ( !document.title ) {
477+
return 'graphic.html';
478+
} else if ( /\.html?$/i.test( document.title ) ) {
479+
return document.title; // already ends in .htm or .html
480+
} else {
481+
return document.title + '.html';
482+
}
483+
}
484+
475485
}
476486

477487
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)
@@ -490,6 +497,14 @@ cdef class Graphics3d(SageObject):
490497
with open(os.path.join(SAGE_EXTCODE, 'threejs', 'animation.js')) as f:
491498
extra_html += '<script>' + f.read() + '</script>'
492499

500+
page_title = options.get('page_title')
501+
if page_title is None:
502+
page_title = ""
503+
else:
504+
from html import escape as html_escape
505+
page_title = html_escape(str(page_title).strip())
506+
507+
html = html.replace('SAGE_TITLE', page_title)
493508
html = html.replace('SAGE_SCRIPTS', scripts)
494509
html = html.replace('SAGE_STYLES', styles)
495510
html = html.replace('SAGE_EXTRA_HTML', extra_html)

0 commit comments

Comments
 (0)