Subversion Repositories Applications.papyrus

Rev

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