Learn more  » Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

nickfrez / api-browser   python

Repository URL to install this package:

/ api_browser / highlight.py

# -*- coding: utf-8 -*-
"""
#########
Highlight
#########

Code highlighting utilities.

.. NOTE:: This is mostly just a thin wrapper around Pygments.

"""

from pygments import highlight as pygments_highlight
from pygments.lexers import get_lexer_by_name as pygments_get_lexer_by_name

from .pygments_formatters import HtmlLinkifyFormatter


def highlight_and_hyperlink_json(json_data, style='trac'):
    """Convert JSON to highlighted HTML.

    Parameters
    ----------
    json_data : dict
        Data to be highlighted.
    style : str
        The Pygments style to use for highlighting.  `See the Pygments docs
        <http://pygments.org/docs/styles/>`_.

    Returns
    -------
    str
        HTML representation of the JSON data.

        .. NOTE:: This does not include css!  To get the css, use
                  ``get_styles``.

    """
    lexer = pygments_get_lexer_by_name('JSON')
    formatter = HtmlLinkifyFormatter(style=style, full=False)
    return pygments_highlight(json_data, lexer, formatter)


def get_styles(style='trac'):
    """Get the highlighting styles for highlighted JSON.

    Parameters
    ----------
    style
        The Pygments style to use for highlighting.  `See the Pygments docs
        <http://pygments.org/docs/styles/>`_.

    Returns
    -------
    str
        The css rules for highlighting.

    """
    formatter = HtmlLinkifyFormatter(style=style, full=False)
    return formatter.get_style_defs('.highlight')