pluf2

pluf2 Git Source Tree


Root/src/Pluf/Text/Wiki/InlineParser.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */
 
/*
 * Based on Wiki Renderer copyright (C) 2003-2004 Laurent Jouanneau
 */
 
/**
 * Moteur permettant de transformer les tags wiki inline
 * d'une chaine en équivalent HTML
 */
class Pluf_Text_Wiki_InlineParser {
 
    public $resultline = '';
    public $error = false;
    public $listTag = array();
    public $str = array();
    public $splitPattern = '';
    public $checkWikiWord = false;
    public $checkWikiWordFunction = null;
    public $simpletags = null;
    public $_separator;
    public $escapeHtml = true;
    public $end = 0;
 
    /**
     * constructeur
     * @param array $inlinetags liste des tags permis
     * @param string caractère séparateur des différents composants
     *               d'un tag wiki
     */
 
    function __construct($inlinetags, $simpletags, $separator='|',
                         $checkWikiWord=false, $funcCheckWikiWord=null,
                         $escapeHtml=true)
    {
        foreach ($inlinetags as $name=>$prop){
            $this->listTag[$prop[0]] = new Pluf_Text_Wiki_Tag($name, $prop);
 
            $this->splitPattern.=preg_replace ( '/([^\w\s\d])/', '\\\\\\1',$prop[0]).')|(';
            if ($prop[1] != $prop[0])
                $this->splitPattern.=preg_replace ( '/([^\w\s\d])/', '\\\\\\1',$prop[1]).')|(';
        }
        foreach ($simpletags as $tag=>$html){
            $this->splitPattern.=preg_replace ( '/([^\w\s\d])/', '\\\\\\1',$tag).')|(';
        }
 
        $this->simpletags = $simpletags;
        $this->_separator = $separator;
        $this->checkWikiWord = $checkWikiWord;
        $this->checkWikiWordFunction = $funcCheckWikiWord;
        $this->escapeHtml = $escapeHtml;
    }
 
    /**
     * fonction principale du parser.
     * @param   string   $line avec des eventuels tag wiki
     * @return  string   chaine $line avec les tags wiki transformé en HTML
     */
    function parse($line) {
        $this->error = false;
 
        $this->str = preg_split('/('.$this->splitPattern.'\\'.$this->_separator.')|(\\\\)/',$line, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
        $this->end = count($this->str);
        if ($this->end > 1) {
            $firsttag = new Pluf_Text_Wiki_Tag('dummie',array('','', null,'Pluf_Text_Wiki_Configuration_builddummie'));
            $pos=-1;
            return $this->_parse($firsttag, $pos);
        }
        else {
            if ($this->escapeHtml) {
                if ($this->checkWikiWord && $this->checkWikiWordFunction !== null)
                    return  $this->_doCheckWikiWord(htmlspecialchars($line));
                else
                    return htmlspecialchars($line);
            }
            else {
                if ($this->checkWikiWord && $this->checkWikiWordFunction !== null)
                    return  $this->_doCheckWikiWord($line);
                else
                    return $line;
            }
 
        }
    }
 
    /**
     * coeur du parseur. Appelé récursivement
     */
 
    function _parse($tag, &$posstart) {
 
        $checkNextTag = true;
        $checkBeginTag = true;
 
        // on parcours la chaine,  morceau aprés morceau
        for($i=$posstart+1; $i < $this->end; $i++) {
 
            $t=&$this->str[$i];
            // a t-on un antislash ?
            if ($t=='\\'){
                if ($checkNextTag){
                    $t=''; // oui -> on l'efface et on ignore le tag (on continue)
                    $checkNextTag = false;
                }
                else {
                    $tag->addContent('\\',false);
                }
 
                // est-ce un séparateur ?
            }
            elseif ($t == $this->_separator) {
                if ($tag->isDummy() || !$checkNextTag)
                    $tag->addContent($this->_separator,false);
                elseif ($tag->useSeparator()) {
                    $checkBeginTag = false;
                    $tag->addSeparator();
                }
                else {
                    $tag->addContent($this->_separator,false);
                }
                // a-t-on une balise de fin du tag ?
            }
            elseif ($checkNextTag && $tag->getEndTag() == $t && !$tag->isDummy()) {
                $posstart = $i;
                return $tag->getHtmlContent();
 
                // a-t-on une balise de debut de tag quelconque ?
            }
            elseif ($checkBeginTag && $checkNextTag && isset($this->listTag[$t]) ) {
 
                $content  $this->_parse(clone($this->listTag[$t]),$i); // clone indispensable sinon plantage !!!
                if ($content)
                    $tag->addContent($content,false);
                else {
                    if ($tag->getNumberSeparator() == 0 && $this->checkWikiWord && $this->checkWikiWordFunction !== null) {
                        if ($this->escapeHtml)
                            $tag->addContent($this->_doCheckWikiWord(htmlspecialchars($t)),false);
                        else
                            $tag->addContent($this->_doCheckWikiWord($t),false);
                    }
                    else
                        $tag->addContent($t,$this->escapeHtml);
                }
 
                // a-t-on un saut de ligne forcé ?
            }
            elseif ($checkNextTag && $checkBeginTag && isset($this->simpletags[$t])) {
                $tag->addContent($this->simpletags[$t],false);
            }
            else {
                if ($tag->getNumberSeparator() == 0 && $this->checkWikiWord && $this->checkWikiWordFunction !== null) {
                    if ($this->escapeHtml)
                        $tag->addContent($this->_doCheckWikiWord(htmlspecialchars($t)),false);
                    else
                        $tag->addContent($this->_doCheckWikiWord($t),false);
                }
                else
                    $tag->addContent($t,$this->escapeHtml);
                $checkNextTag = true;
            }
        }
        if (!$tag->isDummy()) {
            //--- on n'a pas trouvé le tag de fin
            // on met en erreur
            $this->error = true;
            return false;
        }
        else
            return $tag->getHtmlContent();
    }
 
    function _doCheckWikiWord($string) {
        if (preg_match_all("/(?<=\b)[A-Z][a-z]+[A-Z0-9]\w*/", $string, $matches)){
            $fct = $this->checkWikiWordFunction;
            $match = array_unique($matches[0]); // il faut avoir une liste sans doublon, à cause du str_replace plus loin...
            $string = str_replace($match, $fct($match), $string);
        }
        return $string;
    }
 
    function getError() {
        return $this->error;
    }
 
}

Archive Download this file

Branches

Number of commits:
Page rendered in 0.09367s using 11 queries.