From e77c62e0f9725acca092f718b07555e841372190 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Thu, 18 Jul 2019 20:24:32 -0400 Subject: trime whitespace from title, avoid duplicate hyphens in URI --- blog.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/blog.c b/blog.c index c46f302..fc7a8d5 100644 --- a/blog.c +++ b/blog.c @@ -49,11 +49,26 @@ int handle_post(void) mkdirat(blogdir, ymd, 0755); char *title = find_post_data("title"); + while (isblank(*title)) { + title++; + } + size_t len = strlen(title); + while (isblank(title[--len])) { + title[len] = '\0'; + } + char uri[FILENAME_MAX] = { 0 }; char *end = stpcpy(uri, ymd); *end++ = '/'; + int dash = 0; for (char *f = title; *f != '\0'; f++) { - *end++ = isalnum(*f) ? tolower(*f) : '-'; + if (isalnum(*f)) { + *end++ = tolower(*f); + dash = 0; + } else if (!dash) { + dash = 1; + *end++ = '-'; + } } int newpost = openat(blogdir, uri, O_WRONLY | O_CREAT, 0644); -- cgit v1.2.1