Support syntax highlighting for all XML/JSON-based content types via +xml/+json suffix

This commit is contained in:
Pavel Baksy 2026-02-10 23:47:30 +01:00
parent 180e15ac20
commit daf03123a6

View File

@ -1150,6 +1150,18 @@ class RequestTabWidget(Gtk.Box):
return ""
def _is_json_content_type(self, content_type):
"""Check if the content type is JSON or a JSON-based format."""
return ('application/json' in content_type
or 'text/json' in content_type
or '+json' in content_type)
def _is_xml_content_type(self, content_type):
"""Check if the content type is XML or an XML-based format."""
return ('application/xml' in content_type
or 'text/xml' in content_type
or '+xml' in content_type)
def _get_language_from_content_type(self, content_type):
"""Get GtkSourceView language ID from content type."""
if not content_type:
@ -1157,9 +1169,9 @@ class RequestTabWidget(Gtk.Box):
language_manager = GtkSource.LanguageManager.get_default()
if 'application/json' in content_type or 'text/json' in content_type:
if self._is_json_content_type(content_type):
return language_manager.get_language('json')
elif 'application/xml' in content_type or 'text/xml' in content_type:
elif self._is_xml_content_type(content_type):
return language_manager.get_language('xml')
elif 'text/html' in content_type:
return language_manager.get_language('html')
@ -1176,10 +1188,10 @@ class RequestTabWidget(Gtk.Box):
return body
try:
if 'application/json' in content_type or 'text/json' in content_type:
if self._is_json_content_type(content_type):
parsed = json.loads(body)
return json.dumps(parsed, indent=2, ensure_ascii=False)
elif 'application/xml' in content_type or 'text/xml' in content_type:
elif self._is_xml_content_type(content_type):
dom = xml.dom.minidom.parseString(body)
return dom.toprettyxml(indent=" ")
except Exception as e: