Skip to content

Commit a04c547

Browse files
committed
chore: Some tweaks
1 parent 450417f commit a04c547

4 files changed

Lines changed: 31 additions & 29 deletions

File tree

docs/api.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ See :doc:`tmpl` for built-in extra-context names such as ``doc`` and
8383

8484
.. autofunction:: sphinxnotes.render.extra_context
8585

86-
.. autoclass:: sphinxnotes.render.ExtraContext
87-
8886
.. autoclass:: sphinxnotes.render.ParsingPhaseExtraContext
8987
:members: generate
9088
:undoc-members:

docs/tmpl.rst

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,17 @@ Extra Context
154154
-------------
155155

156156
Templates can access additional context through **extra context**. Extra context
157-
must be explicitly declared using the ``:extra:`` option and loaded in the
157+
must be explicitly declared using the :rst:dir:`templat:extra` option and loaded in the
158158
template using the ``load_extra()`` function.
159159

160-
Built-in extra context
161-
......................
160+
Built-in Extra Contexts
161+
.......................
162162

163163
.. list-table::
164164
:header-rows: 1
165165

166166
* - Name
167-
- Available in Pahses
167+
- Available in Phases
168168
- Description
169169
* - ``sphinx``
170170
- all
@@ -237,16 +237,16 @@ Each :py:class:`~sphinxnotes.render.Template` has a render phase controlled by
237237

238238
.. data.render::
239239
:on: parsing
240-
:extra: doc sphinx
240+
:extra: doc env
241241
242242
{% set doc = load_extra('doc') %}
243-
{% set sphinx = load_extra('sphinx') %}
243+
{% set env = load_extra('env') %}
244244
245245
- The current document has
246246
{{ doc.sections | length }}
247247
section(s).
248248
- The current project has
249-
{{ sphinx.env.all_docs | length }}
249+
{{ env.all_docs | length }}
250250
document(s).
251251
252252
``parsed``
@@ -261,41 +261,41 @@ Each :py:class:`~sphinxnotes.render.Template` has a render phase controlled by
261261

262262
.. data.render::
263263
:on: parsed
264-
:extra: doc sphinx
264+
:extra: doc env
265265
266266
{% set doc = load_extra('doc') %}
267-
{% set sphinx = load_extra('sphinx') %}
267+
{% set env = load_extra('env') %}
268268
269269
- The current document has
270270
{{ doc.sections | length }}
271271
section(s).
272272
- The current project has
273-
{{ sphinx.env.all_docs | length }}
273+
{{ env.all_docs | length }}
274274
document(s).
275275
276276
``resolving``
277277
Corresponding to :py:data:`sphinxnotes.render.Phase.Resolving`.
278278
Render late in the build, after references and other transforms are being
279279
resolved.
280280

281-
Choose this when the template depends on project-wide state or on document
281+
Choose this when the template depends on pr
282282
structure that is only stable near the end of the pipeline.
283283

284284
.. example::
285285
:style: grid
286286

287287
.. data.render::
288288
:on: resolving
289-
:extra: doc sphinx
289+
:extra: doc env
290290
291291
{% set doc = load_extra('doc') %}
292-
{% set sphinx = load_extra('sphinx') %}
292+
{% set env = load_extra('env') %}
293293
294294
- The current document has
295295
{{ doc.sections | length }}
296296
section(s).
297297
- The current project has
298-
{{ sphinx.env.all_docs | length }}
298+
{{ env.all_docs | length }}
299299
document(s).
300300
301301
Debugging

src/sphinxnotes/render/extractx.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from __future__ import annotations
2-
from typing import TYPE_CHECKING, override
2+
from typing import TYPE_CHECKING, ClassVar, override
33
from abc import ABC, abstractmethod
44

55
from sphinx.util.docutils import SphinxDirective, SphinxRole
@@ -156,6 +156,8 @@ class ExtraContextGenerator:
156156
todo: set[str]
157157
report: Report
158158

159+
env: ClassVar[BuildEnvironment]
160+
159161
def __init__(self, node: pending_node) -> None:
160162
self.node = node
161163
self.report = Report(
@@ -243,14 +245,12 @@ def generate(self, directive: SphinxDirective | SphinxRole) -> Any:
243245

244246

245247
@extra_context('sphinx')
246-
class SphinxExtraContext(GlobalExtraContext):
247-
app: Sphinx
248-
248+
class SphinxAppExtraContext(GlobalExtraContext):
249249
@override
250250
def generate(self, env: BuildEnvironment) -> Any:
251251
from .utils.ctxproxy import proxy
252252

253-
return proxy(self.app)
253+
return proxy(env.app)
254254

255255

256256
@extra_context('env')
@@ -276,4 +276,4 @@ def generate(self, env: BuildEnvironment) -> Any:
276276

277277

278278
def setup(app: Sphinx):
279-
SphinxExtraContext.app = app
279+
...

src/sphinxnotes/render/pipeline.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from abc import abstractmethod, ABC
66

77
from docutils import nodes
8+
from sphinx.environment import BuildEnvironment
89
from sphinx.util import logging
910
from sphinx.util.docutils import SphinxDirective, SphinxRole
1011
from sphinx.transforms import SphinxTransform
@@ -91,7 +92,7 @@ def queue_context(
9192
return pending
9293

9394
@final
94-
def render_queue(self, app: Sphinx) -> list[pending_node]:
95+
def render_queue(self) -> list[pending_node]:
9596
"""
9697
Try rendering all pending nodes in queue.
9798
@@ -121,10 +122,13 @@ def render_queue(self, app: Sphinx) -> list[pending_node]:
121122
ns.append(pending)
122123
continue
123124

124-
# Generate global extra context for later use.
125-
ExtraContextGenerator(pending).on_anytime(app.env)
126125

127126
host = cast(Host, self)
127+
128+
# Generate global extra context for later use.
129+
ExtraContextGenerator(pending).on_anytime(host.env)
130+
131+
# Perform render.
128132
pending.render(host)
129133

130134
if pending.parent is None:
@@ -205,7 +209,7 @@ def run(self) -> list[nodes.Node]:
205209
self.queue_context(self.current_context(), self.current_template())
206210

207211
ns = []
208-
for x in self.render_queue(self.env.app):
212+
for x in self.render_queue():
209213
if not x.rendered:
210214
ns.append(x)
211215
continue
@@ -233,7 +237,7 @@ def run(self) -> tuple[list[nodes.Node], list[nodes.system_message]]:
233237
pending.inline = True
234238

235239
ns, msgs = [], []
236-
for n in self.render_queue(self.env.app):
240+
for n in self.render_queue():
237241
if not n.rendered:
238242
ns.append(n)
239243
continue
@@ -258,7 +262,7 @@ def apply(self, **kwargs):
258262
for pending in self.document.findall(pending_node):
259263
self.queue_pending_node(pending)
260264

261-
for n in self.render_queue(self.app):
265+
for n in self.render_queue():
262266
...
263267

264268

@@ -275,7 +279,7 @@ def process_pending_node(self, n: pending_node) -> bool:
275279
def apply(self, **kwargs):
276280
for pending in self.document.findall(pending_node):
277281
self.queue_pending_node(pending)
278-
ns = self.render_queue(self.app)
282+
ns = self.render_queue()
279283

280284
# NOTE: Should no node left.
281285
assert len(ns) == 0

0 commit comments

Comments
 (0)