[SPIP] +2.1.12
[velocampus/web/www.git] / www / ecrire / inc / math.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2011 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
8 * *
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 \***************************************************************************/
12
13
14 //
15 if (!defined('_ECRIRE_INC_VERSION')) return;
16
17 //
18 // Gestion du raccourci <math>...</math> en client-serveur
19 //
20
21 // http://doc.spip.org/@produire_image_math
22 function produire_image_math($tex) {
23 global $traiter_math;
24 switch ($traiter_math) {
25 // Attention: mathml desactiv'e pour l'instant
26 case 'mathml':
27 $ext = '.xhtml';
28 $server = $GLOBALS['mathml_server'];
29 break;
30 case 'tex':
31 $ext = '.png';
32 $server = $GLOBALS['tex_server'];
33 break;
34 default:
35 return $tex;
36 }
37
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;
42
43
44 if (!@file_exists($fichier)) {
45 // Aller chercher l'image sur le serveur
46 if ($server) {
47 spip_log($url = $server.'?'.rawurlencode($tex));
48 include_spip('inc/distant');
49 recuperer_page($url,$fichier);
50 }
51 }
52
53
54 // Composer la reponse selon presence ou non de l'image
55 $tex = entites_html($tex);
56 if (@file_exists($fichier)) {
57
58 // MathML
59 if ($traiter_math == 'mathml') {
60 return join(file("$fichier"),"");
61 }
62
63 // TeX
64 else {
65 list(,,,$size) = @getimagesize($fichier);
66 $alt = "alt=\"$tex\" title=\"$tex\"";
67 return "<img src=\"$fichier\" style=\"vertical-align:middle;\" $size $alt />";
68 }
69
70 }
71 else // pas de fichier
72 return "<tt><span class='spip_code' dir='ltr'>$tex</span></tt>";
73
74 }
75
76
77 // Fonction appelee par propre() s'il repere un mode <math>
78 // http://doc.spip.org/@traiter_math
79 function traiter_math($letexte, $source='') {
80
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);
85
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));
91
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]));
99 }
100
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]));
108 }
109
110 $texte_a_voir = $texte_debut.$texte_milieu.$texte_fin;
111 }
112
113 return $texte_a_voir;
114 }
115
116 ?>