import gzip
import re


def search_page(path, pattern):
    """
    Search an OpenAFS man page for a given token.

    """
    with gzip.open(path) as z:
        content = z.read().decode()
    content = re.sub(r'\\&', '', content)
    content = re.sub(r'\\f.', '', content)
    content = re.sub(r'\s+', ' ', content)
    m = re.search(pattern, content)
    if not m:
        raise Exception('Failed to find directory in %s.' % path)
    return m.group(1)