comparison libervia/pages/blog/view/page_meta.py @ 1406:cffa3ae4d0aa

pages (blog/view): move URL friendly code to backend tools: - the code to render an URL friendly is now in `sat.tools.common.regex` - user friendly extra text is now only displayed when no `-` is found in ID. This is a temporary transition behaviour because new blog items IDs are now user friendly by default, and thus extra text is not wanted anymore. For older IDs it is still needed though, and the presence of `-` is used to guess when an ID is user friendly or not.
author Goffi <goffi@goffi.org>
date Fri, 16 Apr 2021 18:44:49 +0200
parents 1357d04107d1
children 6fc41f000d24
comparison
equal deleted inserted replaced
1405:3dff555fe691 1406:cffa3ae4d0aa
1 #!/usr/bin/env python3 1 #!/usr/bin/env python3
2 2
3 import unicodedata
4 import re
5 import html 3 import html
6 from libervia.server.constants import Const as C 4 from libervia.server.constants import Const as C
7 from twisted.words.protocols.jabber import jid 5 from twisted.words.protocols.jabber import jid
8 from sat.core.i18n import _ 6 from sat.core.i18n import _
9 from sat.tools.common.template import safe 7 from sat.tools.common.template import safe
10 from sat.tools.common import uri 8 from sat.tools.common import uri
11 from sat.tools.common import data_format 9 from sat.tools.common import data_format
10 from sat.tools.common import regex
11 from sat.core.log import getLogger
12 from libervia.server import utils 12 from libervia.server import utils
13 from libervia.server.utils import SubPage 13 from libervia.server.utils import SubPage
14 from sat.core.log import getLogger
15 14
16 log = getLogger(__name__) 15 log = getLogger(__name__)
17 16
18 """generic blog (with service/node provided)""" 17 """generic blog (with service/node provided)"""
19 name = 'blog_view' 18 name = 'blog_view'
20 template = "blog/articles.html" 19 template = "blog/articles.html"
21 uri_handlers = {('pubsub', 'microblog'): 'microblog_uri'} 20 uri_handlers = {('pubsub', 'microblog'): 'microblog_uri'}
22 21
23 RE_TEXT_URL = re.compile(r'[^a-zA-Z,_]+')
24 TEXT_MAX_LEN = 60
25 TEXT_WORD_MIN_LENGHT = 4
26 URL_LIMIT_MARK = 90 # if canonical URL is longer than that, text will not be appended 22 URL_LIMIT_MARK = 90 # if canonical URL is longer than that, text will not be appended
27 23
28 24
29 def microblog_uri(self, uri_data): 25 def microblog_uri(self, uri_data):
30 args = [uri_data['path'], uri_data['node']] 26 args = [uri_data['path'], uri_data['node']]
245 241
246 for item in blog_items['items']: 242 for item in blog_items['items']:
247 blog_canonical_url = '/'.join([blog_base_url_item, utils.quote(item['id'])]) 243 blog_canonical_url = '/'.join([blog_base_url_item, utils.quote(item['id'])])
248 if len(blog_canonical_url) > URL_LIMIT_MARK: 244 if len(blog_canonical_url) > URL_LIMIT_MARK:
249 blog_url = blog_canonical_url 245 blog_url = blog_canonical_url
250 else: 246 elif '-' not in item['id']:
251 # we add text from title or body at the end of URL 247 # we add text from title or body at the end of URL
252 # to make it more human readable 248 # to make it more human readable
253 text = item.get('title', item['content']) 249 # we do it only if there is no "-", as a "-" probably means that
254 # we change special chars to ascii one, trick found at https://stackoverflow.com/a/3194567 250 # item's id is already user friendly.
255 text = unicodedata.normalize('NFD', text).encode('ascii', 'ignore').decode('utf-8') 251 # TODO: to be removed, this is only kept for a transition period until
256 text = RE_TEXT_URL.sub(' ', text).lower() 252 # user friendly item IDs are more common.
257 text = '-'.join([t for t in text.split() if t and len(t)>=TEXT_WORD_MIN_LENGHT]) 253 text = regex.urlFriendlyText(item.get('title', item['content']))
258 while len(text) > TEXT_MAX_LEN:
259 if '-' in text:
260 text = text.rsplit('-', 1)[0]
261 else:
262 text = text[:TEXT_MAX_LEN]
263 if text: 254 if text:
264 blog_url = blog_canonical_url + '/' + text 255 blog_url = blog_canonical_url + '/' + text
265 else: 256 else:
266 blog_url = blog_canonical_url 257 blog_url = blog_canonical_url
258 else:
259 blog_url = blog_canonical_url
267 260
268 items_http_uri[item['id']] = self.host.getExtBaseURL(request, blog_url) 261 items_http_uri[item['id']] = self.host.getExtBaseURL(request, blog_url)
269 for tag in item['tags']: 262 for tag in item['tags']:
270 if tag not in tags_http_uri: 263 if tag not in tags_http_uri:
271 tag_url = '/'.join([blog_base_url_tag, utils.quote(tag)]) 264 tag_url = '/'.join([blog_base_url_tag, utils.quote(tag)])