Skip to content

Commit

Permalink
feat: added footer_nav support
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkrzyskow committed Nov 21, 2024
1 parent ca370d2 commit 06f0349
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 1 deletion.
47 changes: 47 additions & 0 deletions mkdocs_nype/plugins/nype_tweaks/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
The tweak sets the required settings to show the default icon on tags.
https://github.com/squidfunk/mkdocs-material/issues/7688
8. footer_nav tweak:
To allow normal file system paths in the nype_config->footer_nav this tweak
needed to be implemented to convert the file system paths into the URLs used in
the copyright.html template.
MIT License 2024 Kamil Krzyśków (HRY) for Nype (npe.cm)
"""

Expand All @@ -50,6 +55,7 @@

import material
from mkdocs.config.defaults import MkDocsConfig
from mkdocs.exceptions import PluginError
from mkdocs.livereload import LiveReloadServer
from mkdocs.plugins import BasePlugin, CombinedEvent, PrefixedLogger, event_priority
from mkdocs.structure.files import Files
Expand All @@ -74,6 +80,11 @@ def __init__(self) -> None:
self.dest_url_mapping = {}
self.draft_paths: GitIgnoreSpec = None
self.nype_config_key = "nype_config"
self.default_footer_nav = {
"Contact": "contact.md",
"Impressum": "impressum.md",
"Offer": "offer.md",
}

@event_priority(110)
def on_config(self, config: MkDocsConfig) -> MkDocsConfig | None:
Expand Down Expand Up @@ -118,10 +129,46 @@ def on_env(
) -> macros_module.Environment | None:
# HEX data obfuscation tweak
env.filters["obfuscate"] = utils.obfuscate

# blog cards tweak
env.filters["post_card_title"] = utils.post_card_title
env.filters["post_card_description"] = utils.post_card_description

# footer_nav tweak
footer_nav = config.theme.get("nype_config", {}).get("footer_nav")

# If the footer_nav is not defined, try to fill it in automatically
if footer_nav is None:
if config.theme.get("nype_config") is None:
config.theme["nype_config"] = {}

config.theme["nype_config"]["footer_nav"] = footer_nav = []

for title, path in self.default_footer_nav.items():
file = files.get_file_from_path(path)
if file:
footer_nav.append({"title": title, "path": path})

for i, item in enumerate(footer_nav):

# Support both title: path dicts and pure path str
if isinstance(item, dict) and item.get("path") is None:
if len(item) > 1:
raise PluginError(f"footer_nav value structure not supported: {item}")
key = next((key for key in item))
footer_nav[i] = item = {"title": key, "path": item[key]}
elif isinstance(item, str):
footer_nav[i] = item = {"title": None, "path": item}

file = files.get_file_from_path(item["path"])
if not file:
LOG.warning(f"footer_nav value {item['path']} doesn't link to a file")
continue

footer_nav[i]["path"] = file.page.url
if footer_nav[i]["title"] is None:
footer_nav[i]["title"] = file.page.title

@event_priority(100)
def on_template_context(
self, context: TemplateContext, /, *, template_name: str, config: MkDocsConfig
Expand Down
33 changes: 33 additions & 0 deletions mkdocs_nype/templates/assets/stylesheets/nype-main.css
Original file line number Diff line number Diff line change
Expand Up @@ -467,4 +467,37 @@
margin-bottom: 0;
}

/* #endregion */

/* #region footer_nav */

.nype-footer-nav {
display: flex;
align-items: center;
flex-grow: 1;
font-size: .64rem;
}

.nype-footer-nav > ul:not([hidden]) {
display: inline-flex;
list-style: none;
}

@media screen and (max-width: 38em) {
.nype-footer-nav > ul:not([hidden]) {
display: initial;
}
}

.nype-footer-nav > ul:not([hidden]) > li {
margin: unset;
margin-left: .2rem;
}

.nype-footer-nav > ul:not([hidden]) > li::before {
content: "·";
margin-left: .2rem;
margin-right: .2rem;
}

/* #endregion */
1 change: 1 addition & 0 deletions mkdocs_nype/templates/mkdocs_theme.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extends: material
# - `{key}` {value} - any key with any value, though deep dictionaries will likely not be recursively added.
# contact_form_action_hex: value
# - `old_prefix` str - for 404 page, old prefix to redirect old links from if not found
# - `footer_nav` list(str | dict(str, str)) - paths to files to include in the footer nav / dict with title and path
#
# Additionally to global overrides pages can include:
# - `container_css` str - (white)space separated names of `nype-{name}` CSS style classes that will influence the container.
Expand Down
63 changes: 63 additions & 0 deletions mkdocs_nype/templates/partials/copyright.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{#-
Copyright (c) 2016-2024 Martin Donath <martin[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
-#}

{#-
Modified for mkdocs-nype, last checked up-to-date with mkdocs-material 9.5.39

- Added a div wrapper for the generator info
- Added support for footer navigation, controlled from mkdocs.yml, and "preprocessed" in `nype_tweaks`

MIT License 2024 Kamil Krzyśków (HRY) for Nype (npe.cm)
-#}

{#- Copyright and theme information -#}
<div class="md-copyright">
{% if config.copyright %}
<div class="md-copyright__highlight">
{{ config.copyright }}
</div>
{% endif %}
{% if not config.extra.generator == false %}
<div>
Made with
<a
href="https://squidfunk.github.io/mkdocs-material/"
target="_blank" rel="noopener"
>
Material for MkDocs
</a>
</div>
{% endif %}
</div>

{#- Footer navigation -#}
{% if config.theme.nype_config.footer_nav -%}
<nav class="nype-footer-nav">
<ul>
{% for item in config.theme.nype_config.footer_nav -%}
<li>
<a href="{{ item.path | url }}">{{ item.title }}</a>
</li>
{% endfor -%}
</ul>
</nav>
{% endif -%}
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ build-backend = "hatchling.build"

[project]
name = "mkdocs-nype"
version = "0.22.2"
version = "0.22.3"
description = "MkDocs theme for Nype MkDocs projects, extends the Material for MkDocs theme"
authors = [
{ name = "Kamil Krzyśków", email = "[email protected]" }
Expand Down

0 comments on commit 06f0349

Please sign in to comment.