Cloned DMOJ
This commit is contained in:
parent
f623974b58
commit
49dc9ff10c
513 changed files with 132349 additions and 39 deletions
24
judge/utils/opengraph.py
Normal file
24
judge/utils/opengraph.py
Normal file
|
@ -0,0 +1,24 @@
|
|||
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
|
||||
for p in tree.iterfind('.//p'):
|
||||
text = p.text_content().strip()
|
||||
if text:
|
||||
description = text
|
||||
break
|
||||
if description:
|
||||
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
|
||||
cache.set(cache_key, metadata, 86400)
|
||||
return metadata
|
Loading…
Add table
Add a link
Reference in a new issue