grim/gplate

01a3a67bdb80
Parents 83675dc8c4fb
Children 6bf8035ff21e
updated the for function to use the new GPlateTemplate->render_until

refs #10
--- a/gplate/functions/gplate-for-function.c Wed Dec 31 20:47:11 2008 -0600
+++ b/gplate/functions/gplate-for-function.c Thu Jan 01 01:04:23 2009 -0600
@@ -26,6 +26,7 @@
#include <gplate/gplate-iterator.h>
#include <gplate/gplate-tag.h>
#include <gplate/gplate-variable.h>
+#include <gplate/tags/gplate-code-tag.h>
#define SYNTAX_REGEX "^(.+)\\s+in\\s+(.+)$"
@@ -116,8 +117,8 @@
iter = gplate_collection_get_iterator(collection);
while(gplate_iterator_has_next(iter)) {
GPlateCollection *tc = GPLATE_COLLECTION(tplate);
- GPlateTag *tag = NULL;
GPlateVariable *var = NULL;
+ gchar *output = NULL;
guint count = 1;
gboolean ret = FALSE;
@@ -130,21 +131,10 @@
ret = gplate_collection_add_variable_with_name(tc, iter_name, var);
/* iterate through the tokens */
- while((tag = gplate_template_next_tag(tplate))) {
- const gchar *contents = NULL;
- gchar *output = NULL;
-
- contents = gplate_tag_get_contents(tag);
-
- if(contents && g_utf8_collate(contents, "endfor") == 0)
- break;
-
- output = gplate_template_render_tag(tplate, tag);
-
- g_string_append_printf(str, "%s", output);
-
- count++;
- }
+ output = gplate_template_render_until(tplate, &count,
+ GPLATE_TYPE_CODE_TAG, "endfor",
+ NULL);
+ g_string_append_printf(str, "%s", output);
if(gplate_iterator_has_next(iter))
gplate_template_nth_previous_tag(tplate, count);
--- a/gplate/gplate-template.c Wed Dec 31 20:47:11 2008 -0600
+++ b/gplate/gplate-template.c Thu Jan 01 01:04:23 2009 -0600
@@ -700,14 +700,28 @@
return ret;
}
+/**
+ * gplate_template_render_until:
+ * @tplate: The #GPlateTemplate
+ * @ntags: A return address for the number of tags that were rendered
+ * @Varargs: A NULL terminated list of #GType, content pairs.
+ *
+ * Renders the tags in a template from the current point until it a tag is
+ * found that is the correct type and has the same contents as one of the
+ * #GType, content pairs.
+ *
+ *
+ */
gchar *
-gplate_template_render_until(GPlateTemplate *tplate, ...) {
+gplate_template_render_until(GPlateTemplate *tplate, guint *ntags, ...) {
GPlateTag *tag = NULL;
GList *l = NULL;
GString *str = NULL;
GQueue *queue = NULL;
+ GType type = G_TYPE_INVALID;
gchar *ret = NULL;
- gchar *contents = NULL;
+ guint count = 0;
+ gboolean backup = FALSE;
va_list args;
g_return_val_if_fail(GPLATE_IS_TEMPLATE(tplate), NULL);
@@ -717,14 +731,17 @@
queue = g_queue_new();
/* store all of the content/tag pairs */
- va_start(args, tplate);
- while((contents = va_arg(args, gchar *)) != NULL) {
+ va_start(args, ntags);
+ while((type = va_arg(args, gsize)) != G_TYPE_INVALID) {
GPlateTemplateTagData *td = NULL;
+ gchar *contents = NULL;
td = g_new(GPlateTemplateTagData, 1);
- td->contents = contents;
- td->type = va_arg(args, GType);
+ contents = va_arg(args, gchar *);
+ td->contents = contents ? g_strdup(contents) : NULL;
+
+ td->type = type;
g_queue_push_tail(queue, td);
}
@@ -746,16 +763,21 @@
g_utf8_collate(ctd.contents, td->contents) == 0)
{
stop = TRUE;
+ backup = TRUE;
}
}
g_free(ctd.contents);
- if(stop)
+ if(stop) {
+ count++;
break;
+ }
g_string_append_printf(str, "%s",
gplate_template_render_tag(tplate, tag));
+
+ count++;
}
/* now clean everything up... */
@@ -771,6 +793,18 @@
ret = str->str;
g_string_free(str, FALSE);
+ /* if backup is set, we back up to the tag which caused us to stop. In
+ * other words, the tag stack will currently be at the tag after the one
+ * we stopped at, but we want to be on the one that we stopped on.
+ */
+ if(backup) {
+ gplate_template_previous_tag(tplate);
+ count--;
+ }
+
+ if(ntags)
+ *ntags = count;
+
return ret;
}
@@ -846,7 +880,7 @@
*
* Moves the tag stack forward one tag and returns it.
*
- * Return Value: The previous tag or NULL.
+ * Return Value: The next tag or NULL.
*/
GPlateTag *
gplate_template_next_tag(GPlateTemplate *tplate) {
@@ -950,7 +984,7 @@
klass->insert_tags(tplate, tags);
}
-gchar *
+static gchar *
gplate_template_render_token(GPlateTemplate *tplate, const gchar *contents,
GType tag, GError **error)
{
--- a/gplate/gplate-template.h Wed Dec 31 20:47:11 2008 -0600
+++ b/gplate/gplate-template.h Thu Jan 01 01:04:23 2009 -0600
@@ -76,7 +76,7 @@
gchar *gplate_template_render(GPlateTemplate *tplate, const gchar *tplate_string, GError **error);
gchar *gplate_template_render_file(GPlateTemplate *tplate, const gchar *filename, GError **error);
-gchar *gplate_template_render_until(GPlateTemplate *tplate, ...);
+gchar *gplate_template_render_until(GPlateTemplate *tplate, guint *ntags, ...);
GPlateTag *gplate_template_first_tag(GPlateTemplate *tplate);
GPlateTag *gplate_template_last_tag(GPlateTemplate *tplate);