def format_json(value: dict, styled: bool = True) -> str: """ Format json and add color using pygments with fallback. Args: value (dict): Dict to be formated to json styled (bool): True if output should be styled Returns: str: JSON formated html string """ value = json.dumps(value, indent=2) value = pygments_highlight(value, 'json', 'tango') style = pygments_css('tango') if styled and style: html = ( f'<style>{style}</style>' f'<pre class="highlight" style="margin: 0; padding: 1em;">{value}</pre>' ) else: html = f'<pre style="margin: 0;">{value}</pre>' return format_html('<div>{}</div>', mark_safe(html))
def format_json(value: dict, context: dict = {}) -> str: """ Format json and add color using pygments with fallback. Args: value (dict): Dict to be formated to json context (dict, optional): Format context data. Defaults to {}. Returns: str: JSON formated html string """ value = json.dumps(value, indent=2) value = pygments_highlight(value, 'json', 'tango') style = pygments_css('tango') styled = context.get('styled', True) # Used for testing. if styled and style: html = ( f'<style>{style}</style>' f'<pre class="highlight" style="margin: 0; padding: 1em;">{value}</pre>' ) else: html = f'<pre style="margin: 0;">{value}</pre>' return format_html('<div>{}</div>', mark_safe(html))
def render(self, context): text = self.nodelist.render(context) return pygments_highlight(text, self.lang, self.style)