[gnome-software] Automatically enable escaping when outputting PangoMarkup



commit d574db1f6a2e5d955a818aa31108fded843e8892
Author: Richard Hughes <richard hughsie com>
Date:   Thu Nov 14 10:51:26 2013 +0000

    Automatically enable escaping when outputting PangoMarkup
    
    Also add quite a few self tests.
    
    Resolves: https://bugzilla.gnome.org/show_bug.cgi?id=712256

 src/gs-markdown.c  |   58 ++++++++----------
 src/gs-markdown.h  |   10 ++--
 src/gs-self-test.c |  176 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 207 insertions(+), 37 deletions(-)
---
diff --git a/src/gs-markdown.c b/src/gs-markdown.c
index ef02dba..570af88 100644
--- a/src/gs-markdown.c
+++ b/src/gs-markdown.c
@@ -749,16 +749,17 @@ out:
 /**
  * gs_markdown_set_output_kind:
  **/
-gboolean
+void
 gs_markdown_set_output_kind (GsMarkdown *self, GsMarkdownOutputKind output)
 {
        GsMarkdownPrivate *priv = gs_markdown_get_instance_private (self);
-       gboolean ret = TRUE;
 
-       g_return_val_if_fail (GS_IS_MARKDOWN (self), FALSE);
+       g_return_if_fail (GS_IS_MARKDOWN (self));
 
-       /* PangoMarkup */
-       if (output == GS_MARKDOWN_OUTPUT_PANGO) {
+       priv->output = output;
+       switch (output) {
+       case GS_MARKDOWN_OUTPUT_PANGO:
+               /* PangoMarkup */
                priv->tags.em_start = "<i>";
                priv->tags.em_end = "</i>";
                priv->tags.strong_start = "<b>";
@@ -772,9 +773,10 @@ gs_markdown_set_output_kind (GsMarkdown *self, GsMarkdownOutputKind output)
                priv->tags.bullet_start = "• ";
                priv->tags.bullet_end = "";
                priv->tags.rule = "⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯\n";
-
-       /* XHTML */
-       } else if (output == GS_MARKDOWN_OUTPUT_HTML) {
+               priv->escape = TRUE;
+               break;
+       case GS_MARKDOWN_OUTPUT_HTML:
+               /* XHTML */
                priv->tags.em_start = "<em>";
                priv->tags.em_end = "<em>";
                priv->tags.strong_start = "<strong>";
@@ -788,9 +790,10 @@ gs_markdown_set_output_kind (GsMarkdown *self, GsMarkdownOutputKind output)
                priv->tags.bullet_start = "<li>";
                priv->tags.bullet_end = "</li>";
                priv->tags.rule = "<hr>";
-
-       /* plain text */
-       } else if (output == GS_MARKDOWN_OUTPUT_TEXT) {
+               priv->escape = TRUE;
+               break;
+       case GS_MARKDOWN_OUTPUT_TEXT:
+               /* plain text */
                priv->tags.em_start = "";
                priv->tags.em_end = "";
                priv->tags.strong_start = "";
@@ -804,65 +807,56 @@ gs_markdown_set_output_kind (GsMarkdown *self, GsMarkdownOutputKind output)
                priv->tags.bullet_start = "* ";
                priv->tags.bullet_end = "";
                priv->tags.rule = " ----- \n";
-
-       /* unknown */
-       } else {
+               priv->escape = FALSE;
+               break;
+       default:
                g_warning ("unknown output enum");
-               ret = FALSE;
+               break;
        }
-
-       /* save if valid */
-       if (ret)
-               priv->output = output;
-       return ret;
 }
 
 /**
  * gs_markdown_set_max_lines:
  **/
-gboolean
+void
 gs_markdown_set_max_lines (GsMarkdown *self, gint max_lines)
 {
        GsMarkdownPrivate *priv = gs_markdown_get_instance_private (self);
-       g_return_val_if_fail (GS_IS_MARKDOWN (self), FALSE);
+       g_return_if_fail (GS_IS_MARKDOWN (self));
        priv->max_lines = max_lines;
-       return TRUE;
 }
 
 /**
  * gs_markdown_set_smart_quoting:
  **/
-gboolean
+void
 gs_markdown_set_smart_quoting (GsMarkdown *self, gboolean smart_quoting)
 {
        GsMarkdownPrivate *priv = gs_markdown_get_instance_private (self);
-       g_return_val_if_fail (GS_IS_MARKDOWN (self), FALSE);
+       g_return_if_fail (GS_IS_MARKDOWN (self));
        priv->smart_quoting = smart_quoting;
-       return TRUE;
 }
 
 /**
  * gs_markdown_set_escape:
  **/
-gboolean
+void
 gs_markdown_set_escape (GsMarkdown *self, gboolean escape)
 {
        GsMarkdownPrivate *priv = gs_markdown_get_instance_private (self);
-       g_return_val_if_fail (GS_IS_MARKDOWN (self), FALSE);
+       g_return_if_fail (GS_IS_MARKDOWN (self));
        priv->escape = escape;
-       return TRUE;
 }
 
 /**
  * gs_markdown_set_autocode:
  **/
-gboolean
+void
 gs_markdown_set_autocode (GsMarkdown *self, gboolean autocode)
 {
        GsMarkdownPrivate *priv = gs_markdown_get_instance_private (self);
-       g_return_val_if_fail (GS_IS_MARKDOWN (self), FALSE);
+       g_return_if_fail (GS_IS_MARKDOWN (self));
        priv->autocode = autocode;
-       return TRUE;
 }
 
 /**
diff --git a/src/gs-markdown.h b/src/gs-markdown.h
index 04dbc1f..fdbb533 100644
--- a/src/gs-markdown.h
+++ b/src/gs-markdown.h
@@ -49,15 +49,15 @@ typedef enum {
 
 GType           gs_markdown_get_type                   (void);
 GsMarkdown     *gs_markdown_new                        (void);
-gboolean        gs_markdown_set_output_kind            (GsMarkdown             *self,
+void            gs_markdown_set_output_kind            (GsMarkdown             *self,
                                                         GsMarkdownOutputKind    output);
-gboolean        gs_markdown_set_max_lines              (GsMarkdown             *self,
+void            gs_markdown_set_max_lines              (GsMarkdown             *self,
                                                         gint                    max_lines);
-gboolean        gs_markdown_set_smart_quoting          (GsMarkdown             *self,
+void            gs_markdown_set_smart_quoting          (GsMarkdown             *self,
                                                         gboolean                smart_quoting);
-gboolean        gs_markdown_set_escape                 (GsMarkdown             *self,
+void            gs_markdown_set_escape                 (GsMarkdown             *self,
                                                         gboolean                escape);
-gboolean        gs_markdown_set_autocode               (GsMarkdown             *self,
+void            gs_markdown_set_autocode               (GsMarkdown             *self,
                                                         gboolean                autocode);
 gchar          *gs_markdown_parse                      (GsMarkdown             *self,
                                                         const gchar            *text);
diff --git a/src/gs-self-test.c b/src/gs-self-test.c
index fb3af1c..b830645 100644
--- a/src/gs-self-test.c
+++ b/src/gs-self-test.c
@@ -27,11 +27,186 @@
 #include <glib/gstdio.h>
 
 #include "gs-app.h"
+#include "gs-markdown.h"
 #include "gs-plugin.h"
 #include "gs-plugin-loader.h"
 #include "gs-plugin-loader-sync.h"
 #include "gs-utils.h"
 
+static void
+gs_markdown_func (void)
+{
+       GsMarkdown *md;
+       gchar *text;
+       const gchar *markdown;
+       const gchar *markdown_expected;
+
+       /* get GsMarkdown object */
+       md = gs_markdown_new ();
+       g_assert (md);
+
+       gs_markdown_set_output_kind (md, GS_MARKDOWN_OUTPUT_PANGO);
+
+       markdown = "OEMs\n"
+                  "====\n"
+                  " - Bullett\n";
+       markdown_expected =
+                  "<big>OEMs</big>\n"
+                  "• Bullett";
+       /* markdown (type2 header) */
+       text = gs_markdown_parse (md, markdown);
+       g_assert_cmpstr (text, ==, markdown_expected);
+       g_free (text);
+
+       /* markdown (autocode) */
+       markdown = "this is http://www.hughsie.com/with_spaces_in_url inline link\n";
+       markdown_expected = "this is <tt>http://www.hughsie.com/with_spaces_in_url</tt> inline link";
+       gs_markdown_set_autocode (md, TRUE);
+       text = gs_markdown_parse (md, markdown);
+       g_assert_cmpstr (text, ==, markdown_expected);
+       g_free (text);
+
+       /* markdown some invalid header */
+       markdown = "*** This software is currently in alpha state ***\n";
+       markdown_expected = "<b><i> This software is currently in alpha state </b></i>";
+       text = gs_markdown_parse (md, markdown);
+       g_assert_cmpstr (text, ==, markdown_expected);
+       g_free (text);
+
+       /* markdown (complex1) */
+       markdown = " - This is a *very*\n"
+                  "   short paragraph\n"
+                  "   that is not usual.\n"
+                  " - Another";
+       markdown_expected =
+                  "• This is a <i>very</i> short paragraph that is not usual.\n"
+                  "• Another";
+       text = gs_markdown_parse (md, markdown);
+       g_assert_cmpstr (text, ==, markdown_expected);
+       g_free (text);
+
+       /* markdown (complex1) */
+       markdown = "*  This is a *very*\n"
+                  "   short paragraph\n"
+                  "   that is not usual.\n"
+                  "*  This is the second\n"
+                  "   bullett point.\n"
+                  "*  And the third.\n"
+                  " \n"
+                  "* * *\n"
+                  " \n"
+                  "Paragraph one\n"
+                  "isn't __very__ long at all.\n"
+                  "\n"
+                  "Paragraph two\n"
+                  "isn't much better.";
+       markdown_expected =
+                  "• This is a <i>very</i> short paragraph that is not usual.\n"
+                  "• This is the second bullett point.\n"
+                  "• And the third.\n"
+                  "⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯\n"
+                  "Paragraph one isn&apos;t <b>very</b> long at all.\n"
+                  "Paragraph two isn&apos;t much better.";
+       text = gs_markdown_parse (md, markdown);
+       g_assert_cmpstr (text, ==, markdown_expected);
+       g_free (text);
+
+       markdown = "This is a spec file description or\n"
+                  "an **update** description in bohdi.\n"
+                  "\n"
+                  "* * *\n"
+                  "# Big title #\n"
+                  "\n"
+                  "The *following* things 'were' fixed:\n"
+                  "- Fix `dave`\n"
+                  "* Fubar update because of \"security\"\n";
+       markdown_expected =
+                  "This is a spec file description or an <b>update</b> description in bohdi.\n"
+                  "⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯\n"
+                  "<big>Big title</big>\n"
+                  "The <i>following</i> things 'were' fixed:\n"
+                  "• Fix <tt>dave</tt>\n"
+                  "• Fubar update because of \"security\"";
+       /* markdown (complex2) */
+       text = gs_markdown_parse (md, markdown);
+       if (g_strcmp0 (text, markdown_expected) == 0)
+       g_assert_cmpstr (text, ==, markdown_expected);
+       g_free (text);
+
+       /* markdown (list with spaces) */
+       markdown = "* list seporated with spaces -\n"
+                  "  first item\n"
+                  "\n"
+                  "* second item\n"
+                  "\n"
+                  "* third item\n";
+       markdown_expected =
+                  "• list seporated with spaces - first item\n"
+                  "• second item\n"
+                  "• third item";
+       text = gs_markdown_parse (md, markdown);
+       g_assert_cmpstr (text, ==, markdown_expected);
+       g_free (text);
+
+       gs_markdown_set_max_lines (md, 1);
+
+       /* markdown (one line limit) */
+       markdown = "* list seporated with spaces -\n"
+                  "  first item\n"
+                  "* second item\n";
+       markdown_expected =
+                  "• list seporated with spaces - first item";
+       text = gs_markdown_parse (md, markdown);
+       g_assert_cmpstr (text, ==, markdown_expected);
+       g_free (text);
+
+       gs_markdown_set_max_lines (md, 1);
+
+       /* markdown (escaping) */
+       markdown = "* list & <spaces>";
+       markdown_expected =
+                  "• list &amp; &lt;spaces&gt;";
+       text = gs_markdown_parse (md, markdown);
+       g_assert_cmpstr (text, ==, markdown_expected);
+       g_free (text);
+
+       /* markdown (free text) */
+       gs_markdown_set_escape (md, FALSE);
+       text = gs_markdown_parse (md, "This isn't a present");
+       g_assert_cmpstr (text, ==, "This isn't a present");
+       g_free (text);
+
+       /* markdown (autotext underscore) */
+       text = gs_markdown_parse (md, "This isn't CONFIG_UEVENT_HELPER_PATH present");
+       g_assert_cmpstr (text, ==, "This isn't <tt>CONFIG_UEVENT_HELPER_PATH</tt> present");
+       g_free (text);
+
+       /* markdown (end of bullett) */
+       markdown = "*Thu Mar 12 12:00:00 2009* Dan Walsh <dwalsh redhat com> - 2.0.79-1\n"
+                  "- Update to upstream \n"
+                  " * Netlink socket handoff patch from Adam Jackson.\n"
+                  " * AVC caching of compute_create results by Eric Paris.\n"
+                  "\n"
+                  "*Tue Mar 10 12:00:00 2009* Dan Walsh <dwalsh redhat com> - 2.0.78-5\n"
+                  "- Add patch from ajax to accellerate X SELinux \n"
+                  "- Update eparis patch\n";
+       markdown_expected =
+                  "<i>Thu Mar 12 12:00:00 2009</i> Dan Walsh <tt>&lt;dwalsh redhat com&gt;</tt> - 2.0.79-1\n"
+                  "• Update to upstream\n"
+                  "• Netlink socket handoff patch from Adam Jackson.\n"
+                  "• AVC caching of compute_create results by Eric Paris.\n"
+                  "<i>Tue Mar 10 12:00:00 2009</i> Dan Walsh <tt>&lt;dwalsh redhat com&gt;</tt> - 2.0.78-5\n"
+                  "• Add patch from ajax to accellerate X SELinux\n"
+                  "• Update eparis patch";
+       gs_markdown_set_escape (md, TRUE);
+       gs_markdown_set_max_lines (md, 1024);
+       text = gs_markdown_parse (md, markdown);
+       g_assert_cmpstr (text, ==, markdown_expected);
+       g_free (text);
+
+       g_object_unref (md);
+}
+
 static gboolean
 gs_plugin_list_filter_cb (GsApp *app, gpointer user_data)
 {
@@ -490,6 +665,7 @@ main (int argc, char **argv)
        g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
 
        /* tests go here */
+       g_test_add_func ("/gnome-software/markdown", gs_markdown_func);
        g_test_add_func ("/gnome-software/plugin-loader{refine}", gs_plugin_loader_refine_func);
        g_test_add_func ("/gnome-software/plugin", gs_plugin_func);
        g_test_add_func ("/gnome-software/app", gs_app_func);


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