--- loncom/interface/lonhtmlcommon.pm 2009/05/22 17:57:19 1.218 +++ loncom/interface/lonhtmlcommon.pm 2009/05/26 20:06:46 1.219 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # a pile of common html routines # -# $Id: lonhtmlcommon.pm,v 1.218 2009/05/22 17:57:19 bisitz Exp $ +# $Id: lonhtmlcommon.pm,v 1.219 2009/05/26 20:06:46 droeschl Exp $ # # Copyright Michigan State University Board of Trustees # @@ -1398,27 +1398,21 @@ returns: nothing $last = $menulink; } } - my $links .= '
  • '. - join('
  • '.$crumbsymbol, + my $links = join "", map { - $faq = $_->{'faq'} if (exists($_->{'faq'})); - $bug = $_->{'bug'} if (exists($_->{'bug'})); + $faq = $_->{'faq'} if (exists($_->{'faq'})); + $bug = $_->{'bug'} if (exists($_->{'bug'})); $help = $_->{'help'} if (exists($_->{'help'})); - my $result = '{'target'}) && $_->{'target'} ne '') { - $result .= 'target="'.$_->{'target'}.'" '; - } - # set the possible translation for title - if ($_->{'no_mt'}) { - $result .='title="'.$_->{'title'}.'">'. - $_->{'text'}.''; - } else { - $result .='title="'.&mt($_->{'title'}).'">'. - &mt($_->{'text'}).''; - } - $result; - } @Crumbs - ).'
  • '; + + my $result = htmltag( 'a', + $_->{no_mt} ? $_->{text} : mt($_->{text}), + { + href => $_->{href}, + title => $_->{no_mt} ? $_->{title} : mt($_->{title}), + target => $_->{target}, + }); + $result = htmltag( 'li', $crumbsymbol.$result); + } @Crumbs; #Workaround for edit course. if(@Crumbs == 0 ){ $links .= '
  • ' if ($links ne ''); @@ -2094,6 +2088,53 @@ END return $scripttag; } + +# USAGE: htmltag(element, content, {attribute => value,...}); +# +# EXAMPLES: +# - htmltag('a', 'this is an anchor', {href => 'www.example.com', +# title => 'this is a title'}) +# +# - You might want to set up needed tags like: +# +# my $h3 = sub { return htmltag( "h3", @_ ) }; +# +# ... and use them: $h3->("This is a headline") +# +# - To set up a couple of tags, see sub inittags +# +# NOTES: +# - Empty elements, such as
    are correctly terminated, +# i.e. htmltag('br') returns
    +# - Empty attributes (title="") are filtered out. +# - The function will not check for deprecated attributes. +# +# OUTPUT: content enclosed in xhtml conform tags +sub htmltag{ + return + qq|<$_[0]| + . join( '', map { qq| $_="${$_[2]}{$_}"| if ${$_[2]}{$_} } keys %{ $_[2] } ) + . ($_[1] ? qq|>$_[1]| : qq|/>|). "\n"; +}; + + +# USAGE: inittags(@tags); +# +# EXAMPLES: +# - my ($h1, $h2, $h3) = initTags( qw( h1 h2 h3 ) ) +# $h1->("This is a headline") #Returns:

    This is a headline

    +# +# NOTES: See sub htmltag for further information. +# +# OUTPUT: List of subroutines. +sub inittags { + my @tags = @_; + return map { my $tag = $_; + sub { return htmltag( $tag, @_ ) } + } @tags; +} + + ############################################## ############################################## @@ -2131,23 +2172,8 @@ END # -------------------------- sub generate_menu { my @menu = @_; - - # usage: $wrap->(element, content, {attribute => value,...}); - # output: content enclosed in html conform tags - my $wrap = sub { - return - qq|<$_[0]| - . join( '', map { qq| $_="${$_[2]}{$_}"| } keys %{ $_[2] } ) - . ($_[1] ? qq|>$_[1]| : qq|/>|). "\n"; - }; - # subs for specific html elements - my $h3 = sub { return $wrap->( "h3", @_ ) }; - my $div = sub { return $wrap->( "div", @_ ) }; - my $ul = sub { return $wrap->( "ul", @_ ) }; - my $li = sub { return $wrap->( "li", @_ ) }; - my $a = sub { return $wrap->( "a", @_ ) }; - my $img = sub { return $wrap->( "img", @_ ) }; + my ($h3, $div, $ul, $li, $a, $img) = inittags( qw(h3 div ul li a img) ); my @categories; # each element represents the entire markup for a category