Skip to content
Back to top
View this page
Edit this page

# xonsh.pygments_cache

A fast, drop-in replacement for pygments `get_*()` and `guess_*()` funtions.

The following pygments API functions are currently supplied here:

    from pygments_cache import get_lexer_for_filename, guess_lexer_for_filename
    from pygments_cache import get_formatter_for_filename, get_formatter_by_name
    from pygments_cache import get_style_by_name, get_all_styles
    from pygments_cache import get_filter_by_name

The cache itself is stored at the location given by the `$PYGMENTS_CACHE_FILE` environment variable, or by default at `~/.local/share/pygments-cache/cache.py`. The cache file is created on first use, if it does not already exist.

xonsh.pygments_cache.add_custom_style(name: str, style: Style)

  Register custom style to be able to retrieve it by `get_style_by_name`.

  Parameters:
    name

      Style name.

    style

      Custom style to add.

xonsh.pygments_cache.build_cache()

  Does the hard work of building a cache from nothing.

xonsh.pygments_cache.cache_filename()

  Gets the name of the cache file to use.

xonsh.pygments_cache.get_all_styles()

  Iterable through all known style names. This mimics the behavior of `pygments.styles.get_all_styles`.

xonsh.pygments_cache.get_filter_by_name(filtername, **options)

  Gets a filter instance from its name. This mimics the behavior of `pygments.filters.get_filtere_by_name()`.

xonsh.pygments_cache.get_formatter_by_name(alias, **options)

  Gets a formatter instance from its name or alias. This mimics the behavior of `pygments.formatters.get_formatter_by_name()`.

xonsh.pygments_cache.get_formatter_for_filename(fn, **options)

  Gets a formatter instance from a filename (usually via the filename extension). This mimics the behavior of `pygments.formatters.get_formatter_for_filename()`.

xonsh.pygments_cache.get_lexer_for_filename(filename, text='', **options)

  Gets a lexer from a filename (usually via the filename extension). This mimics the behavior of `pygments.lexers.get_lexer_for_filename()` and `pygments.lexers.guess_lexer_for_filename()`.

xonsh.pygments_cache.get_style_by_name(name)

  Gets a style class from its name or alias. This mimics the behavior of `pygments.styles.get_style_by_name()`.

xonsh.pygments_cache.guess_lexer_for_filename(filename, text='', **options)

  Gets a lexer from a filename (usually via the filename extension). This mimics the behavior of `pygments.lexers.get_lexer_for_filename()` and `pygments.lexers.guess_lexer_for_filename()`.

xonsh.pygments_cache.load(filename)

  Loads the cache from a filename.

xonsh.pygments_cache.load_or_build()

  Loads the cache from disk. If the cache does not exist, this will build and write it out.

xonsh.pygments_cache.write_cache(filename)

  Writes the current cache to the file