Skip to content

Commit 721bf5b

Browse files
committed
Removed last trailing comma
1 parent 34b0fa9 commit 721bf5b

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

djangocms_rest/plugin_rendering.py

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def escapestr(s: str) -> str:
9090

9191
def highlight_data(json_data: Any, drop_frame: bool = False) -> str:
9292
"""
93-
Highlight JSON data using Pygments.
93+
Highlight single JSON data element.
9494
"""
9595
if isinstance(json_data, str):
9696
if len(json_data) > 60:
@@ -114,9 +114,41 @@ def highlight_data(json_data: Any, drop_frame: bool = False) -> str:
114114
return f'<span class="obj">{json_data}</span>'
115115

116116

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+
117134
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",
119139
) -> 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+
"""
120152
has_children = children is not None
121153
if field in json_data:
122154
del json_data[field]
@@ -139,7 +171,7 @@ def highlight_json(
139171
items.append(
140172
DETAILS_TEMPLATE.format(
141173
key=escape(field),
142-
value=",".join(children),
174+
value=",".join(children) + marker,
143175
open="[",
144176
close="]",
145177
)
@@ -151,15 +183,6 @@ def highlight_json(
151183
}
152184

153185

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-
163186
class RESTRenderer(ContentRenderer):
164187
"""
165188
A custom renderer that uses the serialize_cms_plugin function to render
@@ -217,6 +240,7 @@ def render_plugins(
217240
children=self.get_plugins_and_placeholder_lot(
218241
placeholder, language, context, editable=editable, template=template
219242
),
243+
marker=f'<div class="cms-placeholder cms-placeholder-{placeholder.pk}"></div>',
220244
field="content",
221245
),
222246
)
@@ -228,7 +252,6 @@ def get_plugins_and_placeholder_lot(
228252
yield from super().render_plugins(
229253
placeholder, language, context, editable=editable, template=template
230254
)
231-
yield f'<div class="cms-placeholder cms-placeholder-{placeholder.pk}"></div>'
232255

233256
def serialize_placeholder(self, placeholder, context, language, use_cache=True):
234257
context.update({"request": self.request})

tests/test_plugin_renderer.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@ def get_text_from_html(html, selector):
1818
soup = BeautifulSoup(html, "html.parser")
1919
element = soup.select_one(selector)
2020
if element:
21-
return (
22-
element.get_text(strip=True)
23-
.replace(",]", "]")
24-
.replace(",}", "}")
25-
.rstrip(",")
26-
)
21+
return element.get_text(strip=True)
2722
return None
2823

2924

0 commit comments

Comments
 (0)