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.
This commit is contained in:
parent
c645ba3106
commit
18c4b4bf7a
75
src/markup.c
75
src/markup.c
@ -9,14 +9,9 @@
|
|||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
/*
|
|
||||||
* Quote a text string for rendering with pango
|
|
||||||
*/
|
|
||||||
static char *markup_quote(char *str)
|
static char *markup_quote(char *str)
|
||||||
{
|
{
|
||||||
if (str == NULL) {
|
assert(str != NULL);
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
str = string_replace_all("&", "&", str);
|
str = string_replace_all("&", "&", str);
|
||||||
str = string_replace_all("\"", """, str);
|
str = string_replace_all("\"", """, str);
|
||||||
@ -27,8 +22,40 @@ static char *markup_quote(char *str)
|
|||||||
return 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("<br>", "\n", str);
|
||||||
|
str = string_replace_all("<br/>", "\n", str);
|
||||||
|
str = string_replace_all("<br />", "\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)
|
char *markup_strip(char *str)
|
||||||
{
|
{
|
||||||
@ -40,11 +67,7 @@ char *markup_strip(char *str)
|
|||||||
string_strip_delimited(str, '<', '>');
|
string_strip_delimited(str, '<', '>');
|
||||||
|
|
||||||
/* unquote the remainder */
|
/* unquote the remainder */
|
||||||
str = string_replace_all(""", "\"", str);
|
str = markup_unquote(str);
|
||||||
str = string_replace_all("'", "'", str);
|
|
||||||
str = string_replace_all("&", "&", str);
|
|
||||||
str = string_replace_all("<", "<", str);
|
|
||||||
str = string_replace_all(">", ">", str);
|
|
||||||
|
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
@ -59,24 +82,22 @@ char *markup_transform(char *str, enum markup_mode markup_mode)
|
|||||||
return NULL;
|
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);
|
str = markup_quote(str);
|
||||||
} else {
|
break;
|
||||||
if (settings.ignore_newline) {
|
case MARKUP_STRIP:
|
||||||
str = string_replace_all("<br>", " ", str);
|
str = markup_br2nl(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_strip(str);
|
||||||
str = markup_quote(str);
|
str = markup_quote(str);
|
||||||
}
|
break;
|
||||||
|
case MARKUP_FULL:
|
||||||
|
str = markup_br2nl(str);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (settings.ignore_newline) {
|
if (settings.ignore_newline) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user