Question

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.

Was it helpful?

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

OTHER TIPS

I guess, you'd have to extend the docutils HTML Translator or Writer to include GA.

If possible, I'd recommend to abandon rst2html and plain docutils and use Sphinx instead. It is based on docutils, but far more powerful. Its HTML templates can easily be extended to include arbitrary HTML like script tags for Google Analytics.

You can insert html to rst files using the .. raw:: directive.

As a workaround to your problem, you could use a mass search/replace tool to add the Google Analytics code to the files after they have been through the translator. Just search for the </body> tag and replace it with <!--your tracking code--></body>.

I checked to see if you can include raw HTML in reStructuredText (and have it be untouched), but it doesn't seem possible...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top