@@ -12,6 +12,16 @@ The Field Declaration DSL is a Domain Specific Language (DSL) that used to
1212define the type and structure of field values. A DSL declaration consists of
1313one or more :term: `modifier `\ s separated by commas (``, ``).
1414
15+ Python API
16+ ==========
17+
18+ User can create a :class: `sphinxnotes.render.Field ` from DSL and use it to parse
19+ string to :type: `sphinxnotes.render.Value `:
20+
21+ >>> from sphinxnotes.render import Field
22+ >>> Field.from_dsl(' list of int' ).parse(' 1,2,3' )
23+ [1, 2, 3]
24+
1525Syntax
1626======
1727
@@ -174,65 +184,56 @@ Extending the DSL
174184
175185You can extend the DSL by registering custom types, flags, and by-options
176186through the :attr: `~sphinxnotes.render.Registry.data ` attribute of
177- :class : `sphinxnotes.render.Registry `.
187+ :data : `sphinxnotes.render.REGISTRY `.
178188
179189.. _add-custom-types :
180190
181191Adding Custom Types
182192-------------------
183193
184- Use :meth: `sphinxnotes.render.data.Registry.add_type ` to add a new type:
185-
186- .. code-block :: python
187-
188- from sphinxnotes.render import Registry
189-
190- def parse_color (v : str ):
191- return tuple (int (x) for x in v.split(' ;' ))
192-
193- def color_to_str (v ):
194- return ' ;' .join(str (x) for x in v)
195-
196- Registry.data.add_type(' color' , tuple , parse_color, color_to_str)
197- val = Field.from_dsl(' color' ).parse(' 255;0;0' )
198- assert val == (255 , 0 , 0 )
194+ Use :meth: `~sphinxnotes.render.data.REGISTRY.add_type ` method of
195+ :data: `sphinxnotes.render.REGISTRY ` to add a new type:
196+
197+ >>> from sphinxnotes.render import REGISTRY
198+ >>>
199+ >>> def parse_color (v : str ):
200+ ... return tuple (int (x) for x in v.split(' ;' ))
201+ ...
202+ >>> def color_to_str (v ):
203+ ... return ' ;' .join(str (x) for x in v)
204+ ...
205+ >>> REGISTRY .data.add_type(' color' , tuple , parse_color, color_to_str)
206+ >>> Field.from_dsl(' color' ).parse(' 255;0;0' )
207+ (255, 0, 0)
199208
200209.. _add-custom-flags :
201210
202211Adding Custom Flags
203212-------------------
204213
205- Use :meth: `sphinxnotes.render.data.Registry.add_flag ` to add a new flag:
214+ Use :meth: `~sphinxnotes.render.data.Registry.add_flag ` method of
215+ :data: `sphinxnotes.render.REGISTRY ` to add a new type:
206216
207- .. code-block :: python
208-
209- from sphinxnotes.render import Registry
210-
211- Registry.data.add_flag(' unique' , default = False )
212- field = Field.from_dsl(' int, unique' )
213- assert field.unique is True
217+ >>> from sphinxnotes.render import REGISTRY
218+ >>> REGISTRY .data.add_flag(' unique' , default = False )
219+ >>> field = Field.from_dsl(' int, unique' )
220+ >>> field.unique
221+ True
214222
215223.. _add-custom-by-options :
216224
217225Adding Custom By-Options
218226------------------------
219227
220- Use :meth: `sphinxnotes.render.data.Registry.add_by_option `: to add a new by-option:
221-
222- .. code-block :: python
223-
224- from sphinxnotes.render import Registry
225-
226- Registry.data.add_by_option(' group' , str )
227- field = Field.from_dsl(' str, group by size' )
228- assert field.group == ' size'
229-
230- # For options that can be specified multiple times:
231- Registry.data.add_by_option(' index' , str , store = ' append' )
232- field = Field.from_dsl(' str, index by month, index by year' )
233- assert field.index == [' month' , ' year' ]
234-
235- Usage::
236-
237- int, group by foo
238- int, index by year, index by month
228+ Use :meth: `~sphinxnotes.render.data.Registry.add_by_option ` method of
229+ :data: `sphinxnotes.render.REGISTRY ` to add a new by-option:
230+
231+ >>> from sphinxnotes.render import REGISTRY
232+ >>> REGISTRY .data.add_by_option(' group' , str )
233+ >>> field = Field.from_dsl(' str, group by size' )
234+ >>> field.group
235+ 'size'
236+ >>> REGISTRY .data.add_by_option(' index' , str , store = ' append' )
237+ >>> field = Field.from_dsl(' str, index by month, index by year' )
238+ >>> field.index
239+ ['month', 'year']
0 commit comments