Subversion Repositories Applications.papyrus

Rev

Rev 247 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 247 Rev 336
1
<?php
1
<?php
2
/*vim: set expandtab tabstop=4 shiftwidth=4: */ 
2
/*vim: set expandtab tabstop=4 shiftwidth=4: */ 
3
// +------------------------------------------------------------------------------------------------------+
3
// +------------------------------------------------------------------------------------------------------+
4
// | PHP version 4.3                                                                                      |
4
// | PHP version 4.3                                                                                      |
5
// +------------------------------------------------------------------------------------------------------+
5
// +------------------------------------------------------------------------------------------------------+
6
// | Copyright (C) 2004 Tela Botanica (accueil@tela-botanica.org)                                         |
6
// | Copyright (C) 2004 Tela Botanica (accueil@tela-botanica.org)                                         |
7
// +------------------------------------------------------------------------------------------------------+
7
// +------------------------------------------------------------------------------------------------------+
8
// | This file is part of Papyrus.                                                                        |
8
// | This file is part of Papyrus.                                                                        |
9
// |                                                                                                      |
9
// |                                                                                                      |
10
// | Foobar is free software; you can redistribute it and/or modify                                       |
10
// | Foobar is free software; you can redistribute it and/or modify                                       |
11
// | it under the terms of the GNU General Public License as published by                                 |
11
// | it under the terms of the GNU General Public License as published by                                 |
12
// | the Free Software Foundation; either version 2 of the License, or                                    |
12
// | the Free Software Foundation; either version 2 of the License, or                                    |
13
// | (at your option) any later version.                                                                  |
13
// | (at your option) any later version.                                                                  |
14
// |                                                                                                      |
14
// |                                                                                                      |
15
// | Foobar is distributed in the hope that it will be useful,                                            |
15
// | Foobar is distributed in the hope that it will be useful,                                            |
16
// | but WITHOUT ANY WARRANTY; without even the implied warranty of                                       |
16
// | but WITHOUT ANY WARRANTY; without even the implied warranty of                                       |
17
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                        |
17
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                        |
18
// | GNU General Public License for more details.                                                         |
18
// | GNU General Public License for more details.                                                         |
19
// |                                                                                                      |
19
// |                                                                                                      |
20
// | You should have received a copy of the GNU General Public License                                    |
20
// | You should have received a copy of the GNU General Public License                                    |
21
// | along with Foobar; if not, write to the Free Software                                                |
21
// | along with Foobar; if not, write to the Free Software                                                |
22
// | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                            |
22
// | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                            |
23
// +------------------------------------------------------------------------------------------------------+
23
// +------------------------------------------------------------------------------------------------------+
24
// CVS : $Id: Wikini.class.php,v 1.5 2005-01-20 19:39:43 jpm Exp $
24
// CVS : $Id: Wikini.class.php,v 1.6 2005-04-14 16:36:49 jpm Exp $
25
/**
25
/**
26
* Classe configurant le formatage pour Wikini.
26
* Classe configurant le formatage pour Wikini.
27
*
27
*
28
* Ce fichier contient une classe configurant les règles de formatage de Wikini.
28
* Ce fichier contient une classe configurant les règles de formatage de Wikini.
29
* Nécessite que l'application appelant ce fichier est précédement inclu le fichier de Pear:
29
* Nécessite que l'application appelant ce fichier est précédement inclu le fichier de Pear:
30
* 'Text/Wiki.php';
30
* 'Text/Wiki.php';
31
*
31
*
32
*@package Text_Wiki
32
*@package Text_Wiki
33
*@subpackage Wikini
33
*@subpackage Wikini
34
//Auteur original :
34
//Auteur original :
35
*@author        Jean-Pascal MILCENT <jpm@tela-botanica.org>
35
*@author        Jean-Pascal MILCENT <jpm@tela-botanica.org>
36
//Autres auteurs :
36
//Autres auteurs :
37
*@author        Aucun
37
*@author        Aucun
38
*@copyright     Tela-Botanica 2000-2004
38
*@copyright     Tela-Botanica 2000-2004
39
*@version       $Revision: 1.5 $ $Date: 2005-01-20 19:39:43 $
39
*@version       $Revision: 1.6 $ $Date: 2005-04-14 16:36:49 $
40
// +------------------------------------------------------------------------------------------------------+
40
// +------------------------------------------------------------------------------------------------------+
41
*/
41
*/
42
 
42
 
43
// +------------------------------------------------------------------------------------------------------+
43
// +------------------------------------------------------------------------------------------------------+
44
// |                                            ENTETE du PROGRAMME                                       |
44
// |                                            ENTETE du PROGRAMME                                       |
45
// +------------------------------------------------------------------------------------------------------+
45
// +------------------------------------------------------------------------------------------------------+
46
 
46
 
47
 
47
 
48
// +------------------------------------------------------------------------------------------------------+
48
// +------------------------------------------------------------------------------------------------------+
49
// |                                            CORPS du PROGRAMME                                        |
49
// |                                            CORPS du PROGRAMME                                        |
50
// +------------------------------------------------------------------------------------------------------+
50
// +------------------------------------------------------------------------------------------------------+
51
/**
51
/**
52
* 
52
* 
53
* Parse structured wiki text and render into arbitrary formats such as XHTML.
53
* Parse structured wiki text and render into arbitrary formats such as XHTML.
54
* 
54
* 
55
* Cette classe fille permet de configurer les régles de formatage pour Wikini.
55
* Cette classe fille permet de configurer les régles de formatage pour Wikini.
56
* 
56
* 
57
* @author Paul M. Jones <pmjones@ciaweb.net>
57
* @author Paul M. Jones <pmjones@ciaweb.net>
58
* @package Text_Wiki
58
* @package Text_Wiki
59
* @version 0.23.1
59
* @version 0.23.1
60
* @license LGPL
60
* @license LGPL
61
*/
61
*/
62
class Text_Wikini extends Text_Wiki {
62
class Text_Wikini extends Text_Wiki {
63
    
63
    
64
    /**
64
    /**
65
    * 
65
    * 
66
    * Liste de règles par défaut du format Wikini dans leur ordre d'application au texte
66
    * Liste de règles par défaut du format Wikini dans leur ordre d'application au texte
67
    * à transformer.
67
    * à transformer.
68
    * 
68
    * 
69
    * @access public
69
    * @access public
70
    * 
70
    * 
71
    * @var array
71
    * @var array
72
    * 
72
    * 
73
    */
73
    */
74
    var $rules = array(
74
    var $rules = array(
75
        'Table', // Tableaux
75
        'Table', // Tableaux
76
        'Code', // Inclusion de code avec coloration syntaxique
76
        'Code', // Inclusion de code avec coloration syntaxique
77
        'Emphasis', // Italique
77
        'Emphasis', // Italique
78
        'Strong',// Gras
78
        'Strong',// Gras
79
        'Freelink', // Nom de Page qui ne sont pas au format Wiki
79
        'Freelink', // Nom de Page qui ne sont pas au format Wiki
80
        'Heading', // Titre
80
        'Heading', // Titre
81
        'Horiz', // Ligne horizontale
81
        'Horiz', // Ligne horizontale
82
        'Interwiki', // Affichage de page d'un autre Wiki. Modifié par rapport à l'original de Text_Wiki
82
        'Interwiki', // Affichage de page d'un autre Wiki. Modifié par rapport à l'original de Text_Wiki
83
        'List', // Affichage de listes. Modifié par rapport à l'original de Text_Wiki
83
        'List', // Affichage de listes. Modifié par rapport à l'original de Text_Wiki
84
        'Newline', // Nouveau paragraphe.
84
        //'Newline', // Nouveau paragraphe.
85
        'Paragraph', // Nouveau paragraphe avec une ligne vide.
85
        'Paragraph', // Nouveau paragraphe avec une ligne vide.
86
        'Tighten', // Réduit les lignes vide si on en a 3 ou plus consécutives
86
        'Tighten', // Réduit les lignes vide si on en a 3 ou plus consécutives
87
        'Raw', // Inclusion de HTML et non traitement du contenu par les règles de formatage. Modifié par rapport à l'original de Text_Wiki
87
        'Raw', // Inclusion de HTML et non traitement du contenu par les règles de formatage. Modifié par rapport à l'original de Text_Wiki
88
        'Revise', // Suppression de texte. Modifié par rapport à l'original de Text_Wiki
88
        'Revise', // Suppression de texte. Modifié par rapport à l'original de Text_Wiki
89
        'Tt', // Texte à espacement fixe
89
        'Tt', // Texte à espacement fixe
90
        'Url' // Inclusion d'url dont les url d'images
90
        'Url' // Inclusion d'url dont les url d'images
91
    );
91
    );
92
    /**
92
    /**
93
    * 
93
    * 
94
    * The delimiter for token numbers of parsed elements in source text.
94
    * The delimiter for token numbers of parsed elements in source text.
95
    * 
95
    * 
96
    * @access public
96
    * @access public
97
    * 
97
    * 
98
    * @var string
98
    * @var string
99
    * 
99
    * 
100
    */
100
    */
101
    var $delim = 12;
101
    var $delim = 12;
102
    
102
    
103
    function Text_Wikini($rules = null)
103
    function Text_Wikini($rules = null)
104
    {
104
    {
105
        //Text_Wiki::Text_Wiki();
105
        //Text_Wiki::Text_Wiki();
106
        if (is_array($rules)) {
106
        if (is_array($rules)) {
107
            $this->rules = $rules;
107
            $this->rules = $rules;
108
        }
108
        }
109
        // Nous devons sortir les fichiers de Text_Wiki du dépot Pear car la fonction file_exists de PHP utilisée dans
109
        // Nous devons sortir les fichiers de Text_Wiki du dépot Pear car la fonction file_exists de PHP utilisée dans
110
        // la méthode findfile de Text_Wiki renvoie false.
110
        // la méthode findfile de Text_Wiki renvoie false.
111
        $this->addPath('parse', $this->fixPath(dirname(__FILE__)) .'../../pear/Text/Wiki/Parse/');
111
        $this->addPath('parse', $this->fixPath(dirname(__FILE__)) .'../../pear/Text/Wiki/Parse/');
112
        $this->addPath('render', $this->fixPath(dirname(__FILE__)) .'../../pear/Text/Wiki/Render/');
112
        $this->addPath('render', $this->fixPath(dirname(__FILE__)) .'../../pear/Text/Wiki/Render/');
113
        // Pour les règles spécifiques à Wikini:
113
        // Pour les règles spécifiques à Wikini:
114
        $this->addPath('parse', $this->fixPath(dirname(__FILE__)) . 'Parse/');
114
        $this->addPath('parse', $this->fixPath(dirname(__FILE__)) . 'Parse/');
115
        $this->addPath('render', $this->fixPath(dirname(__FILE__)) . 'Render/');
115
        $this->addPath('render', $this->fixPath(dirname(__FILE__)) . 'Render/');
116
    }
116
    }
117
    
117
    
118
    /**
118
    /**
119
    * 
119
    * 
120
    * Renders tokens back into the source text, based on the requested format.
120
    * Renders tokens back into the source text, based on the requested format.
121
    * 
121
    * 
122
    * @access public
122
    * @access public
123
    * 
123
    * 
124
    * @param string $format The target output format, typically 'xhtml'. 
124
    * @param string $format The target output format, typically 'xhtml'. 
125
    * If a rule does not support a given format, the output from that
125
    * If a rule does not support a given format, the output from that
126
    * rule is rule-specific.
126
    * rule is rule-specific.
127
    * 
127
    * 
128
    * @return string The transformed wiki text.
128
    * @return string The transformed wiki text.
129
    * 
129
    * 
130
    */
130
    */
131
    function render($format = 'Xhtml')
131
    function render($format = 'Xhtml')
132
    {
132
    {
133
        // the rendering method we're going to use from each rule
133
        // the rendering method we're going to use from each rule
134
        $format = ucwords(strtolower($format));
134
        $format = ucwords(strtolower($format));
135
        
135
        
136
        // the eventual output text
136
        // the eventual output text
137
        $output = '';
137
        $output = '';
138
        
138
        
139
        // when passing through the parsed source text, keep track of when
139
        // when passing through the parsed source text, keep track of when
140
        // we are in a delimited section
140
        // we are in a delimited section
141
        $in_delim = false;
141
        $in_delim = false;
142
        
142
        
143
        // when in a delimited section, capture the token key number
143
        // when in a delimited section, capture the token key number
144
        $key = '';
144
        $key = '';
145
        
145
        
146
        // load the format object
146
        // load the format object
147
        $this->loadFormatObj($format);
147
        $this->loadFormatObj($format);
148
        
148
        
149
        // pre-rendering activity
149
        // pre-rendering activity
150
        if (isset($this->formatObj[$format]) && is_object($this->formatObj[$format])) {
150
        if (isset($this->formatObj[$format]) && is_object($this->formatObj[$format])) {
151
            $output .= $this->formatObj[$format]->pre();
151
            $output .= $this->formatObj[$format]->pre();
152
        }
152
        }
153
        
153
        
154
        // load the render objects
154
        // load the render objects
155
        foreach (array_keys($this->parseObj) as $rule) {
155
        foreach (array_keys($this->parseObj) as $rule) {
156
            $this->loadRenderObj($format, $rule);
156
            $this->loadRenderObj($format, $rule);
157
        }
157
        }
158
        
158
        
159
        // pass through the parsed source text character by character
159
        // pass through the parsed source text character by character
160
        $k = strlen($this->source);
160
        $k = strlen($this->source);
161
        for ($i = 0; $i < $k; $i++) {
161
        for ($i = 0; $i < $k; $i++) {
162
            
162
            
163
            // the current character
163
            // the current character
164
            $char = $this->source{$i};
164
            $char = $this->source{$i};
165
            
165
            
166
            // are alredy in a delimited section?
166
            // are alredy in a delimited section?
167
            if ($in_delim) {
167
            if ($in_delim) {
168
            
168
            
169
                // yes; are we ending the section?
169
                // yes; are we ending the section?
170
                if ($char == chr($this->delim)) {
170
                if ($char == chr($this->delim)) {
171
                    
171
                    
172
                    // yes, get the replacement text for the delimited
172
                    // yes, get the replacement text for the delimited
173
                    // token number and unset the flag.
173
                    // token number and unset the flag.
174
                    $key = (int)$key;
174
                    $key = (int)$key;
175
                    $rule = null;
175
                    $rule = null;
176
                    if (isset($this->tokens[$key][0])) {
176
                    if (isset($this->tokens[$key][0])) {
177
                        $rule = $this->tokens[$key][0];
177
                        $rule = $this->tokens[$key][0];
178
                    }
178
                    }
179
                    $opts = null;
179
                    $opts = null;
180
                    if (isset($this->tokens[$key][1])) {
180
                    if (isset($this->tokens[$key][1])) {
181
                        $opts = $this->tokens[$key][1];
181
                        $opts = $this->tokens[$key][1];
182
                    }
182
                    }
183
                    if (isset($this->renderObj[$rule]) && is_object($this->renderObj[$rule])) {
183
                    if (isset($this->renderObj[$rule]) && is_object($this->renderObj[$rule])) {
184
                        $output .= $this->renderObj[$rule]->token($opts);
184
                        $output .= $this->renderObj[$rule]->token($opts);
185
                    }
185
                    }
186
                    $in_delim = false;
186
                    $in_delim = false;
187
                    
187
                    
188
                } else {
188
                } else {
189
                
189
                
190
                    // no, add to the dlimited token key number
190
                    // no, add to the dlimited token key number
191
                    $key .= $char;
191
                    $key .= $char;
192
                    
192
                    
193
                }
193
                }
194
                
194
                
195
            } else {
195
            } else {
196
                
196
                
197
                // not currently in a delimited section.
197
                // not currently in a delimited section.
198
                // are we starting into a delimited section?
198
                // are we starting into a delimited section?
199
                if ($char == chr($this->delim)) {
199
                if ($char == chr($this->delim)) {
200
                    // yes, reset the previous key and
200
                    // yes, reset the previous key and
201
                    // set the flag.
201
                    // set the flag.
202
                    $key = '';
202
                    $key = '';
203
                    $in_delim = true;
203
                    $in_delim = true;
204
                } else {
204
                } else {
205
                    // no, add to the output as-is
205
                    // no, add to the output as-is
206
                    $output .= $char;
206
                    $output .= $char;
207
                }
207
                }
208
            }
208
            }
209
        }
209
        }
210
        
210
        
211
        // post-rendering activity
211
        // post-rendering activity
212
        if (isset($this->formatObj[$format]) && is_object($this->formatObj[$format])) {
212
        if (isset($this->formatObj[$format]) && is_object($this->formatObj[$format])) {
213
            $output .= $this->formatObj[$format]->post();
213
            $output .= $this->formatObj[$format]->post();
214
        }
214
        }
215
        
215
        
216
        // return the rendered source text.
216
        // return the rendered source text.
217
        return $output;
217
        return $output;
218
    }
218
    }
219
    
219
    
220
    /**
220
    /**
221
    * 
221
    * 
222
    * Add a token to the Text_Wiki tokens array, and return a delimited
222
    * Add a token to the Text_Wiki tokens array, and return a delimited
223
    * token number.
223
    * token number.
224
    * 
224
    * 
225
    * @access public
225
    * @access public
226
    * 
226
    * 
227
    * @param array $options An associative array of options for the new
227
    * @param array $options An associative array of options for the new
228
    * token array element.  The keys and values are specific to the
228
    * token array element.  The keys and values are specific to the
229
    * rule, and may or may not be common to other rule options.  Typical
229
    * rule, and may or may not be common to other rule options.  Typical
230
    * options keys are 'text' and 'type' but may include others.
230
    * options keys are 'text' and 'type' but may include others.
231
    * 
231
    * 
232
    * @param boolean $id_only If true, return only the token number, not
232
    * @param boolean $id_only If true, return only the token number, not
233
    * a delimited token string.
233
    * a delimited token string.
234
    * 
234
    * 
235
    * @return string|int By default, return the number of the
235
    * @return string|int By default, return the number of the
236
    * newly-created token array element with a delimiter prefix and
236
    * newly-created token array element with a delimiter prefix and
237
    * suffix; however, if $id_only is set to true, return only the token
237
    * suffix; however, if $id_only is set to true, return only the token
238
    * number (no delimiters).
238
    * number (no delimiters).
239
    * 
239
    * 
240
    */
240
    */
241
    function addToken($rule, $options = array(), $id_only = false)
241
    function addToken($rule, $options = array(), $id_only = false)
242
    {
242
    {
243
        // increment the token ID number.  note that if you parse
243
        // increment the token ID number.  note that if you parse
244
        // multiple times with the same Text_Wiki object, the ID number
244
        // multiple times with the same Text_Wiki object, the ID number
245
        // will not reset to zero.
245
        // will not reset to zero.
246
        static $id;
246
        static $id;
247
        if (! isset($id)) {
247
        if (! isset($id)) {
248
            $id = 0;
248
            $id = 0;
249
        } else {
249
        } else {
250
            $id ++;
250
            $id ++;
251
        }
251
        }
252
        
252
        
253
        // force the options to be an array
253
        // force the options to be an array
254
        settype($options, 'array');
254
        settype($options, 'array');
255
        
255
        
256
        // add the token
256
        // add the token
257
        $this->tokens[$id] = array(
257
        $this->tokens[$id] = array(
258
            0 => $rule,
258
            0 => $rule,
259
            1 => $options
259
            1 => $options
260
        );
260
        );
261
        
261
        
262
        // return a value
262
        // return a value
263
        if ($id_only) {
263
        if ($id_only) {
264
            // return the last token number
264
            // return the last token number
265
            return $id;
265
            return $id;
266
        } else {
266
        } else {
267
            // return the token number with delimiters
267
            // return the token number with delimiters
268
            return chr($this->delim) . $id . chr($this->delim);
268
            return chr($this->delim) . $id . chr($this->delim);
269
        }
269
        }
270
    }
270
    }
271
}
271
}
272
 
272
 
273
// +------------------------------------------------------------------------------------------------------+
273
// +------------------------------------------------------------------------------------------------------+
274
// |                                            PIED du PROGRAMME                                         |
274
// |                                            PIED du PROGRAMME                                         |
275
// +------------------------------------------------------------------------------------------------------+
275
// +------------------------------------------------------------------------------------------------------+
276
 
276
 
277
 
277
 
278
 
278
 
279
/* +--Fin du code ----------------------------------------------------------------------------------------+
279
/* +--Fin du code ----------------------------------------------------------------------------------------+
280
*
280
*
281
* $Log: not supported by cvs2svn $
281
* $Log: not supported by cvs2svn $
-
 
282
* Revision 1.5  2005/01/20 19:39:43  jpm
-
 
283
* Correction bogue du à la fonction file_exists qui renvoie false pour les fichiers présent dans le dossier Pear /usr/local/lib/php/.
-
 
284
*
282
* Revision 1.4  2004/12/07 12:17:41  jpm
285
* Revision 1.4  2004/12/07 12:17:41  jpm
283
* Correction message d'erreur.
286
* Correction message d'erreur.
284
*
287
*
285
* Revision 1.3  2004/11/25 15:53:24  jpm
288
* Revision 1.3  2004/11/25 15:53:24  jpm
286
* Suppression action inclure, migrer dans Papyrus.
289
* Suppression action inclure, migrer dans Papyrus.
287
*
290
*
288
* Revision 1.2  2004/11/25 15:36:41  jpm
291
* Revision 1.2  2004/11/25 15:36:41  jpm
289
* Suppression régle Delimiter car problème avec les délimitations de fin de ligne.
292
* Suppression régle Delimiter car problème avec les délimitations de fin de ligne.
290
*
293
*
291
* Revision 1.1  2004/11/23 17:25:38  jpm
294
* Revision 1.1  2004/11/23 17:25:38  jpm
292
* Début classe PEAR WIKI pour la syntaxe Wikini.
295
* Début classe PEAR WIKI pour la syntaxe Wikini.
293
*
296
*
294
*
297
*
295
* +-- Fin du code ----------------------------------------------------------------------------------------+
298
* +-- Fin du code ----------------------------------------------------------------------------------------+
296
*/
299
*/
297
?>
300
?>