import re


def author_to_query(author):
    """Helper function for template macro to convert an author string into a
    search query.

    :param author: unicode string from creator or contributor field of gutenberg index
    :returns: whoosh query string to search creator/contributor fields for given author.
    """
    # contributor field provides extra details about contirbutor's role in brackets -- strip that off so we can search for author in any role.
    author = re.sub(r'\[[^\]]+\]', '', author).strip()
    return u'creator:"{0}" OR contributor:"{0}"'.format(author)