X-Git-Url: http://git.cyclocoop.org/?p=velocampus%2Fweb%2Fwww.git;a=blobdiff_plain;f=www%2Fecrire%2Finc%2Fmath.php;fp=www%2Fecrire%2Finc%2Fmath.php;h=dc33c3fe9b968a80d7f1de6e4314d6614a98c58a;hp=0000000000000000000000000000000000000000;hb=80b4d3e85f78d402ed2e73f8f5d1bf4c19962eed;hpb=aaf970bf4cdaf76689ecc10609048e18d073820c diff --git a/www/ecrire/inc/math.php b/www/ecrire/inc/math.php new file mode 100644 index 0000000..dc33c3f --- /dev/null +++ b/www/ecrire/inc/math.php @@ -0,0 +1,116 @@ +... en client-serveur +// + +// http://doc.spip.org/@produire_image_math +function produire_image_math($tex) { + global $traiter_math; + switch ($traiter_math) { + // Attention: mathml desactiv'e pour l'instant + case 'mathml': + $ext = '.xhtml'; + $server = $GLOBALS['mathml_server']; + break; + case 'tex': + $ext = '.png'; + $server = $GLOBALS['tex_server']; + break; + default: + return $tex; + } + + // Regarder dans le repertoire local des images TeX et blocs MathML + if (!@is_dir($dir_tex = _DIR_VAR.'cache-TeX/')) + @mkdir ($dir_tex, _SPIP_CHMOD); + $fichier = $dir_tex .md5(trim($tex)).$ext; + + + if (!@file_exists($fichier)) { + // Aller chercher l'image sur le serveur + if ($server) { + spip_log($url = $server.'?'.rawurlencode($tex)); + include_spip('inc/distant'); + recuperer_page($url,$fichier); + } + } + + + // Composer la reponse selon presence ou non de l'image + $tex = entites_html($tex); + if (@file_exists($fichier)) { + + // MathML + if ($traiter_math == 'mathml') { + return join(file("$fichier"),""); + } + + // TeX + else { + list(,,,$size) = @getimagesize($fichier); + $alt = "alt=\"$tex\" title=\"$tex\""; + return ""; + } + + } + else // pas de fichier + return "$tex"; + +} + + +// Fonction appelee par propre() s'il repere un mode +// http://doc.spip.org/@traiter_math +function traiter_math($letexte, $source='') { + + $texte_a_voir = $letexte; + while (($debut = strpos($texte_a_voir, "")) !== false) { + if (!$fin = strpos($texte_a_voir,"")) + $fin = strlen($texte_a_voir); + + $texte_debut = substr($texte_a_voir, 0, $debut); + $texte_milieu = substr($texte_a_voir, + $debut+strlen(""), $fin-$debut-strlen("")); + $texte_fin = substr($texte_a_voir, + $fin+strlen(""), strlen($texte_a_voir)); + + // Les doubles $$x^2$$ en mode 'div' + while((preg_match(",[$][$]([^$]+)[$][$],",$texte_milieu, $regs))) { + $echap = "\n

".produire_image_math($regs[1])."

\n"; + $pos = strpos($texte_milieu, $regs[0]); + $texte_milieu = substr($texte_milieu,0,$pos) + . code_echappement($echap, $source) + . substr($texte_milieu,$pos+strlen($regs[0])); + } + + // Les simples $x^2$ en mode 'span' + while((preg_match(",[$]([^$]+)[$],",$texte_milieu, $regs))) { + $echap = produire_image_math($regs[1]); + $pos = strpos($texte_milieu, $regs[0]); + $texte_milieu = substr($texte_milieu,0,$pos) + . code_echappement($echap, $source) + . substr($texte_milieu,$pos+strlen($regs[0])); + } + + $texte_a_voir = $texte_debut.$texte_milieu.$texte_fin; + } + + return $texte_a_voir; +} + +?>