Subversion Repositories Applications.papyrus

Rev

Details | 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
// +------------------------------------------------------------------------------------------------------+
224 jpm 24
// CVS : $Id: Wikini.class.php,v 1.4 2004-12-07 12:17: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
224 jpm 39
*@version       $Revision: 1.4 $ $Date: 2004-12-07 12:17: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
224 jpm 89
        'Strong'// Gras
165 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
    */
174 jpm 100
    var $delim = 12;
165 jpm 101
 
102
    function Text_Wikini()
103
    {
104
        Text_Wiki::Text_Wiki();
105
 
106
        $this->addPath(
107
            'parse',
108
            $this->fixPath(dirname(__FILE__)) . 'Parse/'
109
        );
110
 
111
        $this->addPath(
112
            'render',
113
            $this->fixPath(dirname(__FILE__)) . 'Render/'
114
        );
115
    }
174 jpm 116
 
117
    /**
118
    *
119
    * Renders tokens back into the source text, based on the requested format.
120
    *
121
    * @access public
122
    *
123
    * @param string $format The target output format, typically 'xhtml'.
124
    * If a rule does not support a given format, the output from that
125
    * rule is rule-specific.
126
    *
127
    * @return string The transformed wiki text.
128
    *
129
    */
130
    function render($format = 'Xhtml')
131
    {
132
        // the rendering method we're going to use from each rule
133
        $format = ucwords(strtolower($format));
134
 
135
        // the eventual output text
136
        $output = '';
137
 
138
        // when passing through the parsed source text, keep track of when
139
        // we are in a delimited section
140
        $in_delim = false;
141
 
142
        // when in a delimited section, capture the token key number
143
        $key = '';
144
 
145
        // load the format object
146
        $this->loadFormatObj($format);
147
 
148
        // pre-rendering activity
224 jpm 149
        if (isset($this->formatObj[$format]) && is_object($this->formatObj[$format])) {
174 jpm 150
            $output .= $this->formatObj[$format]->pre();
151
        }
152
 
153
        // load the render objects
154
        foreach (array_keys($this->parseObj) as $rule) {
155
            $this->loadRenderObj($format, $rule);
156
        }
157
 
158
        // pass through the parsed source text character by character
159
        $k = strlen($this->source);
160
        for ($i = 0; $i < $k; $i++) {
161
 
162
            // the current character
163
            $char = $this->source{$i};
164
 
165
            // are alredy in a delimited section?
166
            if ($in_delim) {
167
 
168
                // yes; are we ending the section?
169
                if ($char == chr($this->delim)) {
170
 
171
                    // yes, get the replacement text for the delimited
172
                    // token number and unset the flag.
173
                    $key = (int)$key;
174
                    $rule = $this->tokens[$key][0];
175
                    $opts = $this->tokens[$key][1];
176
                    $output .= $this->renderObj[$rule]->token($opts);
177
                    $in_delim = false;
178
 
179
                } else {
180
 
181
                    // no, add to the dlimited token key number
182
                    $key .= $char;
183
 
184
                }
185
 
186
            } else {
187
 
188
                // not currently in a delimited section.
189
                // are we starting into a delimited section?
190
                if ($char == chr($this->delim)) {
191
                    // yes, reset the previous key and
192
                    // set the flag.
193
                    $key = '';
194
                    $in_delim = true;
195
                } else {
196
                    // no, add to the output as-is
197
                    $output .= $char;
198
                }
199
            }
200
        }
201
 
202
        // post-rendering activity
224 jpm 203
        if (isset($this->formatObj[$format]) && is_object($this->formatObj[$format])) {
174 jpm 204
            $output .= $this->formatObj[$format]->post();
205
        }
206
 
207
        // return the rendered source text.
208
        return $output;
209
    }
210
 
211
    /**
212
    *
213
    * Add a token to the Text_Wiki tokens array, and return a delimited
214
    * token number.
215
    *
216
    * @access public
217
    *
218
    * @param array $options An associative array of options for the new
219
    * token array element.  The keys and values are specific to the
220
    * rule, and may or may not be common to other rule options.  Typical
221
    * options keys are 'text' and 'type' but may include others.
222
    *
223
    * @param boolean $id_only If true, return only the token number, not
224
    * a delimited token string.
225
    *
226
    * @return string|int By default, return the number of the
227
    * newly-created token array element with a delimiter prefix and
228
    * suffix; however, if $id_only is set to true, return only the token
229
    * number (no delimiters).
230
    *
231
    */
232
    function addToken($rule, $options = array(), $id_only = false)
233
    {
234
        // increment the token ID number.  note that if you parse
235
        // multiple times with the same Text_Wiki object, the ID number
236
        // will not reset to zero.
237
        static $id;
238
        if (! isset($id)) {
239
            $id = 0;
240
        } else {
241
            $id ++;
242
        }
243
 
244
        // force the options to be an array
245
        settype($options, 'array');
246
 
247
        // add the token
248
        $this->tokens[$id] = array(
249
 
250
            1 => $options
251
        );
252
 
253
        // return a value
254
        if ($id_only) {
255
            // return the last token number
256
            return $id;
257
        } else {
258
            // return the token number with delimiters
259
            return chr($this->delim) . $id . chr($this->delim);
260
        }
261
    }
165 jpm 262
}
263
 
264
// +------------------------------------------------------------------------------------------------------+
265
// |                                            PIED du PROGRAMME                                         |
266
// +------------------------------------------------------------------------------------------------------+
267
 
268
 
269
 
270
/* +--Fin du code ----------------------------------------------------------------------------------------+
271
*
272
* $Log: not supported by cvs2svn $
224 jpm 273
* Revision 1.3  2004/11/25 15:53:24  jpm
274
* Suppression action inclure, migrer dans Papyrus.
275
*
177 jpm 276
* Revision 1.2  2004/11/25 15:36:41  jpm
277
* Suppression régle Delimiter car problème avec les délimitations de fin de ligne.
278
*
174 jpm 279
* Revision 1.1  2004/11/23 17:25:38  jpm
280
* Début classe PEAR WIKI pour la syntaxe Wikini.
165 jpm 281
*
174 jpm 282
*
165 jpm 283
* +-- Fin du code ----------------------------------------------------------------------------------------+
284
*/
285
?>