From 4a7e174fe8a69db51b4ac4fff462e5475af52920 Mon Sep 17 00:00:00 2001 From: progandy Date: Wed, 27 Feb 2013 17:00:09 +0100 Subject: [PATCH] add number for links with markup in body, handle them separately from markup removal Conflicts: notification.c --- notification.c | 67 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 60 insertions(+), 7 deletions(-) diff --git a/notification.c b/notification.c index 5565c03..e7c3589 100644 --- a/notification.c +++ b/notification.c @@ -187,28 +187,71 @@ char *notification_fix_markup(char *str) str = string_replace_all("", "", str); str = string_replace_all("", "", str); - start = strstr(str, ""); + while ((start = strstr(str, ""); if (end != NULL) { replace_buf = strndup(start, end - start + 1); str = string_replace(replace_buf, "", str); free(replace_buf); + } else { + break; } } - start = strstr(str, ""); + + while ((start = strstr(str, ""); if (end != NULL) { replace_buf = strndup(start, end - start + 2); str = string_replace(replace_buf, "", str); free(replace_buf); + } else { + break; } } return str; } + +char *notification_extract_markup_urls(char **str_ptr) { + char *start, *end, *replace_buf, *str, *urls = NULL, *url, *index_buf; + int linkno = 1; + + str = *str_ptr; + while ((start = strstr(str, ""); + if (end != NULL) { + replace_buf = strndup(start, end - start + 1); + url = extract_urls(replace_buf); + if (url != NULL) { + str = string_replace(replace_buf, "[", str); + + index_buf = g_strdup_printf("[#%d]", linkno++); + if (urls == NULL) { + urls = g_strconcat(index_buf, " ", url, NULL); + } else { + char *tmp = urls; + urls = g_strconcat(tmp, "\n", index_buf, " ", url, NULL); + free(tmp); + } + + index_buf[0] = ' '; + str = string_replace("", index_buf, str); + free(index_buf); + free(url); + } else { + str = string_replace(replace_buf, "", str); + str = string_replace("", "", str); + } + free(replace_buf); + } else { + break; + } + } + *str_ptr = str; + return urls; +} + /* * Initialize the given notification and add it to * the queue. Replace notification with id if id > 0. @@ -236,6 +279,8 @@ int notification_init(notification * n, int id) rule_apply_all(n); + n->urls = notification_extract_markup_urls(&(n->body)); + n->msg = string_replace("%a", n->appname, g_strdup(n->format)); n->msg = string_replace("%s", n->summary, n->msg); if (n->icon) { @@ -326,7 +371,15 @@ int notification_init(notification * n, int id) char *tmp = g_strconcat(n->summary, " ", n->body, NULL); - n->urls = extract_urls(tmp); + char *tmp_urls = extract_urls(tmp); + if (tmp_urls != NULL) { + if (n->urls != NULL) { + n->urls = string_append(n->urls, tmp_urls, "\n"); + free(tmp_urls); + } else { + n->urls = tmp_urls; + } + } if (n->actions) { n->actions->dmenu_str = NULL;