-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable semantic versioned sidebar (#6)
- Loading branch information
Showing
7 changed files
with
220 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .autodoc import AutoDocProcessor | ||
from .codenotes import CodeNoteProcessor | ||
from .semantic_versioning import SemanticVersioningProcessor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# Copyright 2023 Zachary Mueller. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import logging | ||
|
||
from ..notebook import make_cell | ||
from ..processor import Processor | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
# This is the javascript that will be injected into the top of the notebook | ||
# to enable semantic versioning of the documentation | ||
|
||
REFERENCE_JAVASCRIPT = """/** | ||
* Enables semantic versioning through careful sidebar menu item selection. | ||
* Hide sidebar menu items that are not related to the current page that is open. | ||
* Assumes a directory structure of: | ||
* - version_1 | ||
* - page_1 | ||
* - page_2 | ||
* - version_2 | ||
* - page_2 | ||
* - page_3 | ||
* | ||
* If version_1 is open, then version_2 and it's pages will not be visible to the sidebar. | ||
* These will also link to {url}/{version_num}/page_{num}. | ||
* | ||
* In the `_quarto.yml` sidebar *must* be set to `auto` for this to work. | ||
*/ | ||
var all_versioned_menus = $(".sidebar-menu-container > .list-unstyled").children() | ||
// Get the current url, which should be something like: /branch_name/{version_number}/{something} | ||
// the latter parts after the version number are not important nor will be in there. | ||
// eventually need to handle a special case when we use the latest stable version | ||
var url = window.location.pathname.split("/")[1] | ||
for (var versioned_menu of all_versioned_menus){ | ||
// Check if the current url extension is in the sidebar menu | ||
var active_sidebar = $(versioned_menu).find(`a[href*="${url}"]`)[0] | ||
// If it is, wrap it in a div so it's easily recognizeable | ||
if (active_sidebar !== undefined){ | ||
$(active_sidebar).parent().parent().wrap("<div id='active-sidebar'></div>") | ||
} | ||
// Else hide the additional menus | ||
else { | ||
versioned_menu.style.display = "none" | ||
} | ||
}""" | ||
|
||
# We inject it directly to the markdown so there doesn't have to be other random files we need to worry about | ||
REFERENCE_JQUERY = '<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>' | ||
REFERENCE_JAVASCRIPT = f"<script>{REFERENCE_JAVASCRIPT}</script>" | ||
|
||
|
||
class SemanticVersioningProcessor(Processor): | ||
""" | ||
A processor which will inject javascript into the top of the `qmd` or | ||
notebook to enable semantic versioning of the documentation. | ||
Assumes your documentation structure is as follows: | ||
``` | ||
- docs/ | ||
- version_1 | ||
- page_1.qmd | ||
- page_2.qmd | ||
- version_2 | ||
- page_1.qmd | ||
- page_2.qmd | ||
``` | ||
From here, the sidebar will be populated based on the | ||
current opened page and its semantic version. So if | ||
you are on `/docs/version_1/page_1`, the sidebar will | ||
hide all of `version_2`'s pages (and any others there may be), | ||
and only show `version_1`. | ||
""" | ||
|
||
cell_types = "markdown" | ||
found_markdown_cell = False | ||
markdown_cell_index = None | ||
|
||
def process(self, cell): | ||
if self.is_first_markdown(cell): | ||
# Create the new markdown cell | ||
new_cell = make_cell("\n".join([REFERENCE_JQUERY, REFERENCE_JAVASCRIPT]), "markdown") | ||
# Insert the new cell just after the first markdown cell | ||
self.notebook.cells.insert(self.markdown_cell_index + 1, new_cell) | ||
# Update the notebook order | ||
for i, cell in enumerate(self.notebook.cells): | ||
cell.index_ = i | ||
|
||
def is_first_markdown(self, cell): | ||
if not self.found_markdown_cell: | ||
self.found_markdown_cell = True | ||
self.markdown_cell_index = cell["index_"] | ||
return True | ||
else: | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters