Skip to content

Commit

Permalink
Fix documentation of draft_docs and exclude_docs, add tests (mkdo…
Browse files Browse the repository at this point in the history
  • Loading branch information
pxc authored Nov 6, 2024
1 parent bb7e8b6 commit 7e4892a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 9 deletions.
29 changes: 21 additions & 8 deletions docs/user-guide/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,17 @@ Example:

```yaml
exclude_docs: |
api-config.json # A file with this name anywhere.
/requirements.txt # Top-level "docs/requirements.txt".
*.py # Any file with this extension anywhere.
!/foo/example.py # But keep this particular file.
# A file with this name anywhere.
api-config.json
# Top-level "docs/requirements.txt".
/requirements.txt
# Any file with this extension anywhere.
*.py
# But keep this particular file.
!/foo/example.py
```

This follows the [.gitignore pattern format](https://git-scm.com/docs/gitignore#_pattern_format).
Expand All @@ -327,7 +334,8 @@ Otherwise you could for example opt only certain dot-files back into the site:

```yaml
exclude_docs: |
!.assets # Don't exclude '.assets' although all other '.*' are excluded
# Don't exclude '.assets' although all other '.*' are excluded
!.assets
```

### draft_docs
Expand All @@ -340,9 +348,14 @@ Example:

```yaml
draft_docs: |
drafts/ # A "drafts" directory anywhere.
_unpublished.md # A md file ending in _unpublished.md
!/foo_unpublished.md # But keep this particular file.
# A "drafts" directory anywhere.
drafts/
# A Markdown file ending in _unpublished.md anywhere.
*_unpublished.md
# But keep this particular file.
!/foo_unpublished.md
```

This follows the [.gitignore pattern format](https://git-scm.com/docs/gitignore#_pattern_format).
Expand Down
2 changes: 1 addition & 1 deletion mkdocs/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import unittest.util

unittest.util._MAX_LENGTH = 100000
unittest.util._MAX_LENGTH = 100000 # type: ignore[misc]


class DisallowLogsHandler(logging.Handler):
Expand Down
60 changes: 60 additions & 0 deletions mkdocs/tests/build_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,66 @@ def _assert_build_logs(self, expected):
del msgs[-1]
self.assertEqual('\n'.join(msgs), textwrap.dedent(expected).strip('\n'))

@tempdir(
files={
'foo_unpublished.md': 'unpublished content to include anyway',
'other_unpublished.md': 'unpublished content',
'normal_file.md': 'should not be affected',
'test/other_unpublished.md': 'more unpublished content',
'test/normal_file.md': 'should not be affected',
}
)
@tempdir()
def test_draft_docs_with_comments_from_user_guide(self, site_dir, docs_dir):
cfg = load_config(
docs_dir=docs_dir,
site_dir=site_dir,
use_directory_urls=False,
draft_docs='''
# A "drafts" directory anywhere.
drafts/
# A Markdown file ending in _unpublished.md anywhere.
*_unpublished.md
# But keep this particular file.
!/foo_unpublished.md
''',
)

with self.subTest(serve_url=None):
build.build(cfg)
self.assertPathIsFile(site_dir, 'foo_unpublished.html')
self.assertPathIsFile(site_dir, 'normal_file.html')
self.assertPathIsFile(site_dir, 'test', 'normal_file.html')
self.assertPathNotExists(site_dir, 'other_unpublished.html')
self.assertPathNotExists(site_dir, 'test', 'other_unpublished.html')

serve_url = 'http://localhost:123/documentation/'
with self.subTest(serve_url=serve_url):
expected_logs = '''
INFO:The following pages are being built only for the preview but will be excluded from `mkdocs build` per `draft_docs` config:
- http://localhost:123/documentation/other_unpublished.html
- http://localhost:123/documentation/test/other_unpublished.html
'''
with self._assert_build_logs(expected_logs):
build.build(cfg, serve_url=serve_url)

top_other_path = Path(site_dir, 'other_unpublished.html')
self.assertTrue(top_other_path.is_file())
self.assertIn('DRAFT', top_other_path.read_text())

sub_other_path = Path(site_dir, 'test', 'other_unpublished.html')
self.assertPathIsFile(sub_other_path)
self.assertIn('DRAFT', sub_other_path.read_text())

self.assertPathIsFile(site_dir, 'foo_unpublished.html')
self.assertPathIsFile(site_dir, 'normal_file.html')

good_path = Path(site_dir, 'test', 'normal_file.html')
self.assertPathIsFile(good_path)
self.assertNotIn('DRAFT', good_path.read_text())

@tempdir(
files={
'test/foo.md': 'page1 content, [bar](bar.md)',
Expand Down

0 comments on commit 7e4892a

Please sign in to comment.