|
a |
|
b/hooks/parent_snippets.py |
|
|
1 |
import re |
|
|
2 |
|
|
|
3 |
|
|
|
4 |
def on_page_markdown(markdown, page, **kwargs): |
|
|
5 |
""" |
|
|
6 |
parent_snippet markers are snippets that are intended to be replaced in the parent |
|
|
7 |
site with appropriate snippet notation. The snippets themselves do not live in |
|
|
8 |
this repo. |
|
|
9 |
|
|
|
10 |
on_page_* methods are called for each Page in a mkdocs site and can modify the |
|
|
11 |
markdown they are given as input. We're using this method to look for the |
|
|
12 |
parent_includes markers and replace them with a note box that indicates in the |
|
|
13 |
built docs that this snippet will be replaced in the full docs build. |
|
|
14 |
|
|
|
15 |
For example: |
|
|
16 |
!!! parent_snippet:'includes/glossary.md' |
|
|
17 |
|
|
|
18 |
will be replaced with: |
|
|
19 |
!!! note "TO BE REPLACED IN FULL DOCS BUILD |
|
|
20 |
This snippet will be replaced in the main docs with the parent file 'includes/glossary.md' |
|
|
21 |
|
|
|
22 |
This allows docs imported from other repos (e.g. ehrql) to reference snippets |
|
|
23 |
in the parent docs, such as the glossary. |
|
|
24 |
""" |
|
|
25 |
parent_snippets = set(re.findall(r"!!! parent_snippet:.+\n", markdown)) |
|
|
26 |
for parent_snippet in parent_snippets: |
|
|
27 |
markdown = markdown.replace( |
|
|
28 |
parent_snippet, |
|
|
29 |
'\n\n!!! note "TO BE REPLACED IN FULL DOCS BUILD"\n\n\tThis snippet will be replaced in the main docs ' |
|
|
30 |
f"with the parent file {parent_snippet.lstrip('!!! parent_snippet:')}", |
|
|
31 |
) |
|
|
32 |
return markdown |