grim/gplate

Updating template for the rendering changes
render_rewrite
2008-02-17, grim
e5119dc0462b
Parents 66a8580d5c65
Children 370596911068
Updating template for the rendering changes
--- a/gplate/gplate-template.c Sun Feb 17 03:28:20 2008 -0600
+++ b/gplate/gplate-template.c Sun Feb 17 05:46:51 2008 -0600
@@ -401,6 +401,81 @@
}
/******************************************************************************
+ * Render helpers
+ *****************************************************************************/
+static inline GPlateFunction *
+gplate_template_render_find_function(const gchar *name, GType tag,
+ gboolean *deffunc, GError **error)
+{
+ GPlateFunction *ret = NULL;
+ GType function = G_TYPE_INVALID;
+ gboolean def = FALSE;
+
+ function = gplate_library_lookup_function_for_tag(name, tag, NULL);
+
+ if(function == G_TYPE_INVALID) {
+ function = gplate_library_get_default_function_for_tag(tag, NULL);
+ def = TRUE;
+ }
+
+ if(function != G_TYPE_INVALID) {
+ ret = g_object_new(function, NULL);
+
+ if(deffunc)
+ *deffunc = def;
+ }
+
+ return ret;
+}
+
+static inline gchar *
+gplate_template_render_token(GPlateTemplate *tplate, const gchar *contents,
+ GType tag, GError **error)
+{
+ GPlateFunction *function = NULL;
+ GRegex *regex = NULL;
+ GMatchInfo *info = NULL;
+ gchar *name = NULL, *ret = NULL;
+ gboolean deffunc = FALSE;
+
+ regex = g_regex_new("(.*?)(\\s(.*?)?)",
+ G_REGEX_OPTIMIZE | G_REGEX_MULTILINE, 0, NULL);
+ if(!regex)
+ return g_strdup("");
+
+ if(!g_regex_match(regex, contents, 0, &info)) {
+ g_regex_unref(regex);
+
+ return g_strdup("");
+ }
+
+ name = g_match_info_fetch(info, 1);
+
+ function =
+ gplate_template_render_find_function(name, tag, &deffunc, error);
+
+ if(function) {
+ if(deffunc) {
+ ret = gplate_function_evaluate(function, tplate, contents);
+ } else {
+ gchar *leftovers = g_match_info_fetch(info, 2);
+
+ ret = gplate_function_evaluate(function, tplate, leftovers);
+
+ g_free(leftovers);
+ }
+
+ g_object_unref(G_OBJECT(function));
+ }
+
+ g_free(name);
+ g_regex_unref(regex);
+ g_match_info_free(info);
+
+ return (ret) ? ret : g_strdup("");
+}
+
+/******************************************************************************
* GPlateTemplate API
*****************************************************************************/
GType
@@ -510,12 +585,16 @@
for(l = tokens; l; l = l->next) {
GPlateTag *tag = GPLATE_TAG(l->data);
- gchar *contents = gplate_tag_render(tag, tplate);
+ GType ttype = G_OBJECT_TYPE(tag);
+ gchar *contents = NULL, *output = NULL;
- if(contents)
- g_string_append_printf(str, "%s", contents);
+ contents = gplate_tag_get_contents(tag);
+
+ output = gplate_template_render_token(tplate, contents, ttype, error);
+ g_string_append_printf(str, "%s", output);
g_free(contents);
+ g_free(output);
}
ret = str->str;
--- a/gplate/gplate-template.h Sun Feb 17 03:28:20 2008 -0600
+++ b/gplate/gplate-template.h Sun Feb 17 05:46:51 2008 -0600
@@ -47,6 +47,7 @@
GObjectClass parent;
GList *(*tokenize)(GPlateTemplate *tplate, const gchar *tplate_string, GError **error);
+ gchar *(*render)(GPlateTemplate *tplate, const gchar *tplate_string, GError **error);
GPlateTag *(*first_tag)(GPlateTemplate *tplate);
GPlateTag *(*last_tag)(GPlateTemplate *tplate);