diff --git a/src/sphinxnotes/jinja/context.py b/src/sphinxnotes/jinja/context.py index 65a8029..1e2dc7a 100644 --- a/src/sphinxnotes/jinja/context.py +++ b/src/sphinxnotes/jinja/context.py @@ -27,6 +27,7 @@ if TYPE_CHECKING: from sphinx.application import Sphinx + from sphinx.environment import BuildEnvironment logger = logging.getLogger(__name__) @@ -39,17 +40,17 @@ def gen(self) -> dict[str, Any]: _registry: dict[str, ContextGenerator] = {} class SphinxContext(ContextGenerator): - _app: Sphinx + _env: BuildEnvironment - def __init__(self, app: Sphinx): - self._app = app + def __init__(self, env: BuildEnvironment): + self._env = env def gen(self) -> dict[str, Any]: return { - 'app': self._app, - 'env': self._app.env, - 'cfg': self._app.config, - 'builder': self._app.builder, + 'app': self._env.app, + 'env': self._env, + 'cfg': self._env.config, + 'builder': self._env.app.builder, } class DocContext(ContextGenerator): @@ -118,3 +119,18 @@ def gen(self) -> dict[str, Any]: 'content': self._role.text, } +def _load_single_ctx(env: BuildEnvironment, ctxname: str) -> dict[str, Any]: + if ctxname == 'sphinx': + return SphinxContext(env).gen() + elif ctxname == 'doc': + return DocContext(env).gen() + + +def load_and_fuse( + buildenv: BuildEnvironment, + fuse_ctxs: list[str], + separate_ctxs: list[str], + allow_duplicate: bool = False) -> dict[str, Any]: + + + diff --git a/src/sphinxnotes/jinja/directives.py b/src/sphinxnotes/jinja/directives.py index dd5e120..70106fc 100644 --- a/src/sphinxnotes/jinja/directives.py +++ b/src/sphinxnotes/jinja/directives.py @@ -42,3 +42,20 @@ def run(self) -> list[Node]: return self.parse_text_to_nodes(text, allow_section_headings=True) +class TemplateDirective(SphinxDirective): + required_arguments = 0 + optional_arguments = 10 + final_argument_whitespace = False + option_spec = {} + has_content = True + + def run(self) -> list[nodes.Node]: + ctx = {} + for ctxname in self.arguments: + ctx = { + ctxname: context.load(ctxname) + } + + text = template.render(self, ctx) + + return self.parse_text_to_nodes(text, allow_section_headings=True) diff --git a/src/sphinxnotes/jinja/template.py b/src/sphinxnotes/jinja/template.py index 0035dfd..f0e9fdc 100644 --- a/src/sphinxnotes/jinja/template.py +++ b/src/sphinxnotes/jinja/template.py @@ -38,10 +38,10 @@ def render(obj: SphinxDirective | SphinxRole, ctx: dict[str, Any]): class TemplateDirective(SphinxDirective): required_arguments = 0 - optional_arguments = 1 + optional_arguments = 10 final_argument_whitespace = False option_spec = { - 'extra': directives.unchanged, + 'ctxs': directives.unchanged, } has_content = True