<commit_before>from django.core.cache import cache
from django.template.defaultfilters import truncatewords

from judge.templatetags.reference import reference
from judge.templatetags.markdown import markdown_filter


def generate_opengraph(cache_key, data, style):
    metadata = cache.get(cache_key)
    if metadata is None:
        description = None
        tree = reference(markdown_filter(data, style)).tree
        for p in tree.iterfind('p'):
            text = p.text_content().strip()
            if text:
                description = text
                break
        img = tree.xpath('.//img')
        metadata = truncatewords(description, 60), img[0].get('src') if img else None
        cache.set(cache_key, metadata, 86400)
    return metadata<commit_msg>No longer expect <p> to be top level children.<commit_after>from django.core.cache import cache
from django.template.defaultfilters import truncatewords

from judge.templatetags.reference import reference
from judge.templatetags.markdown import markdown_filter


def generate_opengraph(cache_key, data, style):
    metadata = cache.get(cache_key)
    if metadata is None:
        description = None
        tree = reference(markdown_filter(data, style)).tree
        for p in tree.iterfind('.//p'):
            text = p.text_content().strip()
            if text:
                description = text
                break
        img = tree.xpath('.//img')
        metadata = truncatewords(description, 60), img[0].get('src') if img else None
        cache.set(cache_key, metadata, 86400)
    return metadata
{'fraction_non_alphanumeric': 0.09252217997465145, 'fraction_numerical': 0.010139416983523447, 'mean_word_length': 2.8048192771084337, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2098415', 'n_tokens_mistral': 444, 'n_tokens_neox': 425, 'n_words': 121}