Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverRainZ committed Oct 9, 2024
1 parent 03494b4 commit 101340e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
30 changes: 23 additions & 7 deletions src/sphinxnotes/jinja/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

if TYPE_CHECKING:
from sphinx.application import Sphinx
from sphinx.environment import BuildEnvironment


logger = logging.getLogger(__name__)
Expand All @@ -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):
Expand Down Expand Up @@ -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]:



17 changes: 17 additions & 0 deletions src/sphinxnotes/jinja/directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions src/sphinxnotes/jinja/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 101340e

Please sign in to comment.