chiark / gitweb /
theme: make it possible to add JS files and arbitrary HTML to page head.
authorVladimír Vondruš <mosra@centrum.cz>
Fri, 12 Oct 2018 19:08:41 +0000 (21:08 +0200)
committerVladimír Vondruš <mosra@centrum.cz>
Fri, 12 Oct 2018 19:34:35 +0000 (21:34 +0200)
doc/pelican/theme.rst
pelican-theme/templates/base.html
pelican-theme/templates/page.html
pelican-theme/test/page_extra_css/page.rst [deleted file]
pelican-theme/test/page_extra_html_head/page.html [moved from pelican-theme/test/page_extra_css/page.html with 84% similarity]
pelican-theme/test/page_extra_html_head/page.rst [new file with mode: 0644]
pelican-theme/test/test_page.py

index c7603cd82b941c2a10a047ccdf5eaffb7f76afa4..478757b0664f16ac1be40a5782c3f43acaadde1b 100644 (file)
@@ -337,11 +337,15 @@ Pages can override which menu item in the `top navbar`_ will be highlighted
 by specifying the corresponding menu item slug in the :rst:`:highlight:` field.
 If the field is not present, page's own slug is used instead.
 
-`Extra CSS`_
-------------
+`Extending HTML \<head\>`_
+--------------------------
 
 The :rst:`:css:` field can be used to link additional CSS files in page header.
-Put one URL per line, internal link targets are expanded. Example:
+Put one URL per line, internal link targets are expanded. Similarly :rst:`:js:`
+can be used to link JavaScript files. Besides that, the :rst:`:html_header:`
+field can be used to put arbitrary HTML at the end of the :html:`<head>`
+element. Indenting the lines is possible by putting an escaped space in front
+(the backslash and the escaped space itself won't get inserted). Example:
 
 .. code:: rst
 
@@ -351,6 +355,15 @@ Put one URL per line, internal link targets are expanded. Example:
     :css:
         {filename}/static/webgl.css
         {filename}/static/canvas-controls.css
+    :js:
+        {filename}/static/showcase.js
+    :html_header:
+        <script>
+        \   function handleDrop(event) {
+        \     event.preventDefault();
+        \     ...
+        \   }
+        </script>
 
 `Breadcrumb navigation`_
 ------------------------
index 9c9930d778d27b99bd4d41d4eb5825fbc5c74971..de99b0de71d5d60270b32126069808cf2cc2b1de 100644 (file)
@@ -36,6 +36,8 @@
   {% block social %}
   {% endblock social %}
   {% endif %}
+  {% block extra %}
+  {% endblock extra %}
 </head>
 <body>
 <header><nav id="navigation"{% if page and page.landing and page.cover %} class="m-navbar-landing"{% elif (page and page.cover) or (article and article.cover) %} class="m-navbar-cover"{% endif %}>
index 3250ace9cc2618061fa40c937eff683267fd948c..23bbaf6fe718a8e7fd77b33fb9cf37b98be6a6ee 100644 (file)
   <meta property="og:type" content="page" />
 {% endblock %}
 
+{% block extra %}
+  {% if page.js %}
+  {% set scripts = page.js.strip().split('\n') %}
+  {% for script in scripts %}
+  <script src="{{ script|expand_link(page)|e }}"></script>
+  {% endfor %}
+  {% endif %}
+  {% if page.html_header %}
+  {{ page.html_header|indent(2) }}
+  {% endif %}
+{% endblock %}
+
 {% block main %}
 {% if not page.landing %}
 {% if page.cover %}
diff --git a/pelican-theme/test/page_extra_css/page.rst b/pelican-theme/test/page_extra_css/page.rst
deleted file mode 100644 (file)
index 0a1d86c..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-A page
-######
-
-:css: static/m-debug.css
-      static/m-grid.css
similarity index 84%
rename from pelican-theme/test/page_extra_css/page.html
rename to pelican-theme/test/page_extra_html_head/page.html
index 897030fa0e5ba4595d44352f0a794e2ab6e7d6c8..b69202f949007531216f627da1bf8bea4fe7669e 100644 (file)
@@ -9,6 +9,11 @@
   <link rel="stylesheet" href="static/m-grid.css" />
   <link rel="canonical" href="page.html" />
   <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+  <script src="a.js"></script>
+  <script src="b.js"></script>
+  <!-- this goes into the HTML head
+       another line -->
+  <!-- have to indent them with a backslash -->
 </head>
 <body>
 <header><nav id="navigation">
diff --git a/pelican-theme/test/page_extra_html_head/page.rst b/pelican-theme/test/page_extra_html_head/page.rst
new file mode 100644 (file)
index 0000000..4621b18
--- /dev/null
@@ -0,0 +1,13 @@
+A page
+######
+
+:css:
+    static/m-debug.css
+    static/m-grid.css
+:js:
+    a.js
+    b.js
+:html_header:
+    <!-- this goes into the HTML head
+    \      another line -->
+    <!-- have to indent them with a backslash -->
index 381590a3ecf64a5285e8ff9857d3aaf3b375081c..3666cd63e5854c7f7d8cf703e971ad90892a627d 100644 (file)
@@ -71,14 +71,15 @@ class Breadcrumb(PageTestCase):
         self.assertEqual(*self.actual_expected_contents('page.html'))
         self.assertEqual(*self.actual_expected_contents('subpage.html'))
 
-class ExtraCss(PageTestCase):
+class ExtraHtmlHead(PageTestCase):
     def __init__(self, *args, **kwargs):
-        super().__init__(__file__, 'extra_css', *args, **kwargs)
+        super().__init__(__file__, 'extra_html_head', *args, **kwargs)
 
     def test(self):
         self.run_pelican({})
 
-        # The page should contain two extra CSS links
+        # The page should contain two extra CSS links, two JS references and a
+        # multi-line HTML comment
         self.assertEqual(*self.actual_expected_contents('page.html'))
 
 class HeaderFooter(PageTestCase):