# mamgal - a program for creating static image galleries # Copyright 2007-2010 Marcin Owsiany # See the README file for license information # An output formatting class, for creating the actual index files from some # contents package App::MaMGal::Formatter; use strict; use warnings; use base 'App::MaMGal::Base'; use Carp; use Locale::gettext; use URI::file; use HTML::Entities qw(encode_entities_numeric); use App::MaMGal::Logger; sub init { my $self = shift; my $le = shift or croak "Need a locale env arg"; ref $le and $le->isa('App::MaMGal::LocaleEnv') or croak "Arg is not a App::MaMGal::LocaleEnv, but a [$le]"; $self->set_locale_env($le); } sub set_locale_env { my $self = shift; my $le = shift; $self->{locale_env} = $le; } sub HEADER { my $self = shift; my $head = shift || ''; sprintf("%s", $self->{locale_env}->get_charset, $head); } sub MAYBE_LINK { my $self = shift; my $link = shift; my $text = shift; if ($link) { $self->LINK($link.'.html', $text) } else { $text } } sub MAYBE_IMG { my $self = shift; my $img = shift; if ($img) { sprintf("", encode_entities_numeric(URI::file->new($img)->as_string)); } else { # TRANSLATORS: This text will appear literally where no thumbnail is avaialable # for a given object. # Please use   for whitespace, to avoid line breaks. gettext('[no icon]'); } } sub MAYBE_EMBED { my $self = shift; my $film = shift; if ($film) { sprintf("", encode_entities_numeric(URI::file->new($film)->as_string)); } else { # TRANSLATORS: This text will appear literally where no path is # avaialable for a given film. # Please use   for whitespace, to avoid line breaks. gettext('[no film]'); } } sub LINK { my $self = shift; my $link = encode_entities_numeric(URI::file->new(shift)->as_string); my $text = shift; "$text"; } # TRANSLATORS: The following three are for navigation on a slide page (< is shown as <, and > as >) sub PREV { gettext('<< prev') } sub NEXT { gettext('next >>') } sub LINK_DOWN { $_[0]->LINK('../index.html', gettext('Up a dir')) } sub FOOTER { ""; } sub EMPTY_PAGE_TEXT { gettext("This directory is empty") } sub CURDIR { sprintf '%s', $_[1] } sub format { my $self = shift; my $dir = shift; croak "Only one arg is required" if @_; my @elements = $dir->elements; my @containers = $dir->containers; my $down_dots = join('/', map { ".." } @containers); $down_dots .= '/' if $down_dots; my $ret = $self->HEADER('')."\n"; $ret .= ''; $ret .= ''."\n"; $ret .= ($dir->is_root ? '' : '')."\n"; $ret .= "\n\n"; my $i = 1; if (@elements) { foreach my $e (@elements) { confess "[$e] is not an object" unless ref $e; confess "[$e] is a ".ref($e) unless $e->isa('App::MaMGal::Entry'); $ret .= ' '.$self->entry_cell($e)."\n"; $ret .= "\n\n" if $i % 4 == 0; $i++; } } else { $ret .= ''; } $ret .= "\n"; return $ret.$self->FOOTER; } sub entry_cell { my $self = shift; my $entry = shift; my $path = $entry->page_path; my $thumbnail_path = $entry->thumbnail_path; my $ret = ''; $ret .= ''; return $ret; } sub format_slide { my $self = shift; my $pic = shift or croak "No pic"; croak "Only one arg required." if @_; ref $pic and $pic->isa('App::MaMGal::Entry::Picture') or croak "Arg is not a pic"; my ($prev, $next) = map { defined $_ ? $_->name : '' } $pic->neighbours; my @containers = $pic->containers; my $down_dots = join('/', map { ".." } @containers); my $r = $self->HEADER('')."\n"; $r .= '
'; $r .= $self->MAYBE_LINK($prev, $self->PREV); $r .= ' | '; # TRANSLATORS: This is the text of the link from a slide page to the index page. $r .= $self->LINK('../index.html', gettext('index')); $r .= ' | '; $r .= $self->MAYBE_LINK($next, $self->NEXT); $r .= '
'; $r .= '
[ '; $r .= join(' / ', map { $self->CURDIR($_->name) } @containers); $r .= " ]

\n"; $r .= "

\n"; if ($pic->description) { $r .= sprintf('%s', $pic->description); } else { $r .= sprintf('[%s]', $pic->name); } $r .= "

\n"; if ($pic->isa('App::MaMGal::Entry::Picture::Film')) { $r .= $self->MAYBE_EMBED('../'.$pic->name); $r .= '
'; $r .= $self->LINK('../'.$pic->name, gettext('Download')); } else { $r .= $self->LINK('../'.$pic->name, $self->MAYBE_IMG('../'.$pic->medium_dir.'/'.$pic->name)); } my $time = $pic->creation_time(); $r .= sprintf('
%s%s
', $self->{locale_env}->format_date($time), $self->{locale_env}->format_time($time)); $r .= $self->FOOTER; return $r; } sub stylesheet { my $t = <
'; $ret .= join(' / ', map { $self->CURDIR($_->name) } @containers, $dir); $ret .= '
'.$self->LINK_DOWN.'
'.$self->EMPTY_PAGE_TEXT.'
'; my @timeret; foreach my $time ($entry->creation_time()) { push @timeret, sprintf('%s %s', $self->{locale_env}->format_date($time), $self->{locale_env}->format_time($time)); } $ret .= '
'.join(' — ', @timeret).'
'; $ret .= $self->LINK($path, $self->MAYBE_IMG($thumbnail_path)); if ($entry->description) { $ret .= sprintf('
%s', $entry->description); } else { $ret .= sprintf('
[%s]
', $self->LINK($path, $entry->name)); } $ret .= '