Add extension to remove YAML front matter from includes

Introduced a custom Sphinx extension 'include_remove_yaml' to strip YAML front matter from included files. Updated _config.yml to register the extension, and added YAML front matter to intro.md for demonstration.
This commit is contained in:
Dennis den Ouden-van der Horst 2026-01-04 17:41:29 +01:00
parent a4cb6d7090
commit 68cf7ca756
3 changed files with 40 additions and 0 deletions

View file

@ -11,6 +11,8 @@ html:
baseurl : "https://teachbooks.io/manual/"
sphinx:
local_extensions:
include_remove_yaml: _ext/
extra_extensions:
- sphinx_inline_tabs
- sphinx.ext.imgconverter

View file

@ -0,0 +1,33 @@
from __future__ import annotations
from sphinx.util import logging
logger = logging.getLogger(__name__)
def _strip_yaml(lines: list[str]) -> list[str]:
if not lines:
return lines
for ix,lines_set in enumerate(lines):
new_lines = lines_set.splitlines()
if new_lines[0].strip() != "---":
continue
# yaml front matter detected
# find closing delimiter
index_end = 1
for i in range(1, len(new_lines)):
tok = new_lines[i].strip()
if tok == "---":
index_end = i
break
new_lines = new_lines[index_end+1 :]
lines[ix] = "\n".join(new_lines)
return lines # no closing delimiter -> leave unchanged
def on_include_read(app, docname, path, content):
# content is a list[str]; modify in-place
new = _strip_yaml(content)
if new is not content:
content[:] = new
def setup(app):
app.connect("include-read", on_include_read)
return {}

View file

@ -1,3 +1,8 @@
---
file_format: mystnb
kernelspec:
name: python3
---
# TeachBooks Manual
This manual is primarily designed for and by teachers for use in education, but should be a useful resource for anyone interested in creating and collaborating on a TeachBook: a <a href="https://jupyterbook.org/v1/"><img style="display:inline-block; height:1.5em; width:auto; transform:translate(0, -0.15em)" src="images/logo-wide.svg" alt="Jupyter book v1"> v1</a> using the <a href="https://www.sphinx-doc.org/"><img style="display:inline-block; height:1.5em; width:auto; transform:translate(0, -0.15em)" src="images/sphinx-logo.svg" alt="Sphinx logo"> Sphinx backend</a> with additional editing tools and book features. Our aim is to provide a simple way to start book-making for new users (it only takes 10 clicks!), ease the editing process for anyone, and provide additional features developed for education-purposes. We hope you find this resource useful and refer back to it often.