2015-12-27 23:32:07 +01:00
|
|
|
var nodemcu = nodemcu || {};
|
2018-09-04 22:13:55 +02:00
|
|
|
|
2015-12-27 23:32:07 +01:00
|
|
|
(function () {
|
|
|
|
'use strict';
|
2016-01-16 23:14:27 +01:00
|
|
|
//var languageCodeToNameMap = {en: 'English', de: 'Deutsch'};
|
|
|
|
var languageCodeToNameMap = {en: 'English'};
|
2015-12-27 23:32:07 +01:00
|
|
|
var languageNames = values(languageCodeToNameMap);
|
2015-12-28 17:25:00 +01:00
|
|
|
var defaultLanguageCode = 'en';
|
2015-12-27 23:32:07 +01:00
|
|
|
|
|
|
|
$(document).ready(function () {
|
2016-01-15 22:34:33 +01:00
|
|
|
addToc();
|
2015-12-27 23:32:07 +01:00
|
|
|
hideNavigationForAllButSelectedLanguage();
|
2015-12-28 17:25:00 +01:00
|
|
|
addLanguageSelectorToRtdFlyOutMenu();
|
2016-03-05 10:47:01 +01:00
|
|
|
replaceRelativeLinksWithStaticGitHubUrl();
|
2015-12-27 23:32:07 +01:00
|
|
|
});
|
|
|
|
|
2016-01-15 22:34:33 +01:00
|
|
|
/**
|
|
|
|
* Adds a TOC-style table to each page in the 'Modules' section.
|
|
|
|
*/
|
|
|
|
function addToc() {
|
|
|
|
var func, intro, tocHtmlTable;
|
|
|
|
if (isModulePage()) {
|
|
|
|
tocHtmlTable = '<table class="docutils">';
|
|
|
|
$('h2').each(function (index) {
|
|
|
|
// 'slice' cuts off the single permalink character at the end of the text (e.g. '¶')
|
|
|
|
func = $(this).text().slice(0, -1);
|
|
|
|
// get the first sentence of the paragraph directly below h2
|
|
|
|
intro = $(this).next().text();
|
|
|
|
intro = intro.substring(0, intro.indexOf('.') + 1);
|
|
|
|
tocHtmlTable += createTocTableRow(func, intro);
|
|
|
|
});
|
|
|
|
tocHtmlTable += '</table>';
|
|
|
|
$(tocHtmlTable).insertBefore($('h2').first())
|
|
|
|
}
|
|
|
|
function isModulePage() {
|
|
|
|
// if the breadcrumb contains 'Modules »' it must be an API page
|
|
|
|
return $("ul.wy-breadcrumbs li:contains('Modules »')").size() > 0;
|
|
|
|
}
|
|
|
|
function createTocTableRow(func, intro) {
|
|
|
|
// fragile attempt to auto-create the in-page anchor
|
2017-04-13 21:42:18 +02:00
|
|
|
// good tests: file.md,
|
|
|
|
var href = func.replace(/[\.:\(\)]/g, '').replace(/ --|, | /g, '-');
|
2016-01-16 10:02:06 +01:00
|
|
|
var link = '<a href="#' + href.toLowerCase() + '">' + func + '</a>';
|
2016-01-15 22:34:33 +01:00
|
|
|
return '<tr><td>' + link + '</td><td>' + intro + '</td></tr>';
|
|
|
|
}
|
2016-06-08 22:32:21 +02:00
|
|
|
}
|
|
|
|
|
2015-12-27 23:32:07 +01:00
|
|
|
function hideNavigationForAllButSelectedLanguage() {
|
2015-12-28 17:25:00 +01:00
|
|
|
var selectedLanguageCode = determineSelectedLanguageCode();
|
2015-12-27 23:32:07 +01:00
|
|
|
var selectedLanguageName = languageCodeToNameMap[selectedLanguageCode];
|
|
|
|
// Finds all subnav elements and hides them if they're /language/ subnavs. Hence, all 'Modules' subnav elements
|
|
|
|
// won't be hidden.
|
|
|
|
// <ul class="subnav">
|
|
|
|
// <li><span>Modules</span></li>
|
|
|
|
// <li class="toctree-l1 ">
|
|
|
|
// <a class="" href="EN/modules/node/">node</a>
|
|
|
|
// </li>
|
|
|
|
$('.subnav li span').not(':contains(' + selectedLanguageName + ')').each(function (index) {
|
|
|
|
var spanElement = $(this);
|
|
|
|
if ($.inArray(spanElement.text(), languageNames) > -1) {
|
|
|
|
spanElement.parent().parent().hide();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-12-28 17:25:00 +01:00
|
|
|
/**
|
2015-12-28 23:36:58 +01:00
|
|
|
* Adds a language selector to the RTD fly-out menu found bottom left. Example:
|
|
|
|
*
|
2015-12-28 17:25:00 +01:00
|
|
|
* <dl>
|
|
|
|
* <dt>Languages</dt>
|
2016-04-28 08:09:35 +02:00
|
|
|
* <dd><a href="http://nodemcu.readthedocs.io/en/<branch>/de/">de</a></dd>
|
2015-12-28 17:25:00 +01:00
|
|
|
* <strong>
|
2016-04-28 08:09:35 +02:00
|
|
|
* <dd><a href="http://nodemcu.readthedocs.io/en/<branch>/en/">en</a></dd>
|
2015-12-28 17:25:00 +01:00
|
|
|
* </strong>
|
|
|
|
* </dl>
|
2015-12-28 22:33:48 +01:00
|
|
|
*
|
2015-12-28 23:36:58 +01:00
|
|
|
* UGLY! That fly-out menu is added by RTD with an AJAX call after page load. Hence, we need to
|
|
|
|
* react to the subsequent DOM manipulation using a DOM4 MutationObserver. The provided structure
|
|
|
|
* is as follows:
|
|
|
|
*
|
|
|
|
* <div class="rst-other-versions">
|
|
|
|
* <!-- Inserted RTD Footer -->
|
|
|
|
* <div class="injected">
|
2015-12-28 17:25:00 +01:00
|
|
|
*/
|
|
|
|
function addLanguageSelectorToRtdFlyOutMenu() {
|
2015-12-28 23:36:58 +01:00
|
|
|
var flyOutWrapper = $('.rst-other-versions');
|
|
|
|
// only relevant on RTD
|
|
|
|
if (flyOutWrapper.size() > 0) {
|
|
|
|
var observer = new MutationObserver(function (mutations) {
|
|
|
|
// since mutation on the target node was triggered we can safely assume the injected RTD div has now been added
|
|
|
|
var injectedDiv = $('.rst-other-versions .injected');
|
|
|
|
var selectedLanguageCode = determineSelectedLanguageCode();
|
|
|
|
var dl = document.createElement('dl');
|
|
|
|
var dt = document.createElement('dt');
|
|
|
|
dl.appendChild(dt);
|
|
|
|
dt.appendChild(document.createTextNode('Languages'));
|
|
|
|
for (var languageCode in languageCodeToNameMap) {
|
|
|
|
dl.appendChild(createLanguageLinkFor(languageCode, selectedLanguageCode === languageCode));
|
|
|
|
}
|
|
|
|
injectedDiv.prepend(dl);
|
|
|
|
// no need for that observer anymore
|
|
|
|
observer.disconnect();
|
|
|
|
});
|
2015-12-28 22:33:48 +01:00
|
|
|
|
2015-12-28 23:36:58 +01:00
|
|
|
// observed target node is the fly-out wrapper, the only event we care about is when children are modified
|
|
|
|
observer.observe(flyOutWrapper[0], {childList: true});
|
|
|
|
}
|
2015-12-28 17:25:00 +01:00
|
|
|
}
|
2016-01-15 22:34:33 +01:00
|
|
|
|
2016-03-05 10:47:01 +01:00
|
|
|
/**
|
|
|
|
* The module doc pages contain relative links to artifacts in the GitHub repository. For those links to work both
|
|
|
|
* on GitHub (i.e. when the page is viewed on GitHub) and on RTD they are defined with a relative URL. This function
|
|
|
|
* replaces the relative path with an absolute path based on the selected branch.
|
|
|
|
*/
|
|
|
|
function replaceRelativeLinksWithStaticGitHubUrl() {
|
2018-09-04 22:13:55 +02:00
|
|
|
var relativePath = isOnRtd() ? "../../../.." : "../../..";
|
2016-03-05 10:47:01 +01:00
|
|
|
var gitHubPath = "https://github.com/nodemcu/nodemcu-firmware/tree/" + determineSelectedBranch();
|
|
|
|
var gitHubLinks = $("a[href^='" + relativePath + "']").each(function (index) {
|
|
|
|
var url = $(this).attr('href');
|
|
|
|
$(this).attr('href', url.replace(relativePath, gitHubPath));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-12-28 17:25:00 +01:00
|
|
|
function createLanguageLinkFor(languageCode, isCurrentlySelected) {
|
|
|
|
var strong;
|
2015-12-28 21:57:38 +01:00
|
|
|
// split[0] is an '' because the path starts with the separator
|
2015-12-28 17:25:00 +01:00
|
|
|
var pathSegments = window.location.pathname.split('/');
|
|
|
|
var dd = document.createElement("dd");
|
2015-12-28 21:57:38 +01:00
|
|
|
var href = document.createElement("a");
|
|
|
|
href.setAttribute('href', '/' + pathSegments[1] + '/' + pathSegments[2] + '/' + languageCode);
|
2015-12-28 17:25:00 +01:00
|
|
|
href.appendChild(document.createTextNode(languageCode));
|
|
|
|
dd.appendChild(href);
|
|
|
|
if (isCurrentlySelected) {
|
|
|
|
strong = document.createElement("strong");
|
|
|
|
strong.appendChild(dd);
|
|
|
|
return strong;
|
|
|
|
} else {
|
|
|
|
return dd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-28 23:36:58 +01:00
|
|
|
/**
|
|
|
|
* Analyzes the URL of the current page to find out what the selected language is. It's usually
|
|
|
|
* part of the location path. The code needs to distinguish between running MkDocs standalone
|
|
|
|
* and docs served from RTD. If no valid language could be determined the default language is
|
|
|
|
* returned.
|
|
|
|
*
|
|
|
|
* @returns 2-char language code
|
|
|
|
*/
|
2015-12-28 17:25:00 +01:00
|
|
|
function determineSelectedLanguageCode() {
|
|
|
|
var selectedLanguageCode, path = window.location.pathname;
|
2018-09-04 22:13:55 +02:00
|
|
|
if (isOnRtd()) {
|
2015-12-28 17:25:00 +01:00
|
|
|
// path is like /en/<branch>/<lang>/build/ -> extract 'lang'
|
2015-12-28 21:38:09 +01:00
|
|
|
// split[0] is an '' because the path starts with the separator
|
|
|
|
selectedLanguageCode = path.split('/')[3];
|
2017-04-16 20:31:38 +02:00
|
|
|
} else if (!window.location.href.startsWith('file://')) {
|
2015-12-28 17:25:00 +01:00
|
|
|
// path is like /<lang>/build/ -> extract 'lang'
|
|
|
|
selectedLanguageCode = path.substr(1, 2);
|
|
|
|
}
|
|
|
|
if (!selectedLanguageCode || selectedLanguageCode.length > 2) {
|
|
|
|
selectedLanguageCode = defaultLanguageCode;
|
|
|
|
}
|
|
|
|
return selectedLanguageCode;
|
|
|
|
}
|
|
|
|
|
2016-03-05 10:47:01 +01:00
|
|
|
/**
|
|
|
|
* Analyzes the URL of the current page to find out what the selected GitHub branch is. It's usually
|
|
|
|
* part of the location path. The code needs to distinguish between running MkDocs standalone
|
|
|
|
* and docs served from RTD. If no valid branch could be determined 'dev' returned.
|
|
|
|
*
|
|
|
|
* @returns GitHub branch name
|
|
|
|
*/
|
|
|
|
function determineSelectedBranch() {
|
|
|
|
var branch = 'dev', path = window.location.pathname;
|
2018-09-04 22:13:55 +02:00
|
|
|
if (isOnRtd()) {
|
2016-03-05 10:47:01 +01:00
|
|
|
// path is like /en/<branch>/<lang>/build/ -> extract 'lang'
|
|
|
|
// split[0] is an '' because the path starts with the separator
|
2016-07-10 00:57:50 +02:00
|
|
|
var thirdPathSegment = path.split('/')[2];
|
|
|
|
// 'latest' is an alias on RTD for the 'dev' branch - which is the default for 'branch' here
|
|
|
|
if (thirdPathSegment != 'latest') {
|
|
|
|
branch = thirdPathSegment;
|
|
|
|
}
|
2016-03-05 10:47:01 +01:00
|
|
|
}
|
|
|
|
return branch;
|
|
|
|
}
|
|
|
|
|
2015-12-27 23:32:07 +01:00
|
|
|
function values(associativeArray) {
|
|
|
|
var values = [];
|
|
|
|
for (var key in associativeArray) {
|
|
|
|
if (associativeArray.hasOwnProperty(key)) {
|
|
|
|
values.push(associativeArray[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return values;
|
|
|
|
}
|
2018-09-04 22:13:55 +02:00
|
|
|
|
|
|
|
function isOnRtd() {
|
|
|
|
return window.location.origin.indexOf('readthedocs') > -1;
|
|
|
|
}
|
2017-04-13 21:42:18 +02:00
|
|
|
}());
|