| 1 |
#!/usr/bin/env python |
| 2 |
from __future__ import annotations |
| 3 |
|
| 4 |
import contextlib |
| 5 |
import io |
| 6 |
import os.path |
| 7 |
import re |
| 8 |
import textwrap |
| 9 |
import unittest |
| 10 |
from pathlib import Path |
| 11 |
from typing import TYPE_CHECKING |
| 12 |
from unittest import mock |
| 13 |
|
| 14 |
import markdown.preprocessors |
| 15 |
|
| 16 |
from mkdocs.commands import build |
| 17 |
from mkdocs.config import base |
| 18 |
from mkdocs.exceptions import PluginError |
| 19 |
from mkdocs.structure.files import File, Files |
| 20 |
from mkdocs.structure.nav import get_navigation |
| 21 |
from mkdocs.structure.pages import Page |
| 22 |
from mkdocs.tests.base import PathAssertionMixin, load_config, tempdir |
| 23 |
from mkdocs.utils import meta |
| 24 |
|
| 25 |
if TYPE_CHECKING: |
| 26 |
from mkdocs.config.defaults import MkDocsConfig |
| 27 |
|
| 28 |
|
| 29 |
def build_page(title, path, config, md_src=''): |
| 30 |
"""Helper which returns a Page object.""" |
| 31 |
files = Files([File(path, config.docs_dir, config.site_dir, config.use_directory_urls)]) |
| 32 |
page = Page(title, list(files)[0], config) |
| 33 |
# Fake page.read_source() |
| 34 |
page.markdown, page.meta = meta.get_data(md_src) |
| 35 |
return page, files |
| 36 |
|
| 37 |
|
| 38 |
class BuildTests(PathAssertionMixin, unittest.TestCase): |
| 39 |
def _get_env_with_null_translations(self, config): |
| 40 |
env = config.theme.get_env() |
| 41 |
env.add_extension('jinja2.ext.i18n') |
| 42 |
env.install_null_translations() |
| 43 |
return env |
| 44 |
|
| 45 |
# Test build.get_context |
| 46 |
|
| 47 |
def test_context_base_url_homepage(self): |
| 48 |
nav_cfg = [ |
| 49 |
{'Home': 'index.md'}, |
| 50 |
] |
| 51 |
cfg = load_config(nav=nav_cfg, use_directory_urls=False) |
| 52 |
fs = [File('index.md', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls)] |
| 53 |
files = Files(fs) |
| 54 |
nav = get_navigation(files, cfg) |
| 55 |
context = build.get_context(nav, files, cfg, nav.pages[0]) |
| 56 |
self.assertEqual(context['base_url'], '.') |
| 57 |
|
| 58 |
def test_context_base_url_homepage_use_directory_urls(self): |
| 59 |
nav_cfg = [ |
| 60 |
{'Home': 'index.md'}, |
| 61 |
] |
| 62 |
cfg = load_config(nav=nav_cfg) |
| 63 |
fs = [File('index.md', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls)] |
| 64 |
files = Files(fs) |
| 65 |
nav = get_navigation(files, cfg) |
| 66 |
context = build.get_context(nav, files, cfg, nav.pages[0]) |
| 67 |
self.assertEqual(context['base_url'], '.') |
| 68 |
|
| 69 |
def test_context_base_url_nested_page(self): |
| 70 |
nav_cfg = [ |
| 71 |
{'Home': 'index.md'}, |
| 72 |
{'Nested': 'foo/bar.md'}, |
| 73 |
] |
| 74 |
cfg = load_config(nav=nav_cfg, use_directory_urls=False) |
| 75 |
fs = [ |
| 76 |
File('index.md', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls), |
| 77 |
File('foo/bar.md', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls), |
| 78 |
] |
| 79 |
files = Files(fs) |
| 80 |
nav = get_navigation(files, cfg) |
| 81 |
context = build.get_context(nav, files, cfg, nav.pages[1]) |
| 82 |
self.assertEqual(context['base_url'], '..') |
| 83 |
|
| 84 |
def test_context_base_url_nested_page_use_directory_urls(self): |
| 85 |
nav_cfg = [ |
| 86 |
{'Home': 'index.md'}, |
| 87 |
{'Nested': 'foo/bar.md'}, |
| 88 |
] |
| 89 |
cfg = load_config(nav=nav_cfg) |
| 90 |
fs = [ |
| 91 |
File('index.md', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls), |
| 92 |
File('foo/bar.md', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls), |
| 93 |
] |
| 94 |
files = Files(fs) |
| 95 |
nav = get_navigation(files, cfg) |
| 96 |
context = build.get_context(nav, files, cfg, nav.pages[1]) |
| 97 |
self.assertEqual(context['base_url'], '../..') |
| 98 |
|
| 99 |
def test_context_base_url_relative_no_page(self): |
| 100 |
cfg = load_config(use_directory_urls=False) |
| 101 |
context = build.get_context(mock.Mock(), mock.Mock(), cfg, base_url='..') |
| 102 |
self.assertEqual(context['base_url'], '..') |
| 103 |
|
| 104 |
def test_context_base_url_relative_no_page_use_directory_urls(self): |
| 105 |
cfg = load_config() |
| 106 |
context = build.get_context(mock.Mock(), mock.Mock(), cfg, base_url='..') |
| 107 |
self.assertEqual(context['base_url'], '..') |
| 108 |
|
| 109 |
def test_context_base_url_absolute_no_page(self): |
| 110 |
cfg = load_config(use_directory_urls=False) |
| 111 |
context = build.get_context(mock.Mock(), mock.Mock(), cfg, base_url='/') |
| 112 |
self.assertEqual(context['base_url'], '/') |
| 113 |
|
| 114 |
def test_context_base_url__absolute_no_page_use_directory_urls(self): |
| 115 |
cfg = load_config() |
| 116 |
context = build.get_context(mock.Mock(), mock.Mock(), cfg, base_url='/') |
| 117 |
self.assertEqual(context['base_url'], '/') |
| 118 |
|
| 119 |
def test_context_base_url_absolute_nested_no_page(self): |
| 120 |
cfg = load_config(use_directory_urls=False) |
| 121 |
context = build.get_context(mock.Mock(), mock.Mock(), cfg, base_url='/foo/') |
| 122 |
self.assertEqual(context['base_url'], '/foo/') |
| 123 |
|
| 124 |
def test_context_base_url__absolute_nested_no_page_use_directory_urls(self): |
| 125 |
cfg = load_config() |
| 126 |
context = build.get_context(mock.Mock(), mock.Mock(), cfg, base_url='/foo/') |
| 127 |
self.assertEqual(context['base_url'], '/foo/') |
| 128 |
|
| 129 |
def test_context_extra_css_js_from_homepage(self): |
| 130 |
nav_cfg = [ |
| 131 |
{'Home': 'index.md'}, |
| 132 |
] |
| 133 |
cfg = load_config( |
| 134 |
nav=nav_cfg, |
| 135 |
extra_css=['style.css'], |
| 136 |
extra_javascript=['script.js'], |
| 137 |
use_directory_urls=False, |
| 138 |
) |
| 139 |
fs = [File('index.md', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls)] |
| 140 |
files = Files(fs) |
| 141 |
nav = get_navigation(files, cfg) |
| 142 |
context = build.get_context(nav, files, cfg, nav.pages[0]) |
| 143 |
self.assertEqual(context['extra_css'], ['style.css']) |
| 144 |
self.assertEqual(context['extra_javascript'], ['script.js']) |
| 145 |
|
| 146 |
def test_context_extra_css_js_from_nested_page(self): |
| 147 |
nav_cfg = [ |
| 148 |
{'Home': 'index.md'}, |
| 149 |
{'Nested': 'foo/bar.md'}, |
| 150 |
] |
| 151 |
cfg = load_config( |
| 152 |
nav=nav_cfg, |
| 153 |
extra_css=['style.css'], |
| 154 |
extra_javascript=['script.js'], |
| 155 |
use_directory_urls=False, |
| 156 |
) |
| 157 |
fs = [ |
| 158 |
File('index.md', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls), |
| 159 |
File('foo/bar.md', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls), |
| 160 |
] |
| 161 |
files = Files(fs) |
| 162 |
nav = get_navigation(files, cfg) |
| 163 |
context = build.get_context(nav, files, cfg, nav.pages[1]) |
| 164 |
self.assertEqual(context['extra_css'], ['../style.css']) |
| 165 |
self.assertEqual(context['extra_javascript'], ['../script.js']) |
| 166 |
|
| 167 |
def test_context_extra_css_js_from_nested_page_use_directory_urls(self): |
| 168 |
nav_cfg = [ |
| 169 |
{'Home': 'index.md'}, |
| 170 |
{'Nested': 'foo/bar.md'}, |
| 171 |
] |
| 172 |
cfg = load_config( |
| 173 |
nav=nav_cfg, |
| 174 |
extra_css=['style.css'], |
| 175 |
extra_javascript=['script.js'], |
| 176 |
) |
| 177 |
fs = [ |
| 178 |
File('index.md', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls), |
| 179 |
File('foo/bar.md', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls), |
| 180 |
] |
| 181 |
files = Files(fs) |
| 182 |
nav = get_navigation(files, cfg) |
| 183 |
context = build.get_context(nav, files, cfg, nav.pages[1]) |
| 184 |
self.assertEqual(context['extra_css'], ['../../style.css']) |
| 185 |
self.assertEqual(context['extra_javascript'], ['../../script.js']) |
| 186 |
|
| 187 |
# TODO: This shouldn't pass on Linux |
| 188 |
# @unittest.skipUnless(sys.platform.startswith("win"), "requires Windows") |
| 189 |
def test_context_extra_css_path_warning(self): |
| 190 |
nav_cfg = [ |
| 191 |
{'Home': 'index.md'}, |
| 192 |
] |
| 193 |
cfg = load_config( |
| 194 |
nav=nav_cfg, |
| 195 |
extra_css=['assets\\style.css'], |
| 196 |
use_directory_urls=False, |
| 197 |
) |
| 198 |
fs = [File('index.md', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls)] |
| 199 |
files = Files(fs) |
| 200 |
nav = get_navigation(files, cfg) |
| 201 |
with self.assertLogs('mkdocs') as cm: |
| 202 |
context = build.get_context(nav, files, cfg, nav.pages[0]) |
| 203 |
self.assertEqual(context['extra_css'], ['assets/style.css']) |
| 204 |
self.assertEqual( |
| 205 |
'\n'.join(cm.output), |
| 206 |
"WARNING:mkdocs.utils:Path 'assets\\style.css' uses OS-specific separator '\\'. " |
| 207 |
"That will be unsupported in a future release. Please change it to '/'.", |
| 208 |
) |
| 209 |
|
| 210 |
def test_context_extra_css_js_no_page(self): |
| 211 |
cfg = load_config(extra_css=['style.css'], extra_javascript=['script.js']) |
| 212 |
context = build.get_context(mock.Mock(), mock.Mock(), cfg, base_url='..') |
| 213 |
self.assertEqual(context['extra_css'], ['../style.css']) |
| 214 |
self.assertEqual(context['extra_javascript'], ['../script.js']) |
| 215 |
|
| 216 |
def test_extra_context(self): |
| 217 |
cfg = load_config(extra={'a': 1}) |
| 218 |
context = build.get_context(mock.Mock(), mock.Mock(), cfg) |
| 219 |
self.assertEqual(context['config']['extra']['a'], 1) |
| 220 |
|
| 221 |
# Test build._build_theme_template |
| 222 |
|
| 223 |
@mock.patch('mkdocs.utils.write_file') |
| 224 |
@mock.patch('mkdocs.commands.build._build_template', return_value='some content') |
| 225 |
def test_build_theme_template(self, mock_build_template, mock_write_file): |
| 226 |
cfg = load_config() |
| 227 |
env = cfg.theme.get_env() |
| 228 |
build._build_theme_template('main.html', env, Files([]), cfg, mock.Mock()) |
| 229 |
mock_write_file.assert_called_once() |
| 230 |
mock_build_template.assert_called_once() |
| 231 |
|
| 232 |
@mock.patch('mkdocs.utils.write_file') |
| 233 |
@mock.patch('mkdocs.commands.build._build_template', return_value='some content') |
| 234 |
@mock.patch('gzip.GzipFile') |
| 235 |
@tempdir() |
| 236 |
def test_build_sitemap_template( |
| 237 |
self, site_dir, mock_gzip_gzipfile, mock_build_template, mock_write_file |
| 238 |
): |
| 239 |
cfg = load_config(site_dir=site_dir) |
| 240 |
env = cfg.theme.get_env() |
| 241 |
build._build_theme_template('sitemap.xml', env, Files([]), cfg, mock.Mock()) |
| 242 |
mock_write_file.assert_called_once() |
| 243 |
mock_build_template.assert_called_once() |
| 244 |
mock_gzip_gzipfile.assert_called_once() |
| 245 |
|
| 246 |
@mock.patch('mkdocs.utils.write_file') |
| 247 |
@mock.patch('mkdocs.commands.build._build_template', return_value='') |
| 248 |
def test_skip_missing_theme_template(self, mock_build_template, mock_write_file): |
| 249 |
cfg = load_config() |
| 250 |
env = cfg.theme.get_env() |
| 251 |
with self.assertLogs('mkdocs') as cm: |
| 252 |
build._build_theme_template('missing.html', env, Files([]), cfg, mock.Mock()) |
| 253 |
self.assertEqual( |
| 254 |
'\n'.join(cm.output), |
| 255 |
"WARNING:mkdocs.commands.build:Template skipped: 'missing.html' not found in theme directories.", |
| 256 |
) |
| 257 |
mock_write_file.assert_not_called() |
| 258 |
mock_build_template.assert_not_called() |
| 259 |
|
| 260 |
@mock.patch('mkdocs.utils.write_file') |
| 261 |
@mock.patch('mkdocs.commands.build._build_template', return_value='') |
| 262 |
def test_skip_theme_template_empty_output(self, mock_build_template, mock_write_file): |
| 263 |
cfg = load_config() |
| 264 |
env = cfg.theme.get_env() |
| 265 |
with self.assertLogs('mkdocs') as cm: |
| 266 |
build._build_theme_template('main.html', env, Files([]), cfg, mock.Mock()) |
| 267 |
self.assertEqual( |
| 268 |
'\n'.join(cm.output), |
| 269 |
"INFO:mkdocs.commands.build:Template skipped: 'main.html' generated empty output.", |
| 270 |
) |
| 271 |
mock_write_file.assert_not_called() |
| 272 |
mock_build_template.assert_called_once() |
| 273 |
|
| 274 |
# Test build._build_extra_template |
| 275 |
|
| 276 |
@tempdir() |
| 277 |
@mock.patch('mkdocs.structure.files.open', mock.mock_open(read_data='template content')) |
| 278 |
def test_build_extra_template(self, site_dir): |
| 279 |
cfg = load_config(site_dir=site_dir) |
| 280 |
fs = [File('foo.html', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls)] |
| 281 |
files = Files(fs) |
| 282 |
build._build_extra_template('foo.html', files, cfg, mock.Mock()) |
| 283 |
|
| 284 |
@mock.patch('mkdocs.structure.files.open', mock.mock_open(read_data='template content')) |
| 285 |
def test_skip_missing_extra_template(self): |
| 286 |
cfg = load_config() |
| 287 |
fs = [File('foo.html', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls)] |
| 288 |
files = Files(fs) |
| 289 |
with self.assertLogs('mkdocs') as cm: |
| 290 |
build._build_extra_template('missing.html', files, cfg, mock.Mock()) |
| 291 |
self.assertEqual( |
| 292 |
'\n'.join(cm.output), |
| 293 |
"WARNING:mkdocs.commands.build:Template skipped: 'missing.html' not found in docs_dir.", |
| 294 |
) |
| 295 |
|
| 296 |
@mock.patch('mkdocs.structure.files.open', mock.Mock(side_effect=OSError('Error message.'))) |
| 297 |
def test_skip_ioerror_extra_template(self): |
| 298 |
cfg = load_config() |
| 299 |
fs = [File('foo.html', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls)] |
| 300 |
files = Files(fs) |
| 301 |
with self.assertLogs('mkdocs') as cm: |
| 302 |
build._build_extra_template('foo.html', files, cfg, mock.Mock()) |
| 303 |
self.assertEqual( |
| 304 |
'\n'.join(cm.output), |
| 305 |
"WARNING:mkdocs.commands.build:Error reading template 'foo.html': Error message.", |
| 306 |
) |
| 307 |
|
| 308 |
@mock.patch('mkdocs.structure.files.open', mock.mock_open(read_data='')) |
| 309 |
def test_skip_extra_template_empty_output(self): |
| 310 |
cfg = load_config() |
| 311 |
fs = [File('foo.html', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls)] |
| 312 |
files = Files(fs) |
| 313 |
with self.assertLogs('mkdocs') as cm: |
| 314 |
build._build_extra_template('foo.html', files, cfg, mock.Mock()) |
| 315 |
self.assertEqual( |
| 316 |
'\n'.join(cm.output), |
| 317 |
"INFO:mkdocs.commands.build:Template skipped: 'foo.html' generated empty output.", |
| 318 |
) |
| 319 |
|
| 320 |
# Test build._populate_page |
| 321 |
|
| 322 |
@tempdir(files={'index.md': 'page content'}) |
| 323 |
def test_populate_page(self, docs_dir): |
| 324 |
cfg = load_config(docs_dir=docs_dir) |
| 325 |
file = File('index.md', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls) |
| 326 |
page = Page('Foo', file, cfg) |
| 327 |
build._populate_page(page, cfg, Files([file])) |
| 328 |
self.assertEqual(page.content, '<p>page content</p>') |
| 329 |
|
| 330 |
@tempdir(files={'testing.html': '<p>page content</p>'}) |
| 331 |
def test_populate_page_dirty_modified(self, site_dir): |
| 332 |
cfg = load_config(site_dir=site_dir) |
| 333 |
file = File('testing.md', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls) |
| 334 |
page = Page('Foo', file, cfg) |
| 335 |
build._populate_page(page, cfg, Files([file]), dirty=True) |
| 336 |
self.assertTrue(page.markdown.startswith('# Welcome to MkDocs')) |
| 337 |
self.assertTrue( |
| 338 |
page.content.startswith('<h1 id="welcome-to-mkdocs">Welcome to MkDocs</h1>') |
| 339 |
) |
| 340 |
|
| 341 |
@tempdir(files={'index.md': 'page content'}) |
| 342 |
@tempdir(files={'index.html': '<p>page content</p>'}) |
| 343 |
def test_populate_page_dirty_not_modified(self, site_dir, docs_dir): |
| 344 |
cfg = load_config(docs_dir=docs_dir, site_dir=site_dir) |
| 345 |
file = File('index.md', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls) |
| 346 |
page = Page('Foo', file, cfg) |
| 347 |
build._populate_page(page, cfg, Files([file]), dirty=True) |
| 348 |
# Content is empty as file read was skipped |
| 349 |
self.assertEqual(page.markdown, None) |
| 350 |
self.assertEqual(page.content, None) |
| 351 |
|
| 352 |
@tempdir(files={'index.md': 'new page content'}) |
| 353 |
@mock.patch('mkdocs.structure.files.open', side_effect=OSError('Error message.')) |
| 354 |
def test_populate_page_read_error(self, docs_dir, mock_open): |
| 355 |
cfg = load_config(docs_dir=docs_dir) |
| 356 |
file = File('missing.md', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls) |
| 357 |
page = Page('Foo', file, cfg) |
| 358 |
with self.assertLogs('mkdocs') as cm: |
| 359 |
with self.assertRaises(OSError): |
| 360 |
build._populate_page(page, cfg, Files([file])) |
| 361 |
self.assertEqual( |
| 362 |
cm.output, |
| 363 |
[ |
| 364 |
'ERROR:mkdocs.structure.pages:File not found: missing.md', |
| 365 |
"ERROR:mkdocs.commands.build:Error reading page 'missing.md': Error message.", |
| 366 |
], |
| 367 |
) |
| 368 |
mock_open.assert_called_once() |
| 369 |
|
| 370 |
@tempdir(files={'index.md': 'page content'}) |
| 371 |
def test_populate_page_read_plugin_error(self, docs_dir): |
| 372 |
def on_page_markdown(*args, **kwargs): |
| 373 |
raise PluginError('Error message.') |
| 374 |
|
| 375 |
cfg = load_config(docs_dir=docs_dir) |
| 376 |
cfg.plugins.events['page_markdown'].append(on_page_markdown) |
| 377 |
|
| 378 |
file = File('index.md', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls) |
| 379 |
page = Page('Foo', file, cfg) |
| 380 |
with self.assertLogs('mkdocs') as cm: |
| 381 |
with self.assertRaises(PluginError): |
| 382 |
build._populate_page(page, cfg, Files([file])) |
| 383 |
self.assertEqual( |
| 384 |
'\n'.join(cm.output), |
| 385 |
"ERROR:mkdocs.commands.build:Error reading page 'index.md':", |
| 386 |
) |
| 387 |
|
| 388 |
# Test build._build_page |
| 389 |
|
| 390 |
@tempdir() |
| 391 |
def test_build_page(self, site_dir): |
| 392 |
cfg = load_config(site_dir=site_dir, nav=['index.md']) |
| 393 |
fs = [File('index.md', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls)] |
| 394 |
files = Files(fs) |
| 395 |
nav = get_navigation(files, cfg) |
| 396 |
page = files.documentation_pages()[0].page |
| 397 |
# Fake populate page |
| 398 |
page.title = 'Title' |
| 399 |
page.markdown = 'page content' |
| 400 |
page.content = '<p>page content</p>' |
| 401 |
build._build_page(page, cfg, files, nav, self._get_env_with_null_translations(cfg)) |
| 402 |
self.assertPathIsFile(site_dir, 'index.html') |
| 403 |
|
| 404 |
@tempdir() |
| 405 |
@mock.patch('jinja2.environment.Template.render', return_value='') |
| 406 |
def test_build_page_empty(self, site_dir, render_mock): |
| 407 |
cfg = load_config(site_dir=site_dir, nav=['index.md']) |
| 408 |
fs = [File('index.md', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls)] |
| 409 |
files = Files(fs) |
| 410 |
nav = get_navigation(files, cfg) |
| 411 |
with self.assertLogs('mkdocs') as cm: |
| 412 |
build._build_page( |
| 413 |
files.documentation_pages()[0].page, cfg, files, nav, cfg.theme.get_env() |
| 414 |
) |
| 415 |
self.assertEqual( |
| 416 |
'\n'.join(cm.output), |
| 417 |
"INFO:mkdocs.commands.build:Page skipped: 'index.md'. Generated empty output.", |
| 418 |
) |
| 419 |
self.assertPathNotExists(site_dir, 'index.html') |
| 420 |
render_mock.assert_called_once() |
| 421 |
|
| 422 |
@tempdir(files={'index.md': 'page content'}) |
| 423 |
@tempdir(files={'index.html': '<p>page content</p>'}) |
| 424 |
@mock.patch('mkdocs.utils.write_file') |
| 425 |
def test_build_page_dirty_modified(self, site_dir, docs_dir, mock_write_file): |
| 426 |
cfg = load_config(docs_dir=docs_dir, site_dir=site_dir, nav=['index.md']) |
| 427 |
fs = [File('index.md', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls)] |
| 428 |
files = Files(fs) |
| 429 |
nav = get_navigation(files, cfg) |
| 430 |
page = files.documentation_pages()[0].page |
| 431 |
# Fake populate page |
| 432 |
page.title = 'Title' |
| 433 |
page.markdown = 'new page content' |
| 434 |
page.content = '<p>new page content</p>' |
| 435 |
build._build_page( |
| 436 |
page, cfg, files, nav, self._get_env_with_null_translations(cfg), dirty=True |
| 437 |
) |
| 438 |
mock_write_file.assert_not_called() |
| 439 |
|
| 440 |
@tempdir(files={'testing.html': '<p>page content</p>'}) |
| 441 |
@mock.patch('mkdocs.utils.write_file') |
| 442 |
def test_build_page_dirty_not_modified(self, site_dir, mock_write_file): |
| 443 |
cfg = load_config(site_dir=site_dir, nav=['testing.md']) |
| 444 |
fs = [File('testing.md', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls)] |
| 445 |
files = Files(fs) |
| 446 |
nav = get_navigation(files, cfg) |
| 447 |
page = files.documentation_pages()[0].page |
| 448 |
# Fake populate page |
| 449 |
page.title = 'Title' |
| 450 |
page.markdown = 'page content' |
| 451 |
page.content = '<p>page content</p>' |
| 452 |
build._build_page( |
| 453 |
page, cfg, files, nav, self._get_env_with_null_translations(cfg), dirty=True |
| 454 |
) |
| 455 |
mock_write_file.assert_called_once() |
| 456 |
|
| 457 |
@tempdir() |
| 458 |
def test_build_page_custom_template(self, site_dir): |
| 459 |
cfg = load_config(site_dir=site_dir, nav=['index.md']) |
| 460 |
fs = [File('index.md', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls)] |
| 461 |
files = Files(fs) |
| 462 |
nav = get_navigation(files, cfg) |
| 463 |
page = files.documentation_pages()[0].page |
| 464 |
# Fake populate page |
| 465 |
page.title = 'Title' |
| 466 |
page.meta = {'template': '404.html'} |
| 467 |
page.markdown = 'page content' |
| 468 |
page.content = '<p>page content</p>' |
| 469 |
build._build_page(page, cfg, files, nav, self._get_env_with_null_translations(cfg)) |
| 470 |
self.assertPathIsFile(site_dir, 'index.html') |
| 471 |
|
| 472 |
@tempdir() |
| 473 |
@mock.patch('mkdocs.utils.write_file', side_effect=OSError('Error message.')) |
| 474 |
def test_build_page_error(self, site_dir, mock_write_file): |
| 475 |
cfg = load_config(site_dir=site_dir, nav=['index.md']) |
| 476 |
fs = [File('index.md', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls)] |
| 477 |
files = Files(fs) |
| 478 |
nav = get_navigation(files, cfg) |
| 479 |
page = files.documentation_pages()[0].page |
| 480 |
# Fake populate page |
| 481 |
page.title = 'Title' |
| 482 |
page.markdown = 'page content' |
| 483 |
page.content = '<p>page content</p>' |
| 484 |
with self.assertLogs('mkdocs') as cm: |
| 485 |
with self.assertRaises(OSError): |
| 486 |
build._build_page(page, cfg, files, nav, self._get_env_with_null_translations(cfg)) |
| 487 |
self.assertEqual( |
| 488 |
'\n'.join(cm.output), |
| 489 |
"ERROR:mkdocs.commands.build:Error building page 'index.md': Error message.", |
| 490 |
) |
| 491 |
mock_write_file.assert_called_once() |
| 492 |
|
| 493 |
@tempdir() |
| 494 |
def test_build_page_plugin_error(self, site_dir): |
| 495 |
def on_page_context(*args, **kwargs): |
| 496 |
raise PluginError('Error message.') |
| 497 |
|
| 498 |
cfg = load_config(site_dir=site_dir, nav=['index.md']) |
| 499 |
cfg.plugins.events['page_context'].append(on_page_context) |
| 500 |
fs = [File('index.md', cfg.docs_dir, cfg.site_dir, cfg.use_directory_urls)] |
| 501 |
files = Files(fs) |
| 502 |
nav = get_navigation(files, cfg) |
| 503 |
page = files.documentation_pages()[0].page |
| 504 |
# Fake populate page |
| 505 |
page.title = 'Title' |
| 506 |
page.markdown = 'page content' |
| 507 |
page.content = '<p>page content</p>' |
| 508 |
with self.assertLogs('mkdocs') as cm: |
| 509 |
with self.assertRaises(PluginError): |
| 510 |
build._build_page(page, cfg, files, nav, cfg.theme.get_env()) |
| 511 |
self.assertEqual( |
| 512 |
'\n'.join(cm.output), |
| 513 |
"ERROR:mkdocs.commands.build:Error building page 'index.md':", |
| 514 |
) |
| 515 |
|
| 516 |
# Test build.build |
| 517 |
|
| 518 |
@tempdir( |
| 519 |
files={ |
| 520 |
'index.md': 'page content', |
| 521 |
'empty.md': '', |
| 522 |
'img.jpg': '', |
| 523 |
'static.html': 'content', |
| 524 |
'.hidden': 'content', |
| 525 |
'.git/hidden': 'content', |
| 526 |
} |
| 527 |
) |
| 528 |
@tempdir() |
| 529 |
def test_copying_media(self, site_dir, docs_dir): |
| 530 |
cfg = load_config(docs_dir=docs_dir, site_dir=site_dir) |
| 531 |
build.build(cfg) |
| 532 |
|
| 533 |
# Verify that only non-empty md file (converted to html), static HTML file and image are copied. |
| 534 |
self.assertPathIsFile(site_dir, 'index.html') |
| 535 |
self.assertPathIsFile(site_dir, 'img.jpg') |
| 536 |
self.assertPathIsFile(site_dir, 'static.html') |
| 537 |
self.assertPathNotExists(site_dir, 'empty.md') |
| 538 |
self.assertPathNotExists(site_dir, '.hidden') |
| 539 |
self.assertPathNotExists(site_dir, '.git/hidden') |
| 540 |
|
| 541 |
@tempdir(files={'index.md': 'page content'}) |
| 542 |
@tempdir() |
| 543 |
def test_copy_theme_files(self, site_dir, docs_dir): |
| 544 |
cfg = load_config(docs_dir=docs_dir, site_dir=site_dir) |
| 545 |
build.build(cfg) |
| 546 |
|
| 547 |
# Verify only theme media are copied, not templates, Python or localization files. |
| 548 |
self.assertPathIsFile(site_dir, 'index.html') |
| 549 |
self.assertPathIsFile(site_dir, '404.html') |
| 550 |
self.assertPathIsDir(site_dir, 'js') |
| 551 |
self.assertPathIsDir(site_dir, 'css') |
| 552 |
self.assertPathIsDir(site_dir, 'img') |
| 553 |
self.assertPathNotExists(site_dir, '__init__.py') |
| 554 |
self.assertPathNotExists(site_dir, '__init__.pyc') |
| 555 |
self.assertPathNotExists(site_dir, 'base.html') |
| 556 |
self.assertPathNotExists(site_dir, 'content.html') |
| 557 |
self.assertPathNotExists(site_dir, 'main.html') |
| 558 |
self.assertPathNotExists(site_dir, 'locales') |
| 559 |
|
| 560 |
@contextlib.contextmanager |
| 561 |
def _assert_build_logs(self, expected): |
| 562 |
with self.assertLogs('mkdocs') as cm: |
| 563 |
yield |
| 564 |
msgs = [f'{r.levelname}:{r.message}' for r in cm.records] |
| 565 |
if msgs and msgs[0].startswith('INFO:Cleaning site directory'): |
| 566 |
del msgs[0] |
| 567 |
if msgs and msgs[0].startswith('INFO:Building documentation to directory'): |
| 568 |
del msgs[0] |
| 569 |
if msgs and msgs[-1].startswith('INFO:Documentation built'): |
| 570 |
del msgs[-1] |
| 571 |
self.assertEqual('\n'.join(msgs), textwrap.dedent(expected).strip('\n')) |
| 572 |
|
| 573 |
@tempdir( |
| 574 |
files={ |
| 575 |
'test/foo.md': 'page1 content, [bar](bar.md)', |
| 576 |
'test/bar.md': 'page2 content, [baz](baz.md), [nonexistent](nonexistent.md)', |
| 577 |
'test/baz.md': 'page3 content, [foo](foo.md)', |
| 578 |
'.zoo.md': 'page4 content', |
| 579 |
} |
| 580 |
) |
| 581 |
@tempdir() |
| 582 |
def test_draft_pages_with_invalid_links(self, site_dir, docs_dir): |
| 583 |
cfg = load_config( |
| 584 |
docs_dir=docs_dir, |
| 585 |
site_dir=site_dir, |
| 586 |
use_directory_urls=False, |
| 587 |
draft_docs='ba*.md', |
| 588 |
) |
| 589 |
|
| 590 |
with self.subTest(serve_url=None): |
| 591 |
expected_logs = ''' |
| 592 |
INFO:Doc file 'test/foo.md' contains a link to 'test/bar.md' which is excluded from the built site. |
| 593 |
''' |
| 594 |
with self._assert_build_logs(expected_logs): |
| 595 |
build.build(cfg) |
| 596 |
self.assertPathIsFile(site_dir, 'test', 'foo.html') |
| 597 |
self.assertPathNotExists(site_dir, 'test', 'baz.html') |
| 598 |
self.assertPathNotExists(site_dir, '.zoo.html') |
| 599 |
|
| 600 |
serve_url = 'http://localhost:123/documentation/' |
| 601 |
with self.subTest(serve_url=serve_url): |
| 602 |
expected_logs = ''' |
| 603 |
INFO:Doc file 'test/bar.md' contains a link 'nonexistent.md', but the target 'test/nonexistent.md' is not found among documentation files. |
| 604 |
INFO:Doc file 'test/foo.md' contains a link to 'test/bar.md' which is excluded from the built site. |
| 605 |
INFO:The following pages are being built only for the preview but will be excluded from `mkdocs build` per `draft_docs` config: |
| 606 |
- http://localhost:123/documentation/test/bar.html |
| 607 |
- http://localhost:123/documentation/test/baz.html |
| 608 |
''' |
| 609 |
with self._assert_build_logs(expected_logs): |
| 610 |
build.build(cfg, serve_url=serve_url) |
| 611 |
|
| 612 |
foo_path = Path(site_dir, 'test', 'foo.html') |
| 613 |
self.assertTrue(foo_path.is_file()) |
| 614 |
self.assertNotIn('DRAFT', foo_path.read_text()) |
| 615 |
|
| 616 |
baz_path = Path(site_dir, 'test', 'baz.html') |
| 617 |
self.assertPathIsFile(baz_path) |
| 618 |
self.assertIn('DRAFT', baz_path.read_text()) |
| 619 |
|
| 620 |
self.assertPathNotExists(site_dir, '.zoo.html') |
| 621 |
|
| 622 |
@tempdir( |
| 623 |
files={ |
| 624 |
'foo/README.md': 'page1 content', |
| 625 |
'foo/index.md': 'page2 content', |
| 626 |
} |
| 627 |
) |
| 628 |
@tempdir() |
| 629 |
def test_conflicting_readme_and_index(self, site_dir, docs_dir): |
| 630 |
cfg = load_config(docs_dir=docs_dir, site_dir=site_dir, use_directory_urls=False) |
| 631 |
|
| 632 |
for serve_url in None, 'http://localhost:123/': |
| 633 |
with self.subTest(serve_url=serve_url): |
| 634 |
expected_logs = ''' |
| 635 |
WARNING:Excluding 'foo/README.md' from the site because it conflicts with 'foo/index.md'. |
| 636 |
''' |
| 637 |
with self._assert_build_logs(expected_logs): |
| 638 |
build.build(cfg, serve_url=serve_url) |
| 639 |
|
| 640 |
index_path = Path(site_dir, 'foo', 'index.html') |
| 641 |
self.assertPathIsFile(index_path) |
| 642 |
self.assertRegex(index_path.read_text(), r'page2 content') |
| 643 |
|
| 644 |
@tempdir( |
| 645 |
files={ |
| 646 |
'foo/README.md': 'page1 content', |
| 647 |
'foo/index.md': 'page2 content', |
| 648 |
} |
| 649 |
) |
| 650 |
@tempdir() |
| 651 |
def test_exclude_readme_and_index(self, site_dir, docs_dir): |
| 652 |
cfg = load_config( |
| 653 |
docs_dir=docs_dir, site_dir=site_dir, use_directory_urls=False, exclude_docs='index.md' |
| 654 |
) |
| 655 |
|
| 656 |
for serve_url in None, 'http://localhost:123/': |
| 657 |
with self.subTest(serve_url=serve_url): |
| 658 |
with self._assert_build_logs(''): |
| 659 |
build.build(cfg, serve_url=serve_url) |
| 660 |
|
| 661 |
index_path = Path(site_dir, 'foo', 'index.html') |
| 662 |
self.assertPathIsFile(index_path) |
| 663 |
self.assertRegex(index_path.read_text(), r'page1 content') |
| 664 |
|
| 665 |
@tempdir( |
| 666 |
files={ |
| 667 |
'test/foo.md': '## page1 heading\n\n[bar](bar.md#page1-heading)', |
| 668 |
'test/bar.md': '## page2 heading\n\n[aaa](#a)', |
| 669 |
} |
| 670 |
) |
| 671 |
@tempdir() |
| 672 |
def test_anchor_warning(self, site_dir, docs_dir): |
| 673 |
cfg = load_config(docs_dir=docs_dir, site_dir=site_dir, validation={'anchors': 'warn'}) |
| 674 |
|
| 675 |
expected_logs = ''' |
| 676 |
WARNING:Doc file 'test/bar.md' contains a link '#a', but there is no such anchor on this page. |
| 677 |
WARNING:Doc file 'test/foo.md' contains a link 'bar.md#page1-heading', but the doc 'test/bar.md' does not contain an anchor '#page1-heading'. |
| 678 |
''' |
| 679 |
with self._assert_build_logs(expected_logs): |
| 680 |
build.build(cfg) |
| 681 |
|
| 682 |
@tempdir( |
| 683 |
files={ |
| 684 |
'test/foo.md': '[bar](bar.md#heading1)', |
| 685 |
'test/bar.md': '## heading1', |
| 686 |
} |
| 687 |
) |
| 688 |
@tempdir() |
| 689 |
def test_anchor_no_warning(self, site_dir, docs_dir): |
| 690 |
cfg = load_config(docs_dir=docs_dir, site_dir=site_dir, validation={'anchors': 'warn'}) |
| 691 |
build.build(cfg) |
| 692 |
|
| 693 |
@tempdir( |
| 694 |
files={ |
| 695 |
'test/foo.md': '[bar](bar.md#heading2)', |
| 696 |
'test/bar.md': '## heading1\n\n<a id="heading2">hi</a>', |
| 697 |
} |
| 698 |
) |
| 699 |
@tempdir() |
| 700 |
def test_anchor_no_warning_with_html(self, site_dir, docs_dir): |
| 701 |
cfg = load_config( |
| 702 |
docs_dir=docs_dir, |
| 703 |
site_dir=site_dir, |
| 704 |
validation={'anchors': 'warn'}, |
| 705 |
markdown_extensions=['md_in_html'], |
| 706 |
) |
| 707 |
build.build(cfg) |
| 708 |
|
| 709 |
@tempdir( |
| 710 |
files={ |
| 711 |
'test/foo.md': '[bar1](bar.md?test#heading1), [bar2](bar.md?test#headingno), [bar3](bar.md?test), [self4](?test), [self5](?test#hi)', |
| 712 |
'test/bar.md': '## heading1', |
| 713 |
} |
| 714 |
) |
| 715 |
@tempdir() |
| 716 |
def test_anchor_warning_and_query(self, site_dir, docs_dir): |
| 717 |
cfg = load_config(docs_dir=docs_dir, site_dir=site_dir, validation={'anchors': 'info'}) |
| 718 |
|
| 719 |
expected_logs = ''' |
| 720 |
INFO:Doc file 'test/foo.md' contains a link 'bar.md?test#headingno', but the doc 'test/bar.md' does not contain an anchor '#headingno'. |
| 721 |
INFO:Doc file 'test/foo.md' contains a link '?test#hi', but there is no such anchor on this page. |
| 722 |
''' |
| 723 |
with self._assert_build_logs(expected_logs): |
| 724 |
build.build(cfg) |
| 725 |
|
| 726 |
@tempdir( |
| 727 |
files={ |
| 728 |
'test/foo.md': 'test[^1]\n\n[^1]: footnote1\n[^2]: footnote2', |
| 729 |
} |
| 730 |
) |
| 731 |
@tempdir() |
| 732 |
def test_anchor_warning_for_footnote(self, site_dir, docs_dir): |
| 733 |
cfg = load_config( |
| 734 |
docs_dir=docs_dir, |
| 735 |
site_dir=site_dir, |
| 736 |
validation={'anchors': 'info'}, |
| 737 |
markdown_extensions=['footnotes'], |
| 738 |
) |
| 739 |
|
| 740 |
expected_logs = ''' |
| 741 |
INFO:Doc file 'test/foo.md' contains a link '#fnref:2', but there is no such anchor on this page. This seems to be a footnote that is never referenced. |
| 742 |
''' |
| 743 |
with self._assert_build_logs(expected_logs): |
| 744 |
build.build(cfg) |
| 745 |
|
| 746 |
@tempdir( |
| 747 |
files={ |
| 748 |
'foo.md': 'page1 content', |
| 749 |
'bar.md': 'page2 content', |
| 750 |
} |
| 751 |
) |
| 752 |
@tempdir() |
| 753 |
@tempdir() |
| 754 |
def test_plugins_adding_files_and_interacting(self, tmp_dir, site_dir, docs_dir): |
| 755 |
def on_files_1(files: Files, config: MkDocsConfig) -> Files: |
| 756 |
# Plugin 1 generates a file. |
| 757 |
Path(tmp_dir, 'SUMMARY.md').write_text('foo.md\nbar.md\n') |
| 758 |
files.append(File('SUMMARY.md', tmp_dir, config.site_dir, config.use_directory_urls)) |
| 759 |
return files |
| 760 |
|
| 761 |
def on_files_2(files: Files, config: MkDocsConfig) -> None: |
| 762 |
# Plugin 2 reads that file and uses it to configure the nav. |
| 763 |
f = files.get_file_from_path('SUMMARY.md') |
| 764 |
assert f is not None |
| 765 |
config.nav = f.content_string.splitlines() |
| 766 |
|
| 767 |
for serve_url in None, 'http://localhost:123/': |
| 768 |
for exclude in 'full', 'drafts', 'nav', None: |
| 769 |
with self.subTest(serve_url=serve_url, exclude=exclude): |
| 770 |
cfg = load_config( |
| 771 |
docs_dir=docs_dir, |
| 772 |
site_dir=site_dir, |
| 773 |
use_directory_urls=False, |
| 774 |
exclude_docs='SUMMARY.md' if exclude == 'full' else '', |
| 775 |
draft_docs='SUMMARY.md' if exclude == 'drafts' else '', |
| 776 |
not_in_nav='SUMMARY.md' if exclude == 'nav' else '', |
| 777 |
) |
| 778 |
cfg.plugins.events['files'] += [on_files_1, on_files_2] |
| 779 |
|
| 780 |
expected_logs = '' |
| 781 |
if exclude is None: |
| 782 |
expected_logs = ''' |
| 783 |
INFO:The following pages exist in the docs directory, but are not included in the "nav" configuration: |
| 784 |
- SUMMARY.md |
| 785 |
''' |
| 786 |
if exclude == 'drafts' and serve_url: |
| 787 |
expected_logs = ''' |
| 788 |
INFO:The following pages are being built only for the preview but will be excluded from `mkdocs build` per `draft_docs` config: |
| 789 |
- http://localhost:123/SUMMARY.html |
| 790 |
''' |
| 791 |
with self._assert_build_logs(expected_logs): |
| 792 |
build.build(cfg, serve_url=serve_url) |
| 793 |
|
| 794 |
foo_path = Path(site_dir, 'foo.html') |
| 795 |
self.assertPathIsFile(foo_path) |
| 796 |
self.assertRegex( |
| 797 |
foo_path.read_text(), |
| 798 |
r'href="foo.html"[\s\S]+href="bar.html"', # Nav order is respected |
| 799 |
) |
| 800 |
|
| 801 |
summary_path = Path(site_dir, 'SUMMARY.html') |
| 802 |
if exclude == 'full': |
| 803 |
self.assertPathNotExists(summary_path) |
| 804 |
elif exclude == 'drafts' and not serve_url: |
| 805 |
self.assertPathNotExists(summary_path) |
| 806 |
else: |
| 807 |
self.assertPathExists(summary_path) |
| 808 |
|
| 809 |
@tempdir( |
| 810 |
files={ |
| 811 |
'README.md': 'CONFIG_README\n', |
| 812 |
'docs/foo.md': 'ROOT_FOO\n', |
| 813 |
'docs/test/bar.md': 'TEST_BAR\n', |
| 814 |
'docs/main/foo.md': 'MAIN_FOO\n', |
| 815 |
'docs/main/main.md': ( |
| 816 |
'--8<-- "README.md"\n\n' |
| 817 |
'--8<-- "foo.md"\n\n' |
| 818 |
'--8<-- "test/bar.md"\n\n' |
| 819 |
'--8<-- "../foo.md"\n\n' |
| 820 |
), |
| 821 |
} |
| 822 |
) |
| 823 |
def test_markdown_extension_with_relative(self, config_dir): |
| 824 |
for base_path, expected in { |
| 825 |
'!relative': ''' |
| 826 |
<p>(Failed to read 'README.md')</p> |
| 827 |
<p>MAIN_FOO</p> |
| 828 |
<p>(Failed to read 'test/bar.md')</p> |
| 829 |
<p>ROOT_FOO</p>''', |
| 830 |
'!relative $docs_dir': ''' |
| 831 |
<p>(Failed to read 'README.md')</p> |
| 832 |
<p>ROOT_FOO</p> |
| 833 |
<p>TEST_BAR</p> |
| 834 |
<p>(Failed to read '../foo.md')</p>''', |
| 835 |
'!relative $config_dir/docs': ''' |
| 836 |
<p>(Failed to read 'README.md')</p> |
| 837 |
<p>ROOT_FOO</p> |
| 838 |
<p>TEST_BAR</p> |
| 839 |
<p>(Failed to read '../foo.md')</p>''', |
| 840 |
'!relative $config_dir': ''' |
| 841 |
<p>CONFIG_README</p> |
| 842 |
<p>(Failed to read 'foo.md')</p> |
| 843 |
<p>(Failed to read 'test/bar.md')</p> |
| 844 |
<p>(Failed to read '../foo.md')</p>''', |
| 845 |
}.items(): |
| 846 |
with self.subTest(base_path=base_path): |
| 847 |
cfg = f''' |
| 848 |
site_name: test |
| 849 |
use_directory_urls: false |
| 850 |
markdown_extensions: |
| 851 |
- mkdocs.tests.build_tests: |
| 852 |
base_path: {base_path} |
| 853 |
''' |
| 854 |
config = base.load_config( |
| 855 |
io.StringIO(cfg), config_file_path=os.path.join(config_dir, 'mkdocs.yml') |
| 856 |
) |
| 857 |
|
| 858 |
with self._assert_build_logs(''): |
| 859 |
build.build(config) |
| 860 |
main_path = Path(config_dir, 'site', 'main', 'main.html') |
| 861 |
self.assertTrue(main_path.is_file()) |
| 862 |
self.assertIn(textwrap.dedent(expected), main_path.read_text()) |
| 863 |
|
| 864 |
# Test build.site_directory_contains_stale_files |
| 865 |
|
| 866 |
@tempdir(files=['index.html']) |
| 867 |
def test_site_dir_contains_stale_files(self, site_dir): |
| 868 |
self.assertTrue(build.site_directory_contains_stale_files(site_dir)) |
| 869 |
|
| 870 |
@tempdir() |
| 871 |
def test_not_site_dir_contains_stale_files(self, site_dir): |
| 872 |
self.assertFalse(build.site_directory_contains_stale_files(site_dir)) |
| 873 |
|
| 874 |
|
| 875 |
class _TestPreprocessor(markdown.preprocessors.Preprocessor): |
| 876 |
def __init__(self, base_path: str) -> None: |
| 877 |
self.base_path = base_path |
| 878 |
|
| 879 |
def run(self, lines: list[str]) -> list[str]: |
| 880 |
for i, line in enumerate(lines): |
| 881 |
if m := re.search(r'^--8<-- "(.+)"$', line): |
| 882 |
try: |
| 883 |
lines[i] = Path(self.base_path, m[1]).read_text() |
| 884 |
except OSError: |
| 885 |
lines[i] = f"(Failed to read {m[1]!r})\n" |
| 886 |
return lines |
| 887 |
|
| 888 |
|
| 889 |
class _TestExtension(markdown.extensions.Extension): |
| 890 |
def __init__(self, base_path: str) -> None: |
| 891 |
self.base_path = str(base_path) |
| 892 |
|
| 893 |
def extendMarkdown(self, md: markdown.Markdown) -> None: |
| 894 |
md.preprocessors.register(_TestPreprocessor(self.base_path), "mkdocs_test", priority=32) |
| 895 |
|
| 896 |
|
| 897 |
makeExtension = _TestExtension |