2020-01-21 06:35:58 +00:00
|
|
|
from django.core.cache import cache
|
|
|
|
from django.template.defaultfilters import truncatewords
|
|
|
|
|
|
|
|
from judge.jinja2.markdown import markdown
|
|
|
|
from judge.jinja2.reference import reference
|
|
|
|
|
|
|
|
|
|
|
|
def generate_opengraph(cache_key, data, style):
|
|
|
|
metadata = cache.get(cache_key)
|
|
|
|
if metadata is None:
|
|
|
|
description = None
|
|
|
|
tree = reference(markdown(data, style)).tree
|
2022-05-14 17:57:27 +00:00
|
|
|
for p in tree.iterfind(".//p"):
|
2020-01-21 06:35:58 +00:00
|
|
|
text = p.text_content().strip()
|
|
|
|
if text:
|
|
|
|
description = text
|
|
|
|
break
|
|
|
|
if description:
|
2022-05-14 17:57:27 +00:00
|
|
|
for remove in (r"\[", r"\]", r"\(", r"\)"):
|
|
|
|
description = description.replace(remove, "")
|
|
|
|
img = tree.xpath(".//img")
|
|
|
|
metadata = truncatewords(description, 60), img[0].get("src") if img else None
|
2020-01-21 06:35:58 +00:00
|
|
|
cache.set(cache_key, metadata, 86400)
|
|
|
|
return metadata
|