Subversion Repositories Applications.papyrus

Rev

Rev 841 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 841 Rev 1117
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: Papyrus.class.php,v 1.8 2006-05-10 16:02:49 ddelon Exp $
24
// CVS : $Id: Papyrus.class.php,v 1.9 2006-12-08 15:29:33 jp_milcent Exp $
25
/**
25
/**
26
* Classe configurant le formatage pour Papyrus.
26
* Classe configurant le formatage pour Papyrus.
27
*
27
*
28
* Ce fichier contient une classe configurant les règles de formatage de Papyrus.
28
* Ce fichier contient une classe configurant les règles de formatage de Papyrus.
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 Papyrus
33
*@subpackage Papyrus
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.8 $ $Date: 2006-05-10 16:02:49 $
39
*@version       $Revision: 1.9 $ $Date: 2006-12-08 15:29:33 $
40
// +------------------------------------------------------------------------------------------------------+
40
// +------------------------------------------------------------------------------------------------------+
41
*/
41
*/
42
 
42
 
43
// +------------------------------------------------------------------------------------------------------+
43
// +------------------------------------------------------------------------------------------------------+
44
// |                                            ENTETE du PROGRAMME                                       |
44
// |                                            ENTETE du PROGRAMME                                       |
45
// +------------------------------------------------------------------------------------------------------+
45
// +------------------------------------------------------------------------------------------------------+
46
 
46
 
47
// +------------------------------------------------------------------------------------------------------+
47
// +------------------------------------------------------------------------------------------------------+
48
// |                                            CORPS du PROGRAMME                                        |
48
// |                                            CORPS du PROGRAMME                                        |
49
// +------------------------------------------------------------------------------------------------------+
49
// +------------------------------------------------------------------------------------------------------+
50
/**
50
/**
51
* 
51
* 
52
* Parse structured wiki text and render into arbitrary formats such as XHTML.
52
* Parse structured wiki text and render into arbitrary formats such as XHTML.
53
* 
53
* 
54
* Cette classe fille permet de configurer les régles de formatage pour Papyrus.
54
* Cette classe fille permet de configurer les régles de formatage pour Papyrus.
55
* Généralement nous avons à faire à des actions.
55
* Généralement nous avons à faire à des actions.
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_Papyrus extends Text_Wiki {
62
class Text_Papyrus extends Text_Wiki {
63
    
63
    
64
    /**
64
    /**
65
    * 
65
    * 
66
    * Liste de règles par défaut du format Papyrs dans leur ordre d'application au texte
66
    * Liste de règles par défaut du format Papyrs 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
        'Inclure', // Action Inclure
-
 
76
        'Motcles', // Action Motcles
75
        'Motcles', // Action Motcles
77
        'Categorie', // Action Categorie
-
 
78
        'Nouveaute', // Action Nouveaute
76
        'Nouveaute', // Action Nouveaute
79
        'Plan', // Action Plan
77
        'Plan', // Action Plan
80
        'Lien', // Action Lien
78
        'Lien', // Action Lien
81
        'Syndication', // Action Syndication
79
        'Syndication', // Action Syndication
82
        'Redirection' // Action Redirection
80
        'Redirection' // Action Redirection
83
        
81
        
84
    );
82
    );
85
    
83
    
86
    /**
84
    /**
87
    * 
85
    * 
88
    * The list of rules to not-apply to the source text.
86
    * The list of rules to not-apply to the source text.
89
    * 
87
    * 
90
    * @access public
88
    * @access public
91
    * 
89
    * 
92
    * @var array
90
    * @var array
93
    * 
91
    * 
94
    */
92
    */
95
    var $disable = array();
93
    var $disable = array();
96
    
94
    
97
    /**
95
    /**
98
    * 
96
    * 
99
    * The delimiter for token numbers of parsed elements in source text.
97
    * The delimiter for token numbers of parsed elements in source text.
100
    * 
98
    * 
101
    * @access public
99
    * @access public
102
    * 
100
    * 
103
    * @var string
101
    * @var string
104
    * 
102
    * 
105
    */
103
    */
106
    var $delim = 12;
104
    var $delim = 12;
107
    
105
    
108
    function Text_Papyrus($rules = null)
106
    function Text_Papyrus($rules = null)
