Esempio n. 1
0
def make_valid(verse_refs):
    """ Converts a string of verse references into a sorted list of valid
    references.

    """

    # Get a set of valid verse references asked for.
    verse_refs = sword_search.parse_verse_range(verse_refs)

    # Get a sorted list of the verse set, because it is faster to
    # lookup verses from a sorted list than from a randomized one.
    return sorted(verse_refs, key=sword_search.sort_key)
Esempio n. 2
0
def lookup_verses(verse_refs, search_terms="", context=0):
    """ Looks up the verses in verse_refs, highlights the search_terms, and
    returns a list of verses adding context verses on either side of each.

    """

    # Get a set of valid verse references asked for.
    verse_refs = sword_search.parse_verse_range(verse_refs)

    # Add the context.
    verse_list = sword_search.add_context(verse_refs, context)

    # Get a sorted list of the verse set, because it is faster to lookup
    # verses from a sorted list than from a randomized one.
    verse_list = sorted(verse_list, key=sword_search.sort_key)

    # Get all the strongs numbers out of the search terms.
    strongs_list = re.findall(r"(?i)((?:H|G)\d+)", search_terms)

    # Remove strongs numbers from search_terms.
    search_terms = re.sub(r"(?i)(H|G)\d+", "", search_terms).strip()

    # Split the search terms in to '"' quoted groups.
    terms_list = ["".join(i) for i in search_regx.findall(search_terms)]
    terms_list = [i for i in terms_list if not i.startswith("!")]

    # The results list.
    results_list = []
    last_ref = ""

    # Highlight colors.
    highlight_text = '<span class="query-highlight">\\1</span>'

    # Build dictionary of verse references and text.
    for ref, verse_text in sword_search.VerseTextIter(iter(verse_list), strongs=True, morph=True, render="raw"):

        # Setup the verse text for highlighting and put the headings,
        # notes, and paragraph markers in.
        verse_text = tag_regx.sub(tag_func, verse_text.encode("utf8"))

        # Highlight only in the verses found during the search, not in
        # any of the context verses.
        if ref in verse_refs:
            # Build a regular expression that can be used to highlight
            # the search query in the output text.
            reel = build_highlight_regx(terms_list, False, color_tag="</?span[^>]*>", extra_tag="</span>")
            # Apply the highlight regex to highlight the verse text.
            verse_text = highlight_search_terms(verse_text, reel, highlight_text, color_tag="\\b")  # </?span[^>]*>')

        if results_list and last_ref:
            # last_ref = results_list[-1]['verseref']
            last_book, _ = last_ref.rsplit(" ", 1)
            cur_book, _ = ref.rsplit(" ", 1)

            # Put a break between books.
            if cur_book != last_book:
                results_list.append({"highlight": False, "verseref": "", "versetext": ""})

        last_ref = ref

        # Build a list of the results.
        results_list.append(
            {
                "highlight": ref in verse_refs,
                "verseref": ref if search_terms else "",
                "versetext": verse_text.decode("utf8"),
            }
        )

    # Return the list of results and the list of strongs numbers in the
    # search query.
    return results_list