3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
6 * Copyright (c) 2001-2014 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
15 if (!defined('_ECRIRE_INC_VERSION')) return;
18 // Gestion du raccourci <math>...</math> en client-serveur
21 // http://doc.spip.org/@image_math
22 function produire_image_math($tex) {
24 switch ($traiter_math) {
25 // Attention: mathml desactiv'e pour l'instant
28 $server = $GLOBALS['mathml_server'];
32 $server = $GLOBALS['tex_server'];
38 // Regarder dans le repertoire local des images TeX et blocs MathML
39 if (!@is_dir
($dir_tex = _DIR_VAR
.'cache-TeX/'))
40 @mkdir
($dir_tex, _SPIP_CHMOD
);
41 $fichier = $dir_tex .md5(trim($tex)).$ext;
44 if (!@file_exists
($fichier)) {
45 // Aller chercher l'image sur le serveur
47 spip_log($url = $server.'?'.rawurlencode($tex));
48 include_spip('inc/distant');
49 recuperer_page($url,$fichier);
54 // Composer la reponse selon presence ou non de l'image
55 $tex = entites_html($tex);
56 if (@file_exists
($fichier)) {
59 if ($traiter_math == 'mathml') {
60 return join(file("$fichier"),"");
65 list(,,,$size) = @getimagesize
($fichier);
66 $alt = "alt=\"$tex\" title=\"$tex\"";
67 return "<img src=\"$fichier\" style=\"vertical-align:middle;\" $size $alt />";
71 else // pas de fichier
72 return "<tt><span class='spip_code' dir='ltr'>$tex</span></tt>";
77 // Fonction appelee par propre() s'il repere un mode <math>
78 // http://doc.spip.org/@traiter_math
79 function traiter_math($letexte, $source='') {
81 $texte_a_voir = $letexte;
82 while (($debut = strpos($texte_a_voir, "<math>")) !== false) {
83 if (!$fin = strpos($texte_a_voir,"</math>"))
84 $fin = strlen($texte_a_voir);
86 $texte_debut = substr($texte_a_voir, 0, $debut);
87 $texte_milieu = substr($texte_a_voir,
88 $debut+
strlen("<math>"), $fin-$debut-strlen("<math>"));
89 $texte_fin = substr($texte_a_voir,
90 $fin+
strlen("</math>"), strlen($texte_a_voir));
92 // Les doubles $$x^2$$ en mode 'div'
93 while((preg_match(",[$][$]([^$]+)[$][$],",$texte_milieu, $regs))) {
94 $echap = "\n<p class=\"spip\" style=\"text-align: center;\">".produire_image_math($regs[1])."</p>\n";
95 $pos = strpos($texte_milieu, $regs[0]);
96 $texte_milieu = substr($texte_milieu,0,$pos)
97 . code_echappement($echap, $source)
98 . substr($texte_milieu,$pos+
strlen($regs[0]));
101 // Les simples $x^2$ en mode 'span'
102 while((preg_match(",[$]([^$]+)[$],",$texte_milieu, $regs))) {
103 $echap = produire_image_math($regs[1]);
104 $pos = strpos($texte_milieu, $regs[0]);
105 $texte_milieu = substr($texte_milieu,0,$pos)
106 . code_echappement($echap, $source)
107 . substr($texte_milieu,$pos+
strlen($regs[0]));
110 $texte_a_voir = $texte_debut.$texte_milieu.$texte_fin;
113 return $texte_a_voir;