diff --git a/readthedocs/core/static-src/core/js/doc-embed/mkdocs.js b/readthedocs/core/static-src/core/js/doc-embed/mkdocs.js index 2060a37a4a0..272e7b26496 100644 --- a/readthedocs/core/static-src/core/js/doc-embed/mkdocs.js +++ b/readthedocs/core/static-src/core/js/doc-embed/mkdocs.js @@ -4,6 +4,7 @@ var rtddata = require('./rtd-data'); +var highlightSearchterm = require('../highlight-searchterm'); function init() { @@ -42,6 +43,9 @@ function init() { }; win.on('resize', apply_stickynav); apply_stickynav(); + + // Init searchterm highlighting + highlightSearchterm.init(); } } diff --git a/readthedocs/core/static-src/core/js/highlight-searchterm.js b/readthedocs/core/static-src/core/js/highlight-searchterm.js new file mode 100644 index 00000000000..233c1a94f1c --- /dev/null +++ b/readthedocs/core/static-src/core/js/highlight-searchterm.js @@ -0,0 +1,60 @@ +/* + * Allow highlighting of search terms, passed in as "highlight" GET parameter. + * + * This imitades the behaviour of what is implemented in Sphinx's doctools.js. + * Mkdocs does not provide a similiar logic, we implement it here instead which + * will work theme agnostic. + */ + + +require('./jquery.highlighttext'); + + +function init() { + highlightSearchWords(); +} + + +function urldecode(x) { + return decodeURIComponent(x).replace(/\+/g, ' '); +} + + +function getQueryParameters(s) { + if (typeof s == 'undefined') + s = document.location.search; + var parts = s.substr(s.indexOf('?') + 1).split('&'); + var result = {}; + for (var i = 0; i < parts.length; i++) { + var tmp = parts[i].split('=', 2); + var key = urldecode(tmp[0]); + var value = urldecode(tmp[1]); + if (key in result) + result[key].push(value); + else + result[key] = [value]; + } + return result; +} + + +function highlightSearchWords() { + var params = getQueryParameters(); + var terms = (params.highlight) ? params.highlight[0].split(/\s+/) : []; + if (terms.length) { + var body = $('div.body'); + if (!body.length) { + var body = $('body'); + } + window.setTimeout(function() { + $.each(terms, function() { + body.highlightText(this.toLowerCase(), 'highlighted'); + }); + }, 10); + } +} + + +module.exports = { + init: init +}; diff --git a/readthedocs/core/static-src/core/js/jquery.highlighttext.js b/readthedocs/core/static-src/core/js/jquery.highlighttext.js new file mode 100644 index 00000000000..2e36f913820 --- /dev/null +++ b/readthedocs/core/static-src/core/js/jquery.highlighttext.js @@ -0,0 +1,29 @@ +/** + * highlight a given string on a jquery object by wrapping it in + * span elements with the given class name. + */ +jQuery.fn.highlightText = function(text, className) { + function highlight(node) { + if (node.nodeType == 3) { + var val = node.nodeValue; + var pos = val.toLowerCase().indexOf(text); + if (pos >= 0 && !jQuery(node.parentNode).hasClass(className)) { + var span = document.createElement("span"); + span.className = className; + span.appendChild(document.createTextNode(val.substr(pos, text.length))); + node.parentNode.insertBefore(span, node.parentNode.insertBefore( + document.createTextNode(val.substr(pos + text.length)), + node.nextSibling)); + node.nodeValue = val.substr(0, pos); + } + } + else if (!jQuery(node).is("button, select, textarea")) { + jQuery.each(node.childNodes, function() { + highlight(this); + }); + } + } + return this.each(function() { + highlight(this); + }); +}; diff --git a/readthedocs/core/static/core/js/readthedocs-doc-embed.js b/readthedocs/core/static/core/js/readthedocs-doc-embed.js index bb2e7ebd486..3d476fcb536 100644 --- a/readthedocs/core/static/core/js/readthedocs-doc-embed.js +++ b/readthedocs/core/static/core/js/readthedocs-doc-embed.js @@ -1 +1 @@ -!function t(e,r,o){function n(a,s){if(!r[a]){if(!e[a]){var c="function"==typeof require&&require;if(!s&&c)return c(a,!0);if(i)return i(a,!0);var d=new Error("Cannot find module '"+a+"'");throw d.code="MODULE_NOT_FOUND",d}var p=r[a]={exports:{}};e[a][0].call(p.exports,function(t){var r=e[a][1][t];return n(r?r:t)},p,p.exports,t,e,r,o)}return r[a].exports}for(var i="function"==typeof require&&require,a=0;a").attr({type:"hidden",name:"project",value:t.project}).appendTo("#rtd-search-form"),$("").attr({type:"hidden",name:"version",value:t.version}).appendTo("#rtd-search-form"),$("").attr({type:"hidden",name:"type",value:"file"}).appendTo("#rtd-search-form"),$("#rtd-search-form").prop("action",t.api_host+"/search/");var e=$("nav.wy-nav-side:first"),r=$(window),o="stickynav",i=function(){e.height()<=r.height()?e.addClass(o):e.removeClass(o)};r.on("resize",i),i()}}var n=t("./rtd-data");e.exports={init:o}},{"./rtd-data":4}],4:[function(t,e,r){function o(){var t=Object.create(n),e={api_host:"https://readthedocs.org"};return $.extend(t,e,window.READTHEDOCS_DATA),t}var n={is_rtd_theme:function(){return"sphinx_rtd_theme"===this.get_theme_name()},is_sphinx_builder:function(){return!("builder"in this)||"mkdocs"!=this.builder},get_theme_name:function(){return"sphinx_rtd_theme"!==this.theme&&1===$("div.rst-other-versions").length?"sphinx_rtd_theme":this.theme},show_promo:function(){return"https://readthedocs.com"!==this.api_host&&this.is_sphinx_builder()&&this.is_rtd_theme()}};e.exports={get:o}},{}],5:[function(t,e,r){function o(){function t(t){var e=t.closest("li");e.siblings("li.current").removeClass("current"),e.siblings().find("li.current").removeClass("current"),e.find("> ul li.current").removeClass("current"),e.toggleClass("current")}var e=n.get();$(document).on("click","[data-toggle='rst-current-version']",function(){var t=$("[data-toggle='rst-versions']").hasClass("shift-up")?"was_open":"was_closed";_gaq&&_gaq.push(["rtfd._setAccount","UA-17997319-1"],["rtfd._trackEvent","Flyout","Click",t])}),(!("builder"in e)||"builder"in e&&"mkdocs"!=e.builder)&&($(document).on("click","[data-toggle='wy-nav-top']",function(){$("[data-toggle='wy-nav-shift']").toggleClass("shift"),$("[data-toggle='rst-versions']").toggleClass("shift")}),$(document).on("click",".wy-menu-vertical .current ul li a",function(){var e=$(this);$("[data-toggle='wy-nav-shift']").removeClass("shift"),$("[data-toggle='rst-versions']").toggleClass("shift"),t(e),"undefined"!=typeof window.SphinxRtdTheme&&window.SphinxRtdTheme.StickyNav.hashChange()}),$(document).on("click","[data-toggle='rst-current-version']",function(){$("[data-toggle='rst-versions']").toggleClass("shift-up")}),$("table.docutils:not(.field-list)").wrap("
"),$(".wy-menu-vertical ul").siblings("a").each(function(){var e=$(this);expand=$(''),expand.on("click",function(r){return t(e),r.stopPropagation(),!1}),e.prepend(expand)}),window.SphinxRtdTheme=function(t){var e=function(){var e,r,o=!1,n=!1,i=0,a=function(){s(),c(),r.on("hashchange",c),r.on("scroll",function(){n||(o=!0)}),setInterval(function(){if(o){o=!1;var t=r.scrollTop(),n=e.scrollTop(),a=n+(t-i);e.scrollTop(a),i=t}},25)},s=function(){e=t("nav.wy-nav-side:first"),r=t(window)},c=function(){var t=encodeURI(window.location.hash);if(t)try{var e=$(".wy-menu-vertical").find('[href="'+t+'"]');$(".wy-menu-vertical li.toctree-l1 li.current").removeClass("current"),e.closest("li.toctree-l2").addClass("current"),e.closest("li.toctree-l3").addClass("current"),e.closest("li.toctree-l4").addClass("current")}catch(r){console.log("Error expanding nav for anchor",r)}},d=function(){n=!0,r.one("hashchange",function(){n=!1})};return t(s),{enable:a,hashChange:d}}();return{StickyNav:e}}($))}var n=t("./rtd-data");e.exports={init:o}},{"./rtd-data":4}],6:[function(t,e,r){function o(t){var e=n.get();if(!t.is_highest){var r=window.location.pathname.replace(e.version,t.slug),o=$('

Note

You are not using the most up to date version of the library. is the newest version.

');o.find("a").attr("href",r).text(t.version);var i=$("div.body");i.length||(i=$("div.document")),i.prepend(o)}}var n=t("./rtd-data");e.exports={init:o}},{"./rtd-data":4}],7:[function(t,e,r){var o=(t("./sponsorship"),t("./doc-embed/footer.js")),n=t("./doc-embed/grokthedocs-client"),i=t("./doc-embed/mkdocs"),a=(t("./doc-embed/rtd-data"),t("./doc-embed/sphinx"));$(document).ready(function(){o.init(),a.init(),n.init(),i.init()})},{"./doc-embed/footer.js":1,"./doc-embed/grokthedocs-client":2,"./doc-embed/mkdocs":3,"./doc-embed/rtd-data":4,"./doc-embed/sphinx":5,"./sponsorship":8}],8:[function(t,e,r){function o(t,e,r,o){this.id=t,this.text=e,this.link=r,this.image=o,this.promo=null}e.exports={Promo:o},o.prototype.create=function(){function t(){_gaq&&_gaq.push(["rtfd._setAccount","UA-17997319-1"],["rtfd._trackEvent","Promo","Click",e.id])}var e=this,r=$("nav.wy-nav-side");if(r.length){promo=$("
").attr("class","wy-menu rst-pro");{var o=$("
").attr("class","rst-pro-about"),n=$("").attr("href","http://docs.readthedocs.org/en/latest/sponsors.html#sponsorship-information").appendTo(o);$("").attr("class","fa fa-info-circle").appendTo(n)}if(o.appendTo(promo),e.image){{var i=$("").attr("class","rst-pro-image-wrapper").attr("href",e.link).attr("target","_blank").on("click",t);$("").attr("class","rst-pro-image").attr("src",e.image).appendTo(i)}promo.append(i)}var a=$("").html(e.text);return $(a).find("a").each(function(){$(this).attr("class","rst-pro-link").attr("href",e.link).attr("target","_blank").on("click",t)}),promo.append(a),promo.appendTo(r),promo.wrapper=$("
").attr("class","rst-pro-wrapper").appendTo(r),promo}},o.prototype.display=function(){var t=this.promo;t||(t=this.promo=this.create()),t.show()},o.prototype.disable=function(){},o.from_variants=function(t){if(0==t.length)return null;var e=Math.floor(Math.random()*t.length),r=t[e],n=r.text,i=r.link,a=r.image,s=r.id;return new o(s,n,i,a)}},{}]},{},[7]); \ No newline at end of file +!function t(e,n,r){function o(a,s){if(!n[a]){if(!e[a]){var c="function"==typeof require&&require;if(!s&&c)return c(a,!0);if(i)return i(a,!0);var d=new Error("Cannot find module '"+a+"'");throw d.code="MODULE_NOT_FOUND",d}var l=n[a]={exports:{}};e[a][0].call(l.exports,function(t){var n=e[a][1][t];return o(n?n:t)},l,l.exports,t,e,n,r)}return n[a].exports}for(var i="function"==typeof require&&require,a=0;a").attr({type:"hidden",name:"project",value:t.project}).appendTo("#rtd-search-form"),$("").attr({type:"hidden",name:"version",value:t.version}).appendTo("#rtd-search-form"),$("").attr({type:"hidden",name:"type",value:"file"}).appendTo("#rtd-search-form"),$("#rtd-search-form").prop("action",t.api_host+"/search/");var e=$("nav.wy-nav-side:first"),n=$(window),r="stickynav",a=function(){e.height()<=n.height()?e.addClass(r):e.removeClass(r)};n.on("resize",a),a(),i.init()}}var o=t("./rtd-data"),i=t("../highlight-searchterm");e.exports={init:r}},{"../highlight-searchterm":7,"./rtd-data":4}],4:[function(t,e,n){function r(){var t=Object.create(o),e={api_host:"https://readthedocs.org"};return $.extend(t,e,window.READTHEDOCS_DATA),t}var o={is_rtd_theme:function(){return"sphinx_rtd_theme"===this.get_theme_name()},is_sphinx_builder:function(){return!("builder"in this)||"mkdocs"!=this.builder},get_theme_name:function(){return"sphinx_rtd_theme"!==this.theme&&1===$("div.rst-other-versions").length?"sphinx_rtd_theme":this.theme},show_promo:function(){return"https://readthedocs.com"!==this.api_host&&this.is_sphinx_builder()&&this.is_rtd_theme()}};e.exports={get:r}},{}],5:[function(t,e,n){function r(){function t(t){var e=t.closest("li");e.siblings("li.current").removeClass("current"),e.siblings().find("li.current").removeClass("current"),e.find("> ul li.current").removeClass("current"),e.toggleClass("current")}var e=o.get();$(document).on("click","[data-toggle='rst-current-version']",function(){var t=$("[data-toggle='rst-versions']").hasClass("shift-up")?"was_open":"was_closed";_gaq&&_gaq.push(["rtfd._setAccount","UA-17997319-1"],["rtfd._trackEvent","Flyout","Click",t])}),(!("builder"in e)||"builder"in e&&"mkdocs"!=e.builder)&&($(document).on("click","[data-toggle='wy-nav-top']",function(){$("[data-toggle='wy-nav-shift']").toggleClass("shift"),$("[data-toggle='rst-versions']").toggleClass("shift")}),$(document).on("click",".wy-menu-vertical .current ul li a",function(){var e=$(this);$("[data-toggle='wy-nav-shift']").removeClass("shift"),$("[data-toggle='rst-versions']").toggleClass("shift"),t(e),"undefined"!=typeof window.SphinxRtdTheme&&window.SphinxRtdTheme.StickyNav.hashChange()}),$(document).on("click","[data-toggle='rst-current-version']",function(){$("[data-toggle='rst-versions']").toggleClass("shift-up")}),$("table.docutils:not(.field-list)").wrap("
"),$(".wy-menu-vertical ul").siblings("a").each(function(){var e=$(this);expand=$(''),expand.on("click",function(n){return t(e),n.stopPropagation(),!1}),e.prepend(expand)}),window.SphinxRtdTheme=function(t){var e=function(){var e,n,r=!1,o=!1,i=0,a=function(){s(),c(),n.on("hashchange",c),n.on("scroll",function(){o||(r=!0)}),setInterval(function(){if(r){r=!1;var t=n.scrollTop(),o=e.scrollTop(),a=o+(t-i);e.scrollTop(a),i=t}},25)},s=function(){e=t("nav.wy-nav-side:first"),n=t(window)},c=function(){var t=encodeURI(window.location.hash);if(t)try{var e=$(".wy-menu-vertical").find('[href="'+t+'"]');$(".wy-menu-vertical li.toctree-l1 li.current").removeClass("current"),e.closest("li.toctree-l2").addClass("current"),e.closest("li.toctree-l3").addClass("current"),e.closest("li.toctree-l4").addClass("current")}catch(n){console.log("Error expanding nav for anchor",n)}},d=function(){o=!0,n.one("hashchange",function(){o=!1})};return t(s),{enable:a,hashChange:d}}();return{StickyNav:e}}($))}var o=t("./rtd-data");e.exports={init:r}},{"./rtd-data":4}],6:[function(t,e,n){function r(t){var e=o.get();if(!t.is_highest){var n=window.location.pathname.replace(e.version,t.slug),r=$('
');r.find("a").attr("href",n).text(t.version);var i=$("div.body");i.length||(i=$("div.document")),i.prepend(r)}}var o=t("./rtd-data");e.exports={init:r}},{"./rtd-data":4}],7:[function(t,e,n){function r(){console.log("START"),a()}function o(t){return decodeURIComponent(t).replace(/\+/g," ")}function i(t){"undefined"==typeof t&&(t=document.location.search);for(var e=t.substr(t.indexOf("?")+1).split("&"),n={},r=0;r=0&&!jQuery(r.parentNode).hasClass(e)){var a=document.createElement("span");a.className=e,a.appendChild(document.createTextNode(o.substr(i,t.length))),r.parentNode.insertBefore(a,r.parentNode.insertBefore(document.createTextNode(o.substr(i+t.length)),r.nextSibling)),r.nodeValue=o.substr(0,i)}}else jQuery(r).is("button, select, textarea")||jQuery.each(r.childNodes,function(){n(this)})}return this.each(function(){n(this)})}},{}],9:[function(t,e,n){var r=(t("./sponsorship"),t("./doc-embed/footer.js")),o=t("./doc-embed/grokthedocs-client"),i=t("./doc-embed/mkdocs"),a=(t("./doc-embed/rtd-data"),t("./doc-embed/sphinx"));$(document).ready(function(){r.init(),a.init(),o.init(),i.init()})},{"./doc-embed/footer.js":1,"./doc-embed/grokthedocs-client":2,"./doc-embed/mkdocs":3,"./doc-embed/rtd-data":4,"./doc-embed/sphinx":5,"./sponsorship":10}],10:[function(t,e,n){function r(t,e,n,r){this.id=t,this.text=e,this.link=n,this.image=r,this.promo=null}e.exports={Promo:r},r.prototype.create=function(){function t(){_gaq&&_gaq.push(["rtfd._setAccount","UA-17997319-1"],["rtfd._trackEvent","Promo","Click",e.id])}var e=this,n=$("nav.wy-nav-side");if(n.length){promo=$("
").attr("class","wy-menu rst-pro");{var r=$("
").attr("class","rst-pro-about"),o=$("").attr("href","http://docs.readthedocs.org/en/latest/sponsors.html#sponsorship-information").appendTo(r);$("").attr("class","fa fa-info-circle").appendTo(o)}if(r.appendTo(promo),e.image){{var i=$("").attr("class","rst-pro-image-wrapper").attr("href",e.link).attr("target","_blank").on("click",t);$("").attr("class","rst-pro-image").attr("src",e.image).appendTo(i)}promo.append(i)}var a=$("").html(e.text);return $(a).find("a").each(function(){$(this).attr("class","rst-pro-link").attr("href",e.link).attr("target","_blank").on("click",t)}),promo.append(a),promo.appendTo(n),promo.wrapper=$("
").attr("class","rst-pro-wrapper").appendTo(n),promo}},r.prototype.display=function(){var t=this.promo;t||(t=this.promo=this.create()),t.show()},r.prototype.disable=function(){},r.from_variants=function(t){if(0==t.length)return null;var e=Math.floor(Math.random()*t.length),n=t[e],o=n.text,i=n.link,a=n.image,s=n.id;return new r(s,o,i,a)}},{}]},{},[9]); \ No newline at end of file