-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathhooks_ads.py
More file actions
35 lines (30 loc) · 1.16 KB
/
hooks_ads.py
File metadata and controls
35 lines (30 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""Ищет файл .ads в папке страницы или выше по дереву docs и передаёт HTML в шаблон."""
from pathlib import Path
def on_page_context(context, page, config, nav):
folder_ads_html = ""
try:
f = page.file
abs_src = getattr(f, "abs_src_path", None) if f is not None else None
if abs_src:
src = Path(abs_src).resolve()
docs_dir = Path(config.docs_dir).resolve()
current = src.parent
while True:
try:
current.relative_to(docs_dir)
except ValueError:
break
ads_file = current / ".ads"
if ads_file.is_file():
folder_ads_html = ads_file.read_text(encoding="utf-8")
break
if current == docs_dir:
break
parent = current.parent
if parent == current:
break
current = parent
except (AttributeError, OSError, TypeError, ValueError):
pass
context["folder_ads_html"] = folder_ads_html
return context