From 42b59e1536721cc6a8ac53a203c6bbf6a3926740 Mon Sep 17 00:00:00 2001 From: Anton Akhmerov Date: Sun, 2 May 2021 02:07:38 +0200 Subject: [PATCH] implement passthrough configuration --- sphinx_thebe/__init__.py | 89 +++++++++++----------------------------- 1 file changed, 24 insertions(+), 65 deletions(-) diff --git a/sphinx_thebe/__init__.py b/sphinx_thebe/__init__.py index 13d4682..1f7fddf 100644 --- a/sphinx_thebe/__init__.py +++ b/sphinx_thebe/__init__.py @@ -18,35 +18,22 @@ def st_static_path(app): app.config.html_static_path.append(static_path) -def init_thebe_default_config(app, env, docnames): - thebe_config = app.config.thebe_config - defaults = { - "selector": ".thebe", - "selector_input": "pre", - "selector_output": ".output", - } - for key, val in defaults.items(): - if key not in thebe_config: - thebe_config[key] = val - - def init_thebe_core(app, env): - config_thebe = app.config["thebe_config"] - if not config_thebe: + if not app.config["thebe_config"]: logger.warning("Didn't find `thebe_config` in conf.py, add to use thebe") return # Add core libraries opts = {"async": "async"} - app.add_js_file(filename="https://unpkg.com/thebelab@latest/lib/index.js", **opts) + app.add_js_file(filename=app.config.thebe_url, **opts) # Add configuration variables - thebe_config = f""" - const thebe_selector = "{ app.config.thebe_config['selector'] }" - const thebe_selector_input = "{ app.config.thebe_config['selector_input'] }" - const thebe_selector_output = "{ app.config.thebe_config['selector_output'] }" + selector_config = f""" + const thebe_selector = "{ app.config.thebe_selector['cell'] }" + const thebe_selector_input = "{ app.config.thebe_selector['input'] }" + const thebe_selector_output = "{ app.config.thebe_selector['output'] }" """ - app.add_js_file(None, body=thebe_config) + app.add_js_file(None, body=selector_config) app.add_js_file(filename="sphinx-thebe.js", **opts) @@ -63,7 +50,6 @@ def update_thebe_context(app, doctree, docname): raise ValueError( "thebe configuration must be `True` or a dictionary for configuration." ) - codemirror_theme = config_thebe.get("codemirror-theme", "abcdef") # Thebe configuration # Choose the kernel we'll use @@ -75,41 +61,10 @@ def update_thebe_context(app, doctree, docname): else: kernel_name = "python3" - # Codemirror syntax - cm_language = kernel_name - if "python" in cm_language: - cm_language = "python" - elif cm_language == "ir": - cm_language = "r" - - # Create the URL for the kernel request - repo_url = config_thebe.get( - "repository_url", - "https://github.com/binder-examples/jupyter-stacks-datascience", - ) - branch = config_thebe.get("repository_branch", "master") - path_to_docs = config_thebe.get("path_to_docs", ".").strip("/") + "/" - org, repo = _split_repo_url(repo_url) - # Update the doctree with some nodes for the thebe configuration thebe_html_config = f""" """ @@ -119,17 +74,6 @@ def update_thebe_context(app, doctree, docname): ) -def _split_repo_url(url): - """Split a repository URL into an org / repo combination.""" - if "github.com/" in url: - end = url.split("github.com/")[-1] - org, repo = end.split("/")[:2] - else: - logger.warning(f"Currently Thebe repositories must be on GitHub, got {url}") - org = repo = None - return org, repo - - class ThebeButtonNode(nodes.Element): """Appended to the doctree by the ThebeButton directive @@ -196,7 +140,22 @@ def setup(app): app.connect("env-updated", init_thebe_core) # configuration for this tool - app.add_config_value("thebe_config", {}, "html") + app.add_config_value("thebe_config", None, "html") + app.add_config_value( + "thebe_selector", + { + "cell": ".thebe", + "input": "pre", + "output": ".output", + }, + "html", + ) + app.add_config_value( + "thebe_url", + "https://unpkg.com/thebe@0.6.0/lib/index.js", + "html" + ) + # override=True in case Jupyter Sphinx has already been loaded app.add_directive("thebe-button", ThebeButton, override=True)