109
    {
107
    {
110
        //Text_Wiki::Text_Wiki();
108
        //Text_Wiki::Text_Wiki();
111
        if (is_array($rules)) {
109
        if (is_array($rules)) {
112
            $this->rules = $rules;
110
            $this->rules = $rules;
113
        }
111
        }
114
        // Nous devons sortir les fichiers de Text_Wiki du dépot Pear car la fonction file_exists de PHP utilisée dans
112
        // Nous devons sortir les fichiers de Text_Wiki du dépot Pear car la fonction file_exists de PHP utilisée dans
115
        // la méthode findfile de Text_Wiki renvoie false.
113
        // la méthode findfile de Text_Wiki renvoie false.
116
        $this->addPath('parse', $this->fixPath(dirname(__FILE__)) .'../../pear/Text/Wiki/Parse/');
114
        $this->addPath('parse', $this->fixPath(dirname(__FILE__)) .'../../pear/Text/Wiki/Parse/');
117
        $this->addPath('render', $this->fixPath(dirname(__FILE__)) .'../../pear/Text/Wiki/Render/');
115
        $this->addPath('render', $this->fixPath(dirname(__FILE__)) .'../../pear/Text/Wiki/Render/');
118
        // Pour les règles spécifiques à Papyrus:
116
        // Pour les règles spécifiques à Papyrus:
119
        $this->addPath('parse', $this->fixPath(dirname(__FILE__)) . 'Parse/');
117
        $this->addPath('parse', $this->fixPath(dirname(__FILE__)) . 'Parse/');
120
        $this->addPath('render', $this->fixPath(dirname(__FILE__)) . 'Render/');
118
        $this->addPath('render', $this->fixPath(dirname(__FILE__)) . 'Render/');
121
        
119
        
122
        
120
        
123
    }
121
    }
124
    
122
    
125
    /**
123
    /**
126
    * 
124
    * 
127
    * Renders tokens back into the source text, based on the requested format.
125
    * Renders tokens back into the source text, based on the requested format.
128
    * 
126
    * 
129
    * @access public
127
    * @access public
130
    * 
128
    * 
131
    * @param string $format The target output format, typically 'xhtml'. 
129
    * @param string $format The target output format, typically 'xhtml'. 
132
    * If a rule does not support a given format, the output from that
130
    * If a rule does not support a given format, the output from that
133
    * rule is rule-specific.
131
    * rule is rule-specific.
134
    * 
132
    * 
135
    * @return string The transformed wiki text.
133
    * @return string The transformed wiki text.
136
    * 
134
    * 
137
    */
135
    */
138
    function render($format = 'Xhtml')
136
    function render($format = 'Xhtml')
