Mercurial > piecrust2
comparison piecrust/admin/views/sources.py @ 959:4c69935ca415
admin: When there's no post title, make a title from the first few words.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sat, 07 Oct 2017 12:12:28 -0700 |
parents | 7ecb946bfafd |
children | 0d699f04968c |
comparison
equal
deleted
inserted
replaced
958:e1cadbfddb48 | 959:4c69935ca415 |
---|---|
1 import re | |
1 from flask import g, abort, render_template, url_for | 2 from flask import g, abort, render_template, url_for |
2 from flask.ext.login import login_required | 3 from flask.ext.login import login_required |
3 from piecrust.data.paginator import Paginator | 4 from piecrust.data.paginator import Paginator |
4 from ..blueprint import foodtruck_bp | 5 from ..blueprint import foodtruck_bp |
5 from ..views import with_menu_context | 6 from ..views import with_menu_context |
19 data = {'title': "List %s" % source_name} | 20 data = {'title': "List %s" % source_name} |
20 data['pages'] = [] | 21 data['pages'] = [] |
21 pgn = Paginator(source, None, sub_num=page_num, items_per_page=20) | 22 pgn = Paginator(source, None, sub_num=page_num, items_per_page=20) |
22 for p in pgn.items: | 23 for p in pgn.items: |
23 page_data = { | 24 page_data = { |
24 'title': p['title'], | 25 'title': p.get('title') or _get_first_line_title(p), |
25 'author': p.get('author', default_author), | 26 'author': p.get('author') or default_author, |
26 'timestamp': p['timestamp'], | 27 'timestamp': p.get('timestamp'), |
27 'tags': p.get('tags', []), | 28 'tags': p.get('tags', []), |
28 'category': p.get('category'), | 29 'category': p.get('category'), |
29 'source': source_name, | 30 'source': source_name, |
30 'url': url_for('.edit_page', url=p['rel_url']) | 31 'url': url_for('.edit_page', url=p['rel_url']) |
31 } | 32 } |
56 } | 57 } |
57 | 58 |
58 with_menu_context(data) | 59 with_menu_context(data) |
59 return render_template('list_source.html', **data) | 60 return render_template('list_source.html', **data) |
60 | 61 |
62 | |
63 re_first_line_title = re.compile(r'[\n\r\.\!\?;]') | |
64 | |
65 | |
66 def _get_first_line_title(pagedata): | |
67 content = pagedata.get('raw_content') or '' | |
68 content = content.content.strip() | |
69 if not content: | |
70 return '<empty page>' | |
71 | |
72 m = re_first_line_title.search(content, 1) | |
73 if m: | |
74 content = content[:m.start()] | |
75 | |
76 words = content.split(' ') | |
77 title = words[0] | |
78 cur_word_idx = 1 | |
79 while len(title) < 60 and cur_word_idx < len(words): | |
80 title += ' ' + words[cur_word_idx] | |
81 cur_word_idx += 1 | |
82 | |
83 return content |