Ejemplo n.º 1
0
def _colorize_ansi_answer(topic, answer, color_style,               # pylint: disable=too-many-arguments
                          highlight_all=True, highlight_code=False,
                          unindent_code=False):

    color_style = color_style or "native"
    lexer_class = LEXER['bash']
    if '/' in topic:
        section_name = topic.split('/', 1)[0].lower()
        section_name = LANGUAGE_ALIAS.get(section_name, section_name)
        lexer_class = LEXER.get(section_name, lexer_class)
        if section_name == 'php':
            answer = "<?\n%s?>\n" % answer

    if highlight_all:
        highlight = lambda answer: pygments_highlight(
            answer, lexer_class(), Terminal256Formatter(style=color_style)).strip('\n')+'\n'
    else:
        highlight = lambda x: x

    if highlight_code:
        blocks = code_blocks(answer, wrap_lines=True, unindent_code=(4 if unindent_code else False))
        highlighted_blocks = []
        for block in blocks:
            if block[0] == 1:
                this_block = highlight(block[1])
            else:
                this_block = block[1].strip('\n')+'\n'
            highlighted_blocks.append(this_block)

        result = "\n".join(highlighted_blocks)
    else:
        result = highlight(answer).lstrip('\n')
    return result
Ejemplo n.º 2
0
    def _rewrite_section_name(query):
        """
        """
        if '/' not in query:
            return query

        section_name, rest = query.split('/', 1)
        section_name = LANGUAGE_ALIAS.get(section_name, section_name)
        return "%s/%s" % (section_name, rest)
Ejemplo n.º 3
0
    def _rewrite_section_name(query):
        """
        """
        if '/' not in query:
            return query

        section_name, rest = query.split('/', 1)
        section_name = LANGUAGE_ALIAS.get(section_name, section_name)
        return "%s/%s" % (section_name, rest)
Ejemplo n.º 4
0
    def _rewrite_section_name(query):
        """
        Rewriting special section names:
        * EDITOR:NAME => emacs:go-mode
        """

        if '/' not in query:
            return query

        section_name, rest = query.split('/', 1)

        if ':' in section_name:
            section_name = rewrite_editor_section_name(section_name)
        section_name = LANGUAGE_ALIAS.get(section_name, section_name)

        return "%s/%s" % (section_name, rest)
Ejemplo n.º 5
0
def _colorize_ansi_answer(
        topic,
        answer,
        color_style,  # pylint: disable=too-many-arguments
        highlight_all=True,
        highlight_code=False,
        unindent_code=False):

    color_style = color_style or "native"
    lexer_class = LEXER['bash']
    if '/' in topic:
        section_name = topic.split('/', 1)[0].lower()
        section_name = LANGUAGE_ALIAS.get(section_name, section_name)
        lexer_class = LEXER.get(section_name, lexer_class)
        if section_name == 'php':
            answer = "<?\n%s?>\n" % answer

    if highlight_all:
        highlight = lambda answer: pygments_highlight(
            answer, lexer_class(), Terminal256Formatter(style=color_style)
        ).strip('\n') + '\n'
    else:
        highlight = lambda x: x

    if highlight_code:
        blocks = code_blocks(answer,
                             wrap_lines=True,
                             unindent_code=(4 if unindent_code else False))
        highlighted_blocks = []
        for block in blocks:
            if block[0] == 1:
                this_block = highlight(block[1])
            else:
                this_block = block[1].strip('\n') + '\n'
            highlighted_blocks.append(this_block)

        result = "\n".join(highlighted_blocks)
    else:
        result = highlight(answer).lstrip('\n')
    return result
Ejemplo n.º 6
0
    def _rewrite_section_name(query):
        """
        """
        if '/' not in query:
            return query

        section_name, rest = query.split('/', 1)

        if ':' in section_name:
            # if ':' is in section_name, it means, that we want to
            # translate the answer in the specified human language
            # (experimental)
            language, section_name = section_name.split(':', 1)
        else:
            language = ""

        section_name = LANGUAGE_ALIAS.get(section_name, section_name)

        if language:
            section_name = language + ":" + section_name

        return "%s/%s" % (section_name, rest)