Skip to content

Commit

Permalink
use autogenerated header (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
jobisoft committed Dec 15, 2020
1 parent 039b0ec commit 0b77872
Show file tree
Hide file tree
Showing 8 changed files with 318 additions and 13 deletions.
3 changes: 3 additions & 0 deletions _extensions/versionwarning/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-
name = 'versionwarning'
version = '1.1.2.dev0'
140 changes: 140 additions & 0 deletions _extensions/versionwarning/_static/js/versionwarning.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
function injectVersionWarningBanner(running_version, highest_version, config, versions) {
console.debug("injectVersionWarningBanner");
var current_url = window.location.pathname;
var isIndex = current_url.endsWith(running_version.slug + "/") || current_url.endsWith(running_version.slug + "/index.html");

var others = [];
$.each(versions, function (i, version) {
if (version.slug != running_version.slug && version.slug != highest_version.slug) {
let label = version.slug;
if (label == "latest") {
label = "Latest"
}
others.push("<a href='" + current_url.replace(running_version.slug, version.slug) + "'>" +label + "</a>");
}
});
let other = others.pop();
let first = others.join(", ");
if (first) {
other = first + " & " + other;
}

let msg = (config.banner.older_indexmessage && isIndex)
? config.banner.older_indexmessage
: config.banner.older_message;
let title = config.banner.older_title;
let type = config.banner.older_type
if (running_version.slug == "latest") {
msg = (config.banner.latest_indexmessage && isIndex)
? config.banner.latest_indexmessage
: config.banner.latest_message;
title = config.banner.latest_title;
type = config.banner.latest_type
} else if (running_version.slug == highest_version.slug) {
msg = (config.banner.current_indexmessage && isIndex)
? config.banner.current_indexmessage
: config.banner.current_message;
title = config.banner.current_title;
type = config.banner.current_type
}

if (msg) {
var warning = $(
config.banner.html
.replace("{message}", msg)
.replace("{id_div}", config.banner.id_div)
.replace("{banner_title}", title)
.replace("{admonition_type}", type)
.replace("{newest}", '<a href="' + current_url.replace(running_version.slug, highest_version.slug) + '">' + highest_version.slug + '</a>')
.replace("{this}", running_version.slug)
.replace("{other}", other)
);

var body = $(config.banner.body_selector);
body.prepend(warning);
}
}

function getHighestVersion(versions) {
console.debug("getHighestVersion");
var highest_version;

$.each(versions, function (i, version) {
if (isNaN(version.slug)) {
// Skip versions that are not numbers
}
else if (!highest_version) {
highest_version = version;
}
else if (version.slug > highest_version.slug) {
highest_version = version;
}
});
return highest_version;
}


function checkVersion(config) {
console.debug("checkVersion");
var running_version = config.version;
console.debug("Running version: " + running_version.slug);

var get_data = {
project__slug: config.project.slug,
active: "true"
// format: "jsonp",
};

$.ajax({
url: config.meta.api_url + "version/",
// Used when working locally for development
// crossDomain: true,
// xhrFields: {
// withCredentials: true,
// },
// dataType: "jsonp",
data: get_data,
success: function (versions) {
// TODO: fetch more versions if there are more pages (next)
highest_version = getHighestVersion(versions["results"]);
if (true
// semver.valid(semver.coerce(running_version.slug)) && semver.valid(semver.coerce(highest_version.slug)) &&
// semver.lt(semver.coerce(running_version.slug), semver.coerce(highest_version.slug))
) {
console.debug("Highest version: " + highest_version.slug);
injectVersionWarningBanner(running_version, highest_version, config, versions["results"]);
}
},
error: function () {
console.error("Error loading Read the Docs active versions.");
}
});
}

function init() {
console.debug("init");
// get the base_url so we can get the versionwarning-data.json from
// any page.
var base_url = $('script[src*=versionwarning]').attr('src');
base_url = base_url.replace('versionwarning.js', '');
$.ajax({
url: base_url + "../../_static/data/versionwarning-data.json",
success: function(config) {
// Check if there is already a banner added statically
var banner = document.getElementById(config.banner.id_div);
if (banner) {
console.debug("There is already a banner added. No checking versions.")
} else {
checkVersion(config);
}
},
error: function() {
console.error("Error loading versionwarning-data.json");
},
})
}


$(document).ready(function () {
init();
});
56 changes: 56 additions & 0 deletions _extensions/versionwarning/extension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
import os
import sphinx
from versionwarning import version
from .signals import generate_versionwarning_data_json


def setup(app):
default_message = 'You are not reading the most up to date version of this documentation. {newest} is the newest version.'

banner_html = '''
<div id="{id_div}" class="admonition {admonition_type}">
<p class="first admonition-title">{banner_title}</p>
<p class="last">
{message}
</p>
</div>'''

app.add_config_value('versionwarning_older_message', default_message, 'html')
app.add_config_value('versionwarning_older_indexmessage', '', 'html')
app.add_config_value('versionwarning_older_title', 'Warning', 'html')
app.add_config_value('versionwarning_older_type', 'warning', 'html')

app.add_config_value('versionwarning_current_message', '', 'html')
app.add_config_value('versionwarning_current_indexmessage', '', 'html')
app.add_config_value('versionwarning_current_title', 'Warning', 'html')
app.add_config_value('versionwarning_current_type', 'warning', 'html')

app.add_config_value('versionwarning_latest_message', '', 'html')
app.add_config_value('versionwarning_latest_indexmessage', '', 'html')
app.add_config_value('versionwarning_latest_title', 'Warning', 'html')
app.add_config_value('versionwarning_latest_type', 'warning', 'html')

app.add_config_value('versionwarning_api_url', 'https://readthedocs.org/api/v2/', 'html')
app.add_config_value('versionwarning_banner_html', banner_html, 'html')
app.add_config_value('versionwarning_banner_id_div', 'version-warning-banner', 'html')
app.add_config_value('versionwarning_body_selector', 'div.body', 'html')
app.add_config_value('versionwarning_project_slug', os.environ.get('READTHEDOCS_PROJECT', None), 'html')
app.add_config_value('versionwarning_project_version', os.environ.get('READTHEDOCS_VERSION', None), 'html')

if sphinx.version_info >= (1, 8):
# ``config-initied`` requires Sphinx >= 1.8
app.connect('config-inited', generate_versionwarning_data_json)

# ``add_js_file`` requires Sphinx >= 1.8
app.add_js_file('js/versionwarning.js')
else:
app.connect('builder-inited', generate_versionwarning_data_json)
app.add_javascript('js/versionwarning.js')

return {
'version': version,
'env_version': 1,
'parallel_read_safe': True,
'parallel_write_safe': True,
}
84 changes: 84 additions & 0 deletions _extensions/versionwarning/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# -*- coding: utf-8 -*-

import json
import os


STATIC_PATH = os.path.join(os.path.dirname(__file__), '_static')
JSON_DATA_FILENAME = 'versionwarning-data.json'


def generate_versionwarning_data_json(app, config=None, **kwargs):
"""
Generate the ``versionwarning-data.json`` file.
This file is included in the output and read by the AJAX request when
accessing to the documentation and used to compare the live versions with
the curent one.
Besides, this file contains meta data about the project, the API to use and
the banner itself.
"""

# In Sphinx >= 1.8 we use ``config-initied`` signal which comes with the
# ``config`` object and in Sphinx < 1.8 we use ``builder-initied`` signal
# that doesn't have the ``config`` object and we take it from the ``app``
config = config or kwargs.pop('config', None)
if config is None:
config = app.config

#if config.versionwarning_project_version in config.versionwarning_messages:
# custom = True
# message = config.versionwarning_messages.get(config.versionwarning_project_version)
#else:
# custom = False
# message = config.versionwarning_default_message

#banner_html = config.versionwarning_banner_html.format(
# id_div=config.versionwarning_banner_id_div,
# banner_title=config.versionwarning_banner_title,
# message=message.format(
# **{config.versionwarning_message_placeholder: '<a href="#"></a>'},
# ),
# admonition_type=config.versionwarning_admonition_type,
#)

data = json.dumps({
'meta': {
'api_url': config.versionwarning_api_url,
},
'banner': {
'html': config.versionwarning_banner_html,
'id_div': config.versionwarning_banner_id_div,
'body_selector': config.versionwarning_body_selector,
'older_message': config.versionwarning_older_message,
'older_indexmessage': config.versionwarning_older_indexmessage,
'older_title': config.versionwarning_older_title,
'older_type': config.versionwarning_older_type,
'current_message': config.versionwarning_current_message,
'current_indexmessage': config.versionwarning_current_indexmessage,
'current_title': config.versionwarning_current_title,
'current_type': config.versionwarning_current_type,
'latest_message': config.versionwarning_latest_message,
'latest_indexmessage': config.versionwarning_latest_indexmessage,
'latest_title': config.versionwarning_latest_title,
'latest_type': config.versionwarning_latest_type,
},
'project': {
'slug': config.versionwarning_project_slug,
},
'version': {
'slug': config.versionwarning_project_version,
},
}, indent=4)

data_path = os.path.join(STATIC_PATH, 'data')
if not os.path.exists(data_path):
os.mkdir(data_path)

with open(os.path.join(data_path, JSON_DATA_FILENAME), 'w') as f:
f.write(data)

# Add the path where ``versionwarning-data.json`` file and
# ``versionwarning.js`` are saved
config.html_static_path.append(STATIC_PATH)
2 changes: 1 addition & 1 deletion _static/theme_overrides.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
overflow: visible !important;
}

}
}
29 changes: 28 additions & 1 deletion conf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import sys, os

