Factory to generate syntax highlighter for the given filename.

    If a syntax highlighter is not available for a particular file, this
    function will attempt to generate one based on the lexers in Pygments.  If
    Pygments is not available or does not have an appropriate lexer, TextSH
    will be returned instead.

def guess_pygments_highlighter(filename):
    """Factory to generate syntax highlighter for the given filename.

    If a syntax highlighter is not available for a particular file, this
    function will attempt to generate one based on the lexers in Pygments.  If
    Pygments is not available or does not have an appropriate lexer, TextSH
    will be returned instead.

    """
    try:
        from pygments.lexers import get_lexer_for_filename, get_lexer_by_name
    except Exception:
        return TextSH
    root, ext = os.path.splitext(filename)
    if ext in custom_extension_lexer_mapping:
        try:
            lexer = get_lexer_by_name(custom_extension_lexer_mapping[ext])
        except Exception:
            return TextSH
    else:
        try:
            lexer = get_lexer_for_filename(filename)
        except Exception:
            return TextSH
    class GuessedPygmentsSH(PygmentsSH):
        _lexer = lexer
    return GuessedPygmentsSH