Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify how the theme is override #67

Merged
merged 6 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions src/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@
"""

import tempfile
import logging
import os
from mkdocs.plugins import BasePlugin
from mkdocs.theme import Theme
from mkdocs.utils import warning_filter
from mkdocs.contrib.search import SearchPlugin
from mkdocs_monorepo_plugin.plugin import MonorepoPlugin
from pymdownx.emoji import to_svg

log = logging.getLogger(__name__)
log.addFilter(warning_filter)

TECHDOCS_DEFAULT_THEME = "material"


class TechDocsCore(BasePlugin):
def __init__(self):
Expand All @@ -44,25 +51,16 @@ def on_config(self, config):
mdx_configs_override = config["mdx_configs"].copy()

# Theme
theme_override = {}
if "theme" in config:
theme_override = config["theme"]

config["theme"] = Theme(
name="material",
static_templates=[
"techdocs_metadata.json",
],
)
config["theme"].dirs.append(self.tmp_dir_techdocs_theme.name)
if config["theme"].name != TECHDOCS_DEFAULT_THEME:
config["theme"] = Theme(name=TECHDOCS_DEFAULT_THEME)
elif config["theme"].name == TECHDOCS_DEFAULT_THEME:
log.info(
"[mkdocs-techdocs-core] Overridden '%s' theme settings in use",
TECHDOCS_DEFAULT_THEME,
)

for key in theme_override:
if key not in config["theme"]:
config["theme"][key] = theme_override[key]
elif key in ["static_templates", "dir"]:
default_config = config["theme"][key]
override_config = theme_override[key]
config["theme"][key] = [*default_config, *override_config]
config["theme"].static_templates.update({"techdocs_metadata.json"})
config["theme"].dirs.append(self.tmp_dir_techdocs_theme.name)

# Plugins
del config["plugins"]["techdocs-core"]
Expand Down
35 changes: 34 additions & 1 deletion src/test_core.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
import mkdocs.plugins as plugins
from mkdocs.theme import Theme
from .core import TechDocsCore
from .core import TechDocsCore, TECHDOCS_DEFAULT_THEME
from jinja2 import Environment, PackageLoader, select_autoescape
import json

Expand Down Expand Up @@ -51,6 +51,39 @@ def test_override_default_config_with_user_config(self):
self.assertFalse(final_config["mdx_configs"]["toc"]["permalink"])
self.assertTrue("mdx_truly_sane_lists" in final_config["markdown_extensions"])

def test_theme_overrides_removed_when_name_is_not_material(self):
## we want to force the theme mkdocs to this test
self.mkdocs_yaml_config["theme"] = Theme(name="mkdocs")
self.mkdocs_yaml_config["theme"]["features"] = ["navigation.sections"]
final_config = self.techdocscore.on_config(self.mkdocs_yaml_config)
self.assertFalse("navigation.sections" in final_config["theme"]["features"])

def test_theme_overrides_when_name_is_material(self):
self.mkdocs_yaml_config["theme"] = Theme(name=TECHDOCS_DEFAULT_THEME)
self.mkdocs_yaml_config["theme"]["features"] = ["navigation.sections"]
final_config = self.techdocscore.on_config(self.mkdocs_yaml_config)
self.assertTrue("navigation.sections" in final_config["theme"]["features"])

def test_theme_overrides_techdocs_metadata(self):
self.mkdocs_yaml_config["theme"] = Theme(
name=TECHDOCS_DEFAULT_THEME, static_templates=["my_static_temples"]
)
final_config = self.techdocscore.on_config(self.mkdocs_yaml_config)
self.assertTrue("my_static_temples" in final_config["theme"].static_templates)
self.assertTrue(
"techdocs_metadata.json" in final_config["theme"].static_templates
)

def test_theme_overrides_dirs(self):
custom_theme_dir = "/tmp/my_custom_theme_dir"
self.mkdocs_yaml_config["theme"] = Theme(name=TECHDOCS_DEFAULT_THEME)
self.mkdocs_yaml_config["theme"].dirs.append(custom_theme_dir)
final_config = self.techdocscore.on_config(self.mkdocs_yaml_config)
self.assertTrue(custom_theme_dir in final_config["theme"].dirs)
self.assertTrue(
self.techdocscore.tmp_dir_techdocs_theme.name in final_config["theme"].dirs
)

def test_template_renders__multiline_value_as_valid_json(self):
iamEAP marked this conversation as resolved.
Show resolved Hide resolved
self.techdocscore.on_config(self.mkdocs_yaml_config)
env = Environment(
Expand Down