# local extension folder
sys.path.append(os.path.abspath('_extensions'))

project = u'Thunderbird WebExtension APIs'
source_suffix = '.rst'
master_doc = 'index'
Expand All @@ -18,6 +23,28 @@
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']

# Configure headers
versionwarning_body_selector = 'div[itemprop="articleBody"]'

versionwarning_latest_type = 'tip'
versionwarning_latest_title = 'Note'
versionwarning_latest_message = 'This is the API documentation for pre-release versions of Thunderbird. See version {newest} for the current ESR of Thunderbird.'

versionwarning_current_type = 'tip'
versionwarning_current_title = 'Note'
versionwarning_current_indexmessage = 'This is the API documentation for the current ESR of Thunderbird, version {this}. Other available versions are: {other}'

versionwarning_older_type = 'warning'
versionwarning_older_title = 'Warning'
versionwarning_older_message = 'This is the API documentation for Thunderbird {this}. See version {newest} for the current ESR of Thunderbird.'


extensions = [
# ... other extensions here
'versionwarning.extension',
#'sphinx_toolbox.confval',
]

def setup(app):
#app.add_javascript("custom.js")
app.add_stylesheet("theme_overrides.css")
app.add_stylesheet('theme_overrides.css')
17 changes: 6 additions & 11 deletions index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,8 @@ highly recommended to read our `Guide to MailExtensions`__ or some of the `MDN d
__ https://developer.thunderbird.net/add-ons/mailextensions
__ https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions

.. note::

This documentation is for pre-release versions of Thunderbird. See the `"78" version`__ for
Thunderbird 78, or the `"68" version`__ for Thunderbird 68.
For any problems or feature requests please `file a bug`__.
For any problems or feature requests please `file a bug`__.

__ https://thunderbird-webextensions.readthedocs.io/en/78/
__ https://thunderbird-webextensions.readthedocs.io/en/68/
__ https://bugzilla.mozilla.org/enter_bug.cgi?product=Thunderbird&component=Add-Ons%3A+Extensions+API

.. note::
Expand All @@ -26,6 +20,11 @@ __ https://bugzilla.mozilla.org/enter_bug.cgi?product=Thunderbird&component=Add-
__ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
__ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises

.. note::

In Thunderbird, all WebExtension API can be accessed through the *browser.\** namespace, as with Firefox,
but also through the *messenger.\** namespace, which is a better fit for Thunderbird.

Thunderbird APIs
=================

Expand Down Expand Up @@ -222,10 +221,6 @@ as well. The APIs listed in the following table are known to work with Thunderbi
.. |webNavigation-Description| replace:: Add event listeners for the various stages of a navigation. A navigation consists of a frame in the browser transitioning from one URL to another, usually (but not always) in response to a user action like clicking a link.
.. |webRequest-Description| replace:: Add event listeners for the various stages of making an HTTP request, which includes websocket requests on ws:// and wss://. The event listener receives detailed information about the request and can modify or cancel the request.

.. note::

In Thunderbird, all WebExtension API can be accessed through the *browser.\** namespace, as with Firefox,
but also through the *messenger.\** namespace, which is a better fit for Thunderbird.

.. toctree::
:hidden:
Expand Down
Empty file added requirements.txt
Empty file.

0 comments on commit 0b77872

Please sign in to comment.