From 18c4b4bf7aaefc570a8fde1f8621315821fd3cfa Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 9 Mar 2017 11:33:06 -0500 Subject: [PATCH] markup.c: Tidy, expand comments This turns a hard-to-understand nested if{} chain into a simple switch statement, and pulls some code out in to utility functions. This is strictly a code-organization change, and should contain no functional changes. --- src/markup.c | 79 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 29 deletions(-) diff --git a/src/markup.c b/src/markup.c index 03365be..6ff1014 100644 --- a/src/markup.c +++ b/src/markup.c @@ -9,14 +9,9 @@ #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; - } + assert(str != NULL); str = string_replace_all("&", "&", str); str = string_replace_all("\"", """, str); @@ -27,8 +22,40 @@ static char *markup_quote(char *str) return str; } +static char *markup_unquote(char *str) +{ + assert(str != 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; +} + +static char *markup_br2nl(char *str) +{ + assert(str != NULL); + + str = string_replace_all("
", "\n", str); + str = string_replace_all("
", "\n", str); + str = string_replace_all("
", "\n", str); + return str; +} + /* - * Strip any markup from text + * Strip any markup from text; turn it in to plain text. + * + * For well-formed markup, the following two commands should be + * roughly equivalent: + * + * out = markup_strip(in); + * pango_parse_markup(in, -1, 0, NULL, &out, NULL, NULL); + * + * However, `pango_parse_markup()` balks at invalid markup; + * `markup_strip()` shouldn't care if there is invalid markup. */ char *markup_strip(char *str) { @@ -40,11 +67,7 @@ char *markup_strip(char *str) 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); + str = markup_unquote(str); return str; } @@ -59,24 +82,22 @@ char *markup_transform(char *str, enum markup_mode markup_mode) return NULL; } - if (markup_mode == MARKUP_NO) { + switch (markup_mode) { + case MARKUP_NULL: + /* `assert(false)`, but with a meaningful error message */ + assert(markup_mode != MARKUP_NULL); + break; + case 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); - } - + break; + case MARKUP_STRIP: + str = markup_br2nl(str); + str = markup_strip(str); + str = markup_quote(str); + break; + case MARKUP_FULL: + str = markup_br2nl(str); + break; } if (settings.ignore_newline) {