Skip to content

Commit

Permalink
Support docs from the root directory.
Browse files Browse the repository at this point in the history
  • Loading branch information
athackst committed Oct 29, 2023
1 parent ae93314 commit fb5cd7d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 77 deletions.
15 changes: 0 additions & 15 deletions mkdocs/config/config_options.py
Expand Up @@ -734,14 +734,6 @@ def post_validation(self, config: Config, key_name: str):
if not config.config_file_path:
return

# Validate that the dir is not the parent dir of the config file.
if os.path.dirname(config.config_file_path) == config[key_name]:
raise ValidationError(
f"The '{key_name}' should not be the parent directory of the"
f" config file. Use a child directory instead so that the"
f" '{key_name}' is a sibling of the config file."
)


class File(FilesystemObject):
"""
Expand Down Expand Up @@ -800,13 +792,6 @@ def post_validation(self, config: Config, key_name: str):
f"it will be deleted if --clean is passed to mkdocs build. "
f"(site_dir: '{site_dir}', docs_dir: '{docs_dir}')"
)
elif (site_dir + os.sep).startswith(docs_dir.rstrip(os.sep) + os.sep):
raise ValidationError(
f"The 'site_dir' should not be within the 'docs_dir' as this "
f"leads to the build directory being copied into itself and "
f"duplicate nested files in the 'site_dir'. "
f"(site_dir: '{site_dir}', docs_dir: '{docs_dir}')"
)


class Theme(BaseConfigOption[theme.Theme]):
Expand Down
5 changes: 5 additions & 0 deletions mkdocs/structure/files.py
Expand Up @@ -343,6 +343,11 @@ def get_files(config: MkDocsConfig) -> Files:
files: list[File] = []
conflicting_files: list[tuple[File, File]] = []
for source_dir, dirnames, filenames in os.walk(config['docs_dir'], followlinks=True):
# Skip processing any files in the site_dir
if os.path.abspath(source_dir) == os.path.abspath(config['site_dir']):
log.info(f"Excluding files in site_dir '{config['site_dir']}'.")
continue

relative_dir = os.path.relpath(source_dir, config['docs_dir'])
dirnames.sort()
filenames.sort(key=_file_sort_key)
Expand Down
31 changes: 0 additions & 31 deletions mkdocs/tests/config/config_options_legacy_tests.py
Expand Up @@ -768,20 +768,6 @@ class Schema:
)
self.assertEqual(conf['dir'], os.path.join(base_path, 'foo'))

def test_site_dir_is_config_dir_fails(self):
class Schema:
dir = c.DocsDir()

with self.expect_error(
dir="The 'dir' should not be the parent directory of the config file. "
"Use a child directory instead so that the 'dir' is a sibling of the config file."
):
self.get_config(
Schema,
{'dir': '.'},
config_file_path=os.path.join(os.path.abspath('.'), 'mkdocs.yml'),
)


class ListOfPathsTest(TestCase):
def test_valid_path(self):
Expand Down Expand Up @@ -882,23 +868,6 @@ def test_doc_dir_in_site_dir(self):
):
self.get_config(self.Schema, test_config)

def test_site_dir_in_docs_dir(self):
j = os.path.join

test_configs = (
{'docs_dir': 'docs', 'site_dir': j('docs', 'site')},
{'docs_dir': '.', 'site_dir': 'site'},
{'docs_dir': '', 'site_dir': 'site'},
{'docs_dir': '/', 'site_dir': 'site'},
)

for test_config in test_configs:
with self.subTest(test_config):
with self.expect_error(
site_dir=re.compile(r"The 'site_dir' should not be within the 'docs_dir'.*")
):
self.get_config(self.Schema, test_config)

def test_common_prefix(self):
"""Legitimate settings with common prefixes should not fail validation."""
test_configs = (
Expand Down
31 changes: 0 additions & 31 deletions mkdocs/tests/config/config_options_tests.py
Expand Up @@ -972,20 +972,6 @@ class Schema(Config):
)
self.assertEqual(conf.dir, os.path.join(base_path, 'foo'))

def test_site_dir_is_config_dir_fails(self) -> None:
class Schema(Config):
dir = c.DocsDir()

with self.expect_error(
dir="The 'dir' should not be the parent directory of the config file. "
"Use a child directory instead so that the 'dir' is a sibling of the config file."
):
self.get_config(
Schema,
{'dir': '.'},
config_file_path=os.path.join(os.path.abspath('.'), 'mkdocs.yml'),
)


class ListOfPathsTest(TestCase):
def test_valid_path(self) -> None:
Expand Down Expand Up @@ -1095,23 +1081,6 @@ def test_doc_dir_in_site_dir(self) -> None:
):
self.get_config(self.Schema, test_config)

def test_site_dir_in_docs_dir(self) -> None:
j = os.path.join

test_configs = (
{'docs_dir': 'docs', 'site_dir': j('docs', 'site')},
{'docs_dir': '.', 'site_dir': 'site'},
{'docs_dir': '', 'site_dir': 'site'},
{'docs_dir': '/', 'site_dir': 'site'},
)

for test_config in test_configs:
with self.subTest(test_config):
with self.expect_error(
site_dir=re.compile(r"The 'site_dir' should not be within the 'docs_dir'.*")
):
self.get_config(self.Schema, test_config)

def test_common_prefix(self) -> None:
"""Legitimate settings with common prefixes should not fail validation."""
test_configs = (
Expand Down
12 changes: 12 additions & 0 deletions mkdocs/tests/structure/file_tests.py
Expand Up @@ -672,6 +672,18 @@ def test_get_files_exclude_readme_with_index(self, tdir):
self.assertIsInstance(files, Files)
self.assertEqual([f.src_uri for f in files], ['index.md', 'foo.md'])

@tempdir(
files=[
'index.md',
'site/foo.md',
]
)
def test_get_files_exclude_site_dir(self, tdir):
config = load_config(docs_dir=tdir, site_dir=os.path.join(tdir, "site"))
files = get_files(config)
self.assertIsInstance(files, Files)
self.assertEqual([f.src_uri for f in files], ['index.md'])

@tempdir()
@tempdir(files={'test.txt': 'source content'})
def test_copy_file(self, src_dir, dest_dir):
Expand Down

0 comments on commit fb5cd7d

Please sign in to comment.