API References¶
Roles and Directives¶
See also
For a minimal end-to-end example of creating your own directive, start with Extending Directives/Roles.
Base Role Classes¶
- class sphinxnotes.render.BaseContextRole[source]¶
Bases:
BaseContextSource,SphinxRoleThis class generates
sphinxnotes.render.pending_nodeinSphinxRole.run()method and makes sure it is rendered correctly.User should implement
current_contextandcurrent_templatemethods to provide the constructor parameters ofpending_node.- process_pending_node(n)[source]¶
This method is called when it is the pending node’s turn to be rendered.
Return
trueif you want to render the pending node now; otherwise it will be inserted into the doctree directly and wait for later rendering (and this method will be called again.).You can add hooks to the pending node here, or call
queue_pending_node(). You are responsible for inserting created nodes into the doctree yourself.Note
Please always call
super().process_pending_node(n)to ensure the extension functions properly.- Parameters:
n (pending_node)
- Return type:
- abstractmethod current_context()¶
Return the context to be rendered.
- Return type:
PendingContext | ResolvedContext
- abstractmethod current_template()¶
Return the template for rendering the context.
This method should be implemented to provide the Jinja2 template that will render the context into markup text. The template determines the phase at which rendering occurs.
- Returns:
The template to use for rendering.
- Return type:
- queue_context(ctx, tmpl)¶
A helper method for
queue_pending_node.- Parameters:
ctx (PendingContext | ResolvedContext)
tmpl (Template)
- Return type:
- queue_pending_node(n)¶
Push back a new
pending_nodeto the render queue.- Parameters:
n (pending_node)
- Return type:
None
- class sphinxnotes.render.BaseDataDefineRole[source]¶
Bases:
BaseRawDataSource,BaseContextRole- abstractmethod current_schema()¶
Return the schema for constraining the generated :py:class`~sphinxnotes.render.RawData`. see Templating for more details.
- Return type:
- abstractmethod current_template()¶
Return the template for rendering the context.
This method should be implemented to provide the Jinja2 template that will render the context into markup text. The template determines the phase at which rendering occurs.
- Returns:
The template to use for rendering.
- Return type:
- process_pending_node(n)¶
This method is called when it is the pending node’s turn to be rendered.
Return
trueif you want to render the pending node now; otherwise it will be inserted into the doctree directly and wait for later rendering (and this method will be called again.).You can add hooks to the pending node here, or call
queue_pending_node(). You are responsible for inserting created nodes into the doctree yourself.Note
Please always call
super().process_pending_node(n)to ensure the extension functions properly.- Parameters:
n (pending_node)
- Return type:
- queue_context(ctx, tmpl)¶
A helper method for
queue_pending_node.- Parameters:
ctx (PendingContext | ResolvedContext)
tmpl (Template)
- Return type:
- queue_pending_node(n)¶
Push back a new
pending_nodeto the render queue.- Parameters:
n (pending_node)
- Return type:
None
Base Directive Classes¶
- class sphinxnotes.render.BaseContextDirective(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine)[source]¶
Bases:
BaseContextSource,SphinxDirectiveThis class generates
sphinxnotes.render.pending_nodeinSphinxDirective.run()method and makes sure it is rendered correctly.User should implement
current_contextandcurrent_templatemethods to provide the constructor parameters ofpending_node.- abstractmethod current_context()¶
Return the context to be rendered.
- Return type:
PendingContext | ResolvedContext
- abstractmethod current_template()¶
Return the template for rendering the context.
This method should be implemented to provide the Jinja2 template that will render the context into markup text. The template determines the phase at which rendering occurs.
- Returns:
The template to use for rendering.
- Return type:
- process_pending_node(n)¶
This method is called when it is the pending node’s turn to be rendered.
Return
trueif you want to render the pending node now; otherwise it will be inserted into the doctree directly and wait for later rendering (and this method will be called again.).You can add hooks to the pending node here, or call
queue_pending_node(). You are responsible for inserting created nodes into the doctree yourself.Note
Please always call
super().process_pending_node(n)to ensure the extension functions properly.- Parameters:
n (pending_node)
- Return type:
- queue_context(ctx, tmpl)¶
A helper method for
queue_pending_node.- Parameters:
ctx (PendingContext | ResolvedContext)
tmpl (Template)
- Return type:
- queue_pending_node(n)¶
Push back a new
pending_nodeto the render queue.- Parameters:
n (pending_node)
- Return type:
None
- class sphinxnotes.render.BaseDataDefineDirective(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine)[source]¶
Bases:
BaseRawDataSource,BaseContextDirectiveUser is responsible to implement
current_schemamethod.- current_raw_data()[source]¶
Return the
RawDatagenerating from from directive’s arguments, options, and content, and then it will be parsed bySchemareturned fromcurrent_schemamethod.See Main Context for more details.
Note
In most cases, the default implementation works well and you don’t need to override it.
- Return type:
- abstractmethod current_schema()¶
Return the schema for constraining the generated :py:class`~sphinxnotes.render.RawData`. see Templating for more details.
- Return type:
- abstractmethod current_template()¶
Return the template for rendering the context.
This method should be implemented to provide the Jinja2 template that will render the context into markup text. The template determines the phase at which rendering occurs.
- Returns:
The template to use for rendering.
- Return type:
- process_pending_node(n)¶
This method is called when it is the pending node’s turn to be rendered.
Return
trueif you want to render the pending node now; otherwise it will be inserted into the doctree directly and wait for later rendering (and this method will be called again.).You can add hooks to the pending node here, or call
queue_pending_node(). You are responsible for inserting created nodes into the doctree yourself.Note
Please always call
super().process_pending_node(n)to ensure the extension functions properly.- Parameters:
n (pending_node)
- Return type:
- queue_context(ctx, tmpl)¶
A helper method for
queue_pending_node.- Parameters:
ctx (PendingContext | ResolvedContext)
tmpl (Template)
- Return type:
- queue_pending_node(n)¶
Push back a new
pending_nodeto the render queue.- Parameters:
n (pending_node)
- Return type:
None
- class sphinxnotes.render.StrictDataDefineDirective(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine)[source]¶
Bases:
BaseDataDefineDirective- classmethod derive(name, schema, tmpl)[source]¶
Dynamically derive a new directive class from schema and template.
This method generates a new
StrictDataDefineDirectivesubclass with the given schema and template. It automatically sets the appropriate argument counts, option specifications, and content handling based on the schema definition.- Parameters:
- Return type:
Node¶
- class sphinxnotes.render.pending_node(ctx, tmpl, inline=False, rawsource='', *children, **attributes)[source]¶
A docutils node to be rendered.
- Parameters:
ctx (PendingContext | ResolvedContext)
tmpl (Template)
inline (bool)
Context¶
Context refers to the dynamic content of a Jinja template. It can be:
ResolvedContext:Our dedicated data type (
sphinxnotes.render.ParsedData), or any Pythondict.PendingContext:Context that is not yet available. For example, it may contain
unparsed data, remote data, and more.PendingContextcan be resolved toResolvedContextby callingresolve().
- type sphinxnotes.render.ResolvedContext = ParsedData | dict[str, Any]¶
- class sphinxnotes.render.PendingContext[source]¶
An abstract representation of context that is not currently available.
- abstractmethod resolve()[source]¶
This method will be called when rendering to get the available
ResolvedContext.- Return type:
ResolvedContext
PendingContext Implementations¶
- class sphinxnotes.render.UnparsedData(raw, schema)[source]¶
Bases:
PendingContextA pending context which contains raw data and its schema.
Raw data will be parsed when calling
resolve.
Template¶
See Templating for the higher-level guide.
- class sphinxnotes.render.Template(text: 'str', phase: 'Phase' = <Phase.Parsing: 'parsing'>, debug: 'bool' = False, extra: 'list[str]' = <factory>)[source]¶
- class sphinxnotes.render.Phase(*values)[source]¶
The phase of rendering template.
- Parsing = 'parsing'¶
Render template on document parsing on (
SphinxDirective.run()orSphinxRole.run()).
- Parsed = 'parsed'¶
Render template immediately after document has parsed (on Sphinx event
doctree-read).
- Resolving = 'resolving'¶
Render template immediately after all documents have resolving (before Sphinx event
doctree-resolved).
Extra Context¶
See Templating for built-in extra-context names such as doc and
sphinx, plus usage examples.
- @sphinxnotes.render.extra_context[source]¶
Decorator to register an extra context.
The phase is determined by which ExtraContext class is used:
GlobalExtraContextavailable in all phases
ParsingPhaseExtraContextavailable during Parsing phase
ParsedPhaseExtraContextavailable during Parsed phase
ResolvingPhaseExtraContextavailable during Resolving phase
Example:
@extra_context('doc') class DocExtraContext(ParsingPhaseExtraContext): def generate(self, ctx): return proxy(HostWrapper(ctx).doctree)
- Parameters:
name (str) – The context name, used in templates via
load_extra('name').
- class sphinxnotes.render.ParsingPhaseExtraContext[source]¶
Extra context generated during the
Parsingphase. Thegeneratemethod receives the current directive or role being executed.
- class sphinxnotes.render.ParsedPhaseExtraContext[source]¶
Extra context generated during the
Parsedphase. Thegeneratemethod receives the current Sphinx transform.
Filters¶
Data, Field and Schema¶
- class sphinxnotes.render.RawData(name: 'str | None', attrs: 'dict[str, str]', content: 'str | None')[source]¶
- class sphinxnotes.render.ParsedData(name: 'Value', attrs: 'dict[str, Value]', content: 'Value')[source]¶
-
- name: Value¶
- content: Value¶
- class sphinxnotes.render.Field(etype: 'type' = <class 'str'>, ctype: 'type | None' = None, flags: 'dict[str, Value]'=<factory>, dsl: 'str | None' = None)[source]¶
- class sphinxnotes.render.Schema(name: 'Field | None', attrs: 'dict[str, Field] | Field', content: 'Field | None')[source]¶
- class sphinxnotes.render.data.Registry[source]¶
Stores supported element types and element forms (containers).
- add_type(name, etype, conv, strify, aliases=[])[source]¶
Register a value type for
PlainValue.- Parameters:
name (str) – The name for this scalar type, available as a Type modifier in the DSL
etype (type) – The Python type object
conv (Callable[[str], PlainValue]) – A callable that converts a string to the etype
strify (Callable[[PlainValue], str]) – A callable that converts the etype to a string
- Return type:
None
See also
- add_form(name, ctype, sep, aliases=[])[source]¶
Register a value form with its container type and separator for
Value.
- add_flag(name, default=False, aliases=[])[source]¶
Register a flag.
- Parameters:
- Return type:
None
See also
Registry¶
Developers can extend this extension (for example, to support more data types
or add new extra context) by adding new items to
sphinxnotes.render.REGISTRY.
- sphinxnotes.render.REGISTRY = <sphinxnotes.render.Registry object>¶
The global, all-in-one registry for users.