Provide simple support for fetching entries in AMP format - can't handle images yet and needs some other tweaks, but works nicely so far c:

This commit is contained in:
Danielle McLean 2018-01-13 14:49:38 +11:00
parent 3e4f55fa9c
commit 40ead1bbe1
Signed by: 00dani
GPG key ID: 5A5D2D1AFF12EEC5
9 changed files with 131 additions and 1 deletions

View file

@ -14,6 +14,10 @@ class Entry:
def entry(self):
return self.plural + '_entry'
@property
def entry_amp(self):
return self.entry + '_amp'
@property
def atom(self):
return self.plural + '_atom'

View file

@ -140,6 +140,14 @@ class Entry(ModelMeta, TimeStampedModel):
args.append(self.slug)
return reverse('entries:' + kind.entry, args=args)
@property
def amp_url(self):
kind = kinds.from_id[self.kind]
args = [self.id]
if kind.slug:
args.append(self.slug)
return reverse('entries:' + kind.entry_amp, args=args)
@property
def slug(self):
return slugify(self.name)

View file

@ -1,7 +1,10 @@
{% extends 'lemoncurry/layout.html' %}
{% load shorturl static %}
{% block head %}<link rel="shortlink" href="{% shorturl entry %}" />{% endblock %}
{% block head %}
<link rel="amphtml" href="{{ entry.amp_url }}" />
<link rel="shortlink" href="{% shorturl entry %}" />
{% endblock %}
{% block styles %}
<link rel="stylesheet" type="text/stylus" href="{% static 'entries/css/h-entry.styl' %}" />

View file

@ -0,0 +1,89 @@
{% load favtags jsonify lemoncurry_tags markdown theme_colour %}<!doctype html>
<html lang="en" class="h-entry">
<head>
<meta charset="utf-8" />
<title>{{ entry.title }} ~ {% site_name %}</title>
<link rel="canonical" href="{{ entry.url }}">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<script class="p-json-ld" type="application/ld+json">
{{ entry.json_ld | jsonify }}
</script>
{% placeFavicon %}
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
<style amp-custom>
*, ::after, ::before {
box-sizing: border-box;
}
html {
background-color: {% theme_colour 0 %};
font-family: sans-serif;
line-height: 1.15;
}
body {
color: {% theme_colour 7 %};
display: flex;
flex-direction: column;
font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol";
font-size: 1rem;
font-weight: 400;
line-height: 1.5;
margin: 0;
min-height: 100vh;
}
body > header {
background-color: {% theme_colour 1 %};
padding: .5rem 1rem;
}
body > header > a {
color: inherit;
display: inline-block;
font-size: 1.25rem;
font-weight: unset;
margin-right: 1rem;
margin: 0;
padding-bottom: .3125rem;
padding-top: .3125rem;
text-decoration: none;
}
body > main {
display: flex;
flex: 1;
align-items: center;
justify-content: center;
margin: 2rem 1rem;
}
body > main > article {
background-color: {% theme_colour 2 %};
border: 1px solid rgba(0,0,0,.125);
border-radius: .25rem;
padding: 1.25rem;
}
body > main > article > h4 {
font-size: 1.5rem;
font-weight: 500;
margin: 0;
margin-bottom: .75rem;
}
</style>
<script async src="https://cdn.ampproject.org/v0.js"></script>
</head>
<body>
<header>
<a rel="home" href="{% url 'home:index' %}">{% site_name %}</a>
</header>
<main>
<article>
{% if entry.name %}
<h4 class="p-name">{{ entry.name }}</h4>
{% endif %}
<main class="e-content{% if not entry.name %} p-name{% endif %}">
{{ entry.content | markdown }}
</main>
</article>
</main>
</body>
</html>

View file

@ -35,6 +35,10 @@
{{ entry.updated | naturaltime }}
</time>
{% endif %}
<a class="u-url" href="{{ entry.amp_url }}">
<i class="fas fa-bolt"></i>
amp
</a>
{% shortlink entry as short %}<a class="u-url" href="{{ short }}">
<i class="fas fa-link"></i>
{{ short | friendly_url }}

View file

@ -31,6 +31,7 @@ for k in kinds.all:
url(to_pat(kind, page), views.index, name=k.index, kwargs={'kind': k}),
url(to_pat(kind, '/atom'), feeds.AtomByKind(k), name=k.atom),
url(to_pat(kind, '/rss'), feeds.RssByKind(k), name=k.rss),
url(to_pat(kind, id, slug, '/amp'), views.entry_amp, name=k.entry_amp),
url(to_pat(kind, id, slug), views.entry, name=k.entry),
)

View file

@ -54,3 +54,11 @@ def entry(request, id, slug=None):
'title': entry.title,
'meta': entry.as_meta(request)
}
@render_to('entries/entry_amp.html')
def entry_amp(request, id, slug=None):
entry = Entry.objects.get(pk=id)
if request.path != entry.amp_url:
return redirect(entry.amp_url, permanent=True)
return {'entry': entry}

7
lemoncurry/debug.py Normal file
View file

@ -0,0 +1,7 @@
from debug_toolbar.middleware import show_toolbar as core_show_toolbar
def show_toolbar(request):
if request.path.endswith('/amp'):
return False
return core_show_toolbar(request)

View file

@ -223,6 +223,12 @@ AGENT_COOKIE_SECURE = True
# django-cors-headers
CORS_ORIGIN_ALLOW_ALL = True
# django-debug-toolbar
# https://django-debug-toolbar.readthedocs.io/en/stable/configuration.html
DEBUG_TOOLBAR_CONFIG = {
'SHOW_TOOLBAR_CALLBACK': 'lemoncurry.debug.show_toolbar',
}
# django-shorturls
# https://pypi.python.org/pypi/django-shorturls
SHORTEN_MODELS = {