Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions src/mkdocs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .mkdocs import MkDocs
from .cli import cli
from .cli import cli, build, serve

__all__ = [MkDocs, cli]
__all__ = [MkDocs, cli, build, serve]
62 changes: 59 additions & 3 deletions src/mkdocs/cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import pathlib
import tomllib

import click
import mkdocs

import pathlib
import flask


Expand All @@ -18,6 +19,55 @@
}


def default_config() -> dict:
return {
'mkdocs': {
'nav': [],
},
'loaders': {
'theme': 'pkg://mkdocs/default',
'docs': 'dir://',
},
'context': {
},
'markdown': {
'extensions': [
'fenced_code',
'footnotes',
'tables',
'toc',
# 'pymdownx.tasklist',
# 'gfm_admonition',
'mkdocs.extensions.rewrite_urls',
'mkdocs.extensions.short_codes',
'mkdocs.extensions.strike_thru',
],
'configs': {
'footnotes': {'BACKLINK_TITLE': ''},
'toc': {'anchorlink': True, 'marker': ''}
},
},
# 'commands': {
# 'build': 'mkdocs:build',
# 'serve': 'mkdocs:serve',
# }
}


def load_config(path: pathlib.Path) -> dict:
# Read the mkdocs.toml
text = path.read_text()
config = tomllib.loads(text)

# Merge the default config.
default = default_config()
for key, value in default.items():
if key not in config:
config[key] = value

return config


def build(mk: mkdocs.MkDocs) -> None:
buildpath = pathlib.Path('site')
for resource in mk.resources:
Expand Down Expand Up @@ -53,7 +103,13 @@ def _(path=''):


def cli():
mk = mkdocs.MkDocs()
path = pathlib.Path('mkdocs.toml')
if path.exists():
config = load_config(path)
else:
config = default_config()

mk = mkdocs.MkDocs(config)
group = click.Group(commands=[
click.Command(name='build', callback=lambda: build(mk)),
click.Command(name='serve', callback=lambda: serve(mk)),
Expand Down
76 changes: 2 additions & 74 deletions src/mkdocs/mkdocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,6 @@
from .extensions.rewrite_urls import PageContext


# Config...

class Config:
def __init__(self, config: dict, filename: str) -> None:
self._config = config
self._filename = filename

def get(self, *args: str):
value = self._config
for arg in args:
if not isinstance(value, dict) or arg not in value:
return None
value = value[arg]
return value

def __getitem__(self, key: str) -> typing.Any:
return self._config[key]

def __repr__(self):
return f'<Config {self._filename!r}>'


class ConfigError(Exception):
pass


class Page:
def __init__(self, text: str, html: str, toc: str, title: str, url: str, path: str):
self.text = text # The markdown text
Expand Down Expand Up @@ -244,13 +218,13 @@ def get_source(self, environment: jinja2.Environment, template: str):
# Here we go...

class MkDocs:
def __init__(self):
def __init__(self, config: dict) -> None:
self.loaders = {
'https': ZipURL,
'pkg': Package,
'dir': Directory,
}
self.config = self.load_config('mkdocs.toml')
self.config = config
self.handlers = self.load_handlers(self.config)
self.resources, self.templates = self.load_resources(self.handlers)
self.env = self.load_env(self.templates)
Expand All @@ -272,52 +246,6 @@ def path_to_url(self, path: pathlib.Path) -> str:
# 'css/styles.css' -> '/css/styles.css'
return pathlib.Path('/').joinpath(path).as_posix()

def load_config(self, filename: str) -> dict:
path = pathlib.Path(filename)
if not path.exists():
print("* No 'config.toml' file, using defaults.")
config = {}
# raise ConfigError(f"Missing config '{filename}'")
else:
text = path.read_text()
try:
config = tomllib.loads(text)
except tomllib.TOMLDecodeError as exc:
raise ConfigError(f"Invalid TOML in config '{filename}'\n{exc}")

default = {
'mkdocs': {
'nav': [],
},
'loaders': {
'theme': 'pkg://mkdocs/default',
'docs': 'dir://',
},
'context': {
},
'markdown': {
'extensions': [
'fenced_code',
'footnotes',
'tables',
'toc',
# 'pymdownx.tasklist',
# 'gfm_admonition',
'mkdocs.extensions.rewrite_urls',
'mkdocs.extensions.short_codes',
'mkdocs.extensions.strike_thru',
],
'configs': {
'footnotes': {'BACKLINK_TITLE': ''},
'toc': {'anchorlink': True, 'marker': ''}
},
},
}
for key, value in default.items():
if key not in config:
config[key] = value
return Config(config, filename=filename)

def load_handlers(self, config: dict) -> list[Handler]:
loaders_config = config['loaders']

Expand Down