@@ -277,6 +277,198 @@ at the bottom. You can hide these buttons with the following configuration:
277
277
}
278
278
279
279
280
+ Add a dropdown to switch between docs versions
281
+ ==============================================
282
+
283
+ You can add a button to your site that allows users to
284
+ switch between versions of your documentation. The links in the version
285
+ switcher will differ depending on which page of the docs is being viewed. For
286
+ example, on the page ``https://mysite.org/en/v2.0/changelog.html ``, the
287
+ switcher links will go to ``changelog.html `` in the other versions of your
288
+ docs. When clicked, the switcher will check for the existence of that page, and
289
+ if it doesn't exist, redirect to the homepage of that docs version instead.
290
+
291
+ The switcher requires the following configuration steps:
292
+
293
+ 1. Add a JSON file containing a list of the documentation versions that the
294
+ switcher should show on each page.
295
+
296
+ 2. Add a configuration dictionary called ``switcher `` to the
297
+ ``html_theme_options `` dict in ``conf.py ``. ``switcher `` should have 3 keys:
298
+
299
+ - ``json_url ``: the persistent location of the JSON file described above.
300
+ - ``url_template ``: a template string used to generate the correct URLs for
301
+ the different documentation versions.
302
+ - ``version_match ``: a string stating the version of the documentation that
303
+ is currently being browsed.
304
+
305
+ 3. Specify where to place the switcher in your page layout. For example, add
306
+ the ``"version-switcher" `` template to one of the layout lists in
307
+ ``html_theme_options `` (e.g., ``navbar_end ``, ``footer_items ``, etc).
308
+
309
+ Below is a more in-depth description of each of these configuration steps.
310
+
311
+
312
+ Add a JSON file to define your switcher's versions
313
+ --------------------------------------------------
314
+
315
+ First, write a JSON file stating which versions of your docs will be listed in
316
+ the switcher's dropdown menu. That file should contain a list of entries that
317
+ each have one or two fields:
318
+
319
+ - ``version ``: a version string. This will be inserted into
320
+ ``switcher['url_template'] `` to create the links to other docs versions, and
321
+ also checked against ``switcher['version_match'] `` to provide styling to the
322
+ switcher.
323
+ - ``name ``: an optional name to display in the switcher dropdown instead of the
324
+ version string (e.g., "latest", "stable", "dev", etc).
325
+
326
+ Here is an example JSON file:
327
+
328
+ .. code :: json
329
+
330
+ [
331
+ {
332
+ "name" : " v2.1 (stable)" ,
333
+ "version" : " 2.1"
334
+ },
335
+ {
336
+ "version" : " 2.0"
337
+ },
338
+ {
339
+ "version" : " 1.0"
340
+ },
341
+ ]
342
+
343
+ See the discussion of ``switcher['json_url'] `` (below) for options of where to
344
+ save the JSON file.
345
+
346
+
347
+ Configure ``switcher['json_url'] ``
348
+ ----------------------------------
349
+
350
+ The JSON file needs to be at a stable, persistent, fully-resolved URL (i.e.,
351
+ not specified as a path relative to the sphinx root of the current doc build).
352
+ Each version of your documentation should point to the same URL, so that as new
353
+ versions are added to the JSON file all the older versions of the docs will
354
+ gain switcher dropdown entries linking to the new versions. This could be done
355
+ a few different ways:
356
+
357
+ - The location could be one that is always associated with the most recent
358
+ documentation build (i.e., if your docs server aliases "latest" to the most
359
+ recent version, it could point to a location in the build tree of version
360
+ "latest"). For example:
361
+
362
+ .. code :: python
363
+
364
+ html_theme_options = {
365
+ ... ,
366
+ " switcher" : {
367
+ " json_url" : " https://mysite.org/en/latest/_static/switcher.json" ,
368
+ }
369
+ }
370
+
371
+ In this case the JSON is versioned alongside the rest of the docs pages but
372
+ only the most recent version is ever loaded (even by older versions of the
373
+ docs).
374
+
375
+ - The JSON could be saved in a folder that is listed under your site's
376
+ ``html_static_path `` configuration. See `the Sphinx static path documentation
377
+ <https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-html_static_path> `_
378
+ for more information.
379
+
380
+ - The JSON could be stored outside the doc build trees. This probably means it
381
+ would be outside the software repo, and would require you to add new version
382
+ entries to the JSON file manually as part of your release process. Example:
383
+
384
+ .. code :: python
385
+
386
+ html_theme_options = {
387
+ ... ,
388
+ " switcher" : {
389
+ " json_url" : " https://mysite.org/switcher.json" ,
390
+ }
391
+ }
392
+
393
+
394
+ Configure ``switcher['url_template'] ``
395
+ --------------------------------------
396
+
397
+ The switcher's links to other versions of your docs are made by combining the
398
+ *version strings * from the JSON file with a *template string * you provide in
399
+ ``switcher['url_template'] ``. The template string must contain a placeholder
400
+ ``{version} `` and otherwise be a fully-resolved URL. For example:
401
+
402
+ .. code :: python
403
+
404
+ html_theme_options = {
405
+ ... ,
406
+ " switcher" : {
407
+ " url_template" : " https://mysite.org/en/version-{version} /" ,
408
+ }
409
+ }
410
+
411
+ The example above will result in a link to
412
+ ``https://mysite.org/en/version-1.0/ `` for the JSON entry for version
413
+ ``"1.0" ``.
414
+
415
+
416
+ Configure ``switcher['version_match'] ``
417
+ ---------------------------------------
418
+
419
+ This configuration value tells the switcher what docs version is currently
420
+ being viewed, and is used to style the switcher (i.e., to highlight the current
421
+ docs version in the switcher's dropdown menu, and to change the text displayed
422
+ on the switcher button).
423
+
424
+ Typically you can re-use one of the sphinx variables ``version ``
425
+ or ``release `` as the value of ``switcher['version_match'] ``; which one you use
426
+ depends on how granular your docs versioning is. See
427
+ `the Sphinx "project info" documentation
428
+ <https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information> `__
429
+ for more information). Example:
430
+
431
+ .. code :: python
432
+
433
+ version = my_package_name.__version__ .replace(" dev0" , " " ) # may differ
434
+ html_theme_options = {
435
+ ... ,
436
+ " switcher" : {
437
+ " version_match" : version,
438
+ }
439
+ }
440
+
441
+
442
+ Specify where to display the switcher
443
+ -------------------------------------
444
+
445
+ Finally, tell the theme where on your site's pages you want the switcher to
446
+ appear. There are many choices here: you can add ``"version-switcher" `` to one
447
+ of the locations in ``html_theme_options `` (e.g., ``navbar_end ``,
448
+ ``footer_items ``, etc). For example:
449
+
450
+ .. code :: python
451
+
452
+ html_theme_options = {
453
+ ... ,
454
+ " navbar_end" : [" version-switcher" ]
455
+ }
456
+
457
+
458
+ Alternatively, you could override one of the other templates to include the
459
+ version switcher in a sidebar. For example, you could define
460
+ ``_templates/sidebar-nav-bs.html `` as:
461
+
462
+ .. code :: jinja
463
+
464
+ {%- include 'version-switcher.html' -%}
465
+ {{ super() }}
466
+
467
+ to insert a version switcher at the top of the left sidebar, while still
468
+ keeping the default navigation below it. See :doc: `sections ` for more
469
+ information.
470
+
471
+
280
472
Add an Edit this Page button
281
473
============================
282
474
0 commit comments