[gthumb: 25/57] [webalbum] added for_each_image_caption to the template format



commit ef7b850143fd6e077a49a33dbc10594a9fd660de
Author: Paolo Bacchilega <paobac src gnome org>
Date:   Mon Jun 14 19:04:08 2010 +0200

    [webalbum] added for_each_image_caption  to the template format

 extensions/webalbums/albumtheme-private.c          |   86 ++++++++++++++-----
 extensions/webalbums/albumtheme-private.h          |   29 ++++--
 extensions/webalbums/albumtheme.l                  |   12 ++-
 extensions/webalbums/albumtheme.y                  |   20 ++--
 .../data/albumthemes/ClassicClips/image.gthtml     |    2 +-
 extensions/webalbums/gth-web-exporter.c            |   92 +++++++++++++++----
 6 files changed, 178 insertions(+), 63 deletions(-)
---
diff --git a/extensions/webalbums/albumtheme-private.c b/extensions/webalbums/albumtheme-private.c
index 6122783..1d9dbc1 100644
--- a/extensions/webalbums/albumtheme-private.c
+++ b/extensions/webalbums/albumtheme-private.c
@@ -140,11 +140,14 @@ gth_cell_unref (GthCell *cell)
 		return;
 
 	cell->ref--;
-	if (cell->ref == 0) {
-		if (cell->type == GTH_CELL_TYPE_VAR)
-			g_free (cell->value.var);
-		g_free (cell);
-	}
+	if (cell->ref > 0)
+		return;
+
+	if (cell->type == GTH_CELL_TYPE_VAR)
+		g_free (cell->value.var);
+	else if (cell->type == GTH_CELL_TYPE_STRING)
+		g_string_free (cell->value.string, TRUE);
+	g_free (cell);
 }
 
 
@@ -152,7 +155,9 @@ gth_cell_unref (GthCell *cell)
 
 
 static int
