fix: Interpret custom_dir relative to current directory, not config directory

The custom theme dir handling was not working in case the mkdocs config
  file was not in the top-level directory. Since mkdocs itself seems only
  to work in the top-level directory, this PR modifies mkdocs_semiliterate
  to interpret custom theme dirs relative to the current directory.

  Also, improves the harmonization between current mkdocs_simple_plugin
  code and this code.

  Resolves #28.
This commit is contained in:
Glen Whitney 2024-11-02 12:23:34 -07:00
parent 9b13ee0b3a
commit bceddb0701
10 changed files with 466 additions and 16 deletions

View file

@ -14,6 +14,9 @@ of the `simple` plugin.)
from mkdocs import utils
from mkdocs.config import config_options
from mkdocs.config.defaults import MkDocsConfig
from mkdocs.structure.files import File, Files
from mkdocs_simple_plugin.semiliterate import (
Semiliterate, LazyFile, ExtractionPattern, StreamExtract)
from mkdocs_simple_plugin.simple import (Simple, SimplePath)
@ -23,6 +26,7 @@ import os
import re
import subprocess
import sys
import time
import tempfile
import yaml
@ -352,12 +356,13 @@ terminate: '^\s*\)'
saved_includes = self.config['include']
new_config = super().on_config(config, **kwargs)
self.config['saved_includes'] = saved_includes
cfpath = os.path.dirname(config.config_file_path)
curpath = os.path.abspath('.')
self.custom_dir = None
for themedir in config['theme'].dirs:
common = os.path.commonpath([cfpath, themedir])
if common == cfpath:
self.custom_dir = os.path.relpath(themedir, cfpath)
common = os.path.commonpath(
[os.path.abspath('.'), os.path.abspath(themedir)])
if common == curpath:
self.custom_dir = os.path.relpath(themedir, curpath)
newthemedir = os.path.join(
self.config['build_dir'], self.custom_dir)
utils.log.debug(
@ -368,7 +373,39 @@ terminate: '^\s*\)'
break
return new_config
def on_files(self, files, config):
# Override rather than extend so that we use Semisimple instead of simple
# Note code must track mkdocs_simple_plugin, with the added section for
# the custom_dir at the bottom.
def on_files(self, files: Files, /, *,
config: MkDocsConfig):
"""Update files based on plugin settings."""
# Configure Semisimple
semisimple = Semisimple(**self.config)
# Save paths to add to watch if serving
do_copy = self.config["copy"]
self.paths = semisimple.build_docs(
self.dirty, self.last_build_time, do_copy)
self.last_build_time = time.time()
if not self.config["merge_docs_dir"]:
# If not merging, remove files that are from the 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)
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.src_uri in files.src_uris:
files.remove(file)
files.append(file)
# If we designated a subdirectory for the theme, ignore files in it
if self.custom_dir:
sources = files.src_paths
@ -379,15 +416,7 @@ terminate: '^\s*\)'
+ f"from theme directory {self.custom_dir}")
files.remove(sources[path])
def on_pre_build(self, *, config):
"""Build documentation directory with files according to settings."""
semisimple = Semisimple(**self.config)
# Merge docs
if self.config["merge_docs_dir"]:
semisimple.merge_docs(self.orig_docs_dir)
# Copy all of the valid doc files into build_docs_dir
self.paths = semisimple.build_docs()
return files
class Semisimple(Simple):