sites/flux.vision/plugins/tileweight.py

41 lines
1.3 KiB
Python
Raw Normal View History

"""Give each work a tile size derived from what it actually contains.
A uniform grid gives a ten-hour festival set and a two-line workshop note the
same visual weight, which is both untrue and the most dated thing about the
original layout. Weight is taken from evidence already in the entry: how much
imagery survived, whether there is video, and how much was written about it.
"""
import re
from pelican import signals
IMG = re.compile(r'<img\b', re.I)
VID = re.compile(r'<(?:iframe|embed|video)\b', re.I)
def _weigh(content):
if content.__class__.__name__ != 'Article':
return
body = getattr(content, '_content', '') or ''
imgs = len(IMG.findall(body))
vids = len(VID.findall(body))
words = len(re.sub(r'<[^>]+>', ' ', body).split())
score = imgs + vids * 4 + words / 90.0
# a work with no surviving image cannot carry a large tile: it would be a
# big rectangle of static, which reads as a fault rather than a choice
if content.metadata.get('thumb_missing') or not content.metadata.get('thumb'):
size = 'sm'
elif score >= 9:
size = 'lg'
elif score >= 4.5:
size = 'md'
else:
size = 'sm'
content.metadata['tilesize'] = size
content.metadata['tilescore'] = round(score, 1)
def register():
signals.content_object_init.connect(_weigh)