grim/gplate

Parents 455034934a9a
Children 963f73ea13a5
A ton of updates that aparently don't work... will look into this more tomorrow, now, must sleep
--- a/gplate/Makefile.am Tue Feb 19 03:26:49 2008 -0600
+++ b/gplate/Makefile.am Tue Feb 19 05:05:32 2008 -0600
@@ -21,6 +21,7 @@
gplate-tag.h \
gplate-template.h \
gplate-text-tag.h \
+ gplate-util.h \
gplate-variable.h \
gplate-variable-tag.h
@@ -40,6 +41,7 @@
gplate-tag.c \
gplate-template.c \
gplate-text-tag.c \
+ gplate-util.c \
gplate-variable.c \
gplate-variable-tag.c
--- a/gplate/gplate-errors.h Tue Feb 19 03:26:49 2008 -0600
+++ b/gplate/gplate-errors.h Tue Feb 19 05:05:32 2008 -0600
@@ -27,7 +27,7 @@
GPLATE_ERROR_TYPE_IS_NOT_DESCENDANT = 100,
GPLATE_ERROR_TYPE_IS_ABSTRACT,
- GPLATE_ERROR_TEMPLATE_FILE_DOES_NOT_EXIST = 200,
+ GPLATE_ERROR_UTIL_FILE_DOES_NOT_EXIST = 200,
GPLATE_ERROR_LIBRARY_FUNCTION_NAME_EXISTS = 300,
GPLATE_ERROR_LIBRARY_FUNCTION_NAME_NOT_FOUND,
@@ -49,4 +49,4 @@
G_END_DECLS
-#endif /* GPLATE_H */
+#endif /* GPLATE_ERRORS_H */
--- a/gplate/gplate-include-function.c Tue Feb 19 03:26:49 2008 -0600
+++ b/gplate/gplate-include-function.c Tue Feb 19 05:05:32 2008 -0600
@@ -23,11 +23,25 @@
#include <gplate/gplate-include-function.h>
#include <gplate/gplate-collection.h>
+#include <gplate/gplate-util.h>
#include <gplate/gplate-variable.h>
/******************************************************************************
* IncludeFunction Stuff
*****************************************************************************/
+static void
+gplate_include_function_common(GPlateTemplate *tplate, const gchar *filename) {
+ gchar *contents = NULL;
+
+ if(gplate_util_read_file(filename, &contents, NULL, NULL)) {
+ GList *tokens = gplate_template_tokenize(tplate, contents, NULL);
+
+ g_free(contents);
+
+ gplate_template_insert_tags(tplate, tokens);
+ }
+}
+
static gchar *
gplate_include_function_evaluate(GPlateFunction *function,
GPlateTemplate *tplate,
@@ -35,14 +49,13 @@
{
GRegex *regex = NULL;
GMatchInfo *info = NULL;
- gchar *ret = NULL;
regex = g_regex_new("\"(.+)\"", 0, 0, NULL);
if(g_regex_match(regex, contents, 0, &info)) {
gchar *file = g_match_info_fetch(info, 1);
- ret = gplate_template_render_file(tplate, file, NULL);
+ gplate_include_function_common(tplate, file);
g_match_info_free(info);
g_free(file);
@@ -53,17 +66,18 @@
contents);
if(GPLATE_IS_VARIABLE(variable)) {
- const gchar *value = gplate_variable_get_value(variable);
+ const gchar *file = gplate_variable_get_value(variable);
- ret = gplate_template_render_file(tplate, value, NULL);
+ gplate_include_function_common(tplate, file);
g_object_unref(G_OBJECT(variable));
- } else {
- ret = g_strdup("");
}
}
- return ret;
+ /* we always return an empty string since we just add our tags to the
+ * stack.
+ */
+ return g_strdup("");
}
/******************************************************************************
--- a/gplate/gplate-template.c Tue Feb 19 03:26:49 2008 -0600
+++ b/gplate/gplate-template.c Tue Feb 19 05:05:32 2008 -0600
@@ -26,6 +26,7 @@
#include <gplate/gplate-dictionary-variable.h>
#include <gplate/gplate-errors.h>
#include <gplate/gplate-library.h>
+#include <gplate/gplate-util.h>
#include <stdio.h>
#include <string.h>
@@ -356,6 +357,44 @@
return tag;
}
+static void
+gplate_template_real_insert_tags(GPlateTemplate *tplate, GList *tags) {
+ GPlateTemplatePrivate *priv = GPLATE_TEMPLATE_GET_PRIVATE(tplate);
+
+ /* check if our current node is valid */
+ if(!priv->current_token) {
+ /* check if we have a list of tokens */
+ if(!priv->tokens) {
+ /* no existing tokens, set it to tags and exit */
+ priv->tokens = tags;
+ } else {
+ /* we have tokens, but we've read the last one, move back to the
+ * last one.
+ */
+ priv->tokens = g_list_concat(priv->tokens, tags);
+ }
+ } else {
+ GList *tmp = NULL;
+
+ /* we have a valid node selected, save it's next node */
+ tmp = priv->current_token->next;
+
+ /* now mangle current and the first node in tags to shove tags in */
+ priv->current_token->next = tags;
+ tags->prev = priv->current_token;
+
+ /* if the original next node is valid, we need to put them back into
+ * place
+ */
+ if(tmp) {
+ GList *last = g_list_last(tags);
+
+ last->next = tmp;
+ tmp->prev = last;
+ }
+ }
+}
+
/******************************************************************************
* Object Stuff
*****************************************************************************/
@@ -392,6 +431,8 @@
klass->previous_tag = gplate_template_real_previous_tag;
klass->nth_tag = gplate_template_real_nth_tag;
klass->nth_previous_tag = gplate_template_real_nth_previous_tag;
+
+ klass->insert_tags = gplate_template_real_insert_tags;
}
static void
@@ -629,19 +670,7 @@
g_return_val_if_fail(GPLATE_IS_TEMPLATE(tplate), NULL);
g_return_val_if_fail(filename, NULL);
- if(!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
- if(error) {
- *error =
- g_error_new(GPLATE_DOMAIN,
- GPLATE_ERROR_TEMPLATE_FILE_DOES_NOT_EXIST,
- "File '%s' does not exist.\n",
- filename);
- }
-
- return FALSE;
- }
-
- if(!g_file_get_contents(filename, &contents, NULL, error))
+ if(!gplate_util_read_file(filename, &contents, NULL, error))
return FALSE;
ret = gplate_template_render(tplate, contents, error);
@@ -807,3 +836,22 @@
return NULL;
}
+/**
+ * gplate_template_insert_tags:
+ * @tplate: The #GPlateTemplate.
+ * @tags: A #GList of #GPlateTag's to insert.
+ *
+ * Inserts @tags after the current tag in @tplate's stack.
+ */
+void
+gplate_template_insert_tags(GPlateTemplate *tplate, GList *tags) {
+ GPlateTemplateClass *klass = NULL;
+
+ g_return_if_fail(GPLATE_IS_TEMPLATE(tplate));
+ g_return_if_fail(tags);
+
+ klass = GPLATE_TEMPLATE_GET_CLASS(tplate);
+
+ if(klass && klass->insert_tags)
+ klass->insert_tags(tplate, tags);
+}
--- a/gplate/gplate-template.h Tue Feb 19 03:26:49 2008 -0600
+++ b/gplate/gplate-template.h Tue Feb 19 05:05:32 2008 -0600
@@ -56,6 +56,8 @@
GPlateTag *(*previous_tag)(GPlateTemplate *tplate);
GPlateTag *(*nth_tag)(GPlateTemplate *tplate, guint nth);
GPlateTag *(*nth_previous_tag)(GPlateTemplate *tplate, guint nth);
+
+ void (*insert_tags)(GPlateTemplate *tplate, GList *tags);
void (*_gplate_reserved1)(void);
void (*_gplate_reserved2)(void);
@@ -82,6 +84,8 @@
GPlateTag *gplate_template_nth_tag(GPlateTemplate *tplate, guint nth);
GPlateTag *gplate_template_nth_previous_tag(GPlateTemplate *tplate, guint nth);
+void gplate_template_insert_tags(GPlateTemplate *tplate, GList *tags);
+
G_END_DECLS
#endif /* GPLATE_TEMPLATE_H */
--- a/tests/Makefile.am Tue Feb 19 03:26:49 2008 -0600
+++ b/tests/Makefile.am Tue Feb 19 05:05:32 2008 -0600
@@ -23,6 +23,7 @@
test_LDADD=$(LDADDS)
test_SOURCES=\
test.c \
+ test_include.c \
test_syntax.c \
test_variables.c
--- a/tests/templates/Makefile.am Tue Feb 19 03:26:49 2008 -0600
+++ b/tests/templates/Makefile.am Tue Feb 19 05:05:32 2008 -0600
@@ -1,4 +1,4 @@
EXTRA_DIST=\
- include1.gplate \
- include2.gplate
+ simple.gplate \
+ simple-include.gplate
--- a/tests/templates/include1.gplate Tue Feb 19 03:26:49 2008 -0600
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,4 +0,0 @@
-before the include
-{% include "include2.gplate" %}
-after the include
-
--- a/tests/templates/include2.gplate Tue Feb 19 03:26:49 2008 -0600
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-inside the included file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/templates/simple-include.gplate Tue Feb 19 05:05:32 2008 -0600
@@ -0,0 +1,1 @@
+inside the included file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/templates/simple.gplate Tue Feb 19 05:05:32 2008 -0600
@@ -0,0 +1,4 @@
+before the include
+{% include "templates/simple-include.gplate" %}
+after the include
+
--- a/tests/test.c Tue Feb 19 03:26:49 2008 -0600
+++ b/tests/test.c Tue Feb 19 05:05:32 2008 -0600
@@ -31,6 +31,8 @@
srunner_add_suite(sr, syntax_suite());
srunner_add_suite(sr, variable_suite());
+ srunner_add_suite(sr, include_suite());
+
srunner_run_all(sr, CK_NORMAL);
nfail = srunner_ntests_failed(sr);
srunner_free(sr);
--- a/tests/test.h Tue Feb 19 03:26:49 2008 -0600
+++ b/tests/test.h Tue Feb 19 05:05:32 2008 -0600
@@ -9,6 +9,7 @@
Suite *syntax_suite(void);
Suite *variable_suite(void);
+Suite *include_suite(void);
G_END_DECLS