From a985af97b7a1e208ea0379308cc0e03db28787dc Mon Sep 17 00:00:00 2001 From: Tomas Bzatek Date: Thu, 19 Feb 2009 22:50:50 +0100 Subject: Introduce tag replace table, part I. --- generators.c | 419 ++++++++++++++++++++++++----------------------------------- 1 file changed, 168 insertions(+), 251 deletions(-) (limited to 'generators.c') diff --git a/generators.c b/generators.c index 356c434..8ac90db 100644 --- a/generators.c +++ b/generators.c @@ -29,6 +29,7 @@ #include "items.h" #include "jpeg-utils.h" #include "gallery-utils.h" +#include "generators-replace-table.h" /* @@ -625,6 +626,7 @@ write_html_image (TGallerySetup *setup, int level, old_parent_item_index; gboolean override_title_meta; gboolean image_fullsize; + ReplaceTable *replace_table; fin = fopen (template_src, "r"); @@ -642,6 +644,8 @@ write_html_image (TGallerySetup *setup, buffer = malloc (BUFFER_SIZE); preload_imgname = NULL; + replace_table = replace_table_new (); + /* Get our index in the album */ item_index = 0; real_item_index = 0; @@ -695,6 +699,164 @@ write_html_image (TGallerySetup *setup, if (image_fullsize) get_image_sizes (orig_dst, &img_orig_w, &img_orig_h); + /* Get title and description from IPTC/EXIF/JPEG if not defined */ + title = g_strdup (item->title); + title_desc = g_strdup (item->title_description); + if (setup->use_iptc_exif && title == NULL && title_desc == NULL) { + if (exif->iptc_caption) + title = g_strdup (exif->iptc_caption); + if (exif->jpeg_comment) { + if (! title) + title = g_strdup (exif->jpeg_comment); + else + title_desc = g_strdup (exif->jpeg_comment); + } + if (exif->exif_imgdesc) { + if (! title) + title = g_strdup (exif->exif_imgdesc); +/* if (! title_desc) -- disabled + title_desc = g_strdup (exif->exif_imgdesc); */ + } + if (exif->exif_usercomment) { + if (! title) + title = g_strdup (exif->exif_usercomment); + if (! title_desc) + title_desc = g_strdup (exif->exif_usercomment); + } + /* Convert line breaks to be visible in the HTML code */ + if (title) { + str_replace (&title, "\r\n", "
", NULL); + str_replace (&title, "\n", "
", NULL); + } + if (title_desc) { + str_replace (&title_desc, "\r\n", "
", NULL); + str_replace (&title_desc, "\n", "
", NULL); + } + } + if (title) title = g_strstrip (title); + if (title_desc) title_desc = g_strstrip (title_desc); + + /* Page title */ + s1 = (title && strlen (title) > 0) ? g_strdup_printf("%s | ", title) : NULL; + s2 = g_strdup_printf ("%s [%d/%d]", parent_items->title ? parent_items->title : parent_items->ID, real_item_index, real_total_items); + s3 = setup->site_title ? g_strdup_printf(" | %s", setup->site_title) : NULL; + s4 = g_strconcat (s1 ? s1 : "", s2, s3 ? s3 : "", NULL); + replace_table_add_key (replace_table, "PAGE_TITLE", s4); + if (s1) g_free (s1); + if (s2) g_free (s2); + if (s3) g_free (s3); + g_free (s4); + + /* Simple placeholders */ + replace_table_add_key (replace_table, "FILE_NAME", imgname); + replace_table_add_key (replace_table, "TITLE", title); + replace_table_add_key (replace_table, "DESCRIPTION", title_desc); + replace_table_add_key (replace_table, "FOOTER", setup->footer); + replace_table_add_key_int (replace_table, "TOTAL_ITEMS", real_total_items); + replace_table_add_key_int (replace_table, "FILE_NO", real_item_index); + replace_table_add_key_int (replace_table, "IMG_SIZE_BIG_W", img_big_w); + replace_table_add_key_int (replace_table, "IMG_SIZE_BIG_H", img_big_h); + replace_table_add_key_int (replace_table, "IMG_SIZE_ORIG_W", img_orig_w); + replace_table_add_key_int (replace_table, "IMG_SIZE_ORIG_H", img_orig_h); + s1 = g_strdup_printf ("%s/%s", IMG_BIG_DIR, imgname); + replace_table_add_key (replace_table, "IMG_SRC_BIG", s1); + g_free (s1); + s1 = g_strdup_printf ("%s/%s", IMG_ORIG_DIR, imgname); + replace_table_add_key (replace_table, "IMG_SRC_FULL", s1); + g_free (s1); + replace_table_add_key (replace_table, "IMG_SRC_PRELOAD", preload_imgname ? preload_imgname : ""); + + /* Navigation bar */ + s1 = g_strdup (imgname); + parent = parent_items; + level = 0; + 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); + g_free (s1); + g_free (s4); + g_free (s5); + s1 = s2; + old_parent_item_index = parent->parent_item_index + 1; + parent = parent->parent_index; + level++; + } + replace_table_add_key (replace_table, "NAV_BAR", s1); + g_free (s1); + + /* EXIF values */ + replace_table_add_key (replace_table, "EXIF_ISO", exif->iso ? exif->iso : "??"); + replace_table_add_key (replace_table, "EXIF_TIME", exif->exposure ? exif->exposure : "??"); + replace_table_add_key (replace_table, "EXIF_APERTURE", exif->aperture ? exif->aperture : "??"); + replace_table_add_key (replace_table, "EXIF_FOCAL_LENGTH", exif->focal_length ? exif->focal_length : "??"); + replace_table_add_key (replace_table, "EXIF_FLASH", exif->flash ? exif->flash : "??"); + replace_table_add_key (replace_table, "EXIF_DATE", exif->datetime ? exif->datetime : "??"); + replace_table_add_key (replace_table, "EXIF_CAMERA_MODEL", exif->camera_model ? exif->camera_model : "??"); + s1 = g_strdup_printf ("(%s)", exif->focal_length_35mm); + replace_table_add_key (replace_table, "EXIF_FOCAL_35", exif->focal_length_35mm ? s1 : ""); + g_free (s1); + + /* Border style */ + s1 = item->border_style; + if (s1 == NULL) + s1 = parent_items->border_style; + if (s1 == NULL) + s1 = setup->border_style; + if (s1 == NULL) + s1 = "border_single"; + replace_table_add_key (replace_table, "IMG_BORDER_STYLE", s1); + + /* Next/Previous links */ + if (next_item) { + s2 = g_path_get_basename ((next_item->path == NULL && next_item->preview) ? next_item->preview : next_item->path); + s1 = g_strconcat (s2, ".html", NULL); + replace_table_add_key (replace_table, "LINK_NEXT", s1); + g_free (s1); + g_free (s2); + } + else + replace_table_add_key (replace_table, "LINK_NEXT", "index.html"); + if (previous_item) { + s2 = g_path_get_basename ((previous_item->path == NULL && previous_item->preview) ? previous_item->preview : previous_item->path); + s1 = g_strconcat (s2, ".html", NULL); + replace_table_add_key (replace_table, "LINK_PREV", s1); + g_free (s1); + g_free (s2); + } + else + replace_table_add_key (replace_table, "LINK_PREV", "index.html"); + + /* META tags */ + 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); + 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)); + 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); + g_free (s1); + s1 = s2; + } + replace_table_add_key (replace_table, "CGG_META_TAGS", s1); + g_free (s1); + /* Read through the template and replace placeholders with real data */ while (! feof (fin)) { @@ -703,7 +865,6 @@ write_html_image (TGallerySetup *setup, break; b = strdup (buffer); - /* Block placeholders */ if (strstr (buffer, "")) { in_img_fullsize_link = TRUE; @@ -723,255 +884,10 @@ write_html_image (TGallerySetup *setup, continue; } - /* Get title and description from IPTC/EXIF/JPEG if not defined */ - title = g_strdup (item->title); - title_desc = g_strdup (item->title_description); - if (setup->use_iptc_exif && title == NULL && title_desc == NULL) { - if (exif->iptc_caption) - title = g_strdup (exif->iptc_caption); - if (exif->jpeg_comment) { - if (! title) - title = g_strdup (exif->jpeg_comment); - else - title_desc = g_strdup (exif->jpeg_comment); - } - if (exif->exif_imgdesc) { - if (! title) - title = g_strdup (exif->exif_imgdesc); -/* if (! title_desc) -- disabled - title_desc = g_strdup (exif->exif_imgdesc); */ - } - if (exif->exif_usercomment) { - if (! title) - title = g_strdup (exif->exif_usercomment); - if (! title_desc) - title_desc = g_strdup (exif->exif_usercomment); - } - /* Convert line breaks to be visible in the HTML code */ - if (title) { - str_replace (&title, "\r\n", "
", NULL); - str_replace (&title, "\n", "
", NULL); - } - if (title_desc) { - str_replace (&title_desc, "\r\n", "
", NULL); - str_replace (&title_desc, "\n", "
", NULL); - } - } - if (title) title = g_strstrip (title); - if (title_desc) title_desc = g_strstrip (title_desc); - - /* Page title */ - if (strstr (b, "")) { - s1 = (title && strlen (title) > 0) ? g_strdup_printf("%s | ", title) : NULL; - s2 = g_strdup_printf ("%s [%d/%d]", parent_items->title ? parent_items->title : parent_items->ID, real_item_index, real_total_items); - s3 = setup->site_title ? g_strdup_printf(" | %s", setup->site_title) : NULL; - s4 = g_strconcat (s1 ? s1 : "", s2, s3 ? s3 : "", NULL); - if (s1) g_free (s1); - if (s2) g_free (s2); - if (s3) g_free (s3); - s1 = g_markup_escape_text (s4, -1); - str_replace (&b, "", s1, NULL); - g_free (s4); - g_free (s1); - } - /* Simple placeholders */ - if (strstr (b, "")) - str_replace (&b, "", imgname, NULL); - if (strstr (b, "") && title) { - s1 = g_strdup (title); - fix_entities (&s1); - str_replace (&b, "", s1, NULL); - g_free (s1); - } - if (strstr (b, "") && title_desc) { - s1 = g_strdup (title_desc); - fix_entities (&s1); - str_replace (&b, "", s1, NULL); - g_free (s1); - } - if (strstr (b, "")) { - s1 = g_strdup_printf ("%d", real_total_items); - str_replace (&b, "", s1, NULL); - g_free (s1); - } - if (strstr (b, "")) { - s1 = g_strdup_printf ("%d", real_item_index); - str_replace(&b, "", s1, NULL); - g_free (s1); - } - if (strstr (b, "")) { - s1 = g_strdup (imgname); - parent = parent_items; - level = 0; - while (parent) { - s3 = make_string ("../", level); - s4 = g_strdup (parent->ID); - 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); - g_free (s1); - g_free (s4); - g_free (s5); - s1 = s2; - old_parent_item_index = parent->parent_item_index + 1; - parent = parent->parent_index; - level++; - } - str_replace (&b, "", s1, NULL); - g_free (s1); - } - if (strstr (b, "")) { - s1 = g_strconcat (IMG_BIG_DIR, "/", imgname, NULL); - str_replace (&b, "", s1, NULL); - g_free (s1); - } - if (strstr (b, "")) { - s1 = g_strconcat (IMG_ORIG_DIR, "/", imgname, NULL); - str_replace (&b, "", s1, NULL); - g_free (s1); - } - if (strstr (b, "")) { - s1 = g_strdup_printf ("%lu", img_big_w); - str_replace (&b, "", s1, NULL); - g_free (s1); - } - if (strstr (b, "")) { - s1 = g_strdup_printf ("%lu", img_big_h); - str_replace (&b, "", s1, NULL); - g_free (s1); - } - if (strstr (b, "")) { - s1 = g_strdup_printf ("%lu", img_orig_w); - str_replace (&b, "", s1, NULL); - g_free (s1); - } - if (strstr (b, "")) { - s1 = g_strdup_printf ("%lu", img_orig_h); - str_replace (&b, "", s1, NULL); - g_free (s1); - } - if (strstr (b, "")) { - s1 = item->border_style; - if (s1 == NULL) - s1 = parent_items->border_style; - if (s1 == NULL) - s1 = setup->border_style; - if (s1 == NULL) - s1 = "border_single"; - str_replace (&b, "", s1, NULL); - } - if (strstr (b, "$(IMG_SRC_PRELOAD)")) - str_replace (&b, "$(IMG_SRC_PRELOAD)", preload_imgname ? preload_imgname : "", NULL); - - if (strstr (b, "")) { - if (exif->iso) - str_replace (&b, "", exif->iso, NULL); - else - str_replace (&b, "", "??", NULL); - } - if (strstr (b, "")) { - if (exif->exposure) - str_replace (&b, "", exif->exposure, NULL); - else - str_replace (&b, "", "??", NULL); - } - if (strstr (b, "")) { - if (exif->aperture) - str_replace (&b, "", exif->aperture, NULL); - else - str_replace (&b, "", "??", NULL); - } - if (strstr (b, "")) { - if (exif->focal_length) - str_replace (&b, "", exif->focal_length, NULL); - else - str_replace (&b, "", "??", NULL); - } - if (strstr (b, "")) { - if (exif->flash) - str_replace (&b, "", exif->flash, NULL); - else - str_replace (&b, "", "??", NULL); - } - if (strstr (b, "")) { - if (exif->datetime) - str_replace (&b, "", exif->datetime, NULL); - else - str_replace (&b, "", "??", NULL); - } - if (strstr (b, "")) { - if (exif->camera_model) - str_replace (&b, "", exif->camera_model, NULL); - else - str_replace (&b, "", "??", NULL); - } - if (strstr (b, "")) { - if (exif->focal_length_35mm) { - s1 = g_strconcat ("(", exif->focal_length_35mm, ")", NULL); - str_replace (&b, "", s1, NULL); - g_free (s1); - } - else - str_replace (&b, "", "", NULL); - } - - if (strstr (b, "")) { - if (next_item) { - s2 = g_path_get_basename ((next_item->path == NULL && next_item->preview) ? next_item->preview : next_item->path); - s1 = g_strconcat (s2, ".html", NULL); - str_replace (&b, "", s1, NULL); - g_free (s1); - g_free (s2); - } - else - str_replace (&b, "", "index.html", NULL); - } - if (strstr(b, "")) { - if (previous_item) { - s2 = g_path_get_basename ((previous_item->path == NULL && previous_item->preview) ? previous_item->preview : previous_item->path); - s1 = g_strconcat (s2, ".html", NULL); - str_replace (&b, "", s1, NULL); - g_free (s1); - g_free (s2); - } - else - str_replace (&b, "", "index.html", NULL); - } - - if (strstr (b, "") && setup->footer) { - s1 = g_strdup (setup->footer); - fix_entities (&s1); - str_replace (&b, "", s1, NULL); - g_free (s1); - } - if (strstr (b, "")) { - 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); - 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)); - 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); - g_free (s1); - s1 = s2; - } - str_replace (&b, "", s1, NULL); - g_free (s1); - } + /* Replace all known tags */ + replace_table_process (&b, replace_table); + /* Write to file */ if (! fputs (b, fout)) { fprintf (stderr, "write_html_image: error writing to file \"%s\": %s\n", dst, strerror (errno)); res = FALSE; @@ -979,12 +895,12 @@ write_html_image (TGallerySetup *setup, break; } free (b); - if (title) g_free (title); - if (title_desc) g_free (title_desc); } fclose (fout); fclose (fin); + if (title) g_free (title); + if (title_desc) g_free (title_desc); free (buffer); free (big_dst); free (orig_dst); @@ -993,6 +909,7 @@ write_html_image (TGallerySetup *setup, if (preload_imgname) g_free (preload_imgname); free_exif_data (exif); + replace_table_free (replace_table); return res; } -- cgit v1.2.3