Subversion Repositories Applications.papyrus

Rev

Rev 165 | 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
// +------------------------------------------------------------------------------------------------------+
174 jpm 24
// CVS : $Id: Wikini.class.php,v 1.2 2004-11-25 15:36:41 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
174 jpm 39
*@version       $Revision: 1.2 $ $Date: 2004-11-25 15:36:41 $
165 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 Wikini.
55
*
56
* @author Paul M. Jones <pmjones@ciaweb.net>
57
* @package Text_Wiki
58
* @version 0.23.1
59
* @license LGPL
60
*/
61
class Text_Wikini extends Text_Wiki {
62
 
63
    /**
64
    *
65
    * Liste de règles par défaut du format Wikini dans leur ordre d'application au texte
66
    * à transformer.
67
    *
68
    * @access public
69
    *
70
    * @var array
71
    *
72
    */
73
    var $rules = array(
174 jpm 74
        'Table', // Tableaux
165 jpm 75
        'Code', // Inclusion de code avec coloration syntaxique
76
        'Emphasis', // Italique
77
        'Freelink', // Nom de Page qui ne sont pas au format Wiki
78
        'Heading', // Titre
79
        'Horiz', // Ligne horizontale
80
        'Interwiki', // Affichage de page d'un autre Wiki. Modifié par rapport à l'original de Text_Wiki
81
        'List', // Affichage de listes. Modifié par rapport à l'original de Text_Wiki
82
        'Newline', // Nouveau paragraphe.
83
        'Paragraph', // Nouveau paragraphe avec une ligne vide.
84
        'Tighten', // Réduit les lignes vide si on en a 3 ou plus consécutives
85
        'Raw', // Inclusion de HTML et non traitement du contenu par les règles de formatage. Modifié par rapport à l'original de Text_Wiki
86
        'Revise', // Suppression de texte. Modifié par rapport à l'original de Text_Wiki
87
        'Tt', // Texte à espacement fixe
88
        'Url', // Inclusion d'url dont les url d'images
174 jpm 89
        'Strong',// Gras
90
        'Include'// Action Include
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
 
103
    function Text_Wikini()
104
    {
105
        Text_Wiki::Text_Wiki();
106
 
107
        $this->addPath(
108
            'parse',
109
            $this->fixPath(dirname(__FILE__)) . 'Parse/'
110
        );
111
 
112
        $this->addPath(
113
            'render',
114
            $this->fixPath(dirname(__FILE__)) . 'Render/'
115
        );
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
150
        if (is_object($this->formatObj[$format])) {
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;
175
                    $rule = $this->tokens[$key][0];
176
                    $opts = $this->tokens[$key][1];
177
                    $output .= $this->renderObj[$rule]->token($opts);
178
                    $in_delim = false;
179
 
180
                } else {
181
 
182
                    // no, add to the dlimited token key number
183
                    $key .= $char;
184
 
185
                }
186
 
187
            } else {
188
 
189
                // not currently in a delimited section.
190
                // are we starting into a delimited section?
191
                if ($char == chr($this->delim)) {
192
                    // yes, reset the previous key and
193
                    // set the flag.
194
                    $key = '';
195
                    $in_delim = true;
196
                } else {
197
                    // no, add to the output as-is
198
                    $output .= $char;
199
                }
200
            }
201
        }
202
 
203
        // post-rendering activity
204
        if (is_object($this->formatObj[$format])) {
205
            $output .= $this->formatObj[$format]->post();
206
        }
207
 
208
        // return the rendered source text.
209
        return $output;
210
    }
211
 
212
    /**
213
    *
214
    * Add a token to the Text_Wiki tokens array, and return a delimited
215
    * token number.
216
    *
217
    * @access public
218
    *
219
    * @param array $options An associative array of options for the new
220
    * token array element.  The keys and values are specific to the
221
    * rule, and may or may not be common to other rule options.  Typical
222
    * options keys are 'text' and 'type' but may include others.
223
    *
224
    * @param boolean $id_only If true, return only the token number, not
225
    * a delimited token string.
226
    *
227
    * @return string|int By default, return the number of the
228
    * newly-created token array element with a delimiter prefix and
229
    * suffix; however, if $id_only is set to true, return only the token
230
    * number (no delimiters).
231
    *
232
    */
233
    function addToken($rule, $options = array(), $id_only = false)
234
    {
235
        // increment the token ID number.  note that if you parse
236
        // multiple times with the same Text_Wiki object, the ID number
237
        // will not reset to zero.
238
        static $id;
239
        if (! isset($id)) {
240
            $id = 0;
241
        } else {
242
            $id ++;
243
        }
244
 
245
        // force the options to be an array
246
        settype($options, 'array');
247
 
248
        // add the token
249
        $this->tokens[$id] = array(
250
 
251
            1 => $options
252
        );
253
 
254
        // return a value
255
        if ($id_only) {
256
            // return the last token number
257
            return $id;
258
        } else {
259
            // return the token number with delimiters
260
            return chr($this->delim) . $id . chr($this->delim);
261
        }
262
    }
165 jpm 263
}
264
 
265
// +------------------------------------------------------------------------------------------------------+
266
// |                                            PIED du PROGRAMME                                         |
267
// +------------------------------------------------------------------------------------------------------+
268
 
269
 
270
 
271
/* +--Fin du code ----------------------------------------------------------------------------------------+
272
*
273
* $Log: not supported by cvs2svn $
174 jpm 274
* Revision 1.1  2004/11/23 17:25:38  jpm
275
* Début classe PEAR WIKI pour la syntaxe Wikini.
165 jpm 276
*
174 jpm 277
*
165 jpm 278
* +-- Fin du code ----------------------------------------------------------------------------------------+
279
*/
280
?>