]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-12039 Added ability to configure alt text when using TeX filter
authorsam_marshall <sam_marshall>
Tue, 6 Nov 2007 15:49:03 +0000 (15:49 +0000)
committersam_marshall <sam_marshall>
Tue, 6 Nov 2007 15:49:03 +0000 (15:49 +0000)
filter/tex/filter.php

index e8b0709e0bb1193c3303a1875058a6adc59c05ac..08857337eff98c0b28a261a9b889c24a68e49b78 100644 (file)
 
 $CFG->texfilterdir = "filter/tex";
 
-
-function string_file_picture_tex($imagefile, $tex= "", $height="", $width="", $align="middle") {
-    // Given the path to a picture file in a course, or a URL,
+function string_file_picture_tex($imagefile, $tex= "", $height="", $width="", $align="middle", $alt='') {
+    if($alt==='') {
+        $alt=s($tex);
+    }
+// Given the path to a picture file in a course, or a URL,
     // this function includes the picture in the page.
     global $CFG;
 
@@ -53,6 +55,11 @@ function string_file_picture_tex($imagefile, $tex= "", $height="", $width="", $a
         $tex = str_replace('>','&gt;',$tex);
         $tex = str_replace('"','&quot;',$tex);
         $tex = str_replace("\'",'&#39;',$tex);
+        // Note that we retain the title tag as TeX format rather than using
+        // the alt text, even if supplied. The alt text is intended for blind 
+        // users (to provide a text equivalent to the equation) while the title 
+        // is there as a convenience for sighted users who want to see the TeX 
+        // code. 
         $title = "title=\"$tex\"";
     }
     if ($height) {
@@ -72,7 +79,7 @@ function string_file_picture_tex($imagefile, $tex= "", $height="", $width="", $a
           $output .= urlencode($tex) . "', 'popup', 'menubar=0,location=0,scrollbars,";
           $output .= "resizable,width=300,height=240', 0);\">";
         }
-        $output .= "<img class=\"texrender\" $title alt=\"".s($origtex)."\" src=\"";
+        $output .= "<img class=\"texrender\" $title alt=\"$alt\" src=\"";
         if ($CFG->slasharguments) {        // Use this method if possible for better caching
             $output .= "$CFG->wwwroot/$CFG->texfilterdir/pix.php/$imagefile";
         } else {
@@ -119,12 +126,14 @@ function tex_filter ($courseid, $text) {
     }
 
     // <tex> TeX expression </tex>
+    // or <tex alt="My alternative text to be used instead of the TeX form"> TeX expression </tex>
     // or $$ TeX expression $$
     // or \[ TeX expression \]          // original tag of MathType and TeXaide (dlnsk)
     // or [tex] TeX expression [/tex]   // somtime it's more comfortable than <tex> (dlnsk)
-    preg_match_all('/<tex>(.+?)<\/tex>|\$\$(.+?)\$\$|\\\\\[(.+?)\\\\\]|\\[tex\\](.+?)\\[\/tex\\]/is', $text, $matches);
+    preg_match_all('/<tex(?:\s+alt=["\'](.*?)["\'])?>(.+?)<\/tex>|\$\$(.+?)\$\$|\\\\\[(.+?)\\\\\]|\\[tex\\](.+?)\\[\/tex\\]/is', $text, $matches);
     for ($i=0; $i<count($matches[0]); $i++) {
-        $texexp = $matches[1][$i] . $matches[2][$i] . $matches[3][$i] . $matches[4][$i];
+        $texexp = $matches[2][$i] . $matches[3][$i] . $matches[4][$i] . $matches[5][$i];
+        $alt = $matches[1][$i];
         $texexp = str_replace('<nolink>','',$texexp);
         $texexp = str_replace('</nolink>','',$texexp);
         $texexp = str_replace('<span class="nolink">','',$texexp);
@@ -148,7 +157,7 @@ function tex_filter ($courseid, $text) {
             insert_record("cache_filters",$texcache, false);
         }
         $filename = $md5 . ".gif";
-        $text = str_replace( $matches[0][$i], string_file_picture_tex($filename, $texexp, '', '', $align), $text);
+        $text = str_replace( $matches[0][$i], string_file_picture_tex($filename, $texexp, '', '', $align, $alt), $text);
     }
     return $text; 
 }