[recipes] Make the recipe printer work with our markup



commit b53b19edf94a086472302fda6c5f2819d932ecb8
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Mar 5 21:12:03 2017 -0500

    Make the recipe printer work with our markup
    
    We neglected to update the recipe printer to our new
    markdown-like markup for timers and images. Fix this
    oversight.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=779638

 src/gr-recipe-printer.c |   82 ++++++++--------------------------------------
 1 files changed, 15 insertions(+), 67 deletions(-)
---
diff --git a/src/gr-recipe-printer.c b/src/gr-recipe-printer.c
index b243846..373c50b 100644
--- a/src/gr-recipe-printer.c
+++ b/src/gr-recipe-printer.c
@@ -23,6 +23,7 @@
 #include <glib/gi18n.h>
 
 #include "gr-recipe-printer.h"
+#include "gr-recipe-formatter.h"
 #include "gr-ingredients-list.h"
 #include "gr-images.h"
 #include "gr-chef.h"
@@ -70,80 +71,27 @@ gr_recipe_printer_new (GtkWindow *parent)
         return printer;
 }
 
-typedef struct {
-        GString *string;
-        gboolean ignore;
-} ParserData;
-
-static void
-start_element (GMarkupParseContext  *context,
-               const char           *element_name,
-               const char          **attribute_names,
-               const char          **attribute_values,
-               gpointer              user_data,
-               GError              **error)
-{
-        ParserData *data = user_data;
-
-        if (strcmp (element_name, "a") == 0)
-                data->ignore = TRUE;
-}
-
-static void
-end_element (GMarkupParseContext  *context,
-             const char           *element_name,
-             gpointer              user_data,
-             GError              **error)
+static char *
+process_instructions (const char *instructions)
 {
-        ParserData *data = user_data;
+        g_autoptr(GPtrArray) steps = NULL;
+        GString *s;
+        int i;
 
-        if (strcmp (element_name, "a") == 0)
-                data->ignore = FALSE;
-}
+        steps = gr_recipe_parse_instructions (instructions, TRUE);
 
-static void
-text (GMarkupParseContext  *context,
-      const char           *text,
-      gsize                 text_len,
-      gpointer              user_data,
-      GError              **error)
-{
-        ParserData *data = user_data;
+        s = g_string_new ("");
 
-        if (!data->ignore)
-                g_string_append_len (data->string, text, text_len);
-}
+        for (i = 0; i < steps->len; i++) {
+                GrRecipeStep *step = (GrRecipeStep *)g_ptr_array_index (steps, i);
 
-static GMarkupParser parser = {
-       start_element,
-       end_element,
-       text,
-       NULL,
-       NULL,
-};
+                if (i > 0)
+                        g_string_append (s, "\n\n");
 
-static char *
-strip_links (const char *text)
-{
-        g_autoptr(GMarkupParseContext) context = NULL;
-        g_autoptr(GString) string = NULL;
-        g_autoptr(GError) error = NULL;
-        ParserData data;
-
-        string = g_string_new ("");
-        data.string = string;
-        data.ignore = FALSE;
-
-        context = g_markup_parse_context_new (&parser, 0, &data, NULL);
-
-        if (!g_markup_parse_context_parse (context, "<instructions>", -1, &error) ||
-            !g_markup_parse_context_parse (context, text, -1, &error) ||
-            !g_markup_parse_context_parse (context, "</instructions>", -1, &error)) {
-                g_message ("Failed to parse instructions: %s", error->message);
-                return g_strdup ("");
+                g_string_append (s, step->text);
         }
 
-        return g_strdup (string->str);
+        return g_string_free (s, FALSE);
 }
 
 static void
@@ -296,7 +244,7 @@ begin_print (GtkPrintOperation *operation,
         attr->end_index = s->len + 1;
         pango_attr_list_insert (attrs, attr);
 
-        instructions = strip_links (gr_recipe_get_translated_instructions (printer->recipe));
+        instructions = process_instructions (gr_recipe_get_translated_instructions (printer->recipe));
 
         g_string_append (s, "\n\n");
         g_string_append (s, instructions);


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