Ejemplo n.º 1
0
def get_paragraph(verse_ref):
    """ Finds and returns the paragraph that verse_ref belongs to.

    """

    # Get verses on either side to try and find the entire paragraph.
    verse_list = sword_search.add_context([verse_ref], 200)
    sorted_verse_list = sorted(verse_list, key=sword_search.sort_key)

    # Get the index of this verse.
    verse_index = sorted_verse_list.index(verse_ref)

    # Get the first half including the current reference.
    first_half = reversed(sorted_verse_list[: verse_index + 1])

    # Get the second half not including the current reference.
    last_half = sorted_verse_list[verse_index + 1 :]

    # Find the start of the paragraph by searching backwards from the
    # target reference.
    start_ref = find_paragraph(first_half, inclusive=True, default=verse_ref)

    # Search forwards to find the end of the paragraph (start of the
    # next paragraph).
    end_ref = find_paragraph(last_half, inclusive=False, default=verse_ref)

    # Make it a range.
    return "%s-%s" % (start_ref, end_ref)
Ejemplo 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