Annotation of loncom/homework/loncapamath.pm, revision 1.1
1.1 ! damieng 1: # The LearningOnline Network with CAPA
! 2: # <lm>: math with the LON-CAPA syntax
! 3: #
! 4: # $Id: loncapamath.pm$
! 5: #
! 6: # Copyright Michigan State University Board of Trustees
! 7: #
! 8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
! 9: #
! 10: # LON-CAPA is free software; you can redistribute it and/or modify
! 11: # it under the terms of the GNU General Public License as published by
! 12: # the Free Software Foundation; either version 2 of the License, or
! 13: # (at your option) any later version.
! 14: #
! 15: # LON-CAPA is distributed in the hope that it will be useful,
! 16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
! 17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! 18: # GNU General Public License for more details.
! 19: #
! 20: # You should have received a copy of the GNU General Public License
! 21: # along with LON-CAPA; if not, write to the Free Software
! 22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
! 23: #
! 24: # /home/httpd/html/adm/gpl.txt
! 25: #
! 26: # http://www.lon-capa.org/
! 27: #
! 28:
! 29:
! 30: package Apache::loncapamath;
! 31:
! 32: use strict;
! 33: use warnings;
! 34: use utf8;
! 35:
! 36: use Try::Tiny;
! 37:
! 38: use aliased 'Apache::math_parser::CalcException';
! 39: use aliased 'Apache::math_parser::ParseException';
! 40: use aliased 'Apache::math_parser::Parser';
! 41: use aliased 'Apache::math_parser::ENode';
! 42: use aliased 'Apache::math_parser::CalcEnv';
! 43:
! 44:
! 45: BEGIN {
! 46: &Apache::lonxml::register('Apache::loncapamath',('lm'));
! 47: }
! 48:
! 49: sub start_lm {
! 50: my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
! 51: # Implementation note: this is using ENode::toTex, but we could also
! 52: # use a client-side implementation to display <lm>.
! 53: # While we use LaTeX for printing, turning the math into TeX for the web
! 54: # ensures that results will be more similar between web and print targets
! 55: # than they would be with a client-side implementation turning the
! 56: # parsed math directly into MathML.
! 57: $Apache::lonxml::post_evaluate = 0;
! 58: if ($target ne 'web' && $target ne 'tex') {
! 59: return;
! 60: }
! 61: my $text = &Apache::lonxml::get_all_text_unbalanced("/lm", $parser);
! 62: my $mode = &Apache::lonxml::get_param('mode',$parstack,$safeeval);
! 63: $text = &Apache::run::evaluate($text, $safeeval, $$parstack[-1]);
! 64: my $tex;
! 65: my $implicit_operators = 1;
! 66: my $unit_mode;
! 67: if (defined $mode && $mode eq 'units') {
! 68: $unit_mode = 1;
! 69: } else {
! 70: $unit_mode = 0;
! 71: }
! 72: my ($p, $root, $env);
! 73: my $result;
! 74: try {
! 75: $p = Parser->new($implicit_operators, $unit_mode);
! 76: $root = $p->parse($text);
! 77: $env = CalcEnv->new($unit_mode);
! 78: $tex = $root->toTeX();
! 79: } catch {
! 80: # NOTE: return cannot be used within a try/catch with Try::Tiny
! 81: if (UNIVERSAL::isa($_,CalcException)) {
! 82: $result = "Calculation error: ".$_->getLocalizedMessage();
! 83: } elsif (UNIVERSAL::isa($_,ParseException)) {
! 84: $result = "Parsing error: ".$_->getLocalizedMessage();
! 85: } else {
! 86: $result = "Internal error: $_";
! 87: }
! 88: };
! 89: if (defined $result) {
! 90: if ($target eq 'web') {
! 91: return '<b>'.$result.'</b>';
! 92: } elsif ($target eq 'tex') {
! 93: return '\bf{'.$result.'}';
! 94: }
! 95: }
! 96: if ($target eq 'web') {
! 97: my $display = &Apache::lonxml::get_param('display', $parstack, $safeeval);
! 98: $tex = '$'.$tex.'$';
! 99: $result = &Apache::lontexconvert::converted(\$tex, $display);
! 100: } elsif ($target eq 'tex') {
! 101: $result = '\ensuremath{'.$tex.'}';
! 102: }
! 103:
! 104: return $result;
! 105: }
! 106:
! 107: sub end_lm {
! 108: my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
! 109: return '';
! 110: }
! 111:
! 112:
! 113: 1;
! 114: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>