Skip to content

Commit

Permalink
Conditional disqus.js
Browse files Browse the repository at this point in the history
Conditionally including disqus.js on each page. Not including if the
directive isn't used.

Fixes #3
  • Loading branch information
Robpol86 committed Aug 3, 2016
1 parent f237aa2 commit e44549d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Fixed
* easy_install: https://bitbucket.org/birkenfeld/sphinx-contrib/issues/155/
* https://github.com/Robpol86/sphinxcontrib-disqus/issues/2
* https://github.com/Robpol86/sphinxcontrib-disqus/issues/1
* https://github.com/Robpol86/sphinxcontrib-disqus/issues/3

1.0.0 - 2015-07-31
------------------
Expand Down
22 changes: 15 additions & 7 deletions sphinxcontrib/disqus.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,21 @@ def run(self):
return [DisqusNode(disqus_shortname, disqus_identifier)]


def event_builder_inited(app):
"""Update the Sphinx builder.
def event_html_page_context(app, pagename, templatename, context, doctree):
"""Called when the HTML builder has created a context dictionary to render a template with.
Conditionally adding disqus.js to <head /> if the directive is used in a page.
:param sphinx.application.Sphinx app: Sphinx application object.
:param str pagename: Name of the page being rendered (without .html or any file extension).
:param str templatename: Page name with .html.
:param dict context: Jinja2 HTML context.
:param docutils.nodes.document doctree: Tree of docutils nodes.
"""
# Insert Disqus read-only javascript into the document body during the builder-inited event.
app.config.html_static_path.append(os.path.relpath(STATIC_DIR, app.confdir))
app.add_javascript('disqus.js')
assert app or pagename or templatename # Unused, for linting.
if 'script_files' in context and doctree and any(hasattr(n, 'disqus_shortname') for n in doctree.traverse()):
# Clone list to prevent leaking into other pages and add disqus.js to this page.
context['script_files'] = context['script_files'][:] + ['_static/disqus.js']


def setup(app):
Expand All @@ -118,7 +125,8 @@ def setup(app):
:rtype: dict
"""
app.add_config_value('disqus_shortname', None, True)
app.add_node(DisqusNode, html=(DisqusNode.visit, DisqusNode.depart))
app.add_directive('disqus', DisqusDirective)
app.connect('builder-inited', event_builder_inited)
app.add_node(DisqusNode, html=(DisqusNode.visit, DisqusNode.depart))
app.config.html_static_path.append(os.path.relpath(STATIC_DIR, app.confdir))
app.connect('html-page-context', event_html_page_context)
return dict(version=__version__)
16 changes: 14 additions & 2 deletions tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def test_shortname(tmpdir, tail, expected_error):
"""
tmpdir.join('conf.py').write(BASE_CONFIG.format(py.path.local(__file__).join('..', '..')))
tmpdir.join('conf.py').write(tail, mode='a')
tmpdir.join('index.rst').write('====\nMain\n====\n\n.. toctree::\n :maxdepth: 2\n.. disqus::')
tmpdir.join('index.rst').write('====\nMain\n====\n\n.. toctree::\n sub\n.. disqus::')
tmpdir.join('sub.rst').write('.. _sub:\n\n===\nSub\n===\n\nTest not loading javascript.')

command = ['sphinx-build', '-W', '-b', 'html', '.', '_build/html']
if expected_error:
Expand All @@ -43,10 +44,15 @@ def test_shortname(tmpdir, tail, expected_error):

stdout = check_output(command, cwd=str(tmpdir), stderr=STDOUT).decode('utf-8')
assert 'warning' not in stdout
assert tmpdir.join('_build', 'html', '_static', 'disqus.js').check(file=True)
body_index = tmpdir.join('_build', 'html', 'index.html').read()
assert 'id="disqus_thread"' in body_index
assert 'data-disqus-identifier="Main"' in body_index
assert 'disqus.js' in body_index
body_sub = tmpdir.join('_build', 'html', 'sub.html').read()
assert 'id="disqus_thread"' not in body_sub
assert 'data-disqus-identifier="Main"' not in body_sub
assert 'disqus.js' not in body_sub


@pytest.mark.parametrize('rst_title', ['====\nMain\n====\n\n', ''])
Expand All @@ -58,7 +64,8 @@ def test_identifier(tmpdir, rst_title):
"""
tmpdir.join('conf.py').write(BASE_CONFIG.format(py.path.local(__file__).join('..', '..')))
tmpdir.join('conf.py').write("disqus_shortname = 'good'", mode='a')
tmpdir.join('index.rst').write('{}.. toctree::\n :maxdepth: 2\n.. disqus::'.format(rst_title))
tmpdir.join('index.rst').write('{}.. toctree::\n sub\n.. disqus::'.format(rst_title))
tmpdir.join('sub.rst').write('.. _sub:\n\n===\nSub\n===\n\nTest not loading javascript.')

command = ['sphinx-build', '-b', 'html', '.', '_build/html']
if not rst_title:
Expand All @@ -71,7 +78,12 @@ def test_identifier(tmpdir, rst_title):

stdout = check_output(command, cwd=str(tmpdir), stderr=STDOUT).decode('utf-8')
assert 'warning' not in stdout
assert tmpdir.join('_build', 'html', '_static', 'disqus.js').check(file=True)
body_index = tmpdir.join('_build', 'html', 'index.html').read()
assert 'id="disqus_thread"' in body_index
assert 'data-disqus-identifier="Main"' in body_index
assert 'disqus.js' in body_index
body_sub = tmpdir.join('_build', 'html', 'sub.html').read()
assert 'id="disqus_thread"' not in body_sub
assert 'data-disqus-identifier="Main"' not in body_sub
assert 'disqus.js' not in body_sub
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ commands =
sphinx-versioning -t -S semver,chrono -e .gitignore -e .nojekyll -e README.rst push gh-pages . docs -- -W
deps =
{[testenv:docs]deps}
sphinxcontrib-versioning==1.0.0
sphinxcontrib-versioning==1.0.1
passenv =
HOME
HOSTNAME
Expand Down

0 comments on commit e44549d

Please sign in to comment.