-default_get_var_value_func (const char *var_name,
+default_get_var_value_func (GthExpr    *expr,
+		   	    int        *index,
+		   	    const char *var_name,
 			    gpointer    data)
 {
 	return 0;
@@ -215,7 +220,8 @@ gth_expr_is_empty (GthExpr *e)
 
 
 void
-gth_expr_push_expr (GthExpr *e, GthExpr *e2)
+gth_expr_push_expr (GthExpr *e,
+		    GthExpr *e2)
 {
 	int i;
 
@@ -228,7 +234,8 @@ gth_expr_push_expr (GthExpr *e, GthExpr *e2)
 
 
 void
-gth_expr_push_op (GthExpr *e, GthOp op)
+gth_expr_push_op (GthExpr *e,
+		  GthOp    op)
 {
 	GthCell *cell;
 
@@ -244,7 +251,8 @@ gth_expr_push_op (GthExpr *e, GthOp op)
 
 
 void
-gth_expr_push_var (GthExpr *e, const char *name)
+gth_expr_push_var (GthExpr    *e,
+		   const char *name)
 {
 	GthCell *cell;
 
@@ -260,15 +268,33 @@ gth_expr_push_var (GthExpr *e, const char *name)
 
 
 void
-gth_expr_push_constant (GthExpr *e, int value)
+gth_expr_push_string (GthExpr    *e,
+		      const char *value)
+{
+	GthCell *cell;
+
+	gth_cell_unref (e->data[e->top]);
+
+	cell = gth_cell_new ();
+	cell->type = GTH_CELL_TYPE_STRING;
+	cell->value.string = g_string_new (value);
+	e->data[e->top] = cell;
+
+	e->top++;
+}
+
+
+void
+gth_expr_push_integer (GthExpr *e,
+		       int      value)
 {
 	GthCell *cell;
 
 	gth_cell_unref (e->data[e->top]);
 
 	cell = gth_cell_new ();
-	cell->type = GTH_CELL_TYPE_CONSTANT;
-	cell->value.constant = value;
+	cell->type = GTH_CELL_TYPE_INTEGER;
+	cell->value.integer = value;
 	e->data[e->top] = cell;
 
 	e->top++;
@@ -346,12 +372,18 @@ gth_expr_print (GthExpr *e)
 		case GTH_CELL_TYPE_VAR:
 			printf ("VAR: %s (%d)\n",
 				cell->value.var,
-				e->get_var_value_func (cell->value.var,
+				e->get_var_value_func (e,
+						       &i,
+						       cell->value.var,
 						       e->get_var_value_data));
 			break;
 
-		case GTH_CELL_TYPE_CONSTANT:
-			printf ("NUM: %d\n", cell->value.constant);
+		case GTH_CELL_TYPE_STRING:
+			g_print ("STRING: %s\n", cell->value.string->str);
+			break;
+
+		case GTH_CELL_TYPE_INTEGER:
+			printf ("NUM: %d\n", cell->value.integer);
 			break;
 
 		case GTH_CELL_TYPE_OP:
@@ -378,12 +410,18 @@ gth_expr_eval (GthExpr *e)
 		switch (cell->type) {
 		case GTH_CELL_TYPE_VAR:
 			gth_mem_push (mem,
-				      e->get_var_value_func (cell->value.var,
+				      e->get_var_value_func (e,
+						      	     &i,
+						      	     cell->value.var,
 							     e->get_var_value_data));
 			break;
 
-		case GTH_CELL_TYPE_CONSTANT:
-			gth_mem_push (mem, cell->value.constant);
+		case GTH_CELL_TYPE_STRING:
+			/* only used as argument for a function */
+			break;
+
+		case GTH_CELL_TYPE_INTEGER:
+			gth_mem_push (mem, cell->value.integer);
 			break;
 
 		case GTH_CELL_TYPE_OP:
@@ -498,7 +536,7 @@ gth_expr_eval (GthExpr *e)
 
 
 GthVar *
-gth_var_new_constant (int value)
+gth_var_new_integer (int value)
 {
 	GthVar *var;
 
@@ -506,7 +544,7 @@ gth_var_new_constant (int value)
 	var->name = NULL;
 	var->type = GTH_VAR_EXPR;
 	var->value.expr = gth_expr_new ();
-	gth_expr_push_constant (var->value.expr, value);
+	gth_expr_push_integer (var->value.expr, value);
 
 	return var;
 }
@@ -715,7 +753,9 @@ gth_tag_free (GthTag *tag)
 				NULL);
 		g_list_free (tag->value.cond_list);
 	}
-	else if (tag->type == GTH_TAG_FOR_EACH_THUMBNAIL_CAPTION) {
+	else if ((tag->type == GTH_TAG_FOR_EACH_THUMBNAIL_CAPTION)
+		 || (tag->type == GTH_TAG_FOR_EACH_IMAGE_CAPTION))
+	{
 		gth_loop_free (tag->value.loop);
 	}
 	else {
@@ -754,6 +794,8 @@ gth_tag_get_type_from_name (const char *tag_name)
 		return GTH_TAG_IMAGE_IDX;
 	else if (g_str_equal (tag_name, "image_dim"))
 		return GTH_TAG_IMAGE_DIM;
+	else if (g_str_equal (tag_name, "image_attribute"))
+		return GTH_TAG_IMAGE_ATTRIBUTE;
 	else if (g_str_equal (tag_name, "images"))
 		return GTH_TAG_IMAGES;
 	else if (g_str_equal (tag_name, "file_name"))
@@ -790,6 +832,8 @@ gth_tag_get_type_from_name (const char *tag_name)
 		return GTH_TAG_IF;
 	else if (g_str_equal (tag_name, "for_each_thumbnail_caption"))
 		return GTH_TAG_FOR_EACH_THUMBNAIL_CAPTION;
+	else if (g_str_equal (tag_name, "for_each_image_caption"))
+		return GTH_TAG_FOR_EACH_IMAGE_CAPTION;
 	else if (g_str_equal (tag_name, "item_attribute"))
 		return GTH_TAG_ITEM_ATTRIBUTE;
 
diff --git a/extensions/webalbums/albumtheme-private.h b/extensions/webalbums/albumtheme-private.h
index ab4ca3f..e723496 100644
--- a/extensions/webalbums/albumtheme-private.h
+++ b/extensions/webalbums/albumtheme-private.h
@@ -68,16 +68,18 @@ typedef enum {
 typedef enum {
 	GTH_CELL_TYPE_OP,
 	GTH_CELL_TYPE_VAR,
-	GTH_CELL_TYPE_CONSTANT
+	GTH_CELL_TYPE_STRING,
+	GTH_CELL_TYPE_INTEGER
 } GthCellType;
 
 typedef struct {
 	int         ref;
 	GthCellType type;
 	union {
-		GthOp  op;
-		char  *var;
-		int    constant;
+		GthOp    op;
+		char    *var;
+		GString *string;
+		int      integer;
 	} value;
 } GthCell;
 
@@ -87,15 +89,20 @@ void       gth_cell_unref (GthCell *cell);
 
 /* GthExpr */
 
-typedef int (*GthGetVarValueFunc) (const char *var_name, gpointer data);
+typedef struct _GthExpr GthExpr;
 
-typedef struct {
+typedef int (*GthGetVarValueFunc) (GthExpr    *expr,
+				   int        *index,
+				   const char *var_name,
+				   gpointer    data);
+
+struct _GthExpr {
 	int                  ref;
 	GthCell            **data;
 	int                  top;
 	GthGetVarValueFunc   get_var_value_func;
 	gpointer             get_var_value_data;
-} GthExpr;
+};
 
 GthExpr*  gth_expr_new                    (void);
 GthExpr*  gth_expr_ref                    (GthExpr            *e);
@@ -108,7 +115,9 @@ void      gth_expr_push_op                (GthExpr            *e,
 					   GthOp               op);
 void      gth_expr_push_var               (GthExpr            *e,
 					   const char         *name);
-void      gth_expr_push_constant          (GthExpr            *e,
+void      gth_expr_push_string            (GthExpr            *e,
+					   const char         *value);
+void      gth_expr_push_integer           (GthExpr            *e,
 					   int                 value);
 void      gth_expr_pop                    (GthExpr            *e);
 GthCell*  gth_expr_get_pos                (GthExpr            *e,
@@ -136,7 +145,7 @@ typedef struct {
 	} value;
 } GthVar;
 
-GthVar*  gth_var_new_constant   (int value);
+GthVar*  gth_var_new_integer    (int value);
 GthVar*  gth_var_new_expression (const char *name,
 				 GthExpr    *e);
 GthVar*  gth_var_new_string     (const char *name,
@@ -166,6 +175,7 @@ typedef enum {
 	GTH_TAG_IMAGE_LINK,
 	GTH_TAG_IMAGE_IDX,
 	GTH_TAG_IMAGE_DIM,
+	GTH_TAG_IMAGE_ATTRIBUTE,
 	GTH_TAG_IMAGES,
 	GTH_TAG_FILE_NAME,
 	GTH_TAG_FILE_PATH,
@@ -183,6 +193,7 @@ typedef enum {
 	GTH_TAG_EVAL,
 	GTH_TAG_IF,
 	GTH_TAG_FOR_EACH_THUMBNAIL_CAPTION,
+	GTH_TAG_FOR_EACH_IMAGE_CAPTION,
 	GTH_TAG_ITEM_ATTRIBUTE,
 	GTH_TAG_INVALID
 } GthTagType;
diff --git a/extensions/webalbums/albumtheme.l b/extensions/webalbums/albumtheme.l
index f793cb2..1d31b42 100644
--- a/extensions/webalbums/albumtheme.l
+++ b/extensions/webalbums/albumtheme.l
@@ -31,6 +31,8 @@ GFileInputStream *yy_istream;
 	result = (n <= 0 ? YY_NULL : n);								\
 }
 
+static int before_string = 0; /* the start condition before entering in STRING */
+
 %}
 
 %option noinput nounput
@@ -69,6 +71,11 @@ string           [^']*
 					yylval.ivalue = GTH_TAG_FOR_EACH_THUMBNAIL_CAPTION;
 					return FOR_EACH;
 				}
+"<% for_each_image_caption"	{
+					BEGIN (ATTRIBUTES);
+					yylval.ivalue = GTH_TAG_FOR_EACH_IMAGE_CAPTION;
+					return FOR_EACH;
+				}			
 "<%="                           {
 					BEGIN (FUNCTION);
 					return BEGIN_TAG;
@@ -79,11 +86,12 @@ string           [^']*
 					return FUNCTION_NAME;
 				}
 <ATTRIBUTES>"'"			{
+					before_string = YY_START;
 					BEGIN (STRING);
 					return '\'';
 				}
 <STRING>"'"			{
-					BEGIN (ATTRIBUTES);
+					BEGIN (before_string);
 					return '\'';
 				}
 <STRING>{string}		{
@@ -115,7 +123,7 @@ string           [^']*
 				}
 <QUOTE,CONDITION>{name}		{
 					yylval.text = g_strdup (yytext);
-					return QUOTED_NAME;
+					return VARIABLE;
 				}
 <QUOTE,CONDITION>"="		{
 					return '=';
diff --git a/extensions/webalbums/albumtheme.y b/extensions/webalbums/albumtheme.y
index b01957f..e0fba3f 100644
--- a/extensions/webalbums/albumtheme.y
+++ b/extensions/webalbums/albumtheme.y
@@ -25,7 +25,7 @@
 #include <gio/gio.h>
 #include "albumtheme-private.h"
 
-int   gth_albumtheme_yylex ();
+int   gth_albumtheme_yylex   ();
 void  gth_albumtheme_yyerror (char *fmt, ...);
 int   gth_albumtheme_yywrap  (void);
 
@@ -47,7 +47,7 @@ int   gth_albumtheme_yywrap  (void);
 
 %nonassoc       IF ELSE ELSE_IF END END_TEXT_TAG FOR_EACH_THUMBNAIL_CAPTION SET_VAR
 %token          BEGIN_TAG END_TAG BEGIN_TEXT_TAG
-%token <text>   QUOTED_NAME ATTRIBUTE_NAME FUNCTION_NAME QUOTED_STRING
+%token <text>   VARIABLE ATTRIBUTE_NAME FUNCTION_NAME QUOTED_STRING
 %token <ivalue> FOR_EACH
 %token <ivalue> NUMBER
 %token <ivalue> HEADER FOOTER
@@ -74,7 +74,7 @@ int   gth_albumtheme_yywrap  (void);
 %token <ivalue> SET_VAR
 %token <ivalue> EVAL
 
-%token <text> HTML
+%token <text>   HTML
 
 %type <list>   document
 %type <tag>    gthumb_tag
@@ -139,8 +139,8 @@ document	: HTML document {
 		}
 
 		| error document {
-			if ($2 != NULL)
-				gth_parsed_doc_free ($2);
+			/*if ($2 != NULL)
+				gth_parsed_doc_free ($2);*/
 			$$ = NULL;
 		}
 		;
@@ -180,7 +180,7 @@ opt_else        : gthumb_else document {
 			GthCondition *cond;
 
 			else_expr = gth_expr_new ();
-			gth_expr_push_constant (else_expr, 1);
+			gth_expr_push_integer (else_expr, 1);
 			cond = gth_condition_new (else_expr);
 			gth_condition_add_document (cond, $2);
 
@@ -294,7 +294,7 @@ expr		: '(' expr ')' {
 			$$ = $2;
 		}
 
-		| QUOTED_NAME {
+		| VARIABLE {
 			GthExpr *e = gth_expr_new ();
 			gth_expr_push_var (e, $1);
 			g_free ($1);
@@ -303,7 +303,7 @@ expr		: '(' expr ')' {
 
 		| NUMBER {
 			GthExpr *e = gth_expr_new ();
-			gth_expr_push_constant (e, $1);
+			gth_expr_push_integer (e, $1);
 			$$ = e;
 		}
 		;
@@ -320,7 +320,7 @@ gthumb_tag 	: SET_VAR attribute_list END_TAG {
 				$$ = gth_tag_new (GTH_TAG_TRANSLATE, arg_list);
 			}
 			else {
-				yyerror ("Wrong function: %s", $2);
+				yyerror ("Wrong function: '%s', expected 'translate'", $2);
 				YYERROR;
 			}
 		}
@@ -355,7 +355,7 @@ attribute	: ATTRIBUTE_NAME '=' '"' expr '"' {
 
 		| ATTRIBUTE_NAME {
 		  	GthExpr *e = gth_expr_new ();
-			gth_expr_push_constant (e, 1);
+			gth_expr_push_integer (e, 1);
 			$$ = gth_var_new_expression ($1, e);
 			g_free ($1);
 		}
diff --git a/extensions/webalbums/data/albumthemes/ClassicClips/image.gthtml b/extensions/webalbums/data/albumthemes/ClassicClips/image.gthtml
index 14b1901..8cf76aa 100644
--- a/extensions/webalbums/data/albumthemes/ClassicClips/image.gthtml
+++ b/extensions/webalbums/data/albumthemes/ClassicClips/image.gthtml
@@ -62,7 +62,7 @@
         <% end %>
       </div>
     
-      <% for_each_thumbnail_caption %>
+      <% for_each_image_caption %>
         <% if first_item %>
           <dl class="caption-container">
         <% end %>
diff --git a/extensions/webalbums/gth-web-exporter.c b/extensions/webalbums/gth-web-exporter.c
index 08ee51f..7939949 100644
--- a/extensions/webalbums/gth-web-exporter.c
+++ b/extensions/webalbums/gth-web-exporter.c
@@ -314,7 +314,9 @@ get_style_dir (GthWebExporter *self,
 
 
 static int
-get_var_value (const char *var_name,
+get_var_value (GthExpr    *expr,
+	       int        *index,
+	       const char *var_name,
 	       gpointer    data)
 {
 	GthWebExporter *self = data;
@@ -360,6 +362,19 @@ get_var_value (const char *var_name,
 	else if (g_str_equal (var_name, "last_item"))
 		return (self->priv->loop_info != NULL) ? self->priv->loop_info->last_item : FALSE;
 
+	else if (g_str_equal (var_name, "image_attribute_available")) {
+		GthCell *cell;
+
+		*index += 1;
+		cell = gth_expr_get_pos (expr, *index);
+		if (cell->type == GTH_CELL_TYPE_STRING) {
+			const char *attribute_id;
+
+			attribute_id = cell->value.string->str;
+			/* TODO */
+		}
+	}
+
 	/* FIXME: use a generic function to get an attribute visibility */
 /*
 	else if (strcmp (var_name, "image_dim_visibility_index") == 0)
@@ -637,8 +652,6 @@ write_markup_escape_locale_line (GFileOutputStream  *ostream,
 }
 
 
-/* FIXME */
-G_GNUC_UNUSED
 static char *
 get_image_attribute (GthWebExporter    *self,
 		     GthTag            *tag,
@@ -1177,6 +1190,16 @@ gth_parsed_doc_print (GthWebExporter      *self,
 			write_line (ostream, line, error);
 			break;
 
+		case GTH_TAG_IMAGE_ATTRIBUTE:
+			idx = get_image_idx (tag, self);
+			idata = g_list_nth (self->priv->file_list, idx)->data;
+			id = gth_tag_get_str (self, tag, "id");
+			if (id != NULL) {
+				line = get_image_attribute (self, tag, id);
+				write_line (ostream, line, error);
+			}
+			break;
+
 		case GTH_TAG_IMAGES:
 			line = g_strdup_printf ("%d", self->priv->n_images);
 			write_line (ostream, line, error);
@@ -1530,32 +1553,61 @@ gth_parsed_doc_print (GthWebExporter      *self,
 			break;
 
 		case GTH_TAG_FOR_EACH_THUMBNAIL_CAPTION:
+		case GTH_TAG_FOR_EACH_IMAGE_CAPTION:
 			/* FIXME */
 			{
 				LoopInfo  *inner_loop_info;
 				char     **attributes;
 				int        i;
+				int        first_non_empty;
+				int        last_non_empty;
 
 				idx = MIN (self->priv->image, self->priv->n_images - 1);
 				idata = g_list_nth (self->priv->file_list, idx)->data;
 				self->priv->eval_image = idata;
 
 				inner_loop_info = loop_info_new ();
-				attributes = g_strsplit (self->priv->thumbnail_caption, ",", -1);
+				if (tag->type == GTH_TAG_FOR_EACH_THUMBNAIL_CAPTION)
+					attributes = g_strsplit (self->priv->thumbnail_caption, ",", -1);
+				else
+					attributes = g_strsplit (self->priv->image_caption, ",", -1);
+
+				first_non_empty = -1;
+				last_non_empty = -1;
 				for (i = 0; attributes[i] != NULL; i++) {
-					inner_loop_info->first_item = (i == 0);
-					inner_loop_info->last_item = (attributes[i + 1] == NULL);
-					inner_loop_info->item_index = i;
-					inner_loop_info->item = g_object_ref (idata->file_data);
-					inner_loop_info->attribute = g_strdup (attributes[i]);
+					char *value;
 
-					gth_parsed_doc_print (self,
-							      tag->value.loop->document,
-							      GTH_TEMPLATE_TYPE_FRAGMENT,
-							      inner_loop_info,
-							      relative_to,
-							      ostream,
-							      error);
+					value = gth_file_data_get_attribute_as_string (idata->file_data, attributes[i]);
+					if ((value != NULL) && ! g_str_equal (value, "")) {
+						if (first_non_empty == -1)
+							first_non_empty = i;
+						last_non_empty = i;
+					}
+
+					g_free (value);
+				}
+
+				for (i = 0; attributes[i] != NULL; i++) {
+					char *value;
+
+					value = gth_file_data_get_attribute_as_string (idata->file_data, attributes[i]);
+					if ((value != NULL) && ! g_str_equal (value, "")) {
+						inner_loop_info->first_item = (i == first_non_empty);
+						inner_loop_info->last_item = (i == last_non_empty);
+						inner_loop_info->item_index = i;
+						inner_loop_info->item = g_object_ref (idata->file_data);
+						inner_loop_info->attribute = g_strdup (attributes[i]);
+
+						gth_parsed_doc_print (self,
+								      tag->value.loop->document,
+								      GTH_TEMPLATE_TYPE_FRAGMENT,
+								      inner_loop_info,
+								      relative_to,
+								      ostream,
+								      error);
+					}
+
+					g_free (value);
 				}
 
 				g_strfreev (attributes);
@@ -2528,12 +2580,12 @@ parse_theme_files (GthWebExporter *self)
 		GthTag  *tag;
 
 		expr = gth_expr_new ();
-		gth_expr_push_constant (expr, 0);
+		gth_expr_push_integer (expr, 0);
 		var = gth_var_new_expression ("idx_relative", expr);
 		vars = g_list_prepend (vars, var);
 
 		expr = gth_expr_new ();
-		gth_expr_push_constant (expr, 1);
+		gth_expr_push_integer (expr, 1);
 		var = gth_var_new_expression ("thumbnail", expr);
 		vars = g_list_prepend (vars, var);
 
@@ -2558,12 +2610,12 @@ parse_theme_files (GthWebExporter *self)
 		GthTag  *tag;
 
 		expr = gth_expr_new ();
-		gth_expr_push_constant (expr, 0);
+		gth_expr_push_integer (expr, 0);
 		var = gth_var_new_expression ("idx_relative", expr);
 		vars = g_list_prepend (vars, var);
 
 		expr = gth_expr_new ();
-		gth_expr_push_constant (expr, 0);
+		gth_expr_push_integer (expr, 0);
 		var = gth_var_new_expression ("thumbnail", expr);
 		vars = g_list_prepend (vars, var);
 



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