Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify the <head> in Sphinx documentation so the relative links are updated?

I'm using Sphinx to document a python project. I'm using the canvas to visualize some results in the documentation. My documentation needs to support Firefox and IE. I need to include the excanvas.js library in the documentation, but only if the browser is IE. How can I conditionally include this library so the relative paths are correct?

Example....

Documentation folders

/sphinx
   /source
   working_test_page.rst
   test_block.html
   /nested
      non_working_test_page.rst
      test_block_copy.html
   /_templates
      layout.html
   /_static
      excanvas.js
   ...

Per the notes at Sphinx' documentation pages, the file layout.html was modified. This modification was to insert a conditional block of HTML in the template head which adds excanvas.js on if the page is viewed on IE.

{% extends "!layout.html" %} 
{% block extrahead %} 
  <!--[if IE]>
    <script type="text/javascript" src="_static/excanvas.js"></script>
  <![endif]--> 
{% endblock %} 

The file working_test_page.rst and non_working_test_page.rst have the same contents. The contents follow. The only difference is the location of the files.

Script Test
==========

.. raw:: html
   :file: test_block_1.html

The file test_block.html and test_block_copy.html have the same contents. The two files contain a some HTML which sets up a canvas and uses it.

When sphinx compiles the rst files into the HTML build directory, the following files structure results:

/sphinx
   /build
   working_test_page.html
   /nested
      non_working_test_page.html
   /_static
      excanvas.js
   ...

The file working_test_page.html has the correct path to excanvas.js and loads correctly. The file non_working_test_page.html has the wrong path to excanvas.js and does not load correctly.

How can excanvas.js be conditionally loaded so that the relative paths in the sphinx documentation are correct regardless of the location of the rst files?

like image 890
Ed Tate Avatar asked Aug 07 '12 20:08

Ed Tate


1 Answers

The conditional link will be formatted properly if the helper function pathto(...) is used to modify the conditional HTML snipet. See documentation on pathto(file,1).

{% extends "!layout.html" %}
{% block extrahead %}
    <!--[if IE]>
      <script type="text/javascript" src="{{pathto("_static/excanvas.js", 1)}}"></script>
    <![endif]-->
{% endblock %}
like image 128
Ed Tate Avatar answered Oct 17 '22 05:10

Ed Tate