@@ -68,32 +68,43 @@ by selecting the corresponding argument when calling :code:`bibtexparser.parse`
6868
6969Core Middleware
7070^^^^^^^^^^^^^^^
71- The following middleware layers are part of the core functionality of bibtexparser and maintained as part of the main repository.
72- The functionality is straightforward from the class names, so we will not go into detail here and refer to the
73- docstrings of the classes instead.
7471
75- Middleware Layers Regarding Encoding and Enclosing of Values
72+ bibtexparser comes with a number of middleware options:
73+
74+ .. _middleware_encoding :
75+
76+ Encoding and Enclosing of Values
77+ ::::::::::::::::::::::::::::::::
7678
7779* :mod: `bibtexparser.middlewares.AddEnclosingMiddleware `
7880* :mod: `bibtexparser.middlewares.RemoveEnclosingMiddleware `
7981* :mod: `bibtexparser.middlewares.LatexEncodingMiddleware `
8082* :mod: `bibtexparser.middlewares.LatexDecodingMiddleware `
8183
82- Middleware Layers Regarding Value References and Representation
84+ .. _middleware_references :
85+
86+ Value References and Representation
87+ :::::::::::::::::::::::::::::::::::
8388
8489* :mod: `bibtexparser.middlewares.ResolveStringReferencesMiddleware `
8590* :mod: `bibtexparser.middlewares.MonthIntMiddleware `
8691* :mod: `bibtexparser.middlewares.MonthAbbreviationMiddleware `
8792* :mod: `bibtexparser.middlewares.MonthLongStringMiddleware `
8893
89- Middleware Layers Regarding Names
94+ .. _middleware_names :
95+
96+ Names
97+ :::::
9098
9199* :mod: `bibtexparser.middlewares.SeparateCoAuthors `
92100* :mod: `bibtexparser.middlewares.MergeCoAuthors `
93101* :mod: `bibtexparser.middlewares.SplitNameParts ` (requires SeperateCoAuthors to be applied first)
94102* :mod: `bibtexparser.middlewares.MergeNameParts `
95103
96- Sorting Middleware Layers
104+ .. _middleware_sorting :
105+
106+ Sorting
107+ :::::::
97108
98109* :mod: `bibtexparser.middlewares.SortBlocksByTypeAndKeyMiddleware `
99110* :mod: `bibtexparser.middlewares.SortFieldsAlphabeticallyMiddleware `
@@ -106,17 +117,86 @@ Sorting Middleware Layers
106117 even if it comes at the cost of slightly reduced functionality and performance.
107118 See the migration docs, if you are migrating from bibtexparser v1.
108119
120+ Write your own Middleware
121+ ^^^^^^^^^^^^^^^^^^^^^^^^^
122+
123+ Functions working on blocks individually
124+ ::::::::::::::::::::::::::::::::::::::::
125+
126+ Should extend the :class: `bibtexparser.middlewares.BlockMiddleware ` class.
127+ This includes functionalities similar to
128+ :ref: `middleware_encoding `, :ref: `middleware_references `, and :ref: `middleware_names `.
129+
130+ * Basic example:
131+
132+ .. code-block :: python
133+
134+ from bibtexparser.middlewares import BlockMiddleware
135+
136+ class MyMiddleware (BlockMiddleware ):
137+ def transform_entry (self , entry , * args , ** kwargs ):
138+ # Do something with the entry, e.g.
139+ entry[" title" ] = entry[" title" ].lower()
140+ # Return the transformed entry
141+ return entry
142+
143+ * Initialize the middleware with some parameters:
144+
145+ .. code-block :: python
146+
147+ from bibtexparser.middlewares import BlockMiddleware
148+
149+ class MyMiddleware (BlockMiddleware ):
150+ def __init__ (self , my_param ):
151+ self .my_param = my_param
152+ super ().__init__ ()
153+
154+ def transform_entry (self , entry , * args , ** kwargs ):
155+ # Do something with the entry, e.g.
156+ entry[" title" ] = entry[" title" ].lower()
157+ # Return the transformed entry
158+ return entry
159+
160+ Library-wide transformations
161+ ::::::::::::::::::::::::::::
162+
163+ Should extend the :class: `bibtexparser.middlewares.LibraryMiddleware ` class.
164+ This includes functionalities similar to sorting blocks
165+ (e.g. :mod: `bibtexparser.middlewares.SortBlocksByTypeAndKeyMiddleware `).
166+
167+ Warning
168+ :::::::
169+
170+ :class: `bibtexparser.middlewares.BlockMiddleware ` and :class: `bibtexparser.middlewares.LibraryMiddleware `
171+ have two default arguments:
172+
173+ * ``allow_parallel_execution=True ``, see :py:meth: `bibtexparser.middlewares.Middleware.allow_inplace_modification `.
174+ * ``allow_inplace_modification=True ``, see :py:meth: `bibtexparser.middlewares.Middleware.allow_parallel_execution `.
175+
176+ If you want to change these defaults, specify them in the call to the super constructor. E.g.:
177+
178+ .. code-block :: python
179+
180+ from bibtexparser.middlewares import BlockMiddleware
181+
182+ class MyMiddleware (BlockMiddleware ):
183+ def __init__ (self , my_param ):
184+ self .my_param = my_param
185+ super ().__init__ (
186+ allow_parallel_execution = False ,
187+ allow_inplace_modification = False ,
188+ )
189+
190+ def transform_entry (self , entry , * args , ** kwargs ):
191+ # Do something with the entry, e.g.
192+ entry[" title" ] = entry[" title" ].lower()
193+ # Return the transformed entry
194+ return entry
195+
109196 Community-Provided Middleware
110197^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
111- Aiming to keep the core functionality of bibtexparser simple, we encourage users to provide their own middleware layers
112- and share them with the community.
113- We will be happy to provide a list of community-provided middleware layers here, so please let us know if you have written one!
114-
115- .. note ::
116- To write your own middleware, simply extend the :class: `bibtexparser.middlewares.BlockMiddleware `
117- (for functions working on blocks individually, such as encoding) or :class: `bibtexparser.middlewares.LibraryMiddleware `
118- (for library-wide transformations, such as sorting blocks) and implement the superclass methods
119- according to the python docstrings. Make sure to check out some core middleware layers for examples.
198+ We encourage users to provide their own middleware layers and share them with the community.
199+ We are happy to provide a list of community-provided middleware layers here, so please let us know if you have written one!
120200
121201Metadata Fields
122202^^^^^^^^^^^^^^^
0 commit comments