139
    {
137
    {
140
        // the rendering method we're going to use from each rule
138
        // the rendering method we're going to use from each rule
141
        $format = ucwords(strtolower($format));
139
        $format = ucwords(strtolower($format));
142
        
140
        
143
        // the eventual output text
141
        // the eventual output text
144
        $output = '';
142
        $output = '';
145
        
143
        
146
        // when passing through the parsed source text, keep track of when
144
        // when passing through the parsed source text, keep track of when
147
        // we are in a delimited section
145
        // we are in a delimited section
148
        $in_delim = false;
146
        $in_delim = false;
149
        
147
        
150
        // when in a delimited section, capture the token key number
148
        // when in a delimited section, capture the token key number
151
        $key = '';
149
        $key = '';
152
        
150
        
153
        // load the format object
151
        // load the format object
154
        $this->loadFormatObj($format);
152
        $this->loadFormatObj($format);
155
        
153
        
156
        // pre-rendering activity
154
        // pre-rendering activity
157
        if (isset($this->formatObj[$format]) && is_object($this->formatObj[$format])) {
155
        if (isset($this->formatObj[$format]) && is_object($this->formatObj[$format])) {
158
            $output .= $this->formatObj[$format]->pre();
156
            $output .= $this->formatObj[$format]->pre();
159
        }
157
        }
160
        
158
        
161
        // load the render objects
159
        // load the render objects
162
        foreach (array_keys($this->parseObj) as $rule) {
160
        foreach (array_keys($this->parseObj) as $rule) {
163
            $this->loadRenderObj($format, $rule);
161
            $this->loadRenderObj($format, $rule);
164
        }
162
        }
165
        
163
        
166
        // pass through the parsed source text character by character
164
        // pass through the parsed source text character by character
167
        $k = strlen($this->source);
165
        $k = strlen($this->source);
168
        for ($i = 0; $i < $k; $i++) {
166
        for ($i = 0; $i < $k; $i++) {
169
            
167
            
170
            // the current character
168
            // the current character
171
            $char = $this->source{$i};
169
            $char = $this->source{$i};
172
            
170
            
173
            // are alredy in a delimited section?
171
            // are alredy in a delimited section?
174
            if ($in_delim) {
172
            if ($in_delim) {
175
            
173
            
176
                // yes; are we ending the section?
174
                // yes; are we ending the section?
177
                if ($char == chr($this->delim)) {
175
                if ($char == chr($this->delim)) {
178
                    
176
                    
179
                    // yes, get the replacement text for the delimited
177
                    // yes, get the replacement text for the delimited
180
                    // token number and unset the flag.
178
                    // token number and unset the flag.
181
                    $key = (int)$key;
179
                    $key = (int)$key;
182
                    $rule = null;
180
                    $rule = null;
183
                    if (isset($this->tokens[$key][0])) {
181
                    if (isset($this->tokens[$key][0])) {
184
                        $rule = $this->tokens[$key][0];
182
                        $rule = $this->tokens[$key][0];
185
                    }
183
                    }
186
                    $opts = null;
184
                    $opts = null;
187
                    if (isset($this->tokens[$key][1])) {
185
                    if (isset($this->tokens[$key][1])) {
188
                        $opts = $this->tokens[$key][1];
186
                        $opts = $this->tokens[$key][1];
189
                    }
187
                    }
190
                    if (isset($this->renderObj[$rule]) && is_object($this->renderObj[$rule])) {
188
                    if (isset($this->renderObj[$rule]) && is_object($this->renderObj[$rule])) {
191
                        $output .= $this->renderObj[$rule]->token($opts);
189
                        $output .= $this->renderObj[$rule]->token($opts);
192
                    }
190
                    }
193
                    $in_delim = false;
191
                    $in_delim = false;
194
                    
192
                    
195
                } else {
193
                } else {
196
                
194
                
197
                    // no, add to the dlimited token key number
195
                    // no, add to the dlimited token key number
198
                    $key .= $char;
196
                    $key .= $char;
199
                    
197
                    
200
                }
198
                }
201
                
199
                
202
            } else {
200
            } else {
203
                
201
                
204
                // not currently in a delimited section.
202
                // not currently in a delimited section.
205
                // are we starting into a delimited section?
203
                // are we starting into a delimited section?
206
                if ($char == chr($this->delim)) {
204
                if ($char == chr($this->delim)) {
207
                    // yes, reset the previous key and
205
                    // yes, reset the previous key and
208
                    // set the flag.
206
                    // set the flag.
209
                    $key = '';
207
                    $key = '';
210
                    $in_delim = true;
208
                    $in_delim = true;
211
                } else {
209
                } else {
212
                    // no, add to the output as-is
210
                    // no, add to the output as-is
213
                    $output .= $char;
211
                    $output .= $char;
214
                }
212
                }
215
            }
213
            }
216
        }
214
        }
217
        
215
        
218
        // post-rendering activity
216
        // post-rendering activity
219
        if (isset($this->formatObj[$format]) && is_object($this->formatObj[$format])) {
217
        if (isset($this->formatObj[$format]) && is_object($this->formatObj[$format])) {
220
            $output .= $this->formatObj[$format]->post();
218
            $output .= $this->formatObj[$format]->post();
221
        }
219
        }
222
        
220
        
223
        // return the rendered source text.
221
        // return the rendered source text.
224
        return $output;
222
        return $output;
225
    }
223
    }
226
    
224
    
227
    /**
225
    /**
228
    * 
226
    * 
229
    * Add a token to the Text_Wiki tokens array, and return a delimited
227
    * Add a token to the Text_Wiki tokens array, and return a delimited
230
    * token number.
228
    * token number.
231
    * 
229
    * 
232
    * @access public
230
    * @access public
233
    * 
231
    * 
234
    * @param array $options An associative array of options for the new
232
    * @param array $options An associative array of options for the new
235
    * token array element.  The keys and values are specific to the
233
    * token array element.  The keys and values are specific to the
236
    * rule, and may or may not be common to other rule options.  Typical
234
    * rule, and may or may not be common to other rule options.  Typical
237
    * options keys are 'text' and 'type' but may include others.
235
    * options keys are 'text' and 'type' but may include others.
238
    * 
236
    * 
239
    * @param boolean $id_only If true, return only the token number, not
237
    * @param boolean $id_only If true, return only the token number, not
240
    * a delimited token string.
238
    * a delimited token string.
241
    * 
239
    * 
242
    * @return string|int By default, return the number of the
240
    * @return string|int By default, return the number of the
243
    * newly-created token array element with a delimiter prefix and
241
    * newly-created token array element with a delimiter prefix and
244
    * suffix; however, if $id_only is set to true, return only the token
242
    * suffix; however, if $id_only is set to true, return only the token
245
    * number (no delimiters).
243
    * number (no delimiters).
246
    * 
244
    * 
247
    */
245
    */
248
    function addToken($rule, $options = array(), $id_only = false)
246
    function addToken($rule, $options = array(), $id_only = false)
