forked from Gregwar/RST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpan.php
240 lines (197 loc) · 6.73 KB
/
Span.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
namespace Gregwar\RST;
use Gregwar\RST\Nodes\Node;
abstract class Span extends Node
{
protected $parser;
protected $span;
protected $tokens;
protected $environment;
public function __construct(Parser $parser, $span)
{
if (is_array($span)) {
$span = implode("\n", $span);
}
$tokenId = 0;
$prefix = mt_rand().'|'.time();
$generator = function() use ($prefix, &$tokenId) {
$tokenId++;
return sha1($prefix.'|'.$tokenId);
};
// Replacing literal with tokens
$tokens = array();
$span = preg_replace_callback('/``(.+)``(?!`)/mUsi', function($match) use (&$tokens, $generator) {
$id = $generator();
$tokens[$id] = array(
'type' => 'literal',
'text' => htmlspecialchars($match[1])
);
return $id;
}, $span);
$environment = $parser->getEnvironment();
$this->environment = $environment;
// Replacing numbering
foreach ($environment->getTitleLetters() as $level => $letter) {
$span = preg_replace_callback('/\#\\'.$letter.'/mUsi', function($match) use ($environment, $level) {
return $environment->getNumber($level);
}, $span);
}
// Signaling anonymous names
$environment->resetAnonymousStack();
if (preg_match_all('/(([a-z0-9]+)|(`(.+)`))__/mUsi', $span, $matches)) {
foreach ($matches[2] as $k => $y) {
$name = $matches[2][$k] ?: $matches[4][$k];
$environment->pushAnonymous($name);
}
}
// Looking for references to other documents
$span = preg_replace_callback('/:([a-z0-9]+):`(.+)`/mUsi', function($match) use (&$environment, $generator, &$tokens) {
$section = $match[1];
$url = $match[2];
$id = $generator();
$anchor = null;
$text = null;
if (preg_match('/^(.+)<(.+)>$/mUsi', $url, $match)) {
$text = $match[1];
$url = $match[2];
}
if (preg_match('/^(.+)#(.+)$/mUsi', $url, $match)) {
$url = $match[1];
$anchor = $match[2];
}
$tokens[$id] = array(
'type' => 'reference',
'section' => $section,
'url' => $url,
'text' => $text,
'anchor' => $anchor
);
$environment->found($section, $url);
return $id;
}, $span);
// Link callback
$linkCallback = function($match) use ($environment, $generator, &$tokens) {
$link = $match[2] ?: $match[4];
$id = $generator();
$next = $match[5];
$url = null;
if (preg_match('/^(.+) <(.+)>$/mUsi', $link, $match)) {
$link = $match[1];
$environment->setLink($link, $match[2]);
$url = $match[2];
}
$tokens[$id] = array(
'type' => 'link',
'link' => $link,
'url' => $url
);
return $id.$next;
};
// Replacing anonymous links
$span = preg_replace_callback('/(([a-z0-9]+)|(`(.+)`))__([^a-z0-9]{1}|$)/mUsi', $linkCallback, $span);
// Replacing links
$span = preg_replace_callback('/(([a-z0-9]+)|(`(.+)`))_([^a-z0-9]{1}|$)/mUsi', $linkCallback, $span);
$this->tokens = $tokens;
$this->parser = $parser;
$this->span = $span;
}
/**
* Processes some data in the context of the span, this will process the
* **emphasis**, the nbsp, replace variables and end-of-line brs
*/
public function process($data)
{
$self = $this;
$environment = $this->parser->getEnvironment();
$span = $this->escape($data);
// Emphasis
$span = preg_replace_callback('/\*\*(.+)\*\*/mUsi', function ($matches) use ($self) {
return $self->strongEmphasis($matches[1]);
}, $span);
$span = preg_replace_callback('/\*(.+)\*/mUsi', function ($matches) use ($self) {
return $self->emphasis($matches[1]);
}, $span);
// Nbsp
$span = preg_replace('/~/', $this->nbsp(), $span);
// Replacing variables
$span = preg_replace_callback('/\|(.+)\|/mUsi', function($match) use ($environment) {
return $environment->getVariable($match[1]);
}, $span);
// Adding brs when a space is at the end of a line
$span = preg_replace('/ \n/', $this->br(), $span);
return $span;
}
/**
* Renders the span
*/
public function render()
{
$environment = $this->parser->getEnvironment();
$span = $this->process($this->span);
// Replacing tokens
foreach ($this->tokens as $id => $value) {
switch ($value['type']) {
case 'literal':
$span = str_replace($id, $this->literal($value['text']), $span);
break;
case 'reference':
$reference = $environment->resolve($value['section'], $value['url']);
$link = $this->reference($reference, $value);
$span = str_replace($id, $link, $span);
break;
case 'link':
if ($value['url']) {
if ($environment->useRelativeUrls()) {
$url = $environment->relativeUrl($value['url']);
} else {
$url = $value['url'];
}
} else {
$url = $environment->getLink($value['link']);
}
$link = $this->link($url, $this->process($value['link']));
$span = str_replace($id, $link, $span);
break;
}
}
return $span;
}
public function emphasis($text)
{
return $text;
}
public function strongEmphasis($text)
{
return $text;
}
public function nbsp()
{
return ' ';
}
public function br()
{
return "\n";
}
public function literal($text)
{
return $text;
}
public function link($url, $title)
{
return $title.' ('.$url.')';
}
public function escape($span)
{
return $span;
}
public function reference($reference, $value)
{
if ($reference) {
$text = $value['text'] ?: (isset($reference['title']) ? $reference['title'] : '');
$link = $this->link($url, trim($text));
} else {
$link = $this->link('#', '(unresolved reference)');
}
return $link;
}
}