forked from thoth-station/s2i-minimal-notebook
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from harshad16/modh
enable plugin for jupyter git
- Loading branch information
Showing
16 changed files
with
1,606 additions
and
756 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
const fse = require("fs-extra"); | ||
const path = require("path"); | ||
|
||
const srcDir = path.join(__dirname, "node_modules", "mathjax"); | ||
const dstDir = path.join(__dirname, "jupyter_server_mathjax", "static"); | ||
|
||
/* | ||
* Copy MathJax static assets, but trim which assets in a similar way to what | ||
* notebook does on dist (in setupbase). | ||
*/ | ||
|
||
const rps = path.sep.replace("\\", "\\\\"); | ||
|
||
function re_join(parts) { | ||
return parts.join(rps); | ||
} | ||
|
||
const include = [ | ||
["MathJax.js"], | ||
["LICENSE"], | ||
["config", "TeX-AMS-MML_HTMLorMML-full.js"], | ||
["config", "Safe.js"], | ||
]; | ||
|
||
const re_include = [ | ||
["jax", "output", `[^${rps}]+.js$`], | ||
["jax", "output", "autoload", ".*"], | ||
["localization", ".*"], | ||
["fonts", "HTML-CSS", "STIX-Web", "woff", ".*"], | ||
["extensions", ".*"], | ||
["jax", "input", "TeX", ".*"], | ||
["jax", "output", "HTML-CSS", "fonts", "STIX-Web", ".*"], | ||
["jax", "output", "SVG", "fonts", "STIX-Web", ".*"], | ||
["jax", "element", "mml", ".*"], | ||
]; | ||
|
||
function isPartial(parts, candidate) { | ||
return parts.length <= candidate.length; | ||
} | ||
|
||
function partialPathMatch(parts, candidate) { | ||
const np = Math.min(candidate.length, parts.length); | ||
for (let i = 0; i < np; ++i) { | ||
if (candidate[i] !== parts[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
function pathOk(p) { | ||
if (!p) { | ||
return true; | ||
} | ||
const parts = p.split(path.sep); | ||
for (let c of include) { | ||
if (isPartial(parts, c)) { | ||
// Check for partial matches (to ensure dirs get included) | ||
if (partialPathMatch(parts, c)) { | ||
return true; | ||
} | ||
} else { | ||
if (c.join(path.sep) == p) { | ||
return true; | ||
} | ||
} | ||
} | ||
for (let c of re_include) { | ||
const lead = c.slice(0, c.length - 1); | ||
if (isPartial(parts, lead)) { | ||
// Check for partial matches (to ensure dirs get included) | ||
if (partialPathMatch(parts, lead)) { | ||
return true; | ||
} | ||
} else { | ||
const re = new RegExp(re_join(c)); | ||
if (re.test(p)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
function filterFunc(src, dest) { | ||
const relative = path.relative(srcDir, src); | ||
return pathOk(relative); | ||
} | ||
|
||
fse.copy(srcDir, dstDir, { filter: filterFunc }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Copyright (c) Jupyter Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||
|
||
from .app import MathJaxExtension | ||
|
||
|
||
def _jupyter_server_extension_points(): | ||
return [ | ||
{"module": "jupyter_server_mathjax", "app": MathJaxExtension}, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
version_info = (0, 2, 3) | ||
__version__ = ".".join(map(str, version_info)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Copyright (c) Jupyter Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||
|
||
from pathlib import Path | ||
from traitlets import default, observe, Unicode | ||
|
||
from tornado.web import RedirectHandler | ||
|
||
from jupyter_server.extension.application import ExtensionApp | ||
from jupyter_server.utils import url_path_join | ||
from jupyter_server.transutils import _ | ||
|
||
|
||
STATIC_ASSETS_PATH = Path(__file__).parent / "static" | ||
|
||
|
||
class DeprecatedRedirectHandler(RedirectHandler): | ||
def get(self, *args, **kwargs): | ||
import warnings | ||
|
||
warnings.warn( | ||
"Redirecting old Notebook MathJax URL to new one. This will be removed in a future release.", | ||
PendingDeprecationWarning, | ||
) | ||
super().get(*args, **kwargs) | ||
|
||
|
||
class MathJaxExtension(ExtensionApp): | ||
|
||
name = "jupyter_server_mathjax" | ||
|
||
# By listing the path to the assets here, jupyter_server | ||
# automatically creates a static file handler at | ||
# /static/jupyter_server_mathjax/... | ||
static_paths = [str(STATIC_ASSETS_PATH)] | ||
|
||
mathjax_config = Unicode( | ||
"TeX-AMS-MML_HTMLorMML-full,Safe", | ||
config=True, | ||
help=_("""The MathJax.js configuration file that is to be used."""), | ||
) | ||
|
||
@observe("mathjax_config") | ||
def _update_mathjax_config(self, change): | ||
self.log.info(_("Using MathJax configuration file: %s"), change["new"]) | ||
|
||
def initialize_settings(self): | ||
# Add settings specific to this extension to the | ||
# tornado webapp settings. | ||
self.settings.update( | ||
{ | ||
"mathjax_config": self.mathjax_config, | ||
"mathjax_url": url_path_join(self.static_url_prefix, "MathJax.js"), | ||
} | ||
) | ||
|
||
def initialize_handlers(self): | ||
webapp = self.serverapp.web_app | ||
base_url = self.serverapp.base_url | ||
host_pattern = ".*$" | ||
|
||
# Add a deprecated redirect for all MathJax paths from the classic | ||
# notebook to the static endpoint created for this extension. | ||
webapp.add_handlers( | ||
host_pattern, | ||
[ | ||
( | ||
url_path_join(base_url, "/static/components/MathJax/(.*)"), | ||
DeprecatedRedirectHandler, | ||
{ | ||
"url": url_path_join( | ||
self.static_url_prefix, "/{0}" # {0} = group 0 in url path | ||
) | ||
}, | ||
) | ||
], | ||
) |
Empty file.
14 changes: 14 additions & 0 deletions
14
jupyter-server-mathjax/jupyter_server_mathjax/tests/conftest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Copyright (c) Jupyter Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||
|
||
import io | ||
import logging | ||
import pytest | ||
from traitlets import default | ||
|
||
pytest_plugins = ["jupyter_server.pytest_plugin"] | ||
|
||
|
||
@pytest.fixture | ||
def jp_server_config(): | ||
return {"ServerApp": {"jpserver_extensions": {"jupyter_server_mathjax": True}}} |
47 changes: 47 additions & 0 deletions
47
jupyter-server-mathjax/jupyter_server_mathjax/tests/test_api.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Copyright (c) Jupyter Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||
|
||
"""Basic tests for the notebook handlers. | ||
""" | ||
|
||
import pytest | ||
from tornado.httpclient import HTTPClientError | ||
from jupyter_server.utils import url_path_join as ujoin | ||
|
||
|
||
async def test_mathjax_mainjs_handler(jp_fetch): | ||
r = await jp_fetch("static", "jupyter_server_mathjax", "MathJax.js") | ||
assert r.code == 200 | ||
|
||
|
||
async def test_mathjax_conf_handler(jp_fetch): | ||
r = await jp_fetch( | ||
"static", "jupyter_server_mathjax", "config", "TeX-AMS-MML_HTMLorMML-full.js" | ||
) | ||
assert r.code == 200 | ||
|
||
r = await jp_fetch("static", "jupyter_server_mathjax", "config", "Safe.js") | ||
assert r.code == 200 | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"asset_file", | ||
["MathJax.js", "config/TeX-AMS-MML_HTMLorMML-full.js", "config/Safe.js"], | ||
) | ||
async def test_redirects_from_classic_notebook_endpoints( | ||
jp_fetch, jp_base_url, asset_file | ||
): | ||
old_prefix = ujoin("static", "components", "MathJax") | ||
new_prefix = ujoin("static", "jupyter_server_mathjax") | ||
|
||
# Verify that the redirect is in place | ||
with pytest.raises(HTTPClientError) as error_info, pytest.deprecated_call( | ||
match="Redirecting old Notebook MathJax URL .*" | ||
): | ||
await jp_fetch(old_prefix, asset_file, follow_redirects=False) | ||
|
||
err = error_info.value | ||
assert err.code == 301 | ||
assert err.response.headers["Location"] == ujoin( | ||
jp_base_url, new_prefix, asset_file | ||
) |
Oops, something went wrong.