Subversion Repositories Applications.papyrus

Rev

Rev 1150 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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