Skip to content
Open
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
50 changes: 14 additions & 36 deletions readthedocs/doc_builder/backends/mkdocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
import yaml
from django.conf import settings

from readthedocs.core.utils.filesystem import safe_open
from readthedocs.doc_builder.base import BaseBuilder
from readthedocs.projects.constants import MKDOCS
from readthedocs.projects.constants import MKDOCS_HTML
from readthedocs.projects.exceptions import UserFileNotFound


log = structlog.get_logger(__name__)
Expand Down Expand Up @@ -44,46 +42,25 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

# This is the *MkDocs* yaml file
self.yaml_file = self.get_yaml_config()

def get_final_doctype(self):
"""
Select a doctype based on the ``use_directory_urls`` setting.

https://www.mkdocs.org/user-guide/configuration/#use_directory_urls
"""

# TODO: we should eventually remove this method completely and stop
# relying on "loading the `mkdocs.yml` file in a safe way just to know
# if it's a MKDOCS or MKDOCS_HTML documentation type".
Comment on lines -56 to -58
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are relying on self.config.doctype now which always return mkdocs.


# Allow symlinks, but only the ones that resolve inside the base directory.
with safe_open(
self.yaml_file,
"r",
allow_symlinks=True,
base_path=self.project_path,
) as fh:
config = yaml_load_safely(fh)
use_directory_urls = config.get("use_directory_urls", True)
return MKDOCS if use_directory_urls else MKDOCS_HTML

def get_yaml_config(self):
"""Find the ``mkdocs.yml`` file in the project root."""
mkdocs_path = self.config.mkdocs.configuration
if not mkdocs_path:
mkdocs_path = "mkdocs.yml"
return os.path.join(
self.config_file = os.path.join(
self.project_path,
mkdocs_path,
self.config.mkdocs.configuration,
)

def show_conf(self):
"""Show the current ``mkdocs.yaml`` being used."""
if not os.path.exists(self.config_file):
raise UserFileNotFound(
message_id=UserFileNotFound.FILE_NOT_FOUND,
format_values={
"filename": os.path.relpath(self.config_file, self.project_path),
},
)

# Write the mkdocs.yml to the build logs
self.run(
"cat",
os.path.relpath(self.yaml_file, self.project_path),
os.path.relpath(self.config_file, self.project_path),
cwd=self.project_path,
)

Expand All @@ -97,8 +74,9 @@ def build(self):
"--site-dir",
os.path.join("$READTHEDOCS_OUTPUT", "html"),
"--config-file",
os.path.relpath(self.yaml_file, self.project_path),
os.path.relpath(self.config_file, self.project_path),
]

if self.config.mkdocs.fail_on_warning:
build_command.append("--strict")
cmd_ret = self.run(
Expand Down
2 changes: 1 addition & 1 deletion readthedocs/projects/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
),
Message(
id=RepositoryError.UNSUPPORTED_VCS,
header=_("Repository type not suported"),
header=_("Repository type not supported"),
body=_(
textwrap.dedent(
"""
Expand Down
20 changes: 19 additions & 1 deletion readthedocs/projects/tests/test_build_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,14 @@ def test_build_updates_documentation_type(self, load_yaml_config):
)
).touch()

# Create "mkdocs.yml" for the "cat" command to find it
pathlib.Path(
os.path.join(
self.project.checkout_path(self.version.slug),
"mkdocs.yml",
)
).touch()

self._trigger_update_docs_task()

# Update version state
Expand Down Expand Up @@ -1413,7 +1421,7 @@ def test_build_commands_executed_with_clone_token(
os.makedirs(self.project.artifact_path(version=self.version.slug, type_="epub"))
os.makedirs(self.project.artifact_path(version=self.version.slug, type_="pdf"))

get_clone_token.return_value = "toke:1234"
get_clone_token.return_value = "token:1234"
github_app_installation = get(
GitHubAppInstallation,
installation_id=1234,
Expand Down Expand Up @@ -2626,6 +2634,16 @@ def test_mkdocs_fail_on_warning(self, load_yaml_config):
validate=True,
)

# Create "mkdocs.yaml" for the "cat" command to find it
os.makedirs(os.path.join(self.project.checkout_path(version=self.version.slug), "docs"))
pathlib.Path(
os.path.join(
self.project.checkout_path(self.version.slug),
"docs",
"mkdocs.yaml",
)
).touch()

self._trigger_update_docs_task()

self.mocker.mocks["environment.run"].assert_has_calls(
Expand Down