@@ -90,7 +90,7 @@ def escapestr(s: str) -> str:
90
90
91
91
def highlight_data (json_data : Any , drop_frame : bool = False ) -> str :
92
92
"""
93
- Highlight JSON data using Pygments .
93
+ Highlight single JSON data element .
94
94
"""
95
95
if isinstance (json_data , str ):
96
96
if len (json_data ) > 60 :
@@ -114,9 +114,41 @@ def highlight_data(json_data: Any, drop_frame: bool = False) -> str:
114
114
return f'<span class="obj">{ json_data } </span>'
115
115
116
116
117
+ def highlight_list (json_data : list ) -> dict [str , str ]:
118
+ """
119
+ Transforms a list of JSON data items into a dictionary containing HTML-formatted string representations.
120
+ Args:
121
+ json_data (list): A list of JSON-compatible data items to be highlighted.
122
+ Returns:
123
+ dict[str, str]: A dictionary with keys 'open', 'close', and 'value', where 'value' is a string of highlighted items separated by ',<br>'.
124
+ """
125
+
126
+ items = [highlight_data (item ) for item in json_data ]
127
+ return {
128
+ "open" : "[" ,
129
+ "close" : "]" ,
130
+ "value" : ",<br>" .join (items ),
131
+ }
132
+
133
+
117
134
def highlight_json (
118
- json_data : dict [str , Any ], children : Iterable | None = None , field : str = "children"
135
+ json_data : dict [str , Any ],
136
+ children : Iterable | None = None ,
137
+ marker : str = "" ,
138
+ field : str = "children" ,
119
139
) -> dict [str , str ]:
140
+ """
141
+ Highlights and formats a JSON-like dictionary for display, optionally including child elements.
142
+
143
+ Args:
144
+ json_data (dict[str, Any]): The JSON data to be highlighted and formatted.
145
+ children (Iterable | None, optional): An iterable of child elements to include under the specified field. Defaults to None.
146
+ marker (str, optional): A string marker to append after the children. Defaults to "".
147
+ field (str, optional): The key under which children are added. Defaults to "children".
148
+
149
+ Returns:
150
+ dict[str, str]: A dictionary containing the formatted representation with keys 'open', 'close', and 'value'.
151
+ """
120
152
has_children = children is not None
121
153
if field in json_data :
122
154
del json_data [field ]
@@ -139,7 +171,7 @@ def highlight_json(
139
171
items .append (
140
172
DETAILS_TEMPLATE .format (
141
173
key = escape (field ),
142
- value = "," .join (children ),
174
+ value = "," .join (children ) + marker ,
143
175
open = "[" ,
144
176
close = "]" ,
145
177
)
@@ -151,15 +183,6 @@ def highlight_json(
151
183
}
152
184
153
185
154
- def highlight_list (json_data : list ) -> dict [str , str ]:
155
- items = [highlight_data (item ) for item in json_data ]
156
- return {
157
- "open" : "[" ,
158
- "close" : "]" ,
159
- "value" : ",<br>" .join (items ),
160
- }
161
-
162
-
163
186
class RESTRenderer (ContentRenderer ):
164
187
"""
165
188
A custom renderer that uses the serialize_cms_plugin function to render
@@ -217,6 +240,7 @@ def render_plugins(
217
240
children = self .get_plugins_and_placeholder_lot (
218
241
placeholder , language , context , editable = editable , template = template
219
242
),
243
+ marker = f'<div class="cms-placeholder cms-placeholder-{ placeholder .pk } "></div>' ,
220
244
field = "content" ,
221
245
),
222
246
)
@@ -228,7 +252,6 @@ def get_plugins_and_placeholder_lot(
228
252
yield from super ().render_plugins (
229
253
placeholder , language , context , editable = editable , template = template
230
254
)
231
- yield f'<div class="cms-placeholder cms-placeholder-{ placeholder .pk } "></div>'
232
255
233
256
def serialize_placeholder (self , placeholder , context , language , use_cache = True ):
234
257
context .update ({"request" : self .request })
0 commit comments