From 5cf0c6c91c077b8b69ea159c19f0c6fb02ab9c19 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Tue, 28 Aug 2018 01:56:44 -0700 Subject: [PATCH] New plugin hook: extra_body_script --- datasette/hookspecs.py | 5 +++++ datasette/templates/base.html | 5 +++++ datasette/views/base.py | 9 ++++++++ docs/plugins.rst | 11 ++++++++++ tests/fixtures.py | 17 +++++++++++++++ tests/test_plugins.py | 41 +++++++++++++++++++++++++++++++++++ 6 files changed, 88 insertions(+) diff --git a/datasette/hookspecs.py b/datasette/hookspecs.py index 8523a135..9b34f8a0 100644 --- a/datasette/hookspecs.py +++ b/datasette/hookspecs.py @@ -25,6 +25,11 @@ def extra_js_urls(): "Extra JavaScript URLs added by this plugin" +@hookspec +def extra_body_script(template, database, table, datasette): + "Extra JavaScript code to be included in +{% endfor %} + {% if select_templates %}{% endif %} diff --git a/datasette/views/base.py b/datasette/views/base.py index 8b72d234..422925d8 100644 --- a/datasette/views/base.py +++ b/datasette/views/base.py @@ -52,6 +52,14 @@ class RenderMixin(HTTPMethodView): "{}{}".format("*" if template_name == template.name else "", template_name) for template_name in templates ] + body_scripts = [] + for script in pm.hook.extra_body_script( + template=template.name, + database=context.get("database"), + table=context.get("table"), + datasette=self.ds + ): + body_scripts.append(jinja2.Markup(script)) return response.html( template.render( { @@ -60,6 +68,7 @@ class RenderMixin(HTTPMethodView): "app_css_hash": self.ds.app_css_hash(), "select_templates": select_templates, "zip": zip, + "body_scripts": body_scripts, } } ) diff --git a/docs/plugins.rst b/docs/plugins.rst index c95329d6..d4fc0b8c 100644 --- a/docs/plugins.rst +++ b/docs/plugins.rst @@ -385,3 +385,14 @@ If the value matches that pattern, the plugin returns an HTML link element: href=jinja2.escape(data["href"]), label=jinja2.escape(data["label"] or "") or " " )) + +extra_body_script(template, database, table, datasette) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Extra JavaScript to be added to a ``") + json_data = r.search(app_client.get(path).body.decode("utf8")).group(1) + actual_data = json.loads(json_data) + assert expected_extra_body_script == actual_data