--- loncom/homework/default_homework.lcpm 2004/03/16 19:47:47 1.74
+++ loncom/homework/default_homework.lcpm 2004/03/19 22:02:03 1.75
@@ -1,7 +1,7 @@
# The LearningOnline Network with CAPA
# used by lonxml::xmlparse() as input variable $safeinit to Apache::run::run()
#
-# $Id: default_homework.lcpm,v 1.74 2004/03/16 19:47:47 albertel Exp $
+# $Id: default_homework.lcpm,v 1.75 2004/03/19 22:02:03 albertel Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -485,11 +485,51 @@ sub format {
return $result;
}
+sub chemparse {
+ my ($reaction) = @_;
+ my @tokens = split(/(\s\+|\->|<=>)/,$reaction);
+ my $formula = '';
+ foreach my $token (@tokens) {
+ if ($token eq '->' ) {
+ $formula .= '\ensuremath{\rightarrow} ';
+ next;
+ }
+ if ($token eq '<=>') {
+ if ($external::target eq 'web' &&
+ &EXT('request.browser.unicode')) {
+ $formula .= '⇔ ';
+ } else {
+ $formula .= &web('<=> ','\ensuremath{\rightleftharpoons} ',
+ '<=$gt; ');
+ }
+ next;
+ }
+ $token =~ /^\s*(\d*)(.*)/;
+ $formula .= $1 if ($1 > 1); # stoichiometric coefficient
+
+ my $molecule = $2;
+ # subscripts
+ $molecule =~ s|(?<=[a-zA-Z\[\s])(\d+)|$1|g;
+ # superscripts
+ $molecule =~ s|\^(\d*[+\-]*)|$1|g;
+ # strip whitespace
+ $molecule =~ s/\s*//g;
+ # forced space
+ $molecule =~ s/_/ /g;
+ $formula .= $molecule.' ';
+ }
+ # get rid of trailing space
+ $formula =~ s/(\Q${nbsp}\E| )$//;
+
+ return &xmlparse($formula);
+}
+
sub prettyprint {
my ($value,$fmt,$target)=@_;
my $result;
my $dollarmode;
if (!$target) { $target = $external::target; }
+ if ($fmt =~ /chem/i) { return(&chemparse($value)); }
if ($fmt =~ /^\$(.*)/) { $fmt=$1; $dollarmode=1; }
if ($fmt) { $value=sprintf('%.'.$fmt,$value); }
if ($value =~ /([0-9\.\-\+]+)E([0-9\-\+]+)/i ) {