Subversion Repositories Applications.papyrus

Rev

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

Rev Author Line No. Line
165 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
// +------------------------------------------------------------------------------------------------------+
247 jpm 24
// CVS : $Id: Wikini.class.php,v 1.5 2005-01-20 19:39:43 jpm Exp $
165 jpm 25
/**
26
* Classe configurant le formatage pour Wikini.
27
*
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:
30
* 'Text/Wiki.php';
31
*
32
*@package Text_Wiki
33
*@subpackage Wikini
34
//Auteur original :
35
*@author        Jean-Pascal MILCENT <jpm@tela-botanica.org>
36
//Autres auteurs :
37
*@author        Aucun
38
*@copyright     Tela-Botanica 2000-2004
247 jpm 39
*@version       $Revision: 1.5 $ $Date: 2005-01-20 19:39:43 $
165 jpm 40
// +------------------------------------------------------------------------------------------------------+
41
*/
42
 
43
// +------------------------------------------------------------------------------------------------------+
44
// |                                            ENTETE du PROGRAMME                                       |
45
// +------------------------------------------------------------------------------------------------------+
46
 
247 jpm 47
 
165 jpm 48
// +------------------------------------------------------------------------------------------------------+
49
// |                                            CORPS du PROGRAMME                                        |
50
// +------------------------------------------------------------------------------------------------------+
51
/**
52
*
53
* Parse structured wiki text and render into arbitrary formats such as XHTML.
54
*
55
* Cette classe fille permet de configurer les régles de formatage pour Wikini.
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_Wikini extends Text_Wiki {
63
 
64
    /**
65
    *
66
    * Liste de règles par défaut du format Wikini dans leur ordre d'application au texte
67
    * à transformer.
68
    *
69
    * @access public
70
    *
71
    * @var array
72
    *
73
    */
74
    var $rules = array(
174 jpm 75
        'Table', // Tableaux
165 jpm 76
        'Code', // Inclusion de code avec coloration syntaxique
77
        'Emphasis', // Italique
247 jpm 78
        'Strong',// Gras
165 jpm 79
        'Freelink', // Nom de Page qui ne sont pas au format Wiki
80
        'Heading', // Titre
81
        'Horiz', // Ligne horizontale
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
84
        'Newline', // Nouveau paragraphe.
85
        'Paragraph', // Nouveau paragraphe avec une ligne vide.
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
88
        'Revise', // Suppression de texte. Modifié par rapport à l'original de Text_Wiki
89
        'Tt', // Texte à espacement fixe
247 jpm 90
        'Url' // Inclusion d'url dont les url d'images
165 jpm 91
    );
92
    /**
93
    *
94
    * The delimiter for token numbers of parsed elements in source text.
95
    *
96
    * @access public
97
    *
98
    * @var string
99
    *
100
    */
174 jpm 101
    var $delim = 12;
165 jpm 102
 
247 jpm 103
    function Text_Wikini($rules = null)
165 jpm 104
    {
247 jpm 105
        //Text_Wiki::Text_Wiki();
106
        if (is_array($rules)) {
107
            $this->rules = $rules;
108
        }
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.
111
        $this->addPath('parse', $this->fixPath(dirname(__FILE__)) .'../../pear/Text/Wiki/Parse/');
112
        $this->addPath('render', $this->fixPath(dirname(__FILE__)) .'../../pear/Text/Wiki/Render/');
113
        // Pour les règles spécifiques à Wikini:
114
        $this->addPath('parse', $this->fixPath(dirname(__FILE__)) . 'Parse/');
115
        $this->addPath('render', $this->fixPath(dirname(__FILE__)) . 'Render/');
165 jpm 116
    }
174 jpm 117
 
118
    /**
119
    *
120
    * Renders tokens back into the source text, based on the requested format.
121
    *
122
    * @access public
123
    *
124
    * @param string $format The target output format, typically 'xhtml'.
125
    * If a rule does not support a given format, the output from that
126
    * rule is rule-specific.
127
    *
128
    * @return string The transformed wiki text.
129
    *
130
    */
131
    function render($format = 'Xhtml')
