summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2019-07-18 20:24:32 -0400
committerJakob Kaivo <jkk@ung.org>2019-07-18 20:24:32 -0400
commite77c62e0f9725acca092f718b07555e841372190 (patch)
treebdeb2cc668e285a873bb1594d53b6649262dac6d
parent16714b9d818c059605667fa7f589e6e9f6593833 (diff)
trime whitespace from title, avoid duplicate hyphens in URI
-rw-r--r--blog.c17
1 files changed, 16 insertions, 1 deletions
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);