Skip to content

Commit 5e09570

Browse files
committed
Add a warning in case the theme is not specified in the config
In MkDocs this used to be 'mkdocs' and it was included in the package. Here it still defaults to 'mkdocs' but is *not* included in the package. Now it also prints a warning when no 'theme:' config is specified.
1 parent 604563f commit 5e09570

File tree

8 files changed

+58
-25
lines changed

8 files changed

+58
-25
lines changed

properdocs.yml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,6 @@ site_author: ProperDocs Team
66
repo_url: https://github.com/properdocs/properdocs/
77
edit_uri: blob/master/docs/
88

9-
theme:
10-
name: mkdocs
11-
color_mode: auto
12-
user_color_mode_toggle: true
13-
locale: en
14-
analytics: {gtag: 'G-274394082'}
15-
highlightjs: true
16-
hljs_languages:
17-
- yaml
18-
- django
19-
209
nav:
2110
- Home: index.md
2211
- Getting Started: getting-started.md

properdocs/config/config_options.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,10 @@ def run_validation(self, value: object) -> theme.Theme:
814814
if theme_config['name'] is not None and theme_config['name'] not in themes:
815815
message = f"Unrecognised theme name: '{theme_config['name']}'."
816816
if theme_config['name'] in ('mkdocs', 'readthedocs'):
817-
message += f"\nAdditional dependency is needed:\n pip install properdocs-theme-{theme_config['name']}"
817+
message += (
818+
f"\nAn additional dependency is needed:"
819+
f"\n pip install properdocs-theme-{theme_config['name']}"
820+
)
818821
elif themes:
819822
message += f" The available installed themes are: {', '.join(themes)}"
820823
else:
@@ -842,6 +845,21 @@ def run_validation(self, value: object) -> theme.Theme:
842845
return theme.Theme(**theme_config)
843846

844847

848+
class ProperDocsTheme(Theme):
849+
def run_validation(self, value: object) -> theme.Theme:
850+
if value is None:
851+
value = 'mkdocs'
852+
if self.config_file_path:
853+
config_file_name = repr(os.path.basename(self.config_file_path))
854+
else:
855+
config_file_name = "the configuration file"
856+
log.warning(
857+
f"Please select a theme explicitly in {config_file_name}."
858+
" Defaulted to 'theme: mkdocs', but this may change in the future."
859+
)
860+
return super().run_validation(value)
861+
862+
845863
class Nav(OptionallyRequired):
846864
"""
847865
Nav Config Option.

properdocs/config/defaults.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class ProperDocsConfig(base.Config):
7171
site_author = c.Optional(c.Type(str))
7272
"""The name of the author to add to the HTML meta tags."""
7373

74-
theme = c.Theme(default='mkdocs')
74+
theme = c.ProperDocsTheme()
7575
"""The ProperDocs theme for the documentation."""
7676

7777
docs_dir = c.DocsDir(default='docs', exists=True)

properdocs/tests/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def get_markdown_toc(markdown_source):
2626
def load_config(config_file_path: str | None = None, **cfg) -> ProperDocsConfig:
2727
"""Helper to build a simple config for testing."""
2828
path_base = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'integration', 'minimal')
29-
if 'site_name' not in cfg:
30-
cfg['site_name'] = 'Example'
29+
cfg.setdefault('site_name', 'Example')
30+
cfg.setdefault('theme', 'mkdocs')
3131
if 'docs_dir' not in cfg:
3232
# Point to an actual dir to avoid a 'does not exist' error on validation.
3333
cfg['docs_dir'] = os.path.join(path_base, 'docs')

properdocs/tests/build_tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,7 @@ def test_markdown_extension_with_relative(self, config_dir):
909909
with self.subTest(base_path=base_path):
910910
cfg = f'''
911911
site_name: test
912+
theme: mkdocs
912913
use_directory_urls: false
913914
markdown_extensions:
914915
- properdocs.tests.build_tests:

properdocs/tests/config/base_tests.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def test_load_from_file(self, temp_dir):
4646
Allows users to specify a config other than the default `properdocs.yml`.
4747
"""
4848
with open(os.path.join(temp_dir, 'properdocs.yml'), 'w') as config_file:
49-
config_file.write("site_name: ProperDocs Test\n")
49+
config_file.write("site_name: ProperDocs Test\ntheme: mkdocs\n")
5050
os.mkdir(os.path.join(temp_dir, 'docs'))
5151

5252
cfg = base.load_config(config_file=config_file.name)
@@ -57,7 +57,7 @@ def test_load_from_file(self, temp_dir):
5757
def test_load_default_file(self, temp_dir):
5858
"""Test that `properdocs.yml` will be loaded when '--config' is not set."""
5959
with open(os.path.join(temp_dir, 'properdocs.yml'), 'w') as config_file:
60-
config_file.write("site_name: ProperDocs Test\n")
60+
config_file.write("site_name: ProperDocs Test\ntheme: mkdocs\n")
6161
os.mkdir(os.path.join(temp_dir, 'docs'))
6262
with change_dir(temp_dir):
6363
cfg = base.load_config(config_file=None)
@@ -68,7 +68,7 @@ def test_load_default_file(self, temp_dir):
6868
def test_load_default_file_with_yaml(self, temp_dir):
6969
"""Test that `properdocs.yml` will be loaded when '--config' is not set."""
7070
with open(os.path.join(temp_dir, 'properdocs.yaml'), 'w') as config_file:
71-
config_file.write("site_name: ProperDocs Test\n")
71+
config_file.write("site_name: ProperDocs Test\ntheme: mkdocs\n")
7272
os.mkdir(os.path.join(temp_dir, 'docs'))
7373
with change_dir(temp_dir):
7474
cfg = base.load_config(config_file=None)
@@ -79,9 +79,9 @@ def test_load_default_file_with_yaml(self, temp_dir):
7979
def test_load_default_file_prefer_yml(self, temp_dir):
8080
"""Test that `properdocs.yml` will be loaded when '--config' is not set."""
8181
with open(os.path.join(temp_dir, 'properdocs.yml'), 'w') as config_file1:
82-
config_file1.write("site_name: ProperDocs Test1\n")
82+
config_file1.write("site_name: ProperDocs Test1\ntheme: mkdocs\n")
8383
with open(os.path.join(temp_dir, 'properdocs.yaml'), 'w') as config_file2:
84-
config_file2.write("site_name: ProperDocs Test2\n")
84+
config_file2.write("site_name: ProperDocs Test2\ntheme: mkdocs\n")
8585

