@@ -2222,6 +2222,63 @@ def remove(self, *, title=None, tags=None, remove_all=False):
2222
2222
def _add_or_replace (self , * , title , section , tags , html_partial , replace = False ):
2223
2223
"""Append HTML content report, or replace it if it already exists.
2224
2224
2225
+ Parameters
2226
+ ----------
2227
+ title : str
2228
+ The title entry.
2229
+ %(section_report)s
2230
+ tags : tuple of str
2231
+ The tags associated with the added element.
2232
+ html_partial : callable
2233
+ Callable that renders a HTML string, called as::
2234
+ html_partial(id_=...)
2235
+ replace : bool
2236
+ Whether to replace existing content if the title and section match.
2237
+ If an object is replaced, its dom_id is preserved.
2238
+ """
2239
+ assert callable (html_partial ), type (html_partial )
2240
+
2241
+ # Temporarily set HTML and dom_id to dummy values
2242
+ new_content = _ContentElement (
2243
+ name = title , section = section , dom_id = "" , tags = tags , html = ""
2244
+ )
2245
+
2246
+ append = True
2247
+ if replace :
2248
+ matches = [
2249
+ ii
2250
+ for ii , element in enumerate (self ._content )
2251
+ if (element .name , element .section ) == (title , section )
2252
+ ]
2253
+ if matches :
2254
+ dom_id = self ._content [matches [- 1 ]].dom_id
2255
+ self ._content [matches [- 1 ]] = new_content
2256
+ append = False
2257
+ if append :
2258
+ dom_id = self ._get_dom_id (section = section , title = title )
2259
+ self ._content .append (new_content )
2260
+ new_content .dom_id = dom_id
2261
+ new_content .html = html_partial (id_ = dom_id )
2262
+ assert isinstance (new_content .html , str ), type (new_content .html )
2263
+
2264
+ def _add_code (self , * , code , title , language , section , tags , replace ):
2265
+ if isinstance (code , Path ):
2266
+ code = Path (code ).read_text ()
2267
+ html_partial = partial (
2268
+ _html_code_element ,
2269
+ tags = tags ,
2270
+ title = title ,
2271
+ code = code ,
2272
+ language = language ,
2273
+ )
2274
+ self ._add_or_replace (
2275
+ title = title ,
2276
+ section = section ,
2277
+ tags = tags ,
2278
+ html_partial = html_partial ,
2279
+ replace = replace ,
2280
+ )
2281
+
2225
2282
@fill_doc
2226
2283
def add_code (
2227
2284
self ,
0 commit comments