Esempio n. 1
0
def find_answers_by_keyword(directory,
                            keyword,
                            options="",
                            request_options=None):
    """
    Search in the whole tree of all cheatsheets or in its subtree `directory`
    by `keyword`
    """

    options_dict = _parse_options(options)

    answers_found = []
    for topic in get_topics_list(skip_internal=True, skip_dirs=True):

        if not topic.startswith(directory):
            continue

        subtopic = topic[len(directory):]
        if not options_dict["recursive"] and '/' in subtopic:
            continue

        answer_dicts = get_answers(topic, request_options=request_options)
        for answer_dict in answer_dicts:
            answer_text = answer_dict.get('answer', '')
            if match(answer_text, keyword, options_dict=options_dict):
                answers_found.append(answer_dict)

        if len(answers_found) > CONFIG['search.limit']:
            answers_found.append(_limited_entry())
            break

    return answers_found
Esempio n. 2
0
def find_answers_by_keyword(directory,
                            keyword,
                            options="",
                            request_options=None):
    """
    Search in the whole tree of all cheatsheets or in its subtree `directory`
    by `keyword`
    """

    recursive = 'r' in options

    answers_found = []
    for topic in get_topics_list(skip_internal=True, skip_dirs=True):

        if not topic.startswith(directory):
            continue

        subtopic = topic[len(directory):]
        if not recursive and '/' in subtopic:
            continue

        answer = get_answer_dict(topic, request_options=request_options)

        if answer and answer.get('answer') and keyword.lower() in answer.get(
                'answer', '').lower():
            answers_found.append(answer)

        if len(answers_found) > CONFIG['search.limit']:
            answers_found.append(_limited_entry())
            break

    return answers_found
Esempio n. 3
0
def cheat_wrapper(query, request_options=None, output_format='ansi'):
    """
    Function that delivers cheat sheet for `query`.
    If `html` is True, the answer is formatted as HTML.
    Additional request options specified in `request_options`.
    """

    def _add_section_name(query):
        # temporary solution before we don't find a fixed one
        if ' ' not in query and '+' not in query:
            return query
        if '/' in query:
            return query
        if ' ' in query:
            # for standalone queries only that may contain ' '
            return "%s/%s" % tuple(query.split(' ', 1))
        return "%s/%s" % tuple(query.split('+', 1))

    def _rewrite_aliases(word):
        if word == ':bash.completion':
            return ':bash_completion'
        return word

    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)

    def _sanitize_query(query):
        return re.sub('[<>"]', '', query)

    def _strip_hyperlink(query):
        return re.sub('(,[0-9]+)+$', '', query)

    def _parse_query(query):
        topic = query
        keyword = None
        search_options = ""

        keyword = None
        if '~' in query:
            topic = query
            pos = topic.index('~')
            keyword = topic[pos+1:]
            topic = topic[:pos]

            if '/' in keyword:
                search_options = keyword[::-1]
                search_options = search_options[:search_options.index('/')]
                keyword = keyword[:-len(search_options)-1]

        return topic, keyword, search_options

    query = _sanitize_query(query)
    query = _add_section_name(query)
    query = _rewrite_aliases(query)
    query = _rewrite_section_name(query)


    # at the moment, we just remove trailing slashes
    # so queries python/ and python are equal
    query = _strip_hyperlink(query.rstrip('/'))
    topic, keyword, search_options = _parse_query(query)

    if keyword:
        answers = find_answers_by_keyword(
            topic, keyword, options=search_options, request_options=request_options)
    else:
        answers = [get_answer_dict(topic, request_options=request_options)]

    answers = [
        postprocessing.postprocess(
            answer, keyword, search_options, request_options=request_options)
        for answer in answers
    ]

    answer_data = {
        'query': query,
        'keyword': keyword,
        'answers': answers,
        }

    if output_format == 'html':
        answer_data['topics_list'] = get_topics_list()
        return frontend.html.visualize(answer_data, request_options)
    elif output_format == 'json':
        return json.dumps(answer_data, indent=4)
    return frontend.ansi.visualize(answer_data, request_options)