--- loncom/interface/lonprintout.pm 2006/10/16 10:32:46 1.486 +++ loncom/interface/lonprintout.pm 2006/10/24 10:37:58 1.490 @@ -2,7 +2,7 @@ # The LearningOnline Network # Printout # -# $Id: lonprintout.pm,v 1.486 2006/10/16 10:32:46 foxr Exp $ +# $Id: lonprintout.pm,v 1.490 2006/10/24 10:37:58 foxr Exp $ # # Copyright Michigan State University Board of Trustees # @@ -61,26 +61,38 @@ my $resources_printed; # sub printf_style_subst { my ($item, $format_string, $repl) = @_; - - while ($format_string =~ /%\d*$item/) { - my $start = $-[0]; - my $end = $+[0]; - my $len = $end - $start; - - # see if we need to truncate: - + my $result = ""; + while ($format_string =~ /(%)(\d*)\Q$item\E/g ) { + my $fmt = $1; + my $size = $2; my $subst = $repl; - my $fmt = substr($format_string, $start, $len); - my $size = $fmt; - $size =~ s/%(\d*)$item/$1/; if ($size ne "") { $subst = substr($subst, 0, $size); - } - $format_string =~ s/%(\d*)$item/$subst/; + + # Here's a nice edge case.. supose the end of the + # substring is a \. In that case may have just + # chopped off a TeX escape... in that case, we append + # " " for the trailing character, and let the field + # spill over a bit (sigh). + # We don't just chop off the last character in order to deal + # with one last pathology, and that would be if substr had + # trimmed us to e.g. \\\ + + if ($subst =~ /\\$/) { + $subst .= " "; + } + } + my $item_pos = pos($format_string); + $result .= substr($format_string, 0, $item_pos - length($size) -2) . $subst; + $format_string = substr($format_string, pos($format_string)); } - return $format_string; + # Put the residual format string into the result: + + $result .= $format_string; + + return $result; } @@ -135,6 +147,12 @@ sub format_page_header { $format = &printf_style_subst("a", $format, $assignment); $format = &printf_style_subst("c", $format, $course); $format = &printf_style_subst("n", $format, $student); + + # If the user put %'s in the format string, they must be escaped + # to \% else LaTeX will think they are comments and terminate + # the line.. which is bad!!! + + } @@ -158,7 +176,7 @@ sub num_to_letters { sub letters_to_num { my ($letters) = @_; my @letters = split('', uc($letters)); - my %substitution; + my %substitution; my $digit = 0; foreach my $letter ('A'..'J') { $substitution{$letter} = $digit;