Move markup handling into markup.{c,h}

This is strictly a code-organization change, and should contain no
functional changes.
This commit is contained in:
Luke Shumaker 2017-02-20 01:22:39 -05:00
parent 1d4a26c1d0
commit 0c84e53afb
5 changed files with 104 additions and 69 deletions

89
src/markup.c Normal file
View File

@ -0,0 +1,89 @@
/* copyright 2013 Sascha Kruse and contributors (see LICENSE for licensing information) */
#define _GNU_SOURCE
#include "markup.h"
#include <assert.h>
#include <stdbool.h>
#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("&", "&amp;", str);
str = string_replace_all("\"", "&quot;", str);
str = string_replace_all("'", "&apos;", str);
str = string_replace_all("<", "&lt;", str);
str = string_replace_all(">", "&gt;", 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("&quot;", "\"", str);
str = string_replace_all("&apos;", "'", str);
str = string_replace_all("&amp;", "&", str);
str = string_replace_all("&lt;", "<", str);
str = string_replace_all("&gt;", ">", 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("<br>", " ", str);
str = string_replace_all("<br/>", " ", str);
str = string_replace_all("<br />", " ", str);
} else {
str = string_replace_all("<br>", "\n", str);
str = string_replace_all("<br/>", "\n", str);
str = string_replace_all("<br />", "\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: */

11
src/markup.h Normal file
View File

@ -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: */

View File

@ -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("&quot;", "\"", str);
str = string_replace_all("&apos;", "'", str);
str = string_replace_all("&amp;", "&", str);
str = string_replace_all("&lt;", "<", str);
str = string_replace_all("&gt;", ">", 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("&", "&amp;", str);
str = string_replace_all("\"", "&quot;", str);
str = string_replace_all("'", "&apos;", str);
str = string_replace_all("<", "&lt;", str);
str = string_replace_all(">", "&gt;", 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("<br>", " ", tmp);
tmp = string_replace_all("<br/>", " ", tmp);
tmp = string_replace_all("<br />", " ", tmp);
} else {
tmp = string_replace_all("<br>", "\n", tmp);
tmp = string_replace_all("<br/>", "\n", tmp);
tmp = string_replace_all("<br />", "\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);

View File

@ -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);

View File

@ -22,6 +22,7 @@
#include <unistd.h>
#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);