From 0c84e53afb7ff1a7671fd2568474c7ef77d69b33 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Mon, 20 Feb 2017 01:22:39 -0500 Subject: [PATCH] Move markup handling into markup.{c,h} This is strictly a code-organization change, and should contain no functional changes. --- src/markup.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++ src/markup.h | 11 ++++++ src/notification.c | 68 ++--------------------------------- src/notification.h | 2 -- src/x.c | 3 +- 5 files changed, 104 insertions(+), 69 deletions(-) create mode 100644 src/markup.c create mode 100644 src/markup.h diff --git a/src/markup.c b/src/markup.c new file mode 100644 index 0000000..03365be --- /dev/null +++ b/src/markup.c @@ -0,0 +1,89 @@ +/* copyright 2013 Sascha Kruse and contributors (see LICENSE for licensing information) */ + +#define _GNU_SOURCE +#include "markup.h" + +#include +#include + +#include "settings.h" +#include "utils.h" + +/* + * Quote a text string for rendering with pango + */ +static char *markup_quote(char *str) +{ + if (str == NULL) { + return NULL; + } + + str = string_replace_all("&", "&", str); + str = string_replace_all("\"", """, str); + str = string_replace_all("'", "'", str); + str = string_replace_all("<", "<", str); + str = string_replace_all(">", ">", str); + + return str; +} + +/* + * Strip any markup from text + */ +char *markup_strip(char *str) +{ + if (str == NULL) { + return NULL; + } + + /* strip all tags */ + string_strip_delimited(str, '<', '>'); + + /* unquote the remainder */ + str = string_replace_all(""", "\"", str); + str = string_replace_all("'", "'", str); + str = string_replace_all("&", "&", str); + str = string_replace_all("<", "<", str); + str = string_replace_all(">", ">", str); + + return str; +} + +/* + * Transform the string in accordance with `markup_mode` and + * `settings.ignore_newline` + */ +char *markup_transform(char *str, enum markup_mode markup_mode) +{ + if (str == NULL) { + return NULL; + } + + if (markup_mode == MARKUP_NO) { + str = markup_quote(str); + } else { + if (settings.ignore_newline) { + str = string_replace_all("
", " ", str); + str = string_replace_all("
", " ", str); + str = string_replace_all("
", " ", str); + } else { + str = string_replace_all("
", "\n", str); + str = string_replace_all("
", "\n", str); + str = string_replace_all("
", "\n", str); + } + + if (markup_mode != MARKUP_FULL ) { + str = markup_strip(str); + str = markup_quote(str); + } + + } + + if (settings.ignore_newline) { + str = string_replace_all("\n", " ", str); + } + + return str; +} + +/* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ diff --git a/src/markup.h b/src/markup.h new file mode 100644 index 0000000..8304e2d --- /dev/null +++ b/src/markup.h @@ -0,0 +1,11 @@ +/* copyright 2013 Sascha Kruse and contributors (see LICENSE for licensing information) */ +#ifndef DUNST_MARKUP_H +#define DUNST_MARKUP_H + +#include "settings.h" + +char *markup_strip(char *str); +char *markup_transform(char *str, enum markup_mode markup_mode); + +#endif +/* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ diff --git a/src/notification.c b/src/notification.c index 141fdfe..930a0f8 100644 --- a/src/notification.c +++ b/src/notification.c @@ -16,6 +16,7 @@ #include "dbus.h" #include "dunst.h" +#include "markup.h" #include "menu.h" #include "rules.h" #include "settings.h" @@ -196,46 +197,6 @@ void notification_free(notification * n) g_free(n); } -/* - * Strip any markup from text - */ -char *notification_strip_markup(char *str) -{ - if (str == NULL) { - return NULL; - } - - /* strip all tags */ - string_strip_delimited(str, '<', '>'); - - /* unquote the remainder */ - str = string_replace_all(""", "\"", str); - str = string_replace_all("'", "'", str); - str = string_replace_all("&", "&", str); - str = string_replace_all("<", "<", str); - str = string_replace_all(">", ">", str); - - return str; -} - -/* - * Quote a text string for rendering with pango - */ -char *notification_quote_markup(char *str) -{ - if (str == NULL) { - return NULL; - } - - str = string_replace_all("&", "&", str); - str = string_replace_all("\"", """, str); - str = string_replace_all("'", "'", str); - str = string_replace_all("<", "<", str); - str = string_replace_all(">", ">", str); - - return str; -} - /* * Replace all occurrences of "needle" with a quoted "replacement", * according to the markup settings. @@ -245,32 +206,7 @@ char *notification_replace_format(const char *needle, const char *replacement, char* tmp; char* ret; - if (markup_mode == MARKUP_NO) { - tmp = g_strdup(replacement); - tmp = notification_quote_markup(tmp); - } else { - tmp = g_strdup(replacement); - if (settings.ignore_newline) { - tmp = string_replace_all("
", " ", tmp); - tmp = string_replace_all("
", " ", tmp); - tmp = string_replace_all("
", " ", tmp); - } else { - tmp = string_replace_all("
", "\n", tmp); - tmp = string_replace_all("
", "\n", tmp); - tmp = string_replace_all("
", "\n", tmp); - } - - if (markup_mode != MARKUP_FULL ) { - tmp = notification_strip_markup(tmp); - tmp = notification_quote_markup(tmp); - } - - } - - if (settings.ignore_newline) { - tmp = string_replace_all("\n", " ", tmp); - } - + tmp = markup_transform(g_strdup(replacement), markup_mode); ret = string_replace_all(needle, tmp, haystack); g_free(tmp); diff --git a/src/notification.h b/src/notification.h index 777ce73..5d0fa5d 100644 --- a/src/notification.h +++ b/src/notification.h @@ -69,8 +69,6 @@ int notification_is_duplicate(const notification *a, const notification *b); void notification_run_script(notification * n); int notification_close(notification * n, int reason); void notification_print(notification * n); -char *notification_strip_markup(char *str); -char *notification_quote_markup(char *str); char *notification_replace_format(const char *needle, const char *replacement, char *haystack, enum markup_mode markup); void notification_update_text_to_render(notification *n); int notification_get_ttl(notification *n); diff --git a/src/x.c b/src/x.c index 310c341..dbb074a 100644 --- a/src/x.c +++ b/src/x.c @@ -22,6 +22,7 @@ #include #include "dunst.h" +#include "markup.h" #include "notification.h" #include "settings.h" #include "utils.h" @@ -475,7 +476,7 @@ static colored_layout *r_create_layout_from_notification(cairo_t *c, notificatio pango_layout_set_attributes(cl->l, cl->attr); } else { /* remove markup and display plain message instead */ - n->text_to_render = notification_strip_markup(n->text_to_render); + n->text_to_render = markup_strip(n->text_to_render); cl->text = NULL; cl->attr = NULL; pango_layout_set_text(cl->l, n->text_to_render, -1);