# HG changeset patch # User Ludovic Chabant # Date 1507403548 25200 # Node ID 4c69935ca4153faca48f393ff193ecfef46558b1 # Parent e1cadbfddb48d42e1940890e217c0c81f417188a admin: When there's no post title, make a title from the first few words. diff -r e1cadbfddb48 -r 4c69935ca415 piecrust/admin/views/sources.py --- a/piecrust/admin/views/sources.py Sat Oct 07 12:11:46 2017 -0700 +++ b/piecrust/admin/views/sources.py Sat Oct 07 12:12:28 2017 -0700 @@ -1,3 +1,4 @@ +import re from flask import g, abort, render_template, url_for from flask.ext.login import login_required from piecrust.data.paginator import Paginator @@ -21,9 +22,9 @@ pgn = Paginator(source, None, sub_num=page_num, items_per_page=20) for p in pgn.items: page_data = { - 'title': p['title'], - 'author': p.get('author', default_author), - 'timestamp': p['timestamp'], + 'title': p.get('title') or _get_first_line_title(p), + 'author': p.get('author') or default_author, + 'timestamp': p.get('timestamp'), 'tags': p.get('tags', []), 'category': p.get('category'), 'source': source_name, @@ -58,3 +59,25 @@ with_menu_context(data) return render_template('list_source.html', **data) + +re_first_line_title = re.compile(r'[\n\r\.\!\?;]') + + +def _get_first_line_title(pagedata): + content = pagedata.get('raw_content') or '' + content = content.content.strip() + if not content: + return '' + + m = re_first_line_title.search(content, 1) + if m: + content = content[:m.start()] + + words = content.split(' ') + title = words[0] + cur_word_idx = 1 + while len(title) < 60 and cur_word_idx < len(words): + title += ' ' + words[cur_word_idx] + cur_word_idx += 1 + + return content