def generate_pingback_content(soup, target, max_length, trunc_char='...'):
    """
    Generate a description text for the pingback.
    """
    link = soup.find('a', href=target)

    content = strip_tags(six.text_type(link.findParent()))
    index = content.index(link.string)

    if len(content) > max_length:
        middle = max_length // 2
        start = index - middle
        end = index + middle

        if start <= 0:
            end -= start
            extract = content[0:end]
        else:
            extract = '%s%s' % (trunc_char, content[start:end])

        if end < len(content):
            extract += trunc_char
        return extract

    return content
dataset/ETHPy150Open Fantomas42/django-blog-zinnia/zinnia/xmlrpc/pingback.py/generate_pingback_content/original