132
    {
133
        // the rendering method we're going to use from each rule
134
        $format = ucwords(strtolower($format));
135
 
136
        // the eventual output text
137
        $output = '';
138
 
139
        // when passing through the parsed source text, keep track of when
140
        // we are in a delimited section
141
        $in_delim = false;
142
 
143
        // when in a delimited section, capture the token key number
144
        $key = '';
145
 
146
        // load the format object
147
        $this->loadFormatObj($format);
148
 
149
        // pre-rendering activity
224 jpm 150
        if (isset($this->formatObj[$format]) && is_object($this->formatObj[$format])) {
174 jpm 151
            $output .= $this->formatObj[$format]->pre();
152
        }
153
 
154
        // load the render objects
155
        foreach (array_keys($this->parseObj) as $rule) {
156
            $this->loadRenderObj($format, $rule);
157
        }
158
 
159
        // pass through the parsed source text character by character
160
        $k = strlen($this->source);
161
        for ($i = 0; $i < $k; $i++) {
162
 
163
            // the current character
164
            $char = $this->source{$i};
165
 
166
            // are alredy in a delimited section?
167
            if ($in_delim) {
168
 
169
                // yes; are we ending the section?
170
                if ($char == chr($this->delim)) {
171
 
172
                    // yes, get the replacement text for the delimited
173
                    // token number and unset the flag.
174
                    $key = (int)$key;
247 jpm 175
                    $rule = null;
176
                    if (isset($this->tokens[$key][0])) {
177
                        $rule = $this->tokens[$key][0];
178
                    }
179
                    $opts = null;
180
                    if (isset($this->tokens[$key][1])) {
181
                        $opts = $this->tokens[$key][1];
182
                    }
183
                    if (isset($this->renderObj[$rule]) && is_object($this->renderObj[$rule])) {
184
                        $output .= $this->renderObj[$rule]->token($opts);
185
                    }
174 jpm 186
                    $in_delim = false;
187
 
188
                } else {
189
 
190
                    // no, add to the dlimited token key number
191
                    $key .= $char;
192
 
193
                }
194
 
195
            } else {
196
 
197
                // not currently in a delimited section.
198
                // are we starting into a delimited section?
199
                if ($char == chr($this->delim)) {
200
                    // yes, reset the previous key and
201
                    // set the flag.
202
                    $key = '';
203
                    $in_delim = true;
204
                } else {
205
                    // no, add to the output as-is
206
                    $output .= $char;
207
                }
208
            }
209
        }
210
 
211
        // post-rendering activity
224 jpm 212
        if (isset($this->formatObj[$format]) && is_object($this->formatObj[$format])) {
174 jpm 213
            $output .= $this->formatObj[$format]->post();
214
        }
215
 
216
        // return the rendered source text.
217
        return $output;
218
    }
219
 
220
    /**
221
    *
222
    * Add a token to the Text_Wiki tokens array, and return a delimited
223
    * token number.
224
    *
225
    * @access public
226
    *
227
    * @param array $options An associative array of options for the new
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
230
    * options keys are 'text' and 'type' but may include others.
231
    *
232
    * @param boolean $id_only If true, return only the token number, not
233
    * a delimited token string.
234
    *
235
    * @return string|int By default, return the number of the
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
238
    * number (no delimiters).
239
    *
240
    */
241
    function addToken($rule, $options = array(), $id_only = false)
242
    {
243
        // increment the token ID number.  note that if you parse
244
        // multiple times with the same Text_Wiki object, the ID number
245
        // will not reset to zero.
246
        static $id;
247
        if (! isset($id)) {
248
            $id = 0;
249
        } else {
250
            $id ++;
251
        }
252
 
253
        // force the options to be an array
254
        settype($options, 'array');
255
 
256
        // add the token
257
        $this->tokens[$id] = array(
258
 
259
            1 => $options
260
        );
261
 
262
        // return a value
263
        if ($id_only) {
264
            // return the last token number
265
            return $id;
266
        } else {
267
            // return the token number with delimiters
268
            return chr($this->delim) . $id . chr($this->delim);
269
        }
270
    }
165 jpm 271
}
272
 
273
// +------------------------------------------------------------------------------------------------------+
274
// |                                            PIED du PROGRAMME                                         |
275
// +------------------------------------------------------------------------------------------------------+
276
 
277
 
278
 
279
/* +--Fin du code ----------------------------------------------------------------------------------------+
280
*
281
* $Log: not supported by cvs2svn $
247 jpm 282
* Revision 1.4  2004/12/07 12:17:41  jpm
283
* Correction message d'erreur.
284
*
224 jpm 285
* Revision 1.3  2004/11/25 15:53:24  jpm
286
* Suppression action inclure, migrer dans Papyrus.
287
*
177 jpm 288
* 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.
290
*
174 jpm 291
* Revision 1.1  2004/11/23 17:25:38  jpm
292
* Début classe PEAR WIKI pour la syntaxe Wikini.
165 jpm 293
*
174 jpm 294
*
165 jpm 295
* +-- Fin du code ----------------------------------------------------------------------------------------+
296
*/
297
?>