# -*-ruby-*- # # Picture gallery processor/generator # Part of nysagallery # # Copyright (c) 2006, 2007 Stephen Williams, all rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # 3. The name of the copyright holder may not be used to endorse or # promote products derived from this software without specific prior # written permission. # # THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. # IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING # IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # require './gallery_lib' class GalleryPage RCS_ID = "$Id: gallery.rbx,v 1.9 2007/01/03 12:40:14 stephen Exp $" include GalleryLib attr_reader :cgi # Constructor def initialize @cgi = CGI.new @gallery = nil @gallery_name = nil @section_html = "" @section_title = nil @thumbnail_on_left = true end # Generates the gallery document def generate @gallery_name = @cgi['gallery'] die("BAD_REQUEST", "No gallery specified") if @gallery_name.length == 0 File.open("gallery.dump", "r") do |file| galleries = Marshal.restore(file) @gallery = galleries[@gallery_name] die("NOT_FOUND", "No such gallery") unless @gallery end section_name = @cgi['section'] section_name = nil if section_name.length == 0 if !section_name if @gallery.sections.size > 1 generate_section_selection else generate_all_sections end generate_link_back elsif section_name == "ALL" generate_all_sections else generate_section(section_name) generate_navbar(section_name) if @gallery.sections.size > 1 end end # Outputs the HTML def output desc = @gallery.description ? "

#{@gallery.description}

" : "" subtitle = @section_title ? " : " + @section_title : "" @cgi.out do < #{@gallery.title}#{subtitle}

#{@gallery.title}

Created: #{@gallery.created}

#{desc} #{@section_html}

Generated from #{@gallery_name}.xml
#{GalleryLib.rcs_mangle(RCS_ID)}
#{GalleryLib.rcs_mangle(GalleryLib::RCS_ID)}

END end end private def die(status, message) raise GalleryLib::NysaGalleryException.new(message, status) end # Generates HTML for all the sections in the gallery def generate_all_sections i = 0 @gallery.sections.each_key do |name| generate_section(name) @thumbnail_on_left = !@thumbnail_on_left i += 1 @section_html << "
\n" if i < @gallery.sections.size end @section_title = nil end # Inserts a link back to the galleries index def generate_link_back @section_html << "


" << "<< Back to the main index" << "
" end # Generates the navbar that appears under the section def generate_navbar(section_name) navbar = "


" navbar << "" << @gallery.title << "
" index = @gallery.sections.keys.index(section_name) if index > 0 section_prev = @gallery.sections[@gallery.sections.keys[index - 1]] navbar << "<< #{section_prev.title}" end if index < (@gallery.sections.size - 1) section_next = @gallery.sections[@gallery.sections.keys[index + 1]] navbar << "#{section_next.title} >>" end navbar << "
" @section_html << navbar end # Generates HTML for a named section def generate_section(section_name) section = @gallery.sections[section_name] die("NOT_FOUND", "No such gallery section") unless section @section_html << "

#{section.title}

\n" @section_html << "

#{section.description}

\n" if section.description @section_html << "\n" section.pictures.each do |picture| @section_html << "\n" thumbnail_cell = "" text_cell = "" if @thumbnail_on_left @section_html << thumbnail_cell @section_html << text_cell else @section_html << text_cell @section_html << thumbnail_cell end @section_html << "\n" end @section_html << "
" + GalleryLib.format_thumbnail(@gallery, section, picture) + "
#{picture.width} x #{picture.height}
" + GalleryLib.format_picture(@gallery, section, picture) + "
\n" @section_title = section.title end # Generates HTML for a menu, permitting the user to choose # from the gallery sections. # # Section URLs are formatted thusly: # URL_BASE/gallery_name,section_name # so the web server must rewrite them into the CGI format: # URL_BASE/gallery.rbx?gallery=gallery_name§ion=section_name def generate_section_selection @section_html << "

This gallery consists of #{@gallery.sections.size} sections, " << "containing a total of #{@gallery.picture_count} pictures. " << "Choose which section you would like to view, or " << "view the entire gallery " << "on one page.

\n" @gallery.sections.each_key do |section_name| section = @gallery.sections[section_name] @section_html << "\n" url = "#{URL_BASE}#{@gallery_name},#{section_name}" picture = section.random_picture thumb = GalleryLib.format_thumbnail(@gallery, section, picture, "Sample picture from #{section.title}", url) @section_html << "\n" << "\n" end @section_html << "
#{thumb}

" << "#{section.title}
\n" << "#{section.pictures.size} " << "picture#{section.pictures.size > 1 ? 's' : ''}

" @section_html << "

#{section.description}

" if section.description @section_html << "
\n" end end gallerypage = GalleryPage.new begin gallerypage.generate gallerypage.output rescue GalleryLib::NysaGalleryException => ngex gallerypage.cgi.out("status" => ngex.status.to_s, "type" => "text/plain") do ngex.message end rescue => ex gallerypage.cgi.out("status" => "SERVER_ERROR", "type" => "text/plain") do "#{ex.message}\n#{ex.backtrace.join("\n")}\n" end end