Skip to content

Commit 707455b

Browse files
authored
Merge pull request #15 from humitos/only-dynamic-banner
Support only dynamic banner via Javascript
2 parents c4e2f67 + 40bab04 commit 707455b

11 files changed

Lines changed: 125 additions & 255 deletions

File tree

CHANGELOG.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
1.0.0
2+
-----
3+
4+
:Date: UNRELEASED
5+
6+
* Remove ability to add the warning banner statically
7+
8+
* Make the banner more customizable (all the configs are included in the ``json`` file generated and available from Javascript)
9+
10+
* Rename ``versionwarning_default_admonition_type`` by ``versionwarning_admonition_type``
11+
12+
* Rename ``versionwarning_body_default_selector`` by ``versionwarning_body_selector``
13+
14+
* Remove ``versionwarning_body_extra_selector``
15+
16+
* Refactor the code to avoid potential circular imports
17+
18+
* Filter Read the Docs versions by ``active=True`` when retrieving versions
19+
20+
121
0.2.0
222
-----
323

README.rst

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ How it works?
1818
-------------
1919

2020
When visiting a page in Read the Docs that was built with this extension enabled,
21-
an AJAX request is done to the Read the Docs servers to retrieve all the versions of the project.
21+
an AJAX request is done to the Read the Docs servers to retrieve all the **active versions** of the project.
2222
These versions are compared against the one that we are reading and if it's an old version,
23-
a red *Warning* banner appears at the top of the page.
23+
a *Warning* banner appears at the top of the page.
2424

2525

2626
Examples
@@ -31,13 +31,11 @@ Examples
3131
There is a live example living at Read the Docs:
3232

3333
- `latest`_ version doesn't show any kind of warning banner
34-
- `0.0.1`_ version shows a custom and fixed message added at build time
3534
- `0.0.2`_ version shows a warning banner saying that 0.0.3 is available (at the time of writing this docs)
3635
- `0.0.3`_ version doesn't show any banner since it's the latest version (at the time of writing this docs)
3736

3837

3938
.. _latest: https://sphinx-version-warning-example.readthedocs.io/en/latest/
40-
.. _0.0.1: https://sphinx-version-warning-example.readthedocs.io/en/0.0.1/
4139
.. _0.0.2: https://sphinx-version-warning-example.readthedocs.io/en/0.0.2/
4240
.. _0.0.3: https://sphinx-version-warning-example.readthedocs.io/en/0.0.3/
4341

