Merge pull request #210

This commit is contained in:
Eizen 2016-11-10 00:33:41 -03:00
commit 812b3f972f
3 changed files with 50 additions and 49 deletions

View File

@ -185,51 +185,20 @@ void notification_free(notification * n)
*/ */
char *notification_strip_markup(char *str) char *notification_strip_markup(char *str)
{ {
char *replace_buf, *start, *end;
if (str == NULL) { if (str == NULL) {
return NULL; return NULL;
} }
/* strip all tags */
string_strip_delimited(str, '<', '>');
/* unquote the remainder */
str = string_replace_all("&quot;", "\"", str); str = string_replace_all("&quot;", "\"", str);
str = string_replace_all("&apos;", "'", str); str = string_replace_all("&apos;", "'", str);
str = string_replace_all("&amp;", "&", str); str = string_replace_all("&amp;", "&", str);
str = string_replace_all("&lt;", "<", str); str = string_replace_all("&lt;", "<", str);
str = string_replace_all("&gt;", ">", str); str = string_replace_all("&gt;", ">", str);
/* remove tags */
str = string_replace_all("<b>", "", str);
str = string_replace_all("</b>", "", str);
str = string_replace_all("<br>", " ", str);
str = string_replace_all("<br/>", " ", str);
str = string_replace_all("<br />", " ", str);
str = string_replace_all("<i>", "", str);
str = string_replace_all("</i>", "", str);
str = string_replace_all("<u>", "", str);
str = string_replace_all("</u>", "", str);
str = string_replace_all("</a>", "", str);
while ((start = strstr(str, "<a href")) != NULL) {
end = strstr(start, ">");
if (end != NULL) {
replace_buf = strndup(start, end - start + 1);
str = string_replace(replace_buf, "", str);
free(replace_buf);
} else {
break;
}
}
while ((start = strstr(str, "<img src")) != NULL) {
end = strstr(start, "/>");
if (end != NULL) {
replace_buf = strndup(start, end - start + 2);
str = string_replace(replace_buf, "", str);
free(replace_buf);
} else {
break;
}
}
return str; return str;
} }
@ -270,19 +239,25 @@ char *notification_replace_format(const char *needle, const char *replacement,
tmp = notification_quote_markup(tmp); tmp = notification_quote_markup(tmp);
ret = string_replace_all(needle, tmp, haystack); ret = string_replace_all(needle, tmp, haystack);
free(tmp); free(tmp);
} else if (!allow_markup) { } else {
tmp = strdup(replacement); tmp = strdup(replacement);
if (!settings.ignore_newline) { 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); tmp = string_replace_all("<br/>", "\n", tmp);
tmp = string_replace_all("<br />", "\n", tmp); tmp = string_replace_all("<br />", "\n", tmp);
} }
tmp = notification_strip_markup(tmp);
tmp = notification_quote_markup(tmp); if (!allow_markup) {
tmp = notification_strip_markup(tmp);
tmp = notification_quote_markup(tmp);
}
ret = string_replace_all(needle, tmp, haystack); ret = string_replace_all(needle, tmp, haystack);
free(tmp); free(tmp);
} else {
ret = string_replace_all(needle, replacement, haystack);
} }
return ret; return ret;
@ -320,7 +295,7 @@ char *notification_extract_markup_urls(char **str_ptr) {
} }
free(replace_buf); free(replace_buf);
} else { } else {
break; break;
} }
} }
*str_ptr = str; *str_ptr = str;

37
utils.c
View File

@ -11,10 +11,10 @@
#include "dunst.h" #include "dunst.h"
char *string_replace_char(char needle, char replacement, char *haystack) { char *string_replace_char(char needle, char replacement, char *haystack) {
char *current = haystack; char *current = haystack;
while ((current = strchr (current, needle)) != NULL) while ((current = strchr (current, needle)) != NULL)
*current++ = replacement; *current++ = replacement;
return haystack; return haystack;
} }
char *string_replace_at(char *buf, int pos, int len, const char *repl) char *string_replace_at(char *buf, int pos, int len, const char *repl)
@ -25,13 +25,21 @@ char *string_replace_at(char *buf, int pos, int len, const char *repl)
buf_len = strlen(buf); buf_len = strlen(buf);
repl_len = strlen(repl); repl_len = strlen(repl);
size = (buf_len - len) + repl_len + 1; size = (buf_len - len) + repl_len + 1;
tmp = malloc(size);
if (repl_len <= len) {
tmp = buf;
} else {
tmp = malloc(size);
}
memcpy(tmp, buf, pos); memcpy(tmp, buf, pos);
memcpy(tmp + pos, repl, repl_len); memcpy(tmp + pos, repl, repl_len);
memcpy(tmp + pos + repl_len, buf + pos + len, buf_len - (pos + len) + 1); memmove(tmp + pos + repl_len, buf + pos + len, buf_len - (pos + len) + 1);
if(tmp != buf) {
free(buf);
}
free(buf);
return tmp; return tmp;
} }
@ -105,6 +113,21 @@ char **string_to_argv(const char *s)
return argv; return argv;
} }
void string_strip_delimited(char *str, char a, char b)
{
int iread=-1, iwrite=0, copen=0;
while (str[++iread] != 0) {
if (str[iread] == a) {
++copen;
} else if (str[iread] == b && copen > 0) {
--copen;
} else if (copen == 0) {
str[iwrite++] = str[iread];
}
}
str[iwrite] = 0;
}
int digit_count(int i) int digit_count(int i)
{ {
i = ABS(i); i = ABS(i);

View File

@ -17,6 +17,9 @@ char *string_append(char *a, const char *b, const char *sep);
char **string_to_argv(const char *s); char **string_to_argv(const char *s);
/* strip content between two delimiter characters (inplace) */
void string_strip_delimited(char *str, char a, char b);
/* exit with an error message */ /* exit with an error message */
void die(char *msg, int exit_value); void die(char *msg, int exit_value);