X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Foutput.c;h=663700e691512d6e3b89d2834adc384d4697206d;hb=302966374bd7d9ca5b97171cedd3613ae10360c8;hp=f7a88887a3ca9f6b07a798923ad9dd1f9065605b;hpb=974f95702efbf1ae15882777d191c7fde99bfb9b;p=i3%2Fi3status diff --git a/src/output.c b/src/output.c index f7a8888..663700e 100644 --- a/src/output.c +++ b/src/output.c @@ -78,3 +78,45 @@ void print_separator(const char *separator) { void reset_cursor(void) { printf("\033[?25h"); } + +/* + * Escapes ampersand, less-than, greater-than, single-quote, and double-quote + * characters with the corresponding Pango markup strings if markup is enabled. + * See the glib implementation: + * https://git.gnome.org/browse/glib/tree/glib/gmarkup.c?id=03db1f455b4265654e237d2ad55464b4113cba8a#n2142 + * + */ +void maybe_escape_markup(char *text, char **buffer) { + if (markup_format == M_NONE) { + *buffer += sprintf(*buffer, "%s", text); + return; + } + for (; *text != '\0'; text++) { + switch (*text) { + case '&': + *buffer += sprintf(*buffer, "%s", "&"); + break; + case '<': + *buffer += sprintf(*buffer, "%s", "<"); + break; + case '>': + *buffer += sprintf(*buffer, "%s", ">"); + break; + case '\'': + *buffer += sprintf(*buffer, "%s", "'"); + break; + case '"': + *buffer += sprintf(*buffer, "%s", """); + break; + default: + if ((0x1 <= *text && *text <= 0x8) || + (0xb <= *text && *text <= 0xc) || + (0xe <= *text && *text <= 0x1f)) { + *buffer += sprintf(*buffer, "&#x%x;", *text); + } else { + *(*buffer)++ = *text; + } + break; + } + } +}