From 10a77c7a1c4648693ded958d6ac8641afcdf1d34 Mon Sep 17 00:00:00 2001 From: Tomas Bzatek Date: Wed, 25 Feb 2009 21:25:18 +0100 Subject: The Ultimate Entity Solution (tm) --- .gitignore | 2 + gallery-utils.c | 69 ++++++++++++++++++------------- gallery-utils.h | 10 ++++- generators-replace-table.c | 39 +++++++++++------ generators-replace-table.h | 9 ++++ generators.c | 52 ++++++++++++----------- sample/src/CIAF_1/index.xml | 4 +- sample/src/entities/index.xml | 28 +++++++++++++ sample/src/entities/preview/img_6802.jpg | 1 + sample/src/entities/preview/img_6802b.jpg | 1 + sample/src/index.xml | 5 +++ templates/template-album.tmpl | 14 +++---- templates/template-index.tmpl | 14 +++---- templates/template-view_photo.tmpl | 8 ++-- 14 files changed, 170 insertions(+), 86 deletions(-) create mode 100644 sample/src/entities/index.xml create mode 120000 sample/src/entities/preview/img_6802.jpg create mode 120000 sample/src/entities/preview/img_6802b.jpg diff --git a/.gitignore b/.gitignore index 809d89d..6e80970 100644 --- a/.gitignore +++ b/.gitignore @@ -13,5 +13,7 @@ design/ sample-test/ sample/dst/ web/ +sample-single/ +validation/ *.o diff --git a/gallery-utils.c b/gallery-utils.c index 9642116..6378ae5 100644 --- a/gallery-utils.c +++ b/gallery-utils.c @@ -27,38 +27,13 @@ #include "gallery-utils.h" - -static char * -strstr_exclude (const char *haystack, const char *needle, const char *exclude_when) -{ - const char *src; - char *found; - - src = haystack; - - while (src && strstr (src, needle)) { - found = strstr (src, needle); - - /* skip if found 'exclude_when' at the same place as 'search' */ - if (exclude_when && (found == strstr (haystack, exclude_when))) { - src += strlen (exclude_when); - continue; - } - else - return found; - } - - return NULL; -} - /* * str_replace: replace substring 'search' with a 'replace' string * - multiple occurrences of the string are replaced - * - specify 'exclude_when' if you want to skip replace when a string found at the place of 'search' * - reallocates the original string */ void -str_replace (char **dst, const char *search, const char *replace, const char *exclude_when) +str_replace (char **dst, const char *search, const char *replace) { #define REPLACE_MAX_LENGTH 32768 static char d[REPLACE_MAX_LENGTH]; @@ -73,8 +48,8 @@ str_replace (char **dst, const char *search, const char *replace, const char *ex i = 0; src = *dst; - while (strstr_exclude (src, search, exclude_when)) { - found = strstr_exclude (src, search, exclude_when); + while (strstr (src, search)) { + found = strstr (src, search); /* copy the data between search string */ if (found > src) { @@ -247,3 +222,41 @@ fix_entities (char **str) free (*str); *str = g_strdup (&d[0]); } + + +/* + * remove_tags: remove all occurences of tags beginning with tag_begin and ending with tag_end + * - e.g. remove_tags (&x, "") will remove all comments + * - returns newly allocated string + */ +void +remove_tags (char **str, const char *tag_begin, const char *tag_end) +{ + char *src; + char *found; + char *found2; + char *dest; + + if (! *str || ! tag_begin || ! tag_end || strlen (*str) == 0 || strlen (tag_begin) == 0 || strlen (tag_end) == 0) + return; + + src = *str; + + while ((found = strstr (src, tag_begin)) != NULL) { + found2 = strstr (found, tag_end); + if (found2) { + found2 += strlen (tag_end); + dest = malloc (strlen (src) - (found2 - found) + 1); + memcpy (dest, src, found - src); + memcpy (dest + (found - src), found2, strlen (found2) + 1); +#ifdef __DEBUG_ALL__ + printf ("found = %ld, found2 = %ld, strlen = %d, res = %d\n", (long int)found, (long int)found2, strlen (src), strlen (src) - (found2 - found) + 1); + printf ("orig = %s, new = %s, strlen = %d\n", src, dest, strlen (dest)); +#endif + g_free (src); + src = g_strdup (dest); + free (dest); + } + } + *str = src; +} diff --git a/gallery-utils.h b/gallery-utils.h index dd97663..53c82c0 100644 --- a/gallery-utils.h +++ b/gallery-utils.h @@ -24,10 +24,9 @@ /* * str_replace: replace substring 'search' with a 'replace' string * - multiple occurences of the string are replaced - * - specify 'exclude_when' if you want to skip replace when a string found at the place of 'search' * - reallocates the original string */ -void str_replace (char **dst, const char *search, const char *replace, const char *exclude_when); +void str_replace (char **dst, const char *search, const char *replace); /* * copy_file: copy file from src to dst @@ -45,3 +44,10 @@ char *make_string (const char* substr, const int count); * - returns newly allocated string */ void fix_entities (char **str); + +/* + * remove_tags: remove all occurences of tags beginning with tag_begin and ending with tag_end + * - e.g. remove_tags (&x, "") will remove all comments + * - returns newly allocated string + */ +void remove_tags (char **str, const char *tag_begin, const char *tag_end); diff --git a/generators-replace-table.c b/generators-replace-table.c index 6a84c17..23771fc 100644 --- a/generators-replace-table.c +++ b/generators-replace-table.c @@ -110,16 +110,12 @@ replace_table_process_value (gpointer key, gpointer value, gpointer user_data) g_return_if_fail (value != NULL); g_return_if_fail (user_data != NULL); -// g_print ("replace_table_print: key = '%s', value = '%s'\n", (gchar *) key, (gchar *) value); - /* replace tags */ tag = g_strdup_printf ("", (gchar *) key); tag_value = g_strdup ((gchar *) value); - /* TODO: do something with tag_value? */ -/* fix_entities (&s1); */ -/* s1 = g_markup_escape_text (s4, -1); */ + adjust_tags_normal (&tag_value); while (strstr (*buffer, tag)) { - str_replace (buffer, tag, tag_value, NULL); + str_replace (buffer, tag, tag_value); } g_free (tag_value); g_free (tag); @@ -127,13 +123,9 @@ replace_table_process_value (gpointer key, gpointer value, gpointer user_data) /* replace $(xxx) tags */ tag = g_strdup_printf ("$(%s)", (gchar *) key); tag_value = g_strdup ((gchar *) value); - /* TODO: do something with tag_value? */ -/* fix_entities (&s1); */ -/* s1 = g_markup_escape_text (s4, -1); */ -/* line endings */ -/* remove all tags as we're tag value */ + adjust_tags_parameter (&tag_value); while (strstr (*buffer, tag)) { - str_replace (buffer, tag, tag_value, NULL); + str_replace (buffer, tag, tag_value); } g_free (tag_value); g_free (tag); @@ -157,3 +149,26 @@ replace_table_process (gchar **buffer, ReplaceTable *table) g_print ("replace_table_process: val = '%s'\n", val); */ } + + +/* + * adjust_tags_normal: adjust string for normal HTML use + * adjust_tags_parameter: adjust string for use as tag parameter value + * - both funtions return newly allocated string + */ +void +adjust_tags_normal (char **str) +{ + fix_entities (str); +} + +void +adjust_tags_parameter (char **str) +{ + /* TODO: replace line endings with single space? */ + remove_tags (str, ""); /* comments */ + remove_tags (str, "<", ">"); /* tags */ + fix_entities (str); /* entities */ + str_replace (str, "\"", """); /* " */ + str_replace (str, "'", "'"); /* ' */ +} diff --git a/generators-replace-table.h b/generators-replace-table.h index 860a42b..d72d150 100644 --- a/generators-replace-table.h +++ b/generators-replace-table.h @@ -52,3 +52,12 @@ void replace_table_add_key_printf (ReplaceTable *table, const gchar *tag, const */ void replace_table_process (gchar **buffer, ReplaceTable *table); + +/* + * adjust_tags_normal: adjust string for normal HTML use + * adjust_tags_parameter: adjust string for use as tag parameter value + * - both funtions return newly allocated string + */ +void adjust_tags_normal (char **str); +void adjust_tags_parameter (char **str); + diff --git a/generators.c b/generators.c index fd40490..907412a 100644 --- a/generators.c +++ b/generators.c @@ -287,16 +287,12 @@ write_html_album (TGallerySetup *setup, /* Navigation bar */ s1 = g_strdup (items->ID); - /* TODO: check */ - fix_entities (&s1); old_parent_item_index = items->parent_item_index + 1; parent = items->parent_index; level = 1; while (parent) { s3 = make_string ("../", level); s4 = g_strdup (parent->ID); - /* TODO: check */ - fix_entities (&s4); s5 = setup->use_inpage_links ? g_strdup_printf ("#i%d", old_parent_item_index) : g_strdup (""); s2 = g_strdup_printf ("%s > %s", s3, s5, s4, s1); free (s3); @@ -314,20 +310,26 @@ write_html_album (TGallerySetup *setup, /* META tags */ s1 = g_strdup_printf ("\t\n", APP_VERSION); if (setup->meta_author || items->meta_author) { - s2 = g_strdup_printf ("%s\t\n", s1, - items->meta_author ? items->meta_author : setup->meta_author); + s3 = g_strdup (items->meta_author ? items->meta_author : setup->meta_author); + adjust_tags_parameter (&s3); + s2 = g_strdup_printf ("%s\t\n", s1, s3); + g_free (s3); g_free (s1); s1 = s2; } if (setup->meta_description || items->meta_description) { - s2 = g_strdup_printf ("%s\t\n", s1, - items->meta_description? items->meta_description : setup->meta_description); + s3 = g_strdup (items->meta_description ? items->meta_description : setup->meta_description); + adjust_tags_parameter (&s3); + s2 = g_strdup_printf ("%s\t\n", s1, s3); + g_free (s3); g_free (s1); s1 = s2; } if (setup->meta_keywords || items->meta_keywords) { - s2 = g_strdup_printf ("%s\t\n", s1, - items->meta_keywords ? items->meta_keywords : setup->meta_keywords); + s3 = g_strdup (items->meta_keywords ? items->meta_keywords : setup->meta_keywords); + adjust_tags_parameter (&s3); + s2 = g_strdup_printf ("%s\t\n", s1, s3); + g_free (s3); g_free (s1); s1 = s2; } @@ -651,12 +653,12 @@ write_html_image (TGallerySetup *setup, } /* Convert line breaks to be visible in the HTML code */ if (title) { - str_replace (&title, "\r\n", "
", NULL); - str_replace (&title, "\n", "
", NULL); + str_replace (&title, "\r\n", "
"); + str_replace (&title, "\n", "
"); } if (title_desc) { - str_replace (&title_desc, "\r\n", "
", NULL); - str_replace (&title_desc, "\n", "
", NULL); + str_replace (&title_desc, "\r\n", "
"); + str_replace (&title_desc, "\n", "
"); } } if (title) title = g_strstrip (title); @@ -695,8 +697,6 @@ write_html_image (TGallerySetup *setup, while (parent) { s3 = make_string ("../", level); s4 = g_strdup (parent->ID); - /* TODO: check */ - fix_entities (&s4); s5 = setup->use_inpage_links ? g_strdup_printf ("#i%d", parent == parent_items ? item_index : old_parent_item_index) : g_strdup (""); s2 = g_strdup_printf ("%s > %s", s3, s5, s4, s1); free (s3); @@ -753,22 +753,26 @@ write_html_image (TGallerySetup *setup, override_title_meta = setup->use_title_as_meta && title && (strlen (title) > 0); s1 = g_strdup_printf ("\t\n", APP_VERSION); if (setup->meta_author || parent_items->meta_author) { - s2 = g_strdup_printf ("%s\t\n", s1, - parent_items->meta_author ? parent_items->meta_author : setup->meta_author); + s3 = g_strdup (parent_items->meta_author ? parent_items->meta_author : setup->meta_author); + adjust_tags_parameter (&s3); + s2 = g_strdup_printf ("%s\t\n", s1, s3); + g_free (s3); g_free (s1); s1 = s2; } if (setup->meta_description || parent_items->meta_description || override_title_meta) { - s3 = override_title_meta ? g_markup_escape_text (title, -1) : NULL; - s2 = g_strdup_printf ("%s\t\n", s1, - override_title_meta ? s3 : (parent_items->meta_description ? parent_items->meta_description : setup->meta_description)); + s3 = g_strdup (override_title_meta ? title : (parent_items->meta_description ? parent_items->meta_description : setup->meta_description)); + adjust_tags_parameter (&s3); + s2 = g_strdup_printf ("%s\t\n", s1, s3); + g_free (s3); g_free (s1); - if (s3) g_free (s3); s1 = s2; } if ((setup->meta_keywords || parent_items->meta_keywords) && (! override_title_meta)) { - s2 = g_strdup_printf ("%s\t\n", s1, - parent_items->meta_keywords ? parent_items->meta_keywords : setup->meta_keywords); + s3 = g_strdup (parent_items->meta_keywords ? parent_items->meta_keywords : setup->meta_keywords); + adjust_tags_parameter (&s3); + s2 = g_strdup_printf ("%s\t\n", s1, s3); + g_free (s3); g_free (s1); s1 = s2; } diff --git a/sample/src/CIAF_1/index.xml b/sample/src/CIAF_1/index.xml index 5d40936..8f4abb7 100644 --- a/sample/src/CIAF_1/index.xml +++ b/sample/src/CIAF_1/index.xml @@ -46,7 +46,7 @@ This photo doesn't contain link to original image, but src parameter is specified. - can have CDATA entries here!]]> + can have CDATA entries here!]]> Photo title @@ -69,7 +69,7 @@ - <![CDATA[Photo containing <em>CDATA</em> <u>structures</u>]]> + <![CDATA[Photo containing <em>CDATA</em> <span style="text-decoration: underline;">structures</span>]]> links etc...]]> diff --git a/sample/src/entities/index.xml b/sample/src/entities/index.xml new file mode 100644 index 0000000..54bdb2f --- /dev/null +++ b/sample/src/entities/index.xml @@ -0,0 +1,28 @@ + + + + Entities → '&' "&copy;" + Entities → '&' "&copy;" + ahoj! ]]> + ahoj! ]]> + + + + META Author → '&' "&copy;" + ahoj! ]]> + + + + + + Entities → '&' "&copy;" + Entities → '&' "&copy;" + + + + <![CDATA[Entities → '&' "©" < <b>ahoj!</b> <!-- comment <b>neco</b> -->]]> + ahoj! ]]> + + + + diff --git a/sample/src/entities/preview/img_6802.jpg b/sample/src/entities/preview/img_6802.jpg new file mode 120000 index 0000000..a13dc2a --- /dev/null +++ b/sample/src/entities/preview/img_6802.jpg @@ -0,0 +1 @@ +../../CIAF_1/preview/img_6802.jpg \ No newline at end of file diff --git a/sample/src/entities/preview/img_6802b.jpg b/sample/src/entities/preview/img_6802b.jpg new file mode 120000 index 0000000..a13dc2a --- /dev/null +++ b/sample/src/entities/preview/img_6802b.jpg @@ -0,0 +1 @@ +../../CIAF_1/preview/img_6802.jpg \ No newline at end of file diff --git a/sample/src/index.xml b/sample/src/index.xml index 79a90c1..80f6145 100644 --- a/sample/src/index.xml +++ b/sample/src/index.xml @@ -40,5 +40,10 @@ + + Entities → '&' "&copy;" + ahoj! xx]]> + + diff --git a/templates/template-album.tmpl b/templates/template-album.tmpl index 7123b5a..75d55e5 100644 --- a/templates/template-album.tmpl +++ b/templates/template-album.tmpl @@ -3,7 +3,7 @@ - <!-- $(PAGE_TITLE) --> + $(PAGE_TITLE) @@ -34,17 +34,17 @@
-
- - <!-- $(IMG_TITLE) --> + -
- - <!-- $(IMG_TITLE) --> + diff --git a/templates/template-index.tmpl b/templates/template-index.tmpl index 22a8ad3..edb583a 100644 --- a/templates/template-index.tmpl +++ b/templates/template-index.tmpl @@ -3,7 +3,7 @@ - <!-- $(PAGE_TITLE) --> + $(PAGE_TITLE) @@ -34,9 +34,9 @@