summaryrefslogtreecommitdiff
path: root/libappstream-glib
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2016-03-01 19:18:28 +0000
committerRichard Hughes <richard@hughsie.com>2016-03-01 19:18:28 +0000
commit501a5cad1f9b7c5ac33b2efca783f56391689158 (patch)
tree3d98b6b07e6a00b1e57597c21edb71009bbfab0f /libappstream-glib
parent7163dca61a432b8cb57e2b705d385f6fbe97905a (diff)
downloadappstream-glib-501a5cad1f9b7c5ac33b2efca783f56391689158.tar.gz
Add as_markup_import()
Imports unformatted text and converts to AppStream markup.
Diffstat (limited to 'libappstream-glib')
-rw-r--r--libappstream-glib/as-self-test.c22
-rw-r--r--libappstream-glib/as-utils.c42
-rw-r--r--libappstream-glib/as-utils.h1
3 files changed, 65 insertions, 0 deletions
diff --git a/libappstream-glib/as-self-test.c b/libappstream-glib/as-self-test.c
index ad4aef9..1420139 100644
--- a/libappstream-glib/as-self-test.c
+++ b/libappstream-glib/as-self-test.c
@@ -3727,6 +3727,27 @@ as_test_utils_spdx_token_func (void)
}
static void
+as_test_utils_markup_import_func (void)
+{
+ guint i;
+ struct {
+ const gchar *old;
+ const gchar *new;
+ } table[] = {
+ { "", NULL },
+ { "dave", "<p>dave</p>" },
+ { "dave!\ndave?", "<p>dave! dave?</p>" },
+ { "dave!\n\ndave?", "<p>dave!</p><p>dave?</p>" },
+ { NULL, NULL }
+ };
+ for (i = 0; table[i].old != NULL; i++) {
+ g_autofree gchar *new = NULL;
+ new = as_markup_import (table[i].old);
+ g_assert_cmpstr (new, ==, table[i].new);
+ }
+}
+
+static void
as_test_utils_func (void)
{
gboolean ret;
@@ -4618,6 +4639,7 @@ main (int argc, char **argv)
g_test_add_func ("/AppStream/node{intltool}", as_test_node_intltool_func);
g_test_add_func ("/AppStream/node{sort}", as_test_node_sort_func);
g_test_add_func ("/AppStream/utils", as_test_utils_func);
+ g_test_add_func ("/AppStream/utils{markup-import}", as_test_utils_markup_import_func);
g_test_add_func ("/AppStream/utils{version}", as_test_utils_version_func);
g_test_add_func ("/AppStream/utils{guid}", as_test_utils_guid_func);
g_test_add_func ("/AppStream/utils{icons}", as_test_utils_icons_func);
diff --git a/libappstream-glib/as-utils.c b/libappstream-glib/as-utils.c
index 171ef74..3cf2a24 100644
--- a/libappstream-glib/as-utils.c
+++ b/libappstream-glib/as-utils.c
@@ -58,6 +58,48 @@
G_DEFINE_QUARK (as-utils-error-quark, as_utils_error)
/**
+ * as_markup_import:
+ * @text: the text to import.
+ *
+ * Imports unformatted text and converts to AppStream markup.
+ *
+ * Returns: (transfer full): appstream markup, or %NULL in event of an error
+ *
+ * Since: 0.5.11
+ */
+gchar *
+as_markup_import (const gchar *text)
+{
+ GString *str;
+ guint i;
+ g_auto(GStrv) lines = NULL;
+
+ /* empty */
+ if (text == NULL || text[0] == '\0')
+ return NULL;
+
+ /* just assume paragraphs */
+ str = g_string_new ("<p>");
+ lines = g_strsplit (text, "\n", -1);
+ for (i = 0; lines[i] != NULL; i++) {
+ g_autofree gchar *markup = NULL;
+ if (lines[i][0] == '\0') {
+ if (g_str_has_suffix (str->str, " "))
+ g_string_truncate (str, str->len - 1);
+ g_string_append (str, "</p><p>");
+ continue;
+ }
+ markup = g_markup_escape_text (lines[i], -1);
+ g_string_append (str, markup);
+ g_string_append (str, " ");
+ }
+ if (g_str_has_suffix (str->str, " "))
+ g_string_truncate (str, str->len - 1);
+ g_string_append (str, "</p>");
+ return g_string_free (str, FALSE);
+}
+
+/**
* as_markup_strsplit_words:
* @text: the text to split.
* @line_len: the maximum length of the output line
diff --git a/libappstream-glib/as-utils.h b/libappstream-glib/as-utils.h
index e84c02f..b6dae41 100644
--- a/libappstream-glib/as-utils.h
+++ b/libappstream-glib/as-utils.h
@@ -135,6 +135,7 @@ gboolean as_markup_validate (const gchar *markup,
GError **error);
gchar **as_markup_strsplit_words (const gchar *text,
guint line_len);
+gchar *as_markup_import (const gchar *text);
GQuark as_utils_error_quark (void);
gboolean as_utils_is_stock_icon_name (const gchar *name);