Skip to content

Commit

Permalink
Update plugin to be compatible with mkdocs ver 1.6.0+
Browse files Browse the repository at this point in the history
  • Loading branch information
athackst committed May 5, 2024
1 parent 76fcf27 commit 070e04c
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 35 deletions.
4 changes: 3 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
"streetsidesoftware.code-spell-checker",
"rogalmic.bash-debug",
"ms-python.pylint",
"ms-python.flake8"
"ms-python.flake8",
"ms-python.autopep8",
"ms-python.isort"
]
}
}
Expand Down
7 changes: 5 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
"grepout",
"htmlproofer",
"indentless",
"isort",
"lastindex",
"litcoffee",
"livereload",
"maint",
"markupsafe",
"materialx",
Expand Down Expand Up @@ -82,6 +84,7 @@
],
"[python]": {
"editor.tabSize": 4,
"editor.wordBasedSuggestions": false
}
"editor.wordBasedSuggestions": "off"
},
"pylint.importStrategy": "fromEnvironment",
}
4 changes: 2 additions & 2 deletions mkdocs_simple_plugin/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def write_config(config_file, config):
"""Write configuration file."""
if os.path.dirname(config_file):
os.makedirs(os.path.dirname(config_file), exist_ok=True)
with open(config_file, 'w+') as file:
with open(config_file, 'w+', encoding="utf-8") as file:
try:
yaml.dump(
data=config,
Expand All @@ -105,7 +105,7 @@ def setup_config(config_file="mkdocs.yml"):
# from the folder name.
write_config(config_file, config)
# Open the config file to verify settings.
with open(config_file, 'r') as stream:
with open(config_file, 'r', encoding="utf-8") as stream:
try:
local_config = yaml.load(stream, yaml.Loader)
if local_config:
Expand Down
43 changes: 23 additions & 20 deletions mkdocs_simple_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,16 @@
import os
import tempfile
import time
import yaml

from typing import Callable, Literal

from mkdocs.structure.files import Files, File
from mkdocs.plugins import BasePlugin
from mkdocs.config import config_options
import yaml
from mkdocs import config as mkdocs_config
from mkdocs import utils
from mkdocs.config import config_options
from mkdocs.config.defaults import MkDocsConfig
from mkdocs.livereload import LiveReloadServer
from mkdocs.plugins import BasePlugin
from mkdocs.structure.files import File, Files

from mkdocs_simple_plugin.simple import Simple

Expand Down Expand Up @@ -309,11 +311,16 @@ def __init__(self):
self.dirty = False
self.last_build_time = None

def on_startup(self, *, command, dirty: bool) -> None:
def on_startup(self,
*,
command: Literal['build',
'gh-deploy',
'serve'],
dirty: bool):
"""Configure the plugin on startup."""
self.dirty = dirty

def on_config(self, config, **kwargs):
def on_config(self, config: MkDocsConfig):
"""Update configuration to use a temporary build directory."""
# Save the config for documentation
default_config = dict((name, config_option.default)
Expand Down Expand Up @@ -359,7 +366,8 @@ def on_config(self, config, **kwargs):
os.path.abspath(config['docs_dir']))
return config

def on_files(self, files: Files, *, config):
def on_files(self, files: Files, /, *,
config: MkDocsConfig):
"""Update files based on plugin settings."""
# Configure simple
simple = Simple(**self.config)
Expand All @@ -372,30 +380,25 @@ def on_files(self, files: Files, *, config):

if not self.config["merge_docs_dir"]:
# If not merging, remove files that are from the docs dir
# pylint: disable=protected-access
for file in files._files[:]:
if file.abs_src_path.startswith(
os.path.abspath(config['docs_dir'])):
abs_docs_dir = os.path.abspath(config['docs_dir'])
for _, file in files.src_uris.items():
if file.abs_src_path.startswith(abs_docs_dir):
files.remove(file)

dedupe_files = {}
for file in files:
dedupe_files[file.abs_dest_path] = file

for path in self.paths:
file = File(
src_dir=os.path.abspath(path.output_root),
path=path.output_relpath,
dest_dir=config.site_dir,
use_directory_urls=config["use_directory_urls"]
)
if file.abs_dest_path in dedupe_files:
if file.abs_dest_path in files:
files.remove(dedupe_files[file.abs_dest_path])
if file.src_uri in files.src_uris:
files.remove(file)
files.append(file)
return files

def on_serve(self, server, *, config, builder):
def on_serve(self, server: LiveReloadServer, /, *, config: MkDocsConfig,
builder: Callable):
"""Add files to watch server."""
# don't watch the build directory
# pylint: disable=protected-access
Expand Down
4 changes: 2 additions & 2 deletions mkdocs_simple_plugin/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def is_path_ignored(self, path: str = None) -> bool:
mkdocsignore = os.path.join(base_path, ".mkdocsignore")
if os.path.exists(mkdocsignore):
ignore_list = []
with open(mkdocsignore, "r") as txt_file:
with open(mkdocsignore, mode="r", encoding="utf-8") as txt_file:
ignore_list = txt_file.read().splitlines()
# Remove all comment lines
ignore_list = [x for x in ignore_list if not x.startswith('#')]
Expand Down Expand Up @@ -255,7 +255,7 @@ def get_doc_file(
if not self.is_doc_file(os.path.join(from_dir, name)):
return []

if (do_copy):
if do_copy:
destination = os.path.join(to_dir, name)
os.makedirs(to_dir, exist_ok=True)
copy(original, destination)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ classifiers = [
dependencies = [
"click>=7.1",
"MarkupSafe>=2.1.1",
"mkdocs>=1.4.0",
"mkdocs>=1.6.0",
"PyYAML>=6.0",
]

Expand Down
14 changes: 7 additions & 7 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ click==8.1.7
flake8==7.0.0
Jinja2==3.1.3
MarkupSafe==2.1.5
mike==2.0.0
mike==2.1.1
mkdocs-awesome-pages-plugin==2.9.2
mkdocs-click==0.8.1
mkdocs-macros-plugin==1.0.5
mkdocs-material==9.5.17
mkdocs==1.5.3
mkdocs-material==9.5.21
mkdocs==1.6.0
mkdocstrings-python-legacy==0.2.3
mkdocstrings==0.24.3
mkdocstrings==0.25.0
pillow==10.3.0
pip-upgrader==1.4.15
pydocstyle==6.3.0
pyfakefs==5.4.1
pymdown-extensions==10.7.1
pymdown-extensions==10.8.1
PyYAML==6.0.1
hatch==1.9.4
hatch==1.10.0
typing-extensions==4.11.0
mkdocs-git-revision-date-localized-plugin==1.2.4
mkdocs-git-revision-date-localized-plugin==1.2.5

0 comments on commit 070e04c

Please sign in to comment.