changeset 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 e1cadbfddb48
children 8101692fdc11
files piecrust/admin/views/sources.py
diffstat 1 files changed, 26 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- 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 '<empty page>'
+
+    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