From 2cd3c3a67b34ac03519b4c151935255ff537f3ae Mon Sep 17 00:00:00 2001 From: Michael Hatherly Date: Fri, 18 Nov 2016 11:00:12 +0200 Subject: [PATCH] Add version selector dropdown menu During `deploydocs` we check what documentation folders are currently available and write a `versions.js` file to the root of the repo that contains a list, `DOC_VERSIONS`, which is used to populate the dropdown menu. --- assets/html/documenter.css | 9 +++++++++ assets/html/documenter.js | 7 +++++++ src/Documenter.jl | 4 ++++ src/Writers/HTMLWriter.jl | 40 ++++++++++++++++++++++++++++++++++---- 4 files changed, 56 insertions(+), 4 deletions(-) diff --git a/assets/html/documenter.css b/assets/html/documenter.css index a5ffee126e..223d44d0e5 100644 --- a/assets/html/documenter.css +++ b/assets/html/documenter.css @@ -158,6 +158,15 @@ nav.toc input { font-size: smaller; } +nav.toc select { + display: block; + height: 2em; + width: calc(100% - 3em); + margin: 5px auto; + font-size: smaller; + text-align: center; +} + nav.toc > ul * { margin: 0; } diff --git a/assets/html/documenter.js b/assets/html/documenter.js index 354bc680e9..c86aec0251 100644 --- a/assets/html/documenter.js +++ b/assets/html/documenter.js @@ -52,6 +52,13 @@ require(['mathjax'], function(MathJax) { require(['jquery', 'highlight', 'highlight-julia'], function($, hljs) { $(document).ready(function() { + if (typeof DOC_VERSIONS !== 'undefined') { + var version_selector = $("#version-selector"); + DOC_VERSIONS.forEach(function(each) { + var option = $(""); + version_selector.append(option); + }); + } hljs.initHighlighting(); }) diff --git a/src/Documenter.jl b/src/Documenter.jl index e9410833bd..acda0196ff 100644 --- a/src/Documenter.jl +++ b/src/Documenter.jl @@ -444,6 +444,10 @@ function deploydocs(; end end + # Create the versions.js file containing a list of all docs + # versions. This must always happen after the folder copying. + Writers.HTMLWriter.generate_version_file(pwd()) + # Add, commit, and push the docs to the remote. run(`git add -A .`) try run(`git commit -m "build based on $sha"`) end diff --git a/src/Writers/HTMLWriter.jl b/src/Writers/HTMLWriter.jl index bf558a6bb6..5c939f38ce 100644 --- a/src/Writers/HTMLWriter.jl +++ b/src/Writers/HTMLWriter.jl @@ -192,6 +192,8 @@ function render_head(ctx, navnode, additional_scripts) Symbol("data-main") => relhref(src, ctx.documenter_js) ], + script[:src => relhref(src, "../versions.js")], + # Additional JS files needed for the search page. [script[:src => relhref(src, each)] for each in additional_scripts], @@ -248,12 +250,12 @@ end # ------------------------------------------------------------------------------ function render_navmenu(ctx, navnode) - @tags a form h1 img input nav + @tags a form h1 img input nav div select option src = get(navnode.page) navmenu = nav[".toc"] if !isempty(ctx.logo) push!(navmenu.nodes, - a[:href => ""]( # TODO: link to github? + a[:href => relhref(src, "index.html")]( img[ ".logo", :src => relhref(src, ctx.logo), @@ -265,12 +267,22 @@ function render_navmenu(ctx, navnode) push!(navmenu.nodes, h1(ctx.doc.user.sitename)) push!(navmenu.nodes, form[".search", :action => relhref(src, "search.html")]( + select[ + "#version-selector", + :onChange => "window.location.href=this.value", + ]( + option[ + :value => "#", + :selected => "selected", + :disabled => "disabled", + ]("Version"), + ), input[ "#search-query", :name => "q", :type => "text", - :placeholder => "Search docs" - ] + :placeholder => "Search docs", + ], ) ) push!(navmenu.nodes, navitem(ctx, navnode)) @@ -365,6 +377,26 @@ function render_article(ctx, navnode) article["#docs"](art_header, pagenodes, art_footer) end +function generate_version_file(dir::AbstractString) + local named_folders = [] + local release_folders = [] + local tag_folders = [] + for each in readdir(dir) + each in ("stable", "latest") ? push!(named_folders, each) : + ismatch(r"release\-\d+\.\d+", each) ? push!(release_folders, each) : + ismatch(Base.VERSION_REGEX, each) ? push!(tag_folders, each) : nothing + end + open(joinpath(dir, "versions.js"), "w") do buf + println(buf, "var DOC_VERSIONS = [") + for group in (named_folders, release_folders, tag_folders) + for folder in sort!(group, rev = true) + println(buf, " \"", folder, "\",") + end + end + println(buf, "];") + end +end + ## domify(...) # ------------