Skip to content

Commit b1797ac

Browse files
authored
Merge pull request #10 from sphinx-notes/merge-data-back
Merge `sphinxnotes-data` back
2 parents a0ccb78 + e096240 commit b1797ac

File tree

45 files changed

+1871
-731
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1871
-731
lines changed

.cruft.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"name": "render",
99
"full_name": "sphinxnotes-render",
1010
"author": "Shengyu Zhang",
11-
"description": "A framework to define, constrain, and render data in Sphinx documentation",
11+
"description": "Define, constrain, and render data in Sphinx documentation",
1212
"version": "1.0a0",
1313
"github_owner": "sphinx-notes",
1414
"github_repo": "data",

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ sphinxnotes-render
2020

2121
|docs| |license| |pypi| |download|
2222

23-
A framework to define, constrain, and render data in Sphinx documentation.
23+
Define, constrain, and render data in Sphinx documentation.
2424

2525
.. INTRODUCTION START
2626
(MUST written in standard reStructuredText, without Sphinx stuff)

docs/api.rst

Lines changed: 40 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,52 @@
22
API References
33
==============
44

5-
The Render Pipeline
6-
===================
5+
.. _api-directives:
6+
.. _api-roles:
77

8-
The pipeline defines how nodes carrying data are generated and when they are
9-
rendered as part of the document.
8+
Roles and Directives
9+
====================
1010

11-
1. Generation: :py:class:`~sphinxnotes.render.BaseContextRole`,
12-
:py:class:`~sphinxnotes.render.BaseContextDirective` and their subclasses
13-
create :py:class:`~sphinxnotes.render.pending_node` on document parsing,
14-
and the node will be inserted to the document tree. The node contains:
11+
.. seealso::
1512

16-
- :ref:`context`, the dynamic content of a Jinja template
13+
For a minimal end-to-end example of creating your own directive, start with
14+
:ref:`ext-directives`.
1715

18-
- :py:class:`~sphinxnotes.render.Template`,
19-
the Jinja template for rendering context to markup text
20-
(reStructuredText or Markdown)
16+
Base Role Classes
17+
-----------------
18+
19+
.. autoclass:: sphinxnotes.render.BaseContextRole
20+
:show-inheritance:
21+
:members: process_pending_node, queue_pending_node, queue_context, current_context, current_template
22+
23+
.. autoclass:: sphinxnotes.render.BaseDataDefineRole
24+
:show-inheritance:
25+
:members: process_pending_node, queue_pending_node, queue_context, current_schema, current_template
26+
27+
Base Directive Classes
28+
----------------------
2129

22-
2. Render: the ``pending_node`` node will be rendered at the appropriate
23-
:py:class:`~sphinxnotes.render.Phase`, depending on
24-
:py:attr:`~sphinxnotes.render.pending_node.template.phase`.
30+
.. autoclass:: sphinxnotes.render.BaseContextDirective
31+
:show-inheritance:
32+
:members: process_pending_node, queue_pending_node, queue_context, current_raw_data, current_context, current_template
2533

26-
For a task-oriented explanation of template variables, extra context, and phase
27-
selection, see :doc:`tmpl`.
34+
.. autoclass:: sphinxnotes.render.BaseDataDefineDirective
35+
:show-inheritance:
36+
:members: process_pending_node, queue_pending_node, queue_context, current_raw_data, current_schema, current_template
37+
38+
.. autoclass:: sphinxnotes.render.StrictDataDefineDirective
39+
:show-inheritance:
40+
:members: derive
2841

2942
Node
30-
-----
43+
=====
3144

3245
.. autoclass:: sphinxnotes.render.pending_node
3346

34-
.. _context:
47+
.. _api-context:
3548

3649
Context
37-
-------
50+
=======
3851

3952
Context refers to the dynamic content of a Jinja template. It can be:
4053

@@ -57,15 +70,15 @@ Context refers to the dynamic content of a Jinja template. It can be:
5770
:members: resolve
5871

5972
``PendingContext`` Implementations
60-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
73+
----------------------------------
6174

6275
.. autoclass:: sphinxnotes.render.UnparsedData
6376
:show-inheritance:
6477

