Commit 469a52f6174099d9915680396eba58ded48066c8
Simplify creation of post titles
Attempt to detect headings at the start of a post, and build the title differently depending. If the post starts with a heading, use the entire heading as the title, and strip it from the body text. Otherwise, use the first line truncated as the title, and leave everything in the body text.Greg K Nicholson committed on 10/15/2017, 2:53:01 PM
Parent: 263da30c19e43e7f093fd17ccb3b89c863d73c7c
Files changed
scuttleblog.py | changed |
scuttleblog.py | ||
---|---|---|
@@ -47,29 +47,41 @@ | ||
47 | 47 … | user_posts_json = subprocess.check_output(json_posts_args, stdin = user_posts_stream.stdout) |
48 | 48 … | user_posts = json.loads(user_posts_json) |
49 | 49 … | return user_posts |
50 | 50 … | |
51 | -def define_post_title(text): | |
52 | - title = text.strip().splitlines()[0] | |
51 … | +def define_post_text(text): | |
52 … | + text = text.strip() | |
53 … | + if re.match('#', text): | |
54 … | + # The first line is a heading. Use it as the title and strip it from the body. | |
55 … | + title = text.splitlines()[0] | |
56 … | + body = ''.join(text.splitlines(keepends=True)[1:]).strip() \ | |
57 … | + if len(text.splitlines()) > 1 \ | |
58 … | + else '' | |
59 … | + else: | |
60 … | + # Truncate the first line and use it as the title. Keep everything in the body. | |
61 … | + maxlength = 100 | |
62 … | + ellipsis = '...' | |
63 … | + title = text if len(text) <= maxlength else text[:maxlength].rsplit(' ', 1)[0] + ellipsis | |
64 … | + body = text | |
53 | 65 … | title = re.sub('^\W+', '', title) |
54 | - titleparts = re.split('([.?!\[\]])', title) | |
55 | - return titleparts[0] + titleparts[1] if len(titleparts) > 1 else titleparts[0] | |
66 … | + # FIXME: This doesn't gracefully handle markdown near the start of a post. | |
67 … | + return {'title': title, 'body': body} | |
56 | 68 … | |
57 | 69 … | def build_post_structure(p): |
58 | 70 … | post = {} |
59 | 71 … | post['frontmatter'] = {} |
60 | 72 … | post['frontmatter']['key'] = p['key'] |
61 | - post['frontmatter']['title'] = define_post_title(p['value']['content']['text']) | |
73 … | + post['frontmatter']['title'] = define_post_text(p['value']['content']['text'])['title'] | |
62 | 74 … | post['frontmatter']['date'] = datetime.datetime.fromtimestamp(int(p['value']['timestamp'] / 1000), datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ') |
63 | 75 … | post['frontmatter']['sequence'] = p['value']['sequence'] |
64 | - post['text'] = p['value']['content']['text'] | |
76 … | + post['body'] = define_post_text(p['value']['content']['text'])['body'] | |
65 | 77 … | return (post) |
66 | 78 … | |
67 | 79 … | def format_post_file(post): |
68 | 80 … | content = '' |
69 | 81 … | content += str(json.dumps(post['frontmatter'], indent = 4)) |
70 | 82 … | content += '\n\n' |
71 | - content += str(post['text']) | |
83 … | + content += str(post['body']) | |
72 | 84 … | return content |
73 | 85 … | |
74 | 86 … | def define_post_filename(post): |
75 | 87 … | folder = 'hugo/content/posts' |
Built with git-ssb-web