? format_old.php Index: format.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/format/gift/format.php,v retrieving revision 1.41 diff -u -r1.41 format.php --- format.php 23 Jun 2011 09:40:08 -0000 1.41 +++ format.php 24 Aug 2011 20:15:20 -0000 @@ -56,7 +56,12 @@ * members of the Moodle community. It was originally based on * the missingword format, which included code from Thomas Robb * and others. Paul Tsuchido Shew wrote this filter in December 2003. - * + * + * Modified by Gian Paolo Leonardi in July 2011 to avoid escaping of + * control characters in TeX/LaTeX regions delimited by pairs of double dollars: + * with this new gift format, the insertion of TeX/LaTeX commands in the + * question text is done without escaping of control characters. + * * @copyright 2003 Paul Tsuchido Shew * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -112,19 +117,42 @@ protected function escapedchar_pre($string) { //Replaces escaped control characters with a placeholder BEFORE processing + //Modified (July 2011) by Gian Paolo Leonardi to avoid escaping within TeX/LaTeX regions - $escapedcharacters = array("\\:", "\\#", "\\=", "\\{", "\\}", "\\~", "\\n" ); //dlnsk - $placeholders = array("&&058;", "&&035;", "&&061;", "&&123;", "&&125;", "&&126;", "&&010"); //dlnsk + $escapedcharacters = array("\\:", "\\#", "\\=", "\\{", "\\}", "\\~", "\\n" ); //dlnsk - $string = str_replace("\\\\", "&&092;", $string); - $string = str_replace($escapedcharacters, $placeholders, $string); - $string = str_replace("&&092;", "\\", $string); - return $string; + $placeholders = array("&&058;", "&&035;", "&&061;", "&&123;", "&&125;", "&&126;", "&&010"); //dlnsk + + $texescapedcharacters = array(":", "#", "=", "{", "}", "~"); //dlnsk + + $texplaceholders = array("&&058;", "&&035;", "&&061;", "&&123;", "&&125;", "&&126;"); //dlnsk + + $chunks = explode("$$", $string); + reset($chunks); + $counter = 1; + $stringa = ""; + while ($achunk = each($chunks)) { + $chunk = $achunk[1]; + if ($counter % 2) { + $chunk = str_replace("\\\\", "&&092;", $chunk); + $chunk = str_replace($escapedcharacters, $placeholders, $chunk); + $chunk = str_replace("&&092;", "\\", $chunk); + } + else { + $chunk = str_replace($texescapedcharacters, $texplaceholders, $chunk); + } + $stringa .= $chunk; + $stringa .= "$$"; + $counter += 1; + } + $stringa = rtrim($stringa, "$"); + return $stringa; } protected function escapedchar_post($string) { //Replaces placeholders with corresponding character AFTER processing is done - $placeholders = array("&&058;", "&&035;", "&&061;", "&&123;", "&&125;", "&&126;", "&&010"); //dlnsk + //Modified (July 2011) by Gian Paolo Leonardi to avoid escaping within TeX/LaTeX regions + $placeholders = array("&&058;", "&&035;", "&&061;", "&&123;", "&&125;", "&&126;", "&&010"); //dlnsk $characters = array(":", "#", "=", "{", "}", "~", "\n" ); //dlnsk $string = str_replace($placeholders, $characters, $string); return $string; @@ -552,11 +580,28 @@ protected function repchar($text, $notused = 0) { // Escapes 'reserved' characters # = ~ {) : // Removes new lines + // Modified (July 2011) by Gian Paolo Leonardi to avoid escaping within TeX/LaTeX regions + $reserved = array( '#', '=', '~', '{', '}', ':', "\n", "\r"); $escaped = array('\#','\=','\~','\{','\}','\:', '\n', '' ); - $newtext = str_replace($reserved, $escaped, $text); - return $newtext; + $chunks = explode("$$", $text); + reset($chunks); + $counter = 1; + $newtext = ""; + + while ($achunk = each($chunks)) { + $chunk = $achunk[1]; + if ($counter % 2) { + $chunk = str_replace($reserved, $escaped, $chunk); + } + $newtext .= $chunk; + $newtext .= "$$"; + $counter += 1; + } + $newtext = rtrim($newtext, "$"); + + return $newtext; } /**