[gnome-software] Fix the appearance of pre-formatted AppStream descriptions



commit fa0bcb3847d24c41494b300aa5d24228744c6e73
Author: Richard Hughes <richard hughsie com>
Date:   Mon Oct 21 19:41:21 2013 +0100

    Fix the appearance of pre-formatted AppStream descriptions

 src/plugins/appstream-common.c |   36 ++++++++++++++++++++++++++++++++----
 src/plugins/appstream-common.h |    2 ++
 src/plugins/appstream-markup.c |   22 ++++++++++++++++++++--
 src/plugins/gs-self-test.c     |    5 +++--
 4 files changed, 57 insertions(+), 8 deletions(-)
---
diff --git a/src/plugins/appstream-common.c b/src/plugins/appstream-common.c
index 18ade72..8c1a649 100644
--- a/src/plugins/appstream-common.c
+++ b/src/plugins/appstream-common.c
@@ -200,11 +200,18 @@ out:
        return count;
 }
 
+typedef enum {
+       APPSTREAM_XML_UNMUNGE_FLAGS_DEFAULT,
+       APPSTREAM_XML_UNMUNGE_FLAGS_KEEP_NEWLINES,
+       APPSTREAM_XML_UNMUNGE_FLAGS_LAST
+} AppstreamXmlUnmungeFlags;
+
 /**
- * appstream_xml_unmunge:
+ * appstream_xml_unmunge_full:
  */
-gchar *
-appstream_xml_unmunge (const gchar *text, gssize text_length)
+static gchar *
+appstream_xml_unmunge_full (const gchar *text, gssize text_length,
+                           AppstreamXmlUnmungeFlags flags)
 {
        GString *str;
        gboolean ignore_whitespace = TRUE;
@@ -217,7 +224,8 @@ appstream_xml_unmunge (const gchar *text, gssize text_length)
        /* ignore repeated whitespace */
        str = g_string_sized_new (text_length);
        for (i = 0; i < text_length; i++) {
-               if (text[i] == ' ' || text[i] == '\n') {
+               if (text[i] == ' ' ||
+                   (text[i] == '\n' && (flags != APPSTREAM_XML_UNMUNGE_FLAGS_KEEP_NEWLINES))) {
                        if (!ignore_whitespace)
                                g_string_append_c (str, ' ');
                        ignore_whitespace = TRUE;
@@ -246,3 +254,23 @@ appstream_xml_unmunge (const gchar *text, gssize text_length)
 
        return g_string_free (str, FALSE);
 }
+
+/**
+ * appstream_xml_unmunge:
+ */
+gchar *
+appstream_xml_unmunge (const gchar *text, gssize text_length)
+{
+       return appstream_xml_unmunge_full (text, text_length,
+                                          APPSTREAM_XML_UNMUNGE_FLAGS_DEFAULT);
+}
+
+/**
+ * appstream_xml_unmunge_safe:
+ */
+gchar *
+appstream_xml_unmunge_safe (const gchar *text, gssize text_length)
+{
+       return appstream_xml_unmunge_full (text, text_length,
+                                          APPSTREAM_XML_UNMUNGE_FLAGS_KEEP_NEWLINES);
+}
diff --git a/src/plugins/appstream-common.h b/src/plugins/appstream-common.h
index 51c68b3..44052b3 100644
--- a/src/plugins/appstream-common.h
+++ b/src/plugins/appstream-common.h
@@ -59,6 +59,8 @@ const gchar   *appstream_tag_to_string        (AppstreamTag    tag);
 guint           appstream_get_locale_value     (const gchar    *lang);
 gchar          *appstream_xml_unmunge          (const gchar    *text,
                                                 gssize          text_length);
+gchar          *appstream_xml_unmunge_safe     (const gchar    *text,
+                                                gssize          text_length);
 
 G_END_DECLS
 
diff --git a/src/plugins/appstream-markup.c b/src/plugins/appstream-markup.c
index 4fe59c0..c73a94e 100644
--- a/src/plugins/appstream-markup.c
+++ b/src/plugins/appstream-markup.c
@@ -122,6 +122,23 @@ appstream_markup_set_mode (AppstreamMarkup *markup, AppstreamMarkupMode mode)
 }
 
 /**
+ * appstream_text_is_whitespace:
+ */
+static gboolean
+appstream_text_is_whitespace (const gchar *text)
+{
+       gboolean ret = TRUE;
+       guint i;
+       for (i = 0; text[i] != '\0'; i++) {
+               if (!g_ascii_isspace (text[i])) {
+                       ret = FALSE;
+                       break;
+               }
+       }
+       return ret;
+}
+
+/**
  * appstream_markup_add_content:
  */
 void
@@ -141,10 +158,11 @@ appstream_markup_add_content (AppstreamMarkup *markup,
        switch (markup->mode) {
        case APPSTREAM_MARKUP_MODE_START:
                /* this is for pre-formatted text */
-               tmp = appstream_xml_unmunge (text, length);
+               tmp = appstream_xml_unmunge_safe (text, length);
                if (tmp == NULL)
                        break;
-               g_string_append_printf (markup->string, "%s", tmp);
+               if (!appstream_text_is_whitespace (tmp))
+                       g_string_append (markup->string, tmp);
                break;
        case APPSTREAM_MARKUP_MODE_P_CONTENT:
                tmp = appstream_xml_unmunge (text, length);
diff --git a/src/plugins/gs-self-test.c b/src/plugins/gs-self-test.c
index 5981dd4..b25f9ab 100644
--- a/src/plugins/gs-self-test.c
+++ b/src/plugins/gs-self-test.c
@@ -60,10 +60,10 @@ appstream_markup_plain_func (void)
        appstream_markup_set_enabled (markup, TRUE);
        appstream_markup_set_lang (markup, NULL);
        appstream_markup_set_mode (markup, APPSTREAM_MARKUP_MODE_START);
-       appstream_markup_add_content (markup, "This is preformatted", -1);
+       appstream_markup_add_content (markup, "This is preformatted\n\nOne", -1);
        appstream_markup_set_mode (markup, APPSTREAM_MARKUP_MODE_END);
        tmp = appstream_markup_get_text (markup);
-       g_assert_cmpstr (tmp, ==, "This is preformatted");
+       g_assert_cmpstr (tmp, ==, "This is preformatted\n\nOne");
 
        appstream_markup_free (markup);
 }
@@ -78,6 +78,7 @@ appstream_markup_tags_func (void)
        appstream_markup_set_enabled (markup, TRUE);
        appstream_markup_set_lang (markup, NULL);
        appstream_markup_set_mode (markup, APPSTREAM_MARKUP_MODE_START);
+       appstream_markup_add_content (markup, "    ", -1);
 
        appstream_markup_set_mode (markup, APPSTREAM_MARKUP_MODE_P_START);
        appstream_markup_add_content (markup, "Para1", -1);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]