-
Notifications
You must be signed in to change notification settings - Fork 1
/
Leaf.php
executable file
·216 lines (180 loc) · 4.48 KB
/
Leaf.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
208
209
210
211
212
213
214
215
216
<?php
namespace Overblog\MediaWiki;
/**
* File Leaf
*
* @package MediaWiki
* @subpackage Document
* @version 1.0
* @author Yannick Le Guédart
*/
/**
* Class Leaf
*
* This class handles mediawiki leaf objects.
*
* @package MediaWiki
* @subpackage Document
* @author Yannick Le Guédart
*/
abstract class Leaf
{
/**
* text of the leaf
*
* @var string $_text raw text
*/
protected $_text = null;
/**
* Annotation
*
* @var array $_annotations
*/
protected $_annotations = array();
/**
* attributes
*
* @var array $_annotations
*/
protected $_attributes;
/* SpecialChars converter */
protected $_specialchars = array(
'LT' => array(
'original' => '<',
'replacement' => '⫷',
'entities' => '<'
),
'GT' => array(
'original' => '>',
'replacement' => '⫸',
'entities' => '>'
),
'QUOTE' => array(
'original' => '\'',
'replacement' => '⧘',
'entities' => '''
),
'DOUBLE_QUOTE' => array(
'original' => '"',
'replacement' => '⧚',
'entities' => '"'
)
);
/**
* Class constructor
*
* @param Object $leaf json_object
*/
public function __construct($leaf)
{
$this->_text = $leaf->content->text;
if (isset($leaf->content->annotations))
{
$this->_annotations = $leaf->content->annotations;
}
if (isset($leaf->attributes))
{
$this->_attributes = $leaf->attributes;
}
}
/**
* Encode special chars with strange chars
*
* @param $text
* @return string
*/
protected function _encodeText($text)
{
foreach($this->_specialchars as $special)
{
$text = preg_replace(
'/'.$special['original'].'/',
$special['replacement'],
$text
);
}
return $text;
}
/**
* HTML entities chars that converted by $this->_encodeText()
*
* @param $encodedtext
* @return string
*/
protected function _entitiesText($encodedtext)
{
foreach($this->_specialchars as $special)
{
$encodedtext = preg_replace(
'/'.$special['replacement'].'/',
$special['entities'],
$encodedtext
);
}
return $encodedtext;
}
/**
* Returns the text modified by annotations
*
* @return string
*/
protected function _getAnnotatedText()
{
$list = array();
// creation order for annotation
foreach ($this->_annotations as $i => $a)
{
// end - Must be set first because we close before open tags
if (! isset($list[$a->range->end]))
{
$list[$a->range->end] = array();
}
$list[$a->range->end][] =
array(
'type' => $a->type,
'begin' => false,
'data' => null
);
// begin
if (! isset($list[$a->range->start]))
{
$list[$a->range->start] = array();
}
$list[$a->range->start][] =
array(
'type' => $a->type,
'begin' => true,
'data' => (isset($a->data) ? $a->data : null)
);
}
ksort($list);
// génération du vrai texte
if(count($list) > 0)
{
$length = 0;
$text = $this->_encodeText($this->_text);
foreach ($list as $char => $annotations)
{
$replace = null;
$pos = $char + $length;
foreach($annotations as $ann)
{
$replace.= Annotation::getTag(
$ann['type'],
$ann['begin'],
$ann['data']
);
}
$text = mb_substr($text, 0, $pos) . $replace .
mb_substr($text, $pos);
$length+= mb_strlen($replace);
}
$text = $this->_entitiesText($text);
}
else
{
$text = htmlspecialchars($this->_text, ENT_QUOTES, 'UTF-8');
}
return $text;
}
}