git ssb

0+

Grey the earthling / scuttleblog



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.pychanged
scuttleblog.pyView
@@ -47,29 +47,41 @@
4747 user_posts_json = subprocess.check_output(json_posts_args, stdin = user_posts_stream.stdout)
4848 user_posts = json.loads(user_posts_json)
4949 return user_posts
5050
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
5365 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}
5668
5769 def build_post_structure(p):
5870 post = {}
5971 post['frontmatter'] = {}
6072 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']
6274 post['frontmatter']['date'] = datetime.datetime.fromtimestamp(int(p['value']['timestamp'] / 1000), datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')
6375 post['frontmatter']['sequence'] = p['value']['sequence']
64- post['text'] = p['value']['content']['text']
76 + post['body'] = define_post_text(p['value']['content']['text'])['body']
6577 return (post)
6678
6779 def format_post_file(post):
6880 content = ''
6981 content += str(json.dumps(post['frontmatter'], indent = 4))
7082 content += '\n\n'
71- content += str(post['text'])
83 + content += str(post['body'])
7284 return content
7385
7486 def define_post_filename(post):
7587 folder = 'hugo/content/posts'

Built with git-ssb-web