diff options
| author | Tomas Bzatek <tbzatek@users.sourceforge.net> | 2009-02-22 13:55:03 +0100 |
|---|---|---|
| committer | Tomas Bzatek <tbzatek@users.sourceforge.net> | 2009-02-22 13:55:03 +0100 |
| commit | 9caacb6270bd4b4836c751cf86a25073f776986a (patch) | |
| tree | 4769f246c7aa8edd0faa119f2a67da5c5ea45488 /generators.c | |
| parent | a985af97b7a1e208ea0379308cc0e03db28787dc (diff) | |
| download | cataract-9caacb6270bd4b4836c751cf86a25073f776986a.tar.xz | |
Introduce tag replace table, part II.
Things might be little faster in some cases and slower in others.
Most of the CPU time is lost in image handling anyway.
Diffstat (limited to 'generators.c')
| -rw-r--r-- | generators.c | 359 |
1 files changed, 139 insertions, 220 deletions
diff --git a/generators.c b/generators.c index 8ac90db..fd40490 100644 --- a/generators.c +++ b/generators.c @@ -222,12 +222,14 @@ write_html_album (TGallerySetup *setup, TIndexItem *item; TIndexItem *tmp_item; int level, old_parent_item_index; - gboolean res; + gboolean res, bb; int i; unsigned long img_w, img_h; char *img_src; char *thumb; unsigned int real_total_items; + ReplaceTable *global_replace_table; + ReplaceTable *local_replace_table; fin = fopen (template_src, "r"); @@ -254,6 +256,8 @@ write_html_album (TGallerySetup *setup, in_go_up_string = FALSE; res = TRUE; + global_replace_table = replace_table_new (); + /* Get number of real pictures in the list */ real_total_items = 0; for (i = 0; i < items->items->len; i++) { @@ -261,6 +265,75 @@ write_html_album (TGallerySetup *setup, if (tmp_item->type == INDEX_ITEM_TYPE_PICTURE) real_total_items++; } + replace_table_add_key_int (global_replace_table, "TOTAL_ITEMS", real_total_items); + + /* Page title */ + if (items->parent_index == NULL || setup->site_title == NULL) + s1 = g_strdup (setup->site_title ? setup->site_title : items->ID); + else + s1 = g_strdup_printf ("%s | %s", items->title, setup->site_title); + replace_table_add_key (global_replace_table, "PAGE_TITLE", s1); + g_free (s1); + + /* Simple placeholders */ + replace_table_add_key (global_replace_table, "ID", items->ID); + replace_table_add_key (global_replace_table, "TITLE", items->title); + replace_table_add_key (global_replace_table, "DESCRIPTION", items->desc); + replace_table_add_key (global_replace_table, "FOOTNOTE", items->footnote); + replace_table_add_key (global_replace_table, "FOOTER", setup->footer); + s1 = setup->use_inpage_links ? g_strdup_printf ("../index.html#i%d", items->parent_item_index + 1) : g_strdup ("../index.html"); + replace_table_add_key (global_replace_table, "GO_UP_LINK", s1); + g_free (s1); + + /* 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 ("<a href=\"%sindex.html%s\">%s</a> > %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 (global_replace_table, "NAV_BAR", s1); + g_free (s1); + + /* META tags */ + s1 = g_strdup_printf ("\t<meta name=\"generator\" content=\"Cataract Gallery Generator v%s\" />\n", APP_VERSION); + if (setup->meta_author || items->meta_author) { + s2 = g_strdup_printf ("%s\t<meta name=\"author\" content=\"%s\" />\n", s1, + items->meta_author ? items->meta_author : setup->meta_author); + g_free (s1); + s1 = s2; + } + if (setup->meta_description || items->meta_description) { + s2 = g_strdup_printf ("%s\t<meta name=\"description\" content=\"%s\" />\n", s1, + items->meta_description? items->meta_description : setup->meta_description); + g_free (s1); + s1 = s2; + } + if (setup->meta_keywords || items->meta_keywords) { + s2 = g_strdup_printf ("%s\t<meta name=\"keywords\" content=\"%s\" />\n", s1, + items->meta_keywords ? items->meta_keywords : setup->meta_keywords); + g_free (s1); + s1 = s2; + } + replace_table_add_key (global_replace_table, "CGG_META_TAGS", s1); + g_free (s1); + /* Read through the template and replace placeholders with real data */ while (! feof (fin)) @@ -268,177 +341,58 @@ write_html_album (TGallerySetup *setup, memset (buffer, 0, BUFFER_SIZE); if (! fgets (buffer, BUFFER_SIZE, fin)) break; - if (buffer == NULL) - continue; - b = strdup (buffer); - /* Block placeholders */ if (in_img_list && (strstr (buffer, "<!-- $(BEGIN_IMG_LIST_LANDSCAPE) -->"))) { in_img_list_landscape = TRUE; - free (b); continue; } if (in_img_list && (strstr (buffer, "<!-- $(END_IMG_LIST_LANDSCAPE) -->"))) { in_img_list_landscape = FALSE; - free (b); continue; } if (in_img_list && (strstr (buffer, "<!-- $(BEGIN_IMG_LIST_PORTRAIT) -->"))) { in_img_list_portrait = TRUE; - free (b); continue; } if (in_img_list && (strstr (buffer, "<!-- $(END_IMG_LIST_PORTRAIT) -->"))) { in_img_list_portrait = FALSE; - free (b); continue; } if (in_img_list && (strstr (buffer, "<!-- $(BEGIN_LIST_SEPARATOR) -->"))) { in_img_separator = TRUE; - free (b); continue; } if (in_img_list && (strstr (buffer, "<!-- $(END_LIST_SEPARATOR) -->"))) { in_img_separator = FALSE; - free (b); continue; } if (in_img_list && in_img_list_landscape) { - buf_img_list_landscape = strncat (buf_img_list_landscape, b, BUFFER_SIZE - strlen (buf_img_list_landscape) - 2); - free (b); + buf_img_list_landscape = strncat (buf_img_list_landscape, buffer, BUFFER_SIZE - strlen (buf_img_list_landscape) - 2); continue; } if (in_img_list && in_img_list_portrait) { - buf_img_list_portrait = strncat (buf_img_list_portrait, b, BUFFER_SIZE - strlen (buf_img_list_portrait) - 2); - free (b); + buf_img_list_portrait = strncat (buf_img_list_portrait, buffer, BUFFER_SIZE - strlen (buf_img_list_portrait) - 2); continue; } if (in_img_list && in_img_separator) { - buf_img_separator = strncat (buf_img_separator, b, BUFFER_SIZE - strlen (buf_img_separator) - 2); - free (b); + buf_img_separator = strncat (buf_img_separator, buffer, BUFFER_SIZE - strlen (buf_img_separator) - 2); continue; } if (strstr (buffer, "<!-- $(BEGIN_GO_UP) -->")) { memset (buf_go_up_string, 0, BUFFER_SIZE); in_go_up_string = TRUE; - free (b); continue; } - if (in_go_up_string && (strstr (buffer, "<!-- $(END_GO_UP) -->"))) { + if (in_go_up_string && strstr (buffer, "<!-- $(END_GO_UP) -->")) { in_go_up_string = FALSE; - free (b); - /* print the "Go Up" string if not toplevel */ - if (items->parent_index) { - b = strdup (buf_go_up_string); - if (strstr (b, "$(GO_UP_LINK)")) { - s1 = setup->use_inpage_links ? g_strdup_printf ("../index.html#i%d", items->parent_item_index + 1) : g_strdup ("../index.html"); - str_replace (&b, "$(GO_UP_LINK)", s1, NULL); - g_free (s1); - } - } - else continue; + if (! items->parent_index) + continue; } if (in_go_up_string) { - buf_go_up_string = strncat (buf_go_up_string, b, BUFFER_SIZE - strlen (buf_go_up_string) - 2); - free (b); - continue; - } - - - /* Simple placeholders */ - if (strstr (b, "<!-- $(PAGE_TITLE) -->")) { - if (items->parent_index == NULL || setup->site_title == NULL) - s1 = g_strdup (setup->site_title ? setup->site_title : items->ID); - else - s1 = g_strdup_printf ("%s | %s", items->title, setup->site_title); - s2 = g_markup_escape_text (s1, -1); - str_replace (&b, "<!-- $(PAGE_TITLE) -->", s2, NULL); - g_free (s2); - g_free (s1); - } - if (strstr (b, "<!-- $(ID) -->") && items->ID) { - s1 = g_strdup (items->ID); - fix_entities (&s1); - str_replace (&b, "<!-- $(ID) -->", s1, NULL); - g_free (s1); - } - if (strstr (b, "<!-- $(TITLE) -->") && items->title) { - s1 = g_strdup (items->title); - fix_entities (&s1); - str_replace (&b, "<!-- $(TITLE) -->", s1, NULL); - g_free (s1); - } - if (strstr (b, "<!-- $(DESCRIPTION) -->") && items->desc) { - s1 = g_strdup (items->desc); - fix_entities (&s1); - str_replace (&b, "<!-- $(DESCRIPTION) -->", s1, NULL); - g_free (s1); - } - if (strstr (b, "<!-- $(FOOTNOTE) -->") && items->footnote) { - s1 = g_strdup (items->footnote); - fix_entities (&s1); - str_replace (&b, "<!-- $(FOOTNOTE) -->", s1, NULL); - g_free (s1); - } - if (strstr (b, "<!-- $(FOOTER) -->") && setup->footer) { - s1 = g_strdup (setup->footer); - fix_entities (&s1); - str_replace (&b, "<!-- $(FOOTER) -->", s1, NULL); - g_free (s1); - } - if (strstr (b, "<!-- $(CGG_META_TAGS) -->")) { - s1 = g_strdup_printf ("\t<meta name=\"generator\" content=\"Cataract Gallery Generator v%s\" />\n", APP_VERSION); - if (setup->meta_author || items->meta_author) { - s2 = g_strdup_printf ("%s\t<meta name=\"author\" content=\"%s\" />\n", s1, - items->meta_author ? items->meta_author : setup->meta_author); - g_free (s1); - s1 = s2; - } - if (setup->meta_description || items->meta_description) { - s2 = g_strdup_printf ("%s\t<meta name=\"description\" content=\"%s\" />\n", s1, - items->meta_description? items->meta_description : setup->meta_description); - g_free (s1); - s1 = s2; - } - if (setup->meta_keywords || items->meta_keywords) { - s2 = g_strdup_printf ("%s\t<meta name=\"keywords\" content=\"%s\" />\n", s1, - items->meta_keywords ? items->meta_keywords : setup->meta_keywords); - g_free (s1); - s1 = s2; - } - str_replace (&b, "<!-- $(CGG_META_TAGS) -->", s1, NULL); - g_free (s1); - } - if (strstr (b, "<!-- $(TOTAL_ITEMS) -->")) { - s1 = g_strdup_printf ("%d", real_total_items); - str_replace (&b, "<!-- $(TOTAL_ITEMS) -->", s1, NULL); - g_free (s1); - } - if (strstr (b, "<!-- $(NAV_BAR) -->")) { - s1 = g_strdup (items->ID); - 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); - fix_entities (&s4); - s5 = setup->use_inpage_links ? g_strdup_printf ("#i%d", old_parent_item_index) : g_strdup (""); - s2 = g_strdup_printf ("<a href=\"%sindex.html%s\">%s</a> > %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, "<!-- $(NAV_BAR) -->", s1, NULL); - g_free (s1); + buf_go_up_string = strncat (buf_go_up_string, buffer, BUFFER_SIZE - strlen (buf_go_up_string) - 2); + continue; } /* Image list, nested placeholders */ @@ -450,10 +404,8 @@ write_html_album (TGallerySetup *setup, in_img_list_landscape = FALSE; in_img_list_portrait = FALSE; in_img_separator = FALSE; - free (b); continue; } - if (in_img_list && (strstr (buffer, "<!-- $(END_IMG_LIST) -->"))) { in_img_list = FALSE; in_img_list_landscape = FALSE; @@ -465,19 +417,19 @@ write_html_album (TGallerySetup *setup, item = g_ptr_array_index (items->items, i); if (item == NULL) { fprintf (stderr, "write_html_index: error getting item %d\n", i); - free (b); continue; } /* Generate the images (preview, original, thumbnail) */ + local_replace_table = replace_table_new (); img_w = 0; img_h = 0; - img_src = NULL; - thumb = NULL; s1 = NULL; switch (item->type) { case INDEX_ITEM_TYPE_PICTURE: + img_src = NULL; + thumb = NULL; generate_image (setup, items, item, i, dst, &img_w, &img_h, &img_src, &thumb); /* Skip HTML code generation if it's a hidden item */ if (! item->hidden) { @@ -486,95 +438,69 @@ write_html_album (TGallerySetup *setup, else s1 = strdup (buf_img_list_portrait); - if (strstr (s1, "<!-- $(ALBUM_SUBPATH) -->")) { - s2 = g_strconcat (item->path, "/index.html", NULL); - str_replace (&s1, "<!-- $(ALBUM_SUBPATH) -->", s2, NULL); - g_free (s2); - } - if (strstr (s1, "<!-- $(IMG_SUBPAGE) -->")) { - s2 = g_strconcat (img_src, ".html", NULL); - str_replace (&s1, "<!-- $(IMG_SUBPAGE) -->", s2, NULL); - g_free (s2); - } - if (strstr (s1, "<!-- $(IMG_TITLE) -->") && item->title) { - s2 = g_strdup (item->title); - fix_entities (&s2); - str_replace (&s1, "<!-- $(IMG_TITLE) -->", s2, NULL); - g_free (s2); - } - if (strstr (s1, "<!-- $(IMG_DESCRIPTION) -->") && item->title_description) { - s2 = g_strdup (item->title_description); - fix_entities (&s2); - str_replace (&s1, "<!-- $(IMG_DESCRIPTION) -->", s2, NULL); - g_free (s2); - } - if (strstr (s1, "<!-- $(ALBUM_NUM_ITEMS) -->")) { + replace_table_add_key_printf (local_replace_table, "ALBUM_SUBPATH", "%s/index.html", item->path); + replace_table_add_key_printf (local_replace_table, "IMG_SUBPAGE", "%s.html", img_src); + replace_table_add_key (local_replace_table, "IMG_TITLE", item->title); + replace_table_add_key (local_replace_table, "IMG_DESCRIPTION", item->title_description); + replace_table_add_key_printf (local_replace_table, "IMG_LIST_ID", "i%d", i + 1); + replace_table_add_key_printf (local_replace_table, "IMG_THUMBNAIL", "%s/%s", THUMBNAIL_DIR, thumb); + replace_table_add_key (local_replace_table, "IMG_FILENAME", img_src); + if (items->type == GALLERY_TYPE_INDEX) { s3 = g_strconcat (items->base_dir, "/", item->path, "/index.xml", NULL); - s2 = g_strdup_printf ("%d", get_album_objects_count (s3)); - str_replace (&s1, "<!-- $(ALBUM_NUM_ITEMS) -->", s2, NULL); - g_free (s2); + replace_table_add_key_int (local_replace_table, "ALBUM_NUM_ITEMS", get_album_objects_count (s3)); g_free (s3); } - if (strstr (s1, "<!-- $(IMG_LIST_ID) -->")) { - s2 = g_strdup_printf ("i%d", i + 1); - str_replace (&s1, "<!-- $(IMG_LIST_ID) -->", s2, NULL); - g_free (s2); - } - if (strstr (s1, "<!-- $(IMG_THUMBNAIL) -->")) { - s2 = g_strconcat (THUMBNAIL_DIR, "/", thumb, NULL); - str_replace (&s1, "<!-- $(IMG_THUMBNAIL) -->", s2, NULL); - g_free (s2); - } - if (strstr (s1, "<!-- $(IMG_FILENAME) -->")) - str_replace (&s1, "<!-- $(IMG_FILENAME) -->", img_src, NULL); } + if (img_src) + g_free (img_src); + if (thumb) + g_free (thumb); break; case INDEX_ITEM_TYPE_SEPARATOR: s1 = strdup (buf_img_separator); - if (strstr (s1, "<!-- $(LIST_SEPARATOR_TITLE) -->") && item->title) { - s2 = g_strdup (item->title); - fix_entities (&s2); - str_replace (&s1, "<!-- $(LIST_SEPARATOR_TITLE) -->", s2, NULL); - g_free (s2); - } + replace_table_add_key (local_replace_table, "LIST_SEPARATOR_TITLE", item->title); break; } - #ifdef __DEBUG_ALL__ - printf("***** %s ******\n", s1); - #endif + bb = TRUE; if (s1) { - if (! fputs (s1, fout)) { - fprintf (stderr, "write_html_index: error writing to file \"%s\": %s\n", dst, strerror (errno)); - res = FALSE; - free (s1); - break; - } + replace_table_process (&s1, local_replace_table); + bb = fputs (s1, fout); free (s1); } - if (img_src) - g_free (img_src); - if (thumb) - g_free (thumb); + replace_table_free (local_replace_table); + if (! bb) { + fprintf (stderr, "write_html_index: error writing to file \"%s\": %s\n", dst, strerror (errno)); + res = FALSE; + break; + } } - free (b); continue; /* no need to write anything */ } - /* Write the generated string to target file */ - if (! fputs (b, fout)) { + /* Select apropriate line buffer */ + if (strstr (buffer, "<!-- $(END_GO_UP) -->") && items->parent_index) { + b = strdup (buf_go_up_string); + } else + b = strdup (buffer); + + /* Replace all known tags */ + replace_table_process (&b, global_replace_table); + + /* Write to file */ + bb = fputs (b, fout); + free (b); + if (! bb) { fprintf (stderr, "write_html_index: error writing to file \"%s\": %s\n", dst, strerror (errno)); res = FALSE; - free (b); break; } - free (b); } fclose (fout); fclose (fin); - + replace_table_free (global_replace_table); free (buffer); free (buf_img_list_landscape); free (buf_img_list_portrait); @@ -622,7 +548,7 @@ write_html_image (TGallerySetup *setup, char *imgname, *preload_imgname; char *title, *title_desc; char *b; - gboolean res; + gboolean res, bb; int level, old_parent_item_index; gboolean override_title_meta; gboolean image_fullsize; @@ -758,12 +684,8 @@ write_html_image (TGallerySetup *setup, 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_printf (replace_table, "IMG_SRC_BIG", "%s/%s", IMG_BIG_DIR, imgname); + replace_table_add_key_printf (replace_table, "IMG_SRC_FULL", "%s/%s", IMG_ORIG_DIR, imgname); replace_table_add_key (replace_table, "IMG_SRC_PRELOAD", preload_imgname ? preload_imgname : ""); /* Navigation bar */ @@ -814,18 +736,14 @@ write_html_image (TGallerySetup *setup, /* 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); + replace_table_add_key_printf (replace_table, "LINK_NEXT", "%s.html", s2); 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); + replace_table_add_key_printf (replace_table, "LINK_PREV", "%s.html", s2); g_free (s2); } else @@ -863,38 +781,39 @@ write_html_image (TGallerySetup *setup, memset (buffer, 0, BUFFER_SIZE); if (! fgets (buffer, BUFFER_SIZE, fin)) break; - b = strdup (buffer); /* Block placeholders */ if (strstr (buffer, "<!-- $(BEGIN_IMG_FULLSIZE_LINK) -->")) { in_img_fullsize_link = TRUE; - free (b); continue; } if (strstr (buffer, "<!-- $(END_IMG_FULLSIZE_LINK) -->")) { in_img_fullsize_link = FALSE; - free (b); - if (image_fullsize) - b = strdup (buf_img_fullsize_link); - else continue; + if (! image_fullsize) /* write down the block later in this cycle */ + continue; } if (in_img_fullsize_link) { - buf_img_fullsize_link = strncat (buf_img_fullsize_link, b, BUFFER_SIZE - strlen (buf_img_fullsize_link) - 2); - free (b); + buf_img_fullsize_link = strncat (buf_img_fullsize_link, buffer, BUFFER_SIZE - strlen (buf_img_fullsize_link) - 2); continue; } + /* Select apropriate line buffer */ + if (strstr (buffer, "<!-- $(END_IMG_FULLSIZE_LINK) -->") && image_fullsize) { + b = strdup (buf_img_fullsize_link); + } else + b = strdup (buffer); + /* Replace all known tags */ replace_table_process (&b, replace_table); /* Write to file */ - if (! fputs (b, fout)) { + bb = fputs (b, fout); + free (b); + if (! bb) { fprintf (stderr, "write_html_image: error writing to file \"%s\": %s\n", dst, strerror (errno)); res = FALSE; - free (b); break; } - free (b); } fclose (fout); |
