[LON-CAPA-cvs] cvs: loncom /xml lontexconvert.pm

foxr foxr at source.lon-capa.org
Tue Feb 28 05:38:14 EST 2012


foxr		Tue Feb 28 10:38:14 2012 EDT

  Modified files:              
    /loncom/xml	lontexconvert.pm 
  Log:
  BZ 5837 - 
    1. Add table of conversion string -> unicode equivalents and
       sub to do a conversion.
    2. Use this to turn the entities generated by tth to unicode bracket
  segment characters directly.
  
  
  
Index: loncom/xml/lontexconvert.pm
diff -u loncom/xml/lontexconvert.pm:1.109 loncom/xml/lontexconvert.pm:1.110
--- loncom/xml/lontexconvert.pm:1.109	Tue Dec  6 16:10:53 2011
+++ loncom/xml/lontexconvert.pm	Tue Feb 28 10:38:14 2012
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # TeX Conversion Module
 #
-# $Id: lontexconvert.pm,v 1.109 2011/12/06 16:10:53 dseaton Exp $
+# $Id: lontexconvert.pm,v 1.110 2012/02/28 10:38:14 foxr Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -50,6 +50,39 @@
 use URI::Escape;
 use IO::Socket::INET;
 
+
+#
+# Table of substitutions to unicode characters.
+#
+my %unicode_translations = (
+    '\rightleftharpoons'  => 0x21cc,
+    ''             => 0x23a1,
+    ''             => 0x23a2,
+    ''             => 0x23a3,
+    ''             => 0x23a4,
+    ''             => 0x23a5,
+    ''             => 0x23a6   
+);
+
+##
+# Utility to convert elements of a string to unicode:
+#
+# @param input - Input string
+# @param pattern - Pattern to convert
+# @param unicode - Unicode to substitute for pattern.
+#
+# @return string - resulting string.
+# 
+sub unicode_subst {
+    my ($input, $pattern, $unicode) = @_;
+    
+    my $char = pack('U', $unicode);
+
+    $input =~ s/$pattern/$char/g;
+
+    return $input;
+}
+
 # ====================================================================== Header
 
 sub init_tth {
@@ -93,12 +126,14 @@
     };
     &Apache::lonxml::start_alarm();
     if ($env{'browser.mathml'}) {
+	&Apache::lonnet::logthis("mathml");
 	$xmlstring=&tth::ttm($$texstring);
 	$xmlstring=~s/\<math\>/\<math xmlns=\"\&mathns\;\"\>/g;
 	$xmlstring=~s/\<br\>/\<br\/\>/g;
 	$xmlstring=~s/\<p\>/\<p\>\<\/p\>/g;
 	$errorstring.=&tth::ttmerror();
     } else {
+	&Apache::lonnet::logthis("tth");
 	$xmlstring=&tth::tth($$texstring);
 	$errorstring.=&tth::ttherror();
 	$xmlstring=~s-</font(\s*)>-</font>-g;
@@ -106,15 +141,20 @@
     $xmlstring=~s/^\s*\<br clear\=\"all\"/\<br/s;
     $xmlstring=~s/^\s*//;
     $xmlstring=~s/\s*$//;
+    &Apache::lonxml::end_alarm();
+
     #
-    # \rightleftharpoons is not converted by tth but maps
-    # reasonably well to ⇔.  If we get many more of these,
-    # we're going to need to have a translation sub.
-    #
-    my $lrharpoon = pack("U", 0x21cc);
-    $xmlstring=~s/\\rightleftharpoons/$lrharpoon/g;
+    #  Several strings produced by tth require
+    # transliteration -> unicode equivalents to render reliably
+    # in browsers. %unicode_translations is a table of
+    # string->substitution which we now apply:
+
+    foreach my $pattern (keys(%unicode_translations)) {
+	my $unicode = $unicode_translations{$pattern};
+	$xmlstring = &unicode_subst($xmlstring, $pattern, $unicode);
+    }
+
 
-    &Apache::lonxml::end_alarm();
     return ($xmlstring,$errorstring);
 }
 




More information about the LON-CAPA-cvs mailing list