Skip to content

Commit 0b6ea52

Browse files
committed
feat: ExtraContext register and mangement
1 parent 42d2378 commit 0b6ea52

7 files changed

Lines changed: 408 additions & 151 deletions

File tree

docs/index.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,23 @@ Introduction
3636

3737
__ https://sphinx.silverrainz.me/any/
3838

39+
.. template::
40+
:on: parsed
41+
:debug:
42+
43+
I am {{ name }}. I have the following attributes:
44+
45+
{% for k, v in attrs.items() %}
46+
:{{ k }}: {{ v }}
47+
{%- endfor %}
48+
49+
This document "_doc.title" has {{ _doc.sections | length }} section(s).
50+
51+
{{ content }}
52+
53+
.. data:: Shengyu Zhang
54+
:github: SilverRainZ
55+
:homepage: https://silverrainz.me/
3956

4057
.. INTRODUCTION END
4158

src/sphinxnotes/data/extra_contexts.py

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/sphinxnotes/data/extractx.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from __future__ import annotations
2+
from typing import TYPE_CHECKING, override
3+
4+
from sphinx.util.docutils import SphinxDirective, SphinxRole
5+
from sphinx.transforms import SphinxTransform
6+
7+
from .utils import find_current_document, find_current_section
8+
from .utils.ctxproxy import proxy
9+
from .render import EXTRACTX_REGISTRY, ParsePhaseContextGenerator, FullPhaseContextGenerator, ParseCaller, Caller, pending_node
10+
from .template import Context
11+
12+
13+
class MarkupContextGenerator(ParsePhaseContextGenerator):
14+
@override
15+
def generate(self, caller: ParseCaller, n: pending_node) -> Context:
16+
isdir = isinstance(caller, SphinxDirective)
17+
return {
18+
'type': 'directive' if isdir else 'role',
19+
'name': caller.name,
20+
'lineno': caller.lineno,
21+
'rawtext': caller.block_text if isdir else caller.rawtext,
22+
}
23+
24+
class DocContextGenerator(FullPhaseContextGenerator):
25+
@override
26+
def generate(self, caller: Caller, n: pending_node) -> Context:
27+
if isinstance(caller, SphinxDirective):
28+
return proxy(caller.state.document)
29+
elif isinstance(caller, SphinxRole):
30+
return proxy(caller.inliner.document)
31+
elif isinstance(caller, SphinxTransform):
32+
return proxy(caller.document)
33+
else:
34+
assert False
35+
36+
37+
class SectionContextGenerator(FullPhaseContextGenerator):
38+
@override
39+
def generate(self, caller: Caller, n: pending_node) -> Context:
40+
if n.parent:
41+
return proxy(find_current_section(n.parent))
42+
elif isinstance(caller, SphinxDirective):
43+
return proxy(find_current_section(caller.state.parent))
44+
elif isinstance(caller, SphinxRole):
45+
return proxy(caller.inliner.parent)
46+
else:
47+
assert False
48+
49+
50+
class SphinxEnvContextGenerator(FullPhaseContextGenerator):
51+
@override
52+
def generate(self, caller: Caller, n: pending_node) -> Context:
53+
return proxy(caller.env)
54+
55+
class SphinxConfigContextGenerator(FullPhaseContextGenerator):
56+
@override
57+
def generate(self, caller: Caller, n: pending_node) -> Context:
58+
return proxy(caller.config)
59+
60+
EXTRACTX_REGISTRY.add_parsing_phase_context('markup', MarkupContextGenerator())
61+
EXTRACTX_REGISTRY.add_full_phase_context('doc', DocContextGenerator())
62+
EXTRACTX_REGISTRY.add_full_phase_context('section',SectionContextGenerator())
63+
EXTRACTX_REGISTRY.add_full_phase_context('env', SphinxEnvContextGenerator())
64+
EXTRACTX_REGISTRY.add_full_phase_context('config', SphinxConfigContextGenerator())

0 commit comments

Comments
 (0)