8686
os.mkdir(os.path.join(temp_dir, 'docs'))
8787
with change_dir(temp_dir):
@@ -104,7 +104,15 @@ def test_load_from_open_file(self, temp_path):
104104
config_file.flush()
105105
os.mkdir(os.path.join(temp_path, 'docs'))
106106

107-
cfg = base.load_config(config_file=config_file)
107+
with self.assertLogs('properdocs') as cm:
108+
cfg = base.load_config(config_file=config_file)
109+
self.assertEqual(
110+
cm.output,
111+
[
112+
"WARNING:properdocs.config.config_options:Please select a theme explicitly in 'properdocs.yml'. Defaulted to 'theme: mkdocs', but this may change in the future."
113+
],
114+
)
115+
108116
self.assertTrue(isinstance(cfg, defaults.ProperDocsConfig))
109117
self.assertEqual(cfg.site_name, 'ProperDocs Test')
110118
# load_config will always close the file
@@ -117,7 +125,7 @@ def test_load_from_closed_file(self, temp_dir):
117125
Ensure `load_config` reloads the closed file.
118126
"""
119127
with open(os.path.join(temp_dir, 'properdocs.yml'), 'w') as config_file:
120-
config_file.write("site_name: ProperDocs Test\n")
128+
config_file.write("site_name: ProperDocs Test\ntheme: mkdocs\n")
121129
os.mkdir(os.path.join(temp_dir, 'docs'))
122130

123131
cfg = base.load_config(config_file=config_file)
@@ -248,7 +256,7 @@ def test_load_from_file_with_relative_paths(self, config_dir):
248256
"""
249257
config_fname = os.path.join(config_dir, 'properdocs.yml')
250258
with open(config_fname, 'w') as config_file:
251-
config_file.write("docs_dir: src\nsite_name: ProperDocs Test\n")
259+
config_file.write("docs_dir: src\nsite_name: ProperDocs Test\ntheme: mkdocs\n")
252260
docs_dir = os.path.join(config_dir, 'src')
253261
os.mkdir(docs_dir)
254262

properdocs/tests/config/config_tests.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,27 @@ def test_missing_config_file(self):
2929

3030
def test_missing_site_name(self):
3131
conf = defaults.ProperDocsConfig()
32-
conf.load_dict({})
32+
conf.load_dict({'theme': 'mkdocs'})
3333
errors, warnings = conf.validate()
3434
self.assertEqual(
3535
errors, [('site_name', ValidationError("Required configuration not provided."))]
3636
)
3737
self.assertEqual(warnings, [])
3838

39+
def test_missing_theme(self):
40+
conf = defaults.ProperDocsConfig(config_file_path='a.yml')
41+
conf.load_dict({'site_name': 'Example'})
42+
with self.assertLogs('properdocs') as cm:
43+
errors, warnings = conf.validate()
44+
self.assertEqual(errors, [])
45+
self.assertEqual(warnings, [])
46+
self.assertEqual(
47+
cm.output,
48+
[
49+
"WARNING:properdocs.config.config_options:Please select a theme explicitly in 'a.yml'. Defaulted to 'theme: mkdocs', but this may change in the future."
50+
],
51+
)
52+
3953
def test_nonexistant_config(self):
4054
with self.assertRaises(ConfigurationError):
4155
config.load_config(config_file='/path/that/is/not/real')
@@ -69,6 +83,7 @@ def test_config_option(self, temp_path):
6983
file_contents = dedent(
7084
"""
7185
site_name: Example
86+
theme: mkdocs
7287
nav:
7388
- 'Introduction': 'index.md'
7489
"""
@@ -235,7 +250,7 @@ def test_empty_nav(self):
235250
conf = defaults.ProperDocsConfig(
236251
config_file_path=os.path.join(os.path.abspath('.'), 'properdocs.yml')
237252
)
238-
conf.load_dict({'site_name': 'Example'})
253+
conf.load_dict({'site_name': 'Example', 'theme': 'mkdocs'})
239254
conf.validate()
240255
self.assertEqual(conf['nav'], None)
241256

@@ -244,6 +259,7 @@ def test_error_on_pages(self):
244259
conf.load_dict(
245260
{
246261
'site_name': 'Example',
262+
'theme': 'mkdocs',
247263
'pages': ['index.md', 'about.md'],
248264
}
249265
)

properdocs/tests/structure/page_tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
def load_config(**cfg) -> ProperDocsConfig:
2222
cfg.setdefault('site_name', 'Example')
23+
cfg.setdefault('theme', 'mkdocs')
2324
cfg.setdefault(
2425
'docs_dir',
2526
os.path.join(

0 commit comments

Comments
 (0)