6578
.. _extractx:
6679

6780
Template
68-
--------
81+
========
6982

7083
See :doc:`tmpl` for the higher-level guide.
7184

@@ -76,12 +89,12 @@ See :doc:`tmpl` for the higher-level guide.
7689
:members:
7790

7891
Extra Context
79-
-------------
92+
=============
8093

8194
See :doc:`tmpl` for built-in extra-context names such as ``doc`` and
8295
``sphinx``, plus usage examples.
8396

84-
.. autofunction:: sphinxnotes.render.extra_context
97+
.. autodecorator:: sphinxnotes.render.extra_context
8598

8699
.. autoclass:: sphinxnotes.render.ParsingPhaseExtraContext
87100
:members: phase, generate
@@ -99,36 +112,10 @@ See :doc:`tmpl` for built-in extra-context names such as ``doc`` and
99112
:members: phase, generate
100113
:undoc-members:
101114

102-
Base Roles and Directives
103-
-------------------------
104-
105-
For a minimal end-to-end example of a custom directive, start with :doc:`usage`.
106-
107-
Base Role Classes
108-
~~~~~~~~~~~~~~~~~
109-
110-
.. autoclass:: sphinxnotes.render.BaseContextRole
111-
:show-inheritance:
112-
:members: process_pending_node, queue_pending_node, queue_context, current_context, current_template
113-
114-
.. autoclass:: sphinxnotes.render.BaseDataDefineRole
115-
:show-inheritance:
116-
:members: process_pending_node, queue_pending_node, queue_context, current_schema, current_template
117-
118-
Base Directive Classes
119-
~~~~~~~~~~~~~~~~~~~~~~
115+
Filters
116+
=======
120117

121-
.. autoclass:: sphinxnotes.render.BaseContextDirective
122-
:show-inheritance:
123-
:members: process_pending_node, queue_pending_node, queue_context, current_context, current_template
124-
125-
.. autoclass:: sphinxnotes.render.BaseDataDefineDirective
126-
:show-inheritance:
127-
:members: process_pending_node, queue_pending_node, queue_context, current_schema, current_template
128-
129-
.. autoclass:: sphinxnotes.render.StrictDataDefineDirective
130-
:show-inheritance:
131-
:members: derive
118+
.. autodecorator:: sphinxnotes.render.filter
132119

133120
Data, Field and Schema
134121
======================

docs/conf.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,23 +122,46 @@
122122
sys.path.insert(0, os.path.abspath('../src/'))
123123
extensions.append('sphinxnotes.render')
124124

125-
extensions.append('sphinxnotes.data')
126-
127125
# CUSTOM CONFIGURATION
128126

127+
extensions.append('sphinxnotes.render.ext')
128+
129+
# [example config start]
130+
data_define_directives = {
131+
'cat': {
132+
'schema': {
133+
'name': 'str, required',
134+
'attrs': {
135+
'color': 'str',
136+
},
137+
'content': 'str, required'
138+
},
139+
'template': {
140+
'text': '\n'.join([
141+
'Hi human! I am a cat named {{ name }}, I have {{ color }} fur.',
142+
'',
143+
'{{ content }}.',
144+
]),
145+
},
146+
},
147+
}
148+
# [example config end]
149+
129150
autodoc_default_options = {
130151
'member-order': 'bysource',
131152
}
132153

133154
intersphinx_mapping['python'] = ('https://docs.python.org/3', None)
134155
intersphinx_mapping['sphinx'] = ('https://www.sphinx-doc.org/en/master', None)
135-
intersphinx_mapping['data'] = ('https://sphinx.silverrainz.me/data', None)
136156

137-
ctxdir_usage_example_path = os.path.abspath('../tests/roots/test-ctxdir-usage')
157+
test_roots = os.path.abspath('../tests/roots')
138158

139159
def setup(app):
140160
app.add_object_type('event', 'event') # for intersphinx
141161

142-
sys.path.insert(0, ctxdir_usage_example_path)
143-
from conf import setup as setup_ctxdir_usage_example
144-
setup_ctxdir_usage_example(app)
162+
sys.path.insert(0, test_roots)
163+
__import__('test-extra-context.conf').conf.setup(app)
164+
__import__('test-base-context-directive-example.conf').conf.setup(app)
165+
__import__('test-base-data-define-directive-example.conf').conf.setup(app)
166+
__import__('test-strict-data-define-directive-example.conf').conf.setup(app)
167+
__import__('test-filter-example.conf').conf.setup(app)

docs/conf.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
=============
2+
Configuration
3+
=============
4+
5+
The extension provides the following configuration:
6+
7+
.. autoconfval:: data_define_directives
8+
9+
A dictionary ``dict[str, directive_def]`` for creating custom directives for
10+
data definition.
11+
12+
The ``str`` key is the name of the directive to be created;
13+
The ``directive_def`` value is a ``dict`` with the following keys:
14+
15+
- ``schema`` (dict): Schema definition, works same as the
16+
:rst:dir:`data.schema` directive, which has the following keys:
17+
18+
- ``name`` (str, optional): same as the directive argument
19+
- ``attr`` (dict, can be empty): same as the directive options
20+
- ``content`` (str, optional): same as the directive content
21+
22+
- ``template`` (dict): Template definition, works same as the
23+
:rst:dir:`data.template` directive, which has the following keys:
24+
25+
- ``text`` (str): the Jinja2 template text.
26+
- ``on`` (str, optional): same as :rst:dir:`data.template:on`
27+
- ``debug`` (bool, optional): same as :rst:dir:`data.template:debug`
28+
29+
See :ref:`custom-dir` for example.
30+

docs/dsl.rst

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -180,61 +180,3 @@ DSL Input Result
180180
``int, sep by ':'`` ``1:2:3`` :py:`[1, 2, 3]`
181181
=================== ========= ================
182182

183-
Extending the FDL
184-
=================
185-
186-
You can extend the FDL by registering custom types, flags, and by-options
187-
through the :attr:`~sphinxnotes.render.Registry.data` attribute of
188-
:data:`sphinxnotes.render.REGISTRY`.
189-
190-
.. _add-custom-types:
191-
192-
Adding Custom Types
193-
-------------------
194-
195-
Use :meth:`~sphinxnotes.render.data.REGISTRY.add_type` method of
196-
:data:`sphinxnotes.render.REGISTRY` to add a new type:
197-
198-
>>> from sphinxnotes.render import REGISTRY
199-
>>>
200-
>>> def parse_color(v: str):
201-
... return tuple(int(x) for x in v.split(';'))
202-
...
203-
>>> def color_to_str(v):
204-
... return ';'.join(str(x) for x in v)
205-
...
206-
>>> REGISTRY.data.add_type('color', tuple, parse_color, color_to_str)
207-
>>> Field.from_dsl('color').parse('255;0;0')
208-
(255, 0, 0)
209-
210-
.. _add-custom-flags:
211-
212-
Adding Custom Flags
213-
-------------------
214-
215-
Use :meth:`~sphinxnotes.render.data.Registry.add_flag` method of
216-
:data:`sphinxnotes.render.REGISTRY` to add a new flag:
217-
218-
>>> from sphinxnotes.render import REGISTRY
219-
>>> REGISTRY.data.add_flag('unique', default=False)
220-
>>> field = Field.from_dsl('int, unique')
221-
>>> field.unique
222-
True
223-
224-
.. _add-custom-by-options:
225-
226-
Adding Custom By-Options
227-
------------------------
228-
229-
Use :meth:`~sphinxnotes.render.data.Registry.add_by_option` method of
230-
:data:`sphinxnotes.render.REGISTRY` to add a new by-option:
231-
232-
>>> from sphinxnotes.render import REGISTRY
233-
>>> REGISTRY.data.add_by_option('group', str)
234-
>>> field = Field.from_dsl('str, group by size')
235-
>>> field.group
236-
'size'
237-
>>> REGISTRY.data.add_by_option('index', str, store='append')
238-
>>> field = Field.from_dsl('str, index by month, index by year')
239-
>>> field.index
240-
['month', 'year']

0 commit comments

Comments
 (0)