Search code examples
pythonrestructuredtextdocutils

How to add Google Analytics to reStructuredText?


I am using reStructured text to create some easy websites. So I have got a lot of *.rst files in which I want to add the Google Analytics code. But as far as I know it is not possible to add something like this?!

I am using rst2html to convert the files to html.


Solution

  • I've just discovered an easy way to add custom content to .rst files. All you need to do it to modify the template for html files.

    Make a new template template.txt and the following contents to it (based on the default template):

    %(head_prefix)s
    %(head)s
    <!--your tracking code-->
    %(stylesheet)s
    %(body_prefix)s
    %(body_pre_docinfo)s
    %(docinfo)s
    %(body)s
    %(body_suffix)s
    

    The format is pretty self explanatory and its also a good way to remove the default CSS and specify a link to another one in the template etc.

    Now you can use your custom template with the rst2html writer:

    rst2html.py --template=template.txt document.rst