249
    {
247
    {
250
        // increment the token ID number.  note that if you parse
248
        // increment the token ID number.  note that if you parse
251
        // multiple times with the same Text_Wiki object, the ID number
249
        // multiple times with the same Text_Wiki object, the ID number
252
        // will not reset to zero.
250
        // will not reset to zero.
253
        static $id;
251
        static $id;
254
        if (! isset($id)) {
252
        if (! isset($id)) {
255
            $id = 0;
253
            $id = 0;
256
        } else {
254
        } else {
257
            $id ++;
255
            $id ++;
258
        }
256
        }
259
        
257
        
260
        // force the options to be an array
258
        // force the options to be an array
261
        settype($options, 'array');
259
        settype($options, 'array');
262
        
260
        
263
        // add the token
261
        // add the token
264
        $this->tokens[$id] = array(
262
        $this->tokens[$id] = array(
265
            0 => $rule,
263
            0 => $rule,
266
            1 => $options
264
            1 => $options
267
        );
265
        );
268
        
266
        
269
        // return a value
267
        // return a value
270
        if ($id_only) {
268
        if ($id_only) {
271
            // return the last token number
269
            // return the last token number
272
            return $id;
270
            return $id;
273
        } else {
271
        } else {
274
            // return the token number with delimiters
272
            // return the token number with delimiters
275
            return chr($this->delim) . $id . chr($this->delim);
273
            return chr($this->delim) . $id . chr($this->delim);
276
        }
274
        }
277
    }
275
    }
278
}
276
}
279
 
277
 
280
// +------------------------------------------------------------------------------------------------------+
278
// +------------------------------------------------------------------------------------------------------+
281
// |                                            PIED du PROGRAMME                                         |
279
// |                                            PIED du PROGRAMME                                         |
282
// +------------------------------------------------------------------------------------------------------+
280
// +------------------------------------------------------------------------------------------------------+
283
 
281
 
284
 
282
 
285
 
283
 
286
/* +--Fin du code ----------------------------------------------------------------------------------------+
284
/* +--Fin du code ----------------------------------------------------------------------------------------+
287
*
285
*
288
* $Log: not supported by cvs2svn $
286
* $Log: not supported by cvs2svn $
-
 
287
* Revision 1.8  2006/05/10 16:02:49  ddelon
-
 
288
* Finition multilinguise et schizo flo
-
 
289
*
289
* Revision 1.7  2005/09/23 13:58:07  ddelon
290
* Revision 1.7  2005/09/23 13:58:07  ddelon
290
* Php5, Projet et Redirection
291
* Php5, Projet et Redirection
291
*
292
*
292
* Revision 1.6  2005/04/18 16:41:53  jpm
293
* Revision 1.6  2005/04/18 16:41:53  jpm
293
* Ajout des actions Plan et Syndication.
294
* Ajout des actions Plan et Syndication.
294
*
295
*
295
* Revision 1.5  2005/04/14 16:35:42  jpm
296
* Revision 1.5  2005/04/14 16:35:42  jpm
296
* Ajout de nouvelles actions pour Papyrus XHTML.
297
* Ajout de nouvelles actions pour Papyrus XHTML.
297
*
298
*
298
* Revision 1.4  2005/01/20 19:39:39  jpm
299
* Revision 1.4  2005/01/20 19:39:39  jpm
299
* Correction bogue du à la fonction file_exists qui renvoie false pour les fichiers présent dans le dossier Pear /usr/local/lib/php/.
300
* Correction bogue du à la fonction file_exists qui renvoie false pour les fichiers présent dans le dossier Pear /usr/local/lib/php/.
300
*
301
*
301
* Revision 1.3  2004/12/07 12:17:37  jpm
302
* Revision 1.3  2004/12/07 12:17:37  jpm
302
* Correction message d'erreur.
303
* Correction message d'erreur.
303
*
304
*
304
* Revision 1.2  2004/11/26 12:13:03  jpm
305
* Revision 1.2  2004/11/26 12:13:03  jpm
305
* Correction de résidu...
306
* Correction de résidu...
306
*
307
*
307
* Revision 1.1  2004/11/26 12:11:49  jpm
308
* Revision 1.1  2004/11/26 12:11:49  jpm
308
* Ajout des action Papyrus à Text_Wiki.
309
* Ajout des action Papyrus à Text_Wiki.
309
*
310
*
310
* Revision 1.2  2004/11/25 15:36:41  jpm
311
* Revision 1.2  2004/11/25 15:36:41  jpm
311
* Suppression régle Delimiter car problème avec les délimitations de fin de ligne.
312
* Suppression régle Delimiter car problème avec les délimitations de fin de ligne.
312
*
313
*
313
* Revision 1.1  2004/11/23 17:25:38  jpm
314
* Revision 1.1  2004/11/23 17:25:38  jpm
314
* Début classe PEAR WIKI pour la syntaxe Wikini.
315
* Début classe PEAR WIKI pour la syntaxe Wikini.
315
*
316
*
316
*
317
*
317
* +-- Fin du code ----------------------------------------------------------------------------------------+
318
* +-- Fin du code ----------------------------------------------------------------------------------------+
318
*/
319
*/
319
?>
320
?>