Smarter generation of OGP/Schema.org/etc. metadata, with HTML tags stripped out so the result looks nice and clean

This commit is contained in:
Danielle McLean 2017-12-11 13:30:46 +11:00
parent b8a74443c9
commit ea241577f1
Signed by untrusted user: 00dani
GPG key ID: 5A5D2D1AFF12EEC5
3 changed files with 17 additions and 4 deletions

View file

@ -13,7 +13,7 @@ from model_utils.models import TimeStampedModel
from users.models import Profile from users.models import Profile
from . import kinds from . import kinds
from lemoncurry import requests from lemoncurry import requests, utils
ENTRY_KINDS = [(k.id, k.id) for k in kinds.all] ENTRY_KINDS = [(k.id, k.id) for k in kinds.all]
@ -98,12 +98,12 @@ class Entry(ModelMeta, TimeStampedModel):
def title(self): def title(self):
if self.name: if self.name:
return self.name return self.name
return shorten(self.paragraphs[0], width=100, placeholder='') return shorten(utils.to_plain(self.paragraphs[0]), width=100, placeholder='')
@property @property
def excerpt(self): def excerpt(self):
try: try:
return self.paragraphs[0 if self.name else 1] return utils.to_plain(self.paragraphs[0 if self.name else 1])
except IndexError: except IndexError:
return ' ' return ' '

View file

@ -1,14 +1,18 @@
import html
import json import json
from accept_types import get_best_match from accept_types import get_best_match
from django.conf import settings from django.conf import settings
from django.http import HttpResponse, JsonResponse from django.http import HttpResponse, JsonResponse
from django.http import HttpResponseForbidden, HttpResponseBadRequest from django.http import HttpResponseForbidden, HttpResponseBadRequest
from django.utils.html import strip_tags
from os.path import join from os.path import join
from shorturls import default_converter as converter from shorturls import default_converter as converter
from shorturls.templatetags.shorturl import ShortURL from shorturls.templatetags.shorturl import ShortURL
from types import SimpleNamespace from types import SimpleNamespace
from urllib.parse import urlencode, urljoin from urllib.parse import urlencode, urljoin
from .templatetags.markdown import markdown
cache = SimpleNamespace(package_json=None) cache = SimpleNamespace(package_json=None)
@ -63,3 +67,7 @@ def bad_req(message):
def forbid(message): def forbid(message):
return HttpResponseForbidden(message, content_type='text/plain') return HttpResponseForbidden(message, content_type='text/plain')
def to_plain(md):
return html.unescape(strip_tags(markdown(md)))

View file

@ -4,6 +4,7 @@ from django.contrib.sites.models import Site as DjangoSite
from django.utils.functional import cached_property from django.utils.functional import cached_property
from meta.models import ModelMeta from meta.models import ModelMeta
from urllib.parse import urljoin from urllib.parse import urljoin
from lemoncurry import utils
def avatar_path(instance, name): def avatar_path(instance, name):
@ -45,6 +46,10 @@ class User(ModelMeta, AbstractUser):
def get_absolute_url(self): def get_absolute_url(self):
return self.url return self.url
@property
def description(self):
return utils.to_plain(self.note)
@property @property
def avatar_url(self): def avatar_url(self):
return self.avatar.url return self.avatar.url
@ -81,7 +86,7 @@ class User(ModelMeta, AbstractUser):
_metadata = { _metadata = {
'image': 'avatar_url', 'image': 'avatar_url',
'description': 'note', 'description': 'description',
'og_type': 'profile', 'og_type': 'profile',
'og_profile_id': 'facebook_id', 'og_profile_id': 'facebook_id',
'twitter_creator': 'twitter_username', 'twitter_creator': 'twitter_username',