Skip to content

Commit

Permalink
Tentative workaround for sphinxcontrib-googleanalytics broken extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefano David committed Apr 19, 2019
1 parent 44d9bd8 commit 65a1ec8
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
'sphinx.ext.ifconfig',
'sphinx.ext.extlinks',
'sphinx.ext.imgconverter',
'sphinx_tabs.tabs',
'sphinxcontrib.googleanalytics'
'sphinx_tabs.tabs'
]

# Add any paths that contain templates here, relative to this directory.
Expand Down Expand Up @@ -467,6 +466,38 @@
#
# epub_use_index = True

# Tentative workaround for sphinxcontrib-googleanalytics which uses an
# old, obsolete import.

from sphinx.errors import ExtensionError

def add_ga_javascript(app, pagename, templatename, context, doctree):
if not app.config.googleanalytics_enabled:
return

metatags = context.get('metatags', '')
metatags += """<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '%s']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>""" % app.config.googleanalytics_id
context['metatags'] = metatags

def check_config(app):
if not app.config.googleanalytics_id:
raise ExtensionError("'googleanalytics_id' config value must be set for ga statistics to function properly.")

def setup(app):
app.add_stylesheet('https://use.fontawesome.com/releases/v5.7.0/css/all.css')
app.add_stylesheet('https://use.fontawesome.com/releases/v5.7.0/css/all.css')
app.add_config_value('googleanalytics_id', '', 'html')
app.add_config_value('googleanalytics_enabled', True, 'html')
app.connect('html-page-context', add_ga_javascript)
app.connect('builder-inited', check_config)
return {'version': '0.1'}

0 comments on commit 65a1ec8

Please sign in to comment.