[LON-CAPA-cvs] cvs: loncom /interface lonhtmlcommon.pm

droeschl droeschl@source.lon-capa.org
Tue, 26 May 2009 20:06:46 -0000


droeschl		Tue May 26 20:06:46 2009 EDT

  Modified files:              
    /loncom/interface	lonhtmlcommon.pm 
  Log:
  Refactoring of sub breadcrumbs (work in progress) and sub generate_menu.
  Added subs htmltag, inittags (see comments). 
  
  
Index: loncom/interface/lonhtmlcommon.pm
diff -u loncom/interface/lonhtmlcommon.pm:1.218 loncom/interface/lonhtmlcommon.pm:1.219
--- loncom/interface/lonhtmlcommon.pm:1.218	Fri May 22 17:57:19 2009
+++ loncom/interface/lonhtmlcommon.pm	Tue May 26 20:06:46 2009
@@ -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 @@
                 $last = $menulink;
             }
         }
-        my $links .= '<li>'. 
-            join('</li><li>'.$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 = '<a href="'.$_->{'href'}.'" ';
-                     if (defined($_->{'target'}) && $_->{'target'} ne '') {
-                         $result .= 'target="'.$_->{'target'}.'" ';
-                     }
-			# set the possible translation for title 
-		     if ($_->{'no_mt'}) {
-			 $result .='title="'.$_->{'title'}.'">'.
-			     $_->{'text'}.'</a>';
-		     } else {
-			 $result .='title="'.&mt($_->{'title'}).'">'.
-			     &mt($_->{'text'}).'</a>';
-		     }
-                     $result;
-                     } @Crumbs
-                 ).'</li>';
+
+                     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 .= '<li>' if ($links ne '');
@@ -2094,6 +2088,53 @@
     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 <br/> are correctly terminated, 
+#   i.e. htmltag('br') returns <br/> 
+# - 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]</$_[0]>| : qq|/>|). "\n";
+};
+
+
+# USAGE: inittags(@tags);
+#
+# EXAMPLES:
+#  - my ($h1, $h2, $h3) = initTags( qw( h1 h2 h3 ) )
+#    $h1->("This is a headline") #Returns: <h1>This is a headline</h1>
+#
+# 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 @@
 # --------------------------
 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]</$_[0]>| : 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