diff options
| author | Tomas Bzatek <tbzatek@users.sourceforge.net> | 2014-09-17 23:46:41 +0200 |
|---|---|---|
| committer | Tomas Bzatek <tbzatek@users.sourceforge.net> | 2014-09-17 23:46:41 +0200 |
| commit | 8846646f54a3740d272387511b91c3d3e5ae3283 (patch) | |
| tree | 7af82c3257f713d7e048a9c920ea778d1fc032d5 | |
| parent | d8708c220afc84549e5e4feaa09f32aa46aba4b5 (diff) | |
| download | cataract-8846646f54a3740d272387511b91c3d3e5ae3283.tar.xz | |
theming: Make navigation bar strongly themed
Navigation bar can be complex and comprises of several different elements.
Until now the navigation bar was almost hardcoded. We needed to separate
different types of navigation bar elements:
* NAV_BAR_FIRST
- the first element, usually denoting gallery root. This element takes
priority and is used when there are no other elements in the path.
* NAV_BAR_ELEM
- normal element used in the path between first and last elements.
* NAV_BAR_LAST
- ending element, guaranteed there's at least the NAV_BAR_FIRST element
before.
* NAV_BAR_CURRENT
- non-clickable informational element displaying the current page title.
Optional, in contrast to the former three defines.
* NAV_BAR_CURRENT_ROOT
- a special case of the latter one, displayed at the very root page.
Also optional, in that case the navigation bar would become empty.
| -rw-r--r-- | src/generators.c | 250 | ||||
| -rw-r--r-- | templates/template_album.html | 20 | ||||
| -rw-r--r-- | templates/template_index.html | 20 | ||||
| -rw-r--r-- | templates/template_picture.html | 20 |
4 files changed, 226 insertions, 84 deletions
diff --git a/src/generators.c b/src/generators.c index 3d04ad6..44aa46c 100644 --- a/src/generators.c +++ b/src/generators.c @@ -275,7 +275,7 @@ generate_image (TGallerySetup *setup, } -static gchar * +static const gchar * get_index_filename (TAlbum *items, TGalleryDesignTheme *theme, TPathInfo *path_info, TIndexItem *retrieve_child) { gchar *s; @@ -291,6 +291,127 @@ get_index_filename (TAlbum *items, TGalleryDesignTheme *theme, TPathInfo *path_i return (gallery_type == GALLERY_TYPE_ALBUM || theme->index_filename == NULL) ? theme->album_filename : theme->index_filename; } +/* Calculates number of directory levels one step up (in case of complex paths) */ +static int +get_parent_dir_level (TAlbum *items) +{ + TAlbum *parent; + TIndexItem *tmp_item; + int level = 0; + + parent = items->parent_index; + if (parent) { + level = 1; + tmp_item = g_ptr_array_index (parent->items, items->parent_item_index); + if (tmp_item && tmp_item->path && strlen (tmp_item->path) > 0) + level += count_dir_levels (tmp_item->path) - 1; + } + return level; +} + +/* Calculates number of directory levels to the gallery root */ +static int +get_root_level (TAlbum *items) +{ + TAlbum *parent = items; + int level = 0; + + do { + level += get_parent_dir_level (parent); + } while ((parent = parent->parent_index)); + + return level; +} + +static gchar * +process_block (TGallerySetup *setup, + ReplaceTable *local_replace_table, + GHashTable *local_defines, + const gchar *str) +{ + BlockParser *local_block_parser; + gchar *s; + + replace_table_set_strip_unused_tags (local_replace_table, setup->strip_unused_tags); + replace_table_set_defines (local_replace_table, local_defines); + local_block_parser = block_parser_new (); + block_parser_set_conditionals (local_block_parser, local_defines); + s = block_parser_process (local_block_parser, str); + block_parser_free (local_block_parser); + replace_table_process (&s, local_replace_table); + + return s; +} + + +static gchar * +make_navbar_string (TGallerySetup *setup, + TGalleryDesignTheme *theme, + BlockParser *block_parser, + GHashTable *defines, + TAlbum *items, + int item_index, + const gchar *current_title) +{ + gchar *s1, *s2, *s3; + int level, old_parent_item_index; + TAlbum *parent; + gboolean picture_element = (item_index >= 0); + gboolean first = TRUE; + ReplaceTable *local_replace_table; + GHashTable *local_defines; + GString *block; + + block = g_string_new (""); + + /* the "current" element */ + s1 = block_parser_get_data (block_parser, items->parent_index ? "NAV_BAR_CURRENT" : "NAV_BAR_CURRENT_ROOT"); + if (s1) { + local_replace_table = replace_table_new (); + local_defines = clone_string_hash_table (defines); + replace_table_add_key (local_replace_table, "NAV_BAR_ELEM_TITLE", current_title); + s2 = process_block (setup, local_replace_table, local_defines, s1); + g_string_prepend (block, s2); + g_free (s2); + g_free (s1); + replace_table_free (local_replace_table); + g_hash_table_destroy (local_defines); + } + + parent = items; + old_parent_item_index = items->parent_item_index + 1; + level = picture_element ? 0 : get_parent_dir_level (items); + + while ((parent = picture_element ? items : parent->parent_index)) { + /* NAV_BAR_FIRST takes priority and is guaranteed to be used */ + s1 = block_parser_get_data (block_parser, parent->parent_index ? (first ? "NAV_BAR_LAST" : "NAV_BAR_ELEM") : "NAV_BAR_FIRST"); + local_replace_table = replace_table_new (); + local_defines = clone_string_hash_table (defines); + + s2 = make_string ("../", level); + s3 = setup->use_inpage_links ? g_strdup_printf ("#i%d", picture_element ? item_index : old_parent_item_index) : g_strdup (""); + replace_table_add_key_printf (local_replace_table, "NAV_BAR_ELEM_LINK", "%s%s%s", s2, get_index_filename (parent, theme, NULL, NULL), s3); + replace_table_add_key (local_replace_table, "NAV_BAR_ELEM_TITLE", parent->ID); + g_free (s2); + g_free (s3); + + s2 = process_block (setup, local_replace_table, local_defines, s1); + g_string_prepend (block, s2); + g_free (s2); + g_free (s1); + replace_table_free (local_replace_table); + g_hash_table_destroy (local_defines); + + old_parent_item_index = parent->parent_item_index + 1; + level += get_parent_dir_level (parent); + picture_element = FALSE; + first = FALSE; + } + + return g_string_free (block, FALSE); +} + + /* * write_html_album: process album and index template files * @@ -313,11 +434,11 @@ write_html_album (TGallerySetup *setup, gchar *line; GString *block; gchar *templates_path; - gchar *s1, *s2, *s3, *s4, *s5; + gchar *s1, *s2, *s3; TAlbum *parent; TIndexItem *item; TIndexItem *tmp_item; - int level, old_parent_item_index; + int level; gboolean res; int bb; int i; @@ -327,7 +448,7 @@ write_html_album (TGallerySetup *setup, gboolean album_protected; ReplaceTable *global_replace_table; ReplaceTable *local_replace_table; - BlockParser *block_parser, *local_block_parser; + BlockParser *block_parser; TImageSize *image_size, *thumb_image_size; GHashTable *defines, *local_defines; @@ -377,20 +498,12 @@ write_html_album (TGallerySetup *setup, replace_table_add_key (global_replace_table, "FOOTNOTE", items->footnote); replace_table_add_key (global_replace_table, "FOOTER", setup->footer); - /* Navigation bar (NOTE: 'int level' is used below + favicon) */ - s1 = g_strdup (items->ID); - old_parent_item_index = items->parent_item_index + 1; - parent = items->parent_index; - level = 1; - if (parent) { - tmp_item = g_ptr_array_index (parent->items, old_parent_item_index - 1); - if (tmp_item && tmp_item->path && strlen (tmp_item->path) > 0) - level += count_dir_levels (tmp_item->path) - 1; - } /* Go Up string */ + parent = items->parent_index; + level = get_parent_dir_level (items); if (parent) { s3 = make_string ("../", level); - s2 = setup->use_inpage_links ? g_strdup_printf ("#i%d", old_parent_item_index) : g_strdup (""); + s2 = setup->use_inpage_links ? g_strdup_printf ("#i%d", items->parent_item_index + 1) : g_strdup (""); replace_table_add_key_printf (global_replace_table, "GO_UP_LINK", "%s%s%s", s3, get_index_filename (parent, theme, NULL, NULL), s2); g_free (s2); g_free (s3); @@ -398,30 +511,9 @@ write_html_album (TGallerySetup *setup, if (! parent) g_hash_table_replace (defines, g_strdup ("IS_ROOT"), g_strdup ("")); - while (parent) { - s3 = make_string ("../", level); - s4 = g_strdup (parent->ID); - s5 = setup->use_inpage_links ? g_strdup_printf ("#i%d", old_parent_item_index) : g_strdup (""); - s2 = g_strdup_printf ("<a href=\"%s%s%s\"%s>%s</a> > %s", s3, get_index_filename (parent, theme, NULL, NULL), s5, (parent != NULL && parent->parent_index == NULL) ? " rel=\"home\"" : "", s4, s1); - g_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++; - if (parent) { - tmp_item = g_ptr_array_index (parent->items, old_parent_item_index - 1); - if (tmp_item && tmp_item->path && strlen (tmp_item->path) > 0) - level += count_dir_levels (tmp_item->path) - 1; - } - } - replace_table_add_key (global_replace_table, "NAV_BAR", s1); - g_free (s1); - /* Supportfiles path */ - templates_path = setup->supplemental_files_use_common_root ? make_string ("../", level - 1) : g_strdup (""); + level = get_root_level (items); + templates_path = setup->supplemental_files_use_common_root ? make_string ("../", level) : g_strdup (""); replace_table_add_key (global_replace_table, "TEMPLATES_PATH", templates_path); /* META tags */ @@ -451,7 +543,7 @@ write_html_album (TGallerySetup *setup, s1 = s2; } if (setup->favicon_file && strlen (setup->favicon_file) > 0) { - s3 = make_string ("../", level - 1); + s3 = make_string ("../", level); if (setup->favicon_type) s2 = g_strdup_printf ("%s\t<link rel=\"icon\" type=\"%s\" href=\"%s%s\" />\n", s1, setup->favicon_type, setup->supplemental_files_use_common_root ? s3 : "", setup->favicon_file); else @@ -465,7 +557,7 @@ write_html_album (TGallerySetup *setup, /* Atom feeds */ if (setup->feed_enabled) { - s3 = make_string ("../", level - 1); + s3 = make_string ("../", level); s1 = g_strdup_printf ("\t<link href=\"%s%s\" type=\"application/atom+xml\" rel=\"alternate\" title=\"%s\" />\n", s3, setup->feed_filename, setup->feed_title); g_free (s3); replace_table_add_key (global_replace_table, "CGG_ATOM_FEED_TAGS", s1); @@ -485,6 +577,12 @@ write_html_album (TGallerySetup *setup, block_parser_register_key (block_parser, "IMG_LIST_SQUARED", NULL); block_parser_register_key (block_parser, "LIST_SEPARATOR", NULL); block_parser_register_key (block_parser, "LIST_INTERSPACE", NULL); + block_parser_register_key (block_parser, "NAV_BAR", "NAV_BAR"); + block_parser_register_key (block_parser, "NAV_BAR_FIRST", NULL); + block_parser_register_key (block_parser, "NAV_BAR_ELEM", NULL); + block_parser_register_key (block_parser, "NAV_BAR_LAST", NULL); + block_parser_register_key (block_parser, "NAV_BAR_CURRENT", NULL); + block_parser_register_key (block_parser, "NAV_BAR_CURRENT_ROOT", NULL); /* Read through the template and replace placeholders with real data */ @@ -504,9 +602,7 @@ write_html_album (TGallerySetup *setup, } local_replace_table = replace_table_new (); - replace_table_set_strip_unused_tags (local_replace_table, setup->strip_unused_tags); local_defines = clone_string_hash_table (defines); - replace_table_set_defines (local_replace_table, local_defines); s1 = NULL; switch (item->type) { @@ -576,12 +672,7 @@ write_html_album (TGallerySetup *setup, } if (s1) { - local_block_parser = block_parser_new (); - block_parser_set_conditionals (local_block_parser, local_defines); - s2 = block_parser_process (local_block_parser, s1); - block_parser_free (local_block_parser); - - replace_table_process (&s2, local_replace_table); + s2 = process_block (setup, local_replace_table, local_defines, s1); g_string_append (block, s2); g_free (s2); g_free (s1); @@ -597,6 +688,14 @@ write_html_album (TGallerySetup *setup, block_parser_set_as_used (block_parser, "IMG_LIST"); } + if (block_parser_has_unused_data (block_parser, "NAV_BAR")) { + s1 = make_navbar_string (setup, theme, block_parser, defines, items, -1, items->ID); + replace_table_process (&s1, global_replace_table); + replace_table_add_key (global_replace_table, "NAV_BAR", s1); + g_free (s1); + block_parser_set_as_used (block_parser, "NAV_BAR"); + } + /* Replace all known tags */ replace_table_process (&line, global_replace_table); @@ -690,15 +789,14 @@ write_html_image (TGallerySetup *setup, TIndexItem *previous_item; TIndexItem *next_item; TIndexItem *tmp_item; - TAlbum *parent; int i; gchar *line; - gchar *s1, *s2, *s3, *s4, *s5; + gchar *s1, *s2, *s3, *s4; gchar *imgname, *preload_imgname; gchar *title, *title_desc; gboolean res; int bb; - int level, old_parent_item_index; + int level; gboolean override_title_meta; gboolean image_fullsize; gboolean theme_size_is_original; @@ -890,35 +988,9 @@ write_html_image (TGallerySetup *setup, replace_table_add_key (replace_table, "IMG_SRC_FULL", img_orig_dst_page); } - /* Navigation bar (NOTE: 'int level' is used below + favicon) */ - s1 = g_strdup (imgname); - parent = parent_items; - old_parent_item_index = -1; - level = 0; - while (parent) { - s3 = make_string ("../", level); - s4 = g_strdup (parent->ID); - 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 ("<a href=\"%s%s%s\"%s>%s</a> > %s", s3, get_index_filename (parent, theme, NULL, NULL), s5, (parent != NULL && parent->parent_index == NULL) ? " rel=\"home\"" : "", s4, s1); - g_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++; - if (parent) { - tmp_item = g_ptr_array_index (parent->items, old_parent_item_index - 1); - if (tmp_item && tmp_item->path && strlen (tmp_item->path) > 0) - level += count_dir_levels (tmp_item->path) - 1; - } - } - replace_table_add_key (replace_table, "NAV_BAR", s1); - g_free (s1); - /* Supportfiles path */ - s1 = make_string ("../", level - 1); + level = get_root_level (parent_items); + s1 = make_string ("../", level); replace_table_add_key (replace_table, "TEMPLATES_PATH", setup->supplemental_files_use_common_root ? s1 : ""); g_free (s1); @@ -975,7 +1047,7 @@ write_html_image (TGallerySetup *setup, s1 = s2; } if (setup->favicon_file && strlen (setup->favicon_file) > 0) { - s3 = make_string ("../", level - 1); + s3 = make_string ("../", level); if (setup->favicon_type) s2 = g_strdup_printf ("%s\t<link rel=\"icon\" type=\"%s\" href=\"%s%s\" />\n", s1, setup->favicon_type, setup->supplemental_files_use_common_root ? s3 : "", setup->favicon_file); else @@ -989,19 +1061,35 @@ write_html_image (TGallerySetup *setup, /* Atom feeds */ if (setup->feed_enabled) { - s3 = make_string ("../", level - 1); + s3 = make_string ("../", level); s1 = g_strdup_printf ("\t<link href=\"%s%s\" type=\"application/atom+xml\" rel=\"alternate\" title=\"%s\" />\n", s3, setup->feed_filename, setup->feed_title); g_free (s3); replace_table_add_key (replace_table, "CGG_ATOM_FEED_TAGS", s1); g_free (s1); } + /* Setup block parser */ + block_parser_register_key (block_parser, "NAV_BAR", "NAV_BAR"); + block_parser_register_key (block_parser, "NAV_BAR_FIRST", NULL); + block_parser_register_key (block_parser, "NAV_BAR_ELEM", NULL); + block_parser_register_key (block_parser, "NAV_BAR_LAST", NULL); + block_parser_register_key (block_parser, "NAV_BAR_CURRENT", NULL); + block_parser_register_key (block_parser, "NAV_BAR_CURRENT_ROOT", NULL); + /* Read through the template and replace placeholders with real data */ buffer = g_malloc0 (BUFFER_SIZE); while (fgets (buffer, BUFFER_SIZE, fin)) { line = block_parser_process (block_parser, buffer); + if (block_parser_has_unused_data (block_parser, "NAV_BAR")) { + s1 = make_navbar_string (setup, theme, block_parser, defines, parent_items, item_index, imgname); + replace_table_process (&s1, replace_table); + replace_table_add_key (replace_table, "NAV_BAR", s1); + g_free (s1); + block_parser_set_as_used (block_parser, "NAV_BAR"); + } + /* Replace all known tags */ replace_table_process (&line, replace_table); diff --git a/templates/template_album.html b/templates/template_album.html index 3d0af4f..5413c5e 100644 --- a/templates/template_album.html +++ b/templates/template_album.html @@ -14,7 +14,25 @@ <!-- ## Navigation bar --> <div class="navigation"> - <div class="navlinks"><!-- $(NAV_BAR) --></div> + <div class="navlinks"> + <!-- $(BEGIN_NAV_BAR) --> + <!-- $(BEGIN_NAV_BAR_FIRST) --> + <a href="$(NAV_BAR_ELEM_LINK)" rel="home"><!-- $(NAV_BAR_ELEM_TITLE) --></a> > + <!-- $(END_NAV_BAR_FIRST) --> + <!-- $(BEGIN_NAV_BAR_ELEM) --> + <a href="$(NAV_BAR_ELEM_LINK)"><!-- $(NAV_BAR_ELEM_TITLE) --></a> > + <!-- $(END_NAV_BAR_ELEM) --> + <!-- $(BEGIN_NAV_BAR_LAST) --> + <a href="$(NAV_BAR_ELEM_LINK)"><!-- $(NAV_BAR_ELEM_TITLE) --></a> > + <!-- $(END_NAV_BAR_LAST) --> + <!-- $(BEGIN_NAV_BAR_CURRENT) --> + <!-- $(NAV_BAR_ELEM_TITLE) --> + <!-- $(END_NAV_BAR_CURRENT) --> + <!-- $(BEGIN_NAV_BAR_CURRENT_ROOT) --> + <!-- $(NAV_BAR_ELEM_TITLE) --> + <!-- $(END_NAV_BAR_CURRENT_ROOT) --> + <!-- $(END_NAV_BAR) --> + </div> <div class="navposition"> <span class="navposnumber"><!-- $(TOTAL_ITEMS) --> items</span> </div> diff --git a/templates/template_index.html b/templates/template_index.html index 7229933..9f90bfe 100644 --- a/templates/template_index.html +++ b/templates/template_index.html @@ -14,7 +14,25 @@ <!-- ## Navigation bar --> <div class="navigation"> - <div class="navlinks"><!-- $(NAV_BAR) --></div> + <div class="navlinks"> + <!-- $(BEGIN_NAV_BAR) --> + <!-- $(BEGIN_NAV_BAR_FIRST) --> + <a href="$(NAV_BAR_ELEM_LINK)" rel="home"><!-- $(NAV_BAR_ELEM_TITLE) --></a> > + <!-- $(END_NAV_BAR_FIRST) --> + <!-- $(BEGIN_NAV_BAR_ELEM) --> + <a href="$(NAV_BAR_ELEM_LINK)"><!-- $(NAV_BAR_ELEM_TITLE) --></a> > + <!-- $(END_NAV_BAR_ELEM) --> + <!-- $(BEGIN_NAV_BAR_LAST) --> + <a href="$(NAV_BAR_ELEM_LINK)"><!-- $(NAV_BAR_ELEM_TITLE) --></a> > + <!-- $(END_NAV_BAR_LAST) --> + <!-- $(BEGIN_NAV_BAR_CURRENT) --> + <!-- $(NAV_BAR_ELEM_TITLE) --> + <!-- $(END_NAV_BAR_CURRENT) --> + <!-- $(BEGIN_NAV_BAR_CURRENT_ROOT) --> + <!-- $(NAV_BAR_ELEM_TITLE) --> + <!-- $(END_NAV_BAR_CURRENT_ROOT) --> + <!-- $(END_NAV_BAR) --> + </div> <div class="navposition"> <span class="navposnumber"><!-- $(TOTAL_ITEMS) --> albums</span> </div> diff --git a/templates/template_picture.html b/templates/template_picture.html index 1e500a7..6bd5291 100644 --- a/templates/template_picture.html +++ b/templates/template_picture.html @@ -14,7 +14,25 @@ <!-- ## Navigation bar --> <div class="navigation"> - <div class="navlinks"><!-- $(NAV_BAR) --></div> + <div class="navlinks"> + <!-- $(BEGIN_NAV_BAR) --> + <!-- $(BEGIN_NAV_BAR_FIRST) --> + <a href="$(NAV_BAR_ELEM_LINK)" rel="home"><!-- $(NAV_BAR_ELEM_TITLE) --></a> > + <!-- $(END_NAV_BAR_FIRST) --> + <!-- $(BEGIN_NAV_BAR_ELEM) --> + <a href="$(NAV_BAR_ELEM_LINK)"><!-- $(NAV_BAR_ELEM_TITLE) --></a> > + <!-- $(END_NAV_BAR_ELEM) --> + <!-- $(BEGIN_NAV_BAR_LAST) --> + <a href="$(NAV_BAR_ELEM_LINK)"><!-- $(NAV_BAR_ELEM_TITLE) --></a> > + <!-- $(END_NAV_BAR_LAST) --> + <!-- $(BEGIN_NAV_BAR_CURRENT) --> + <!-- $(NAV_BAR_ELEM_TITLE) --> + <!-- $(END_NAV_BAR_CURRENT) --> + <!-- $(BEGIN_NAV_BAR_CURRENT_ROOT) --> + <!-- $(NAV_BAR_ELEM_TITLE) --> + <!-- $(END_NAV_BAR_CURRENT_ROOT) --> + <!-- $(END_NAV_BAR) --> + </div> <div class="navposition"> <span class="navposnumber"><!-- $(FILE_NO) --></span> of <span class="navposnumber"><!-- $(TOTAL_ITEMS) --></span> <span class="navposspacer"></span> |