@@ -57,22 +55,24 @@ Then in your ``conf.py`` you have to add ``versionwarning`` in the ``extensions`
5755
]
5856

5957

60-
Remember to configure the ``version`` of your Sphinx project since it's the key for this to work properly::
58+
Remember to configure the ``versionwarning_project_version`` and ``versionwarning_project_slug`` of your Sphinx project since it's the key for this to work properly::
6159

62-
version = '0.0.1'
60+
versionwarning_project_version = '0.0.1'
61+
versionwarning_project_slug = 'sphinx-version-warning'
6362

6463
.. warning::
6564

66-
If ``READTHEDOCS_VERSION`` variable is defined, the extension will use its value.
67-
Otherwise, ``versionwarning_project_version`` will be used and if it's not defined, ``version`` as last resource.
65+
If you are building your documentation under Read the Docs,
66+
``READTHEDOCS_VERSION`` and ``READTHEDOCS_PROECT`` environment variables will be defined and there is no need to define these variables,
67+
unless you want to override the default values.
6868

6969

7070
Customization
7171
-------------
7272

7373
Some customization can be done using the ``conf.py`` file of your Sphinx project:
7474

75-
versionwarning_default_admonition_type (string)
75+
versionwarning_admonition_type (string)
7676
type of admonition for the banner (warning, admonition or note)
7777

7878
versionwarning_default_message (string)
@@ -88,7 +88,7 @@ versionwarning_project_slug (string)
8888
slug of the project under Read the Docs (default to ``READTHEDOCS_PROJECT`` environment variable)
8989

9090
versionwarning_project_version (string)
91-
slug of the version for the current documentation (default to ``READTHEDOCS_VERSION`` environment variable or ``version`` variable from ``conf.py``)
91+
slug of the version for the current documentation (default to ``READTHEDOCS_VERSION`` environment variable)
9292

9393
versionwarning_api_url (string)
9494
API URL to retrieve all versions for this project
@@ -99,11 +99,8 @@ versionwarning_banner_html (string)
9999
versionwarning_banner_id_div (string)
100100
HTML element ID used for the <div> inject as banner
101101

102-
versionwarning_body_default_selector (string)
103-
jQuery selector to find the body element in the page
104-
105-
versionwarning_body_extra_selector (string)
106-
jQuery selector to find the body element in the page if ``versionwarning_body_default_selector`` wasn't found
102+
versionwarning_body_selector (string)
103+
jQuery selector to find the body element in the page and *prepend* the banner
107104

108105

109106
How to contribute?

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "sphinx-version-warning",
3-
"version": "0.2.0",
3+
"version": "1.0.0",
44
"dependencies": {
5-
"semver": "^5.5.0",
6-
"webpack": "^4.16.3",
7-
"webpack-cli": "^3.1.0"
5+
"semver": "^5.6.0",
6+
"webpack": "^4.22.0",
7+
"webpack-cli": "^3.1.2"
88
}
99
}

setup.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
'Operating System :: OS Independent',
2525
),
2626
install_requires=[
27-
'munch',
28-
'slumber',
2927
'sphinx',
3028
],
3129
)

versionwarning/__init__.py

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,3 @@
11
# -*- coding: utf-8 -*-
2-
32
name = 'versionwarning'
4-
version = '0.2.0'
5-
6-
7-
def setup(app):
8-
import os
9-
import sphinx
10-
from .signals import process_version_warning_banner, generate_versionwarning_data_json
11-
12-
default_message = 'You are not reading the most up to date version of this documentation. {newest} is the newest version.'
13-
14-
banner_html = '''
15-
<div id="{id_div}" class="admonition warning">
16-
<p class="first admonition-title">{banner_title}</p>
17-
<p class="last">
18-
{message}
19-
</p>
20-
</div>'''.format(
21-
id_div='version-warning-banner',
22-
banner_title='Warning',
23-
message=default_message.format(newest='<a href="#"></a>'),
24-
)
25-
26-
app.add_config_value('versionwarning_message_placeholder', '{newest}', 'html')
27-
app.add_config_value('versionwarning_default_admonition_type', 'warning', 'html')
28-
app.add_config_value('versionwarning_default_message', default_message, 'html')
29-
app.add_config_value('versionwarning_messages', {}, 'html')
30-
31-
app.add_config_value('versionwarning_api_url', 'https://readthedocs.org/api/v2/', 'html')
32-
app.add_config_value('versionwarning_banner_html', banner_html, 'html')
33-
app.add_config_value('versionwarning_banner_id_div', 'version-warning-banner', 'html')
34-
app.add_config_value('versionwarning_body_default_selector', 'div.body', 'html')
35-
app.add_config_value('versionwarning_body_extra_selector', 'div.document', 'html')
36-
app.add_config_value('versionwarning_project_slug', os.environ.get('READTHEDOCS_PROJECT', None), 'html')
37-
app.add_config_value('versionwarning_project_version', os.environ.get('READTHEDOCS_VERSION', None), 'html')
38-
39-
app.connect('doctree-resolved', process_version_warning_banner)
40-
41-
if sphinx.version_info >= (1, 8):
42-
# ``config-initied`` requires Sphinx >= 1.8
43-
app.connect('config-inited', generate_versionwarning_data_json)
44-
45-
# ``add_js_file`` requires Sphinx >= 1.8
46-
app.add_js_file('js/versionwarning.js')
47-
else:
48-
app.connect('builder-inited', generate_versionwarning_data_json)
49-
app.add_javascript('js/versionwarning.js')
50-
51-
return {
52-
'version': version,
53-
'env_version': 1,
54-
'parallel_read_safe': True,
55-
'parallel_write_safe': True,
56-
}
3+
version = '1.0.0'

versionwarning/_static/js/versionwarning.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

versionwarning/_static/js/versionwarning.src.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const semver = require('semver');
22

33
function injectVersionWarningBanner(running_version, version, config) {
4+
console.debug("injectVersionWarningBanner");
45
var version_url = window.location.pathname.replace(running_version.slug, version.slug);
56
var warning = $(config.banner.html);
67

@@ -9,15 +10,21 @@ function injectVersionWarningBanner(running_version, version, config) {
910
.attr("href", version_url)
1011
.text(version.slug);
1112

12-
var body = $(config.banner.body_default_selector);
13-
if (!body.length) {
14-
body = $(config.banner.body_extra_selector);
15-
}
13+
var body = $(config.banner.body_selector);
14+
body.prepend(warning);
15+
}
16+
17+
18+
function injectCustomWarningBanner(config) {
19+
console.debug("injectCustomWarningBanner");
20+
var warning = $(config.banner.html);
21+
var body = $(config.banner.body_selector);
1622
body.prepend(warning);
1723
}
1824

1925

2026
function getHighestVersion(versions) {
27+
console.debug("getHighestVersion");
2128
var highest_version;
2229

2330
$.each(versions, function (i, version) {
@@ -38,13 +45,13 @@ function getHighestVersion(versions) {
3845

3946

4047
function checkVersion(config) {
48+
console.debug("checkVersion");
4149
var running_version = config.version;
4250
console.debug("Running version: " + running_version.slug);
4351

4452
var get_data = {
4553
project__slug: config.project.slug,
46-
// active is not yet deployed
47-
// active: "true",
54+
active: "true"
4855
// format: "jsonp",
4956
};
5057

@@ -74,6 +81,7 @@ function checkVersion(config) {
7481
}
7582

7683
function init() {
84+
console.debug("init");
7785
$.ajax({
7886
url: "_static/data/versionwarning-data.json",
7987
success: function(config) {
@@ -82,6 +90,9 @@ function init() {
8290
if (banner) {
8391
console.debug("There is already a banner added. No checking versions.")
8492
}
93+
else if (config.banner.custom) {
94+
injectCustomWarningBanner(config);
95+
}
8596
else {
8697
checkVersion(config);
8798
}

versionwarning/banner.py

Lines changed: 0 additions & 105 deletions
This file was deleted.

0 commit comments

Comments
 (0)