[recipes/temperature: 1/3] Consolidate instruction parsing in one place



commit 9fddb7007540e4a13e027f540b2e3b9a9870afc0
Author: Matthias Clasen <mclasen redhat com>
Date:   Wed Feb 15 07:28:12 2017 -0500

    Consolidate instruction parsing in one place
    
    Reuse the same instruction parsing code in the details page,
    so we avoid repetition.

 src/gr-details-page.c |   69 ++++++++++++++++++++-----------------------------
 1 files changed, 28 insertions(+), 41 deletions(-)
---
diff --git a/src/gr-details-page.c b/src/gr-details-page.c
index 2d8c1e2..4e0fbe2 100644
--- a/src/gr-details-page.c
+++ b/src/gr-details-page.c
@@ -40,6 +40,7 @@
 #include "gr-timer.h"
 #include "gr-recipe-printer.h"
 #include "gr-recipe-exporter.h"
+#include "gr-recipe-formatter.h"
 
 
 struct _GrDetailsPage
@@ -519,58 +520,44 @@ populate_ingredients (GrDetailsPage *page,
 static char *
 process_instructions (const char *instructions)
 {
+        g_autoptr(GPtrArray) steps = NULL;
         GString *s;
-        const char *p, *p2, *t, *q;
+        int i;
 
-        s = g_string_new ("");
-
-        t = instructions;
+        steps = gr_recipe_parse_instructions (instructions);
 
-        while (*t) {
-                const char *sym = "?";
-                int idx;
-                g_autofree char *title = NULL;
-
-                p = strstr (t, "[image:");
-                q = strstr (t, "[timer:");
-                if (q && (!p || q < p)) {
-                        p = q;
-                        sym = "ā°";
-                }
-                else if (p) {
-                        idx = atoi (p + strlen ("[image:"));
-                        sym = "šŸ‘";
-                }
+        s = g_string_new ("");
 
-                if (p == NULL) {
-                        g_string_append (s, t);
-                        break;
-                }
+        for (i = 0; i < steps->len; i++) {
+                GrRecipeStep *step = (GrRecipeStep *)g_ptr_array_index (steps, i);
 
-                g_string_append_len (s, t, p - t);
+                if (i > 0)
+                        g_string_append (s, "\n\n");
 
-                p2 = strstr (p, "]");
+                if (step->timer != 0) {
+                        int seconds;
+                        int minutes;
+                        int hours;
+                        g_autofree char *str = NULL;
 
-                if (p == q) {
-                        const char *q2;
-                        g_autofree char *timer = NULL;
+                        seconds = (int)(step->timer / G_TIME_SPAN_SECOND);
+                        minutes = seconds / 60;
+                        seconds = seconds - 60 * minutes;
+                        hours = minutes / 60;
+                        minutes = minutes - 60 * hours;
 
-                        q2 = q + strlen ("[timer:");
-                        timer = g_strndup (q2, p2 - q2);
-                        title = g_strdup_printf (_("Timer: %s"), timer);
+                        str = g_strdup_printf ("%02dāˆ¶%02dāˆ¶%02d", hours, minutes, seconds);
+                        g_string_append (s, "<a href=\"timer\" title=\"");
+                        g_string_append_printf (s, _("Timer: %s"), str);
+                        g_string_append (s, "\">ā°</a>");
                 }
-                else {
-                        title = g_strdup_printf (_("Image %d"), idx + 1);
+                else if (step->image != -1) {
+                        g_string_append_printf (s, "<a href=\"image:%d\" title=\"", step->image);
+                        g_string_append_printf (s, _("Image %d"), step->image + 1);
+                        g_string_append (s, "\">šŸ‘</a>");
                 }
 
-                g_string_append (s, "<a href=\"");
-                g_string_append_len (s, p + 1, p2 - p);
-                g_string_append (s, "\" title=\"");
-                g_string_append (s, title);
-                g_string_append (s, "\">");
-                g_string_append (s, sym);
-                g_string_append (s, "</a>");
-                t = p2 + 1;
+                g_string_append (s, step->text);
         }
 
         return g_string_free (s, FALSE);


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