Semantic markup for blog posts

merge-requests/30/head
Bob Mottram 2021-05-15 20:39:34 +01:00
parent 121f625dd4
commit 818a920365
3 changed files with 65 additions and 3 deletions

29
blog.py
View File

@ -11,9 +11,11 @@ from datetime import datetime
from content import replaceEmojiFromTags
from webapp_utils import htmlHeaderWithExternalStyle
from webapp_utils import htmlHeaderWithBlogMarkup
from webapp_utils import htmlFooter
from webapp_utils import getPostAttachmentsAsHtml
from webapp_media import addEmbeddedElements
from utils import removeHtml
from utils import getConfigParam
from utils import getFullDomain
from utils import getMediaFormats
@ -375,11 +377,28 @@ def _htmlBlogRemoveCwButton(blogStr: str, translate: {}) -> str:
return blogStr
def _getSnippetFromBlogContent(postJsonObject: {}) -> str:
"""Returns a snippet of text from the blog post as a preview
"""
content = postJsonObject['object']['content']
if len(content) < 256:
return removeHtml(content)
if '<p>' in content:
content = content.split('<p>', 1)[1]
if '</p>' in content:
content = content.split('</p>', 1)[0]
content = removeHtml(content)
if len(content) >= 256:
content = content[:252] + '...'
return content
def htmlBlogPost(authorized: bool,
baseDir: str, httpPrefix: str, translate: {},
nickname: str, domain: str, domainFull: str,
postJsonObject: {},
peertubeInstances: []) -> str:
peertubeInstances: [],
systemLanguage: str) -> str:
"""Returns a html blog post
"""
blogStr = ''
@ -389,7 +408,13 @@ def htmlBlogPost(authorized: bool,
cssFilename = baseDir + '/blog.css'
instanceTitle = \
getConfigParam(baseDir, 'instanceTitle')
blogStr = htmlHeaderWithExternalStyle(cssFilename, instanceTitle)
published = postJsonObject['object']['published']
title = postJsonObject['object']['summary']
snippet = _getSnippetFromBlogContent(postJsonObject)
blogStr = htmlHeaderWithBlogMarkup(cssFilename, instanceTitle,
httpPrefix, domainFull, nickname,
systemLanguage, published,
title, snippet)
_htmlBlogRemoveCwButton(blogStr, translate)
blogStr += _htmlBlogPostContent(authorized, baseDir,

View File

@ -11094,7 +11094,8 @@ class PubServer(BaseHTTPRequestHandler):
nickname, self.server.domain,
self.server.domainFull,
postJsonObject,
self.server.peertubeInstances)
self.server.peertubeInstances,
self.server.systemLanguage)
if msg is not None:
msg = msg.encode('utf-8')
msglen = len(msg)

View File

@ -791,6 +791,42 @@ def htmlHeaderWithWebsiteMarkup(cssFilename: str, instanceTitle: str,
return htmlStr
def htmlHeaderWithBlogMarkup(cssFilename: str, instanceTitle: str,
httpPrefix: str, domain: str, nickname: str,
systemLanguage: str, published: str,
title: str, snippet: str) -> str:
"""html header which includes blog post markup
https://schema.org/BlogPosting
"""
htmlStr = htmlHeaderWithExternalStyle(cssFilename, instanceTitle,
systemLanguage)
authorUrl = httpPrefix + '://' + domain + '/users/' + nickname
blogMarkup = \
' <script type="application/ld+json">\n' + \
' {\n' + \
' "@context" : "http://schema.org",\n' + \
' "@type" : "BlogPosting",\n' + \
' "headline": "' + title + '",\n' + \
' "datePublished": "' + published + '",\n' + \
' "dateModified": "' + published + '",\n' + \
' "author": {\n' + \
' "@type": "Person",\n' + \
' "name": "' + nickname + '",\n' + \
' "url": "' + authorUrl + '"\n' + \
' },\n' + \
' "publisher": {\n' + \
' "@type": "WebSite",\n' + \
' "name": "' + instanceTitle + '",\n' + \
' "url": "' + httpPrefix + '://' + domain + '/about.html"\n' + \
' },\n' + \
' "description": "' + snippet + '"\n' + \
' }\n' + \
' </script>\n'
htmlStr = htmlStr.replace('<head>\n', '<head>\n' + blogMarkup)
return htmlStr
def htmlFooter() -> str:
htmlStr = ' </body>\n'
htmlStr